Skip to main content

A Python library for language detection and translation using OpenAI's GPT-4o.

Project description

SimpleAITranslator

Test Python package - Publish Python Versions PyPI version License: MIT

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-3 format.
  • Translation: Translate text containing multiple languages into another language in ISO 639-3 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
language_code = translator.get_text_language("Hello world")
print(language_code)  # Output: 'eng'

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", "eng")
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
language_code = translator.get_text_language("jak ty się nazywasz")
print(language_code)  # Output: 'pol'

# Translate text
translated_text = translator.translate("Cześć jak się masz? Meu nome é Adam", "eng")
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-3 website.

Here are some of the most popular languages and their ISO 639-3 codes:

  • English (eng)
  • Spanish (spa)
  • French (fra)
  • German (deu)
  • Chinese (zho)
  • Japanese (jpn)
  • Korean (kor)
  • Portuguese (por)
  • Russian (rus)
  • Italian (ita)
  • Dutch (nld)
  • Arabic (ara)
  • Hindi (hin)
  • Bengali (ben)
  • Turkish (tur)
  • Polish (pol)
  • Swedish (swe)
  • Norwegian (nor)
  • Danish (dan)
  • Finnish (fin)
  • Greek (ell)
  • Hebrew (heb)

Additional Resources

Authors

License

SimpleAITranslator is licensed under the MIT License. See the LICENSE file for more details.

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

simpleaitranslator-2.2.1.tar.gz (7.9 kB view hashes)

Uploaded Source

Built Distribution

simpleaitranslator-2.2.1-py3-none-any.whl (9.0 kB view hashes)

Uploaded Python 3

Supported by

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