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.
Metadata
Release files for openaitrans 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| openaitrans-0.1.1.tar.gz | 6.6 kB | Without provenance | Details for openaitrans-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| openaitrans-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for openaitrans-0.1.1-py3-none-any.whl |
Total release size: 12.9 kB
Release files / openaitrans-0.1.1.tar.gz
| Download URL | openaitrans-0.1.1.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
077679e28f3499792b98351d376d918fe1f70eaedecb3ffaae7f355f31c72fae
|
|
BLAKE2b-256 checksum How to use checksums |
94d01363f4d831576ca68d65cdbe09b9e4172bd9049baa5c6a08194d5a4ceccf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / openaitrans-0.1.1-py3-none-any.whl
| Download URL | openaitrans-0.1.1-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e7d5367ef9580f307fa56d9f853a3155d2713a853a25f6816b96f1a2ae055991
|
|
BLAKE2b-256 checksum How to use checksums |
afab10cec6ee366dd3ddb650cc7de06264d94a6048e293339a024f66a5fc0984
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Security for openaitrans 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.