Skip to main content

An AI-powered translation tool

Project description

AI Translator

English | 繁體中文

An AI-powered translation tool that uses OpenAI's GPT models to translate text and files between different languages.

Features

  • Text translation
  • File translation
  • Folder translation
  • Support for multiple languages
  • Customizable language configuration
  • JSON configuration support
  • Support for different GPT model settings

Installation

pip install ai-translator

Usage

Basic Text Translation

from translator import Translator

translator = Translator(api_key="your-openai-api-key")

# Translate text
text = "Hello, world!"
result = translator.translate(text, target_language="zh-tw")
print(result)  # 你好,世界!

Advanced Initialization Options

# Using different models and parameters
translator = Translator(
    api_key="your-openai-api-key",
    model="gpt-4o-mini",  # Default model
    temperature=0.0,      # Controls creativity, 0 for most deterministic output
    max_tokens=1000       # Maximum output length
)

# View currently available languages
for code, name in translator.available_languages.items():
    print(f"{code}: {name}")

File Translation

# Translate a single file
translator.translate_file(
    file_path="input.txt",
    target_language="es",
    output_path="output.txt"
)

# Translate all files in a folder
translator.translate_folder(
    folder_path="input_folder",
    target_language="ja",
    output_dir="output_folder",
    file_extension=".txt",
    recursive=True  # Set to True to process subdirectories
)

Custom Language Configuration

# Using default configuration
translator = Translator(api_key="your-openai-api-key")

# Using custom configuration
custom_config = {
    "zh-tw": "繁體中文",
    "en": "English",
    "fr": "French"
}
translator = Translator(api_key="your-openai-api-key", language_config=custom_config)

# Update configuration of an existing instance (after initialization)
translator.config(
    model="gpt-4",
    temperature=0.3,
    max_tokens=2000,
    language_config=custom_config
)

# Update language configuration from JSON file
translator.update_language_config_from_json("languages.json")

Configuration

Default Supported Languages

  • zh-tw: 繁體中文
  • en: English
  • es: Español
  • ja: 日本語
  • ko: 한국어

JSON Configuration File Format

{
    "zh-tw": "繁體中文",
    "en": "English",
    "fr": "French",
    "de": "Deutsch"
}

Error Handling

The translation functions include robust error handling mechanisms for the following situations:

  • Files or directories don't exist
  • JSON configuration file format errors
  • API call failures
  • File read/write errors

Error messages are printed via standard output, and translation methods return None when errors occur.

Requirements

  • Python 3.8 or higher
  • OpenAI API key
  • openai>=1.65.0
  • python-dotenv>=1.0.0

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_translator-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_translator-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file ai_translator-0.1.0.tar.gz.

File metadata

  • Download URL: ai_translator-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for ai_translator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4b1dfe0e4555061fc283be240f94d58ceb70b1549d34458892af2c5c2568e2b2
MD5 522e37164eca58d03d16446b4ec0f728
BLAKE2b-256 85c691d9881acd9642a0ef38d506ac720ddfc6d34d81366cb580e12275c87b3a

See more details on using hashes here.

File details

Details for the file ai_translator-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ai_translator-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for ai_translator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e344555aedf6a095ac491a1f1da21cc22c955b01bff6efb0f9587d3a4c8938d7
MD5 9257a3fa523dd5400449fde7a0454f3e
BLAKE2b-256 632440ee150426c8ebdf3040b5ba04b8b5335380440c4f2544e1032282cd44ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page