A Python library for language detection and translation using OpenAI's GPT-4o.
Project description
SimpleAITranslator
Overview
SimpleAITranslator is a Python library designed to identify the language of a given text and translate text between multiple languages using OpenAI's GPT-4o. The library is especially useful for translating text containing multiple languages into a single target language.
Features
- Language Detection: Identify the language of a given text in ISO 639-1 format.
- Translation: Translate text containing multiple languages into another language in ISO 639-1 format.
Requirements
To use this library, you must have an OpenAI API key. This key allows the library to utilize OpenAI's GPT-4o for translation and language detection.
Installation
You can install the SimpleAITranslator library from PyPI:
pip install simpleaitranslator
Usage
Setting the OpenAI API Key
Before using SimpleAITranslator with OpenAI, you need to set your OpenAI API key. You can do this by creating an instance of the TranslatorOpenAI class.
from simpleaitranslator.translator import TranslatorOpenAI
# Set your OpenAI API key
translator = TranslatorOpenAI(open_ai_api_key="YOUR_OPENAI_API_KEY")
Setting the Azure OpenAI API Key
If you are using Azure's OpenAI services, you need to set your Azure OpenAI API key along with additional required parameters. Use the TranslatorAzureOpenAI class for this.
from simpleaitranslator.translator import TranslatorAzureOpenAI
# Set your Azure OpenAI API key and related parameters
translator = TranslatorAzureOpenAI(
azure_endpoint="YOUR_AZURE_ENDPOINT",
api_key="YOUR_AZURE_API_KEY",
api_version="YOUR_API_VERSION",
azure_deployment="YOUR_AZURE_DEPLOYMENT"
)
Language Detection
To detect the language of a given text:
from simpleaitranslator.translator import TranslatorOpenAI
# Set your OpenAI API key
translator = TranslatorOpenAI(open_ai_api_key="YOUR_OPENAI_API_KEY")
# Detect language
detected_language = translator.get_text_language("Hello world")
print(detected_language.language_ISO_639_1_code) # Output: 'en'
print(detected_language.language_name) # Output: 'English'
Translation
To translate text containing multiple languages into another language:
from simpleaitranslator.translator import TranslatorOpenAI
# Set your OpenAI API key
translator = TranslatorOpenAI(open_ai_api_key="YOUR_OPENAI_API_KEY")
# Translate text
translated_text = translator.translate("Cześć jak się masz? Meu nome é Adam", "en")
print(translated_text) # Output: "Hello how are you? My name is Adam"
Full Example
Here is a complete example demonstrating how to use the library:
from simpleaitranslator.translator import TranslatorOpenAI
# Initialize the translator with your OpenAI API key
translator = TranslatorOpenAI(open_ai_api_key="YOUR_OPENAI_API_KEY")
# Detect language
detected_language = translator.get_text_language("jak ty się nazywasz")
print(detected_language.language_ISO_639_1_code) # Output: 'pl'
print(detected_language.language_name) # Output 'Polish'
# Translate text
translated_text = translator.translate("Cześć jak się masz? Meu nome é Adam", "en")
print(translated_text) # Output: "Hello how are you? My name is Adam"
Supported Languages
SimpleAITranslator supports all languages supported by GPT-4o. For a complete list of language codes, you can visit the ISO 639-1 website.
Here are some of the most popular languages and their ISO 639-1 codes:
- English:
en
- Spanish:
es
- French:
fr
- German:
de
- Chinese:
zh
- Japanese:
ja
- Korean:
ko
- Portuguese:
pt
- Russian:
ru
- Italian:
it
- Dutch:
nl
- Arabic:
ar
- Hindi:
hi
- Bengali:
bn
- Turkish:
tr
- Polish:
pl
- Swedish:
sv
- Norwegian:
no
- Danish:
da
- Finnish:
fi
- Greek:
el
- Hebrew:
he
Additional Resources
Authors
License
SimpleAITranslator is licensed under the MIT License. See the LICENSE file for more details.
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
File details
Details for the file simpleaitranslator-2.3.6.tar.gz
.
File metadata
- Download URL: simpleaitranslator-2.3.6.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa007ccdedf9ad5e1832702f25883eb8e1013418435b2e2a963e694f45078472 |
|
MD5 | 4825ca805864cc0ca378363772cf3087 |
|
BLAKE2b-256 | 3e7f562c9211a858026cdff6a774dd1c9a0789c7773a29c739fe981f8325d5be |
File details
Details for the file simpleaitranslator-2.3.6-py3-none-any.whl
.
File metadata
- Download URL: simpleaitranslator-2.3.6-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7f5b35dd2065a31f910d84f3d46f6d35176e7c35e78e97c305cd0849393114c |
|
MD5 | f055f795765a1bb9e2c21a5126474b3c |
|
BLAKE2b-256 | e0ac6b3f67e2c3132119872ddf8dd5c3149aa15935d2d49b96ac6caf98d67bbd |