A powerful AI-based text translator using ChatGPT API
Project description
OpenAI Translator
A powerful AI-based text translator using ChatGPT API. This package provides an easy-to-use interface for translating text between different languages while preserving formatting and structure.
Features
- Automatic language detection
- Support for multiple languages
- Preserves text formatting (Markdown, HTML, JSON, etc.)
- Streaming translation support
- Token usage tracking
- Configurable formality level
- Easy-to-use Python interface
Installation
pip install openaitrans
Usage
First, set up your OpenAI API key in your environment:
export OPENAI_API_KEY='your-api-key-here'
Or create a .env file in your project root:
OPENAI_API_KEY=your-api-key-here
Basic Usage
from openaitrans import translator
# Simple translation (auto-detect source language to Persian)
result = translator.translate("Hello, how are you?")
print(result.result)
# Specify source and target languages
result = translator.translate(
"Hello, how are you?",
t_from="en",
t_to="fr"
)
print(result.result)
# Streaming translation
for chunk in translator.stream_translate():
print(chunk)
Advanced Usage
from openaitrans import Translator
# Create a custom translator instance with specific settings
custom_translator = Translator(
model="gpt-4",
temperature=0.7,
max_tokens=1000
)
# Translate with specific formatting
result = custom_translator.translate(
"Hello, how are you?",
t_from="en",
t_to="fr",
text_format="markdown"
)
# Get token usage information
print(custom_translator.token_usage)
API Reference
Translator Class
The main class for translation operations. Use this when you need custom settings.
Methods
translate(t_text, model="gpt-4o-mini", t_to=None, t_from=None): Translate textstream_translate(): Stream translation resultscount_tokens(): Get token usage information
Parameters
t_text: Text to translatemodel: OpenAI model to use (default: "gpt-4o-mini")t_to: Target language codet_from: Source language code
Default Instance
The package provides a default translator instance for quick and easy use:
from openaitrans import translator
# Use the default instance
result = translator.translate("Hello, how are you?")
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openaitrans-0.1.0.tar.gz.
File metadata
- Download URL: openaitrans-0.1.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9a08dc8a8a172c91ebbeb0d02092f30e546fa8782f7eb046e5888010069e1fa
|
|
| MD5 |
d5b562c59f2dd1be1f0ae5724b579929
|
|
| BLAKE2b-256 |
1cdb4f75bd2fc1469aa2e1182d8239f8c27c8148f60b8062632d87f4939a8463
|
File details
Details for the file openaitrans-0.1.0-py3-none-any.whl.
File metadata
- Download URL: openaitrans-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78afef963cb3dd0a15f93c22469dadf009dd13fce6f25dc3f6bd6086a36f113f
|
|
| MD5 |
d34868f7d7d007927ac7293225df7ee7
|
|
| BLAKE2b-256 |
a4a12097429bd848ea8e45b0d448f2b713b57ce81a1f63c708ed4a946e7a1276
|