Skip to main content

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 text
  • stream_translate(): Stream translation results
  • count_tokens(): Get token usage information

Parameters

  • t_text: Text to translate
  • model: OpenAI model to use (default: "gpt-4o-mini")
  • t_to: Target language code
  • t_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)

Source distribution for openaitrans 0.1.1
File Size Uploaded
openaitrans-0.1.1.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openaitrans 0.1.1
File Interpreter ABI Platform
openaitrans-0.1.1-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page