-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6276f93
commit 957998e
Showing
20 changed files
with
6,754 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Python package - Publish | ||
|
||
on: | ||
workflow_run: | ||
workflows: ["Test"] | ||
types: | ||
- completed | ||
|
||
jobs: | ||
publish: | ||
if: ${{ github.event.workflow_run.conclusion == 'success' }} | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.11' | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install poetry | ||
- name: Build package | ||
run: | | ||
poetry build | ||
- name: Publish package | ||
env: | ||
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_API_TOKEN }} | ||
run: | | ||
poetry publish --username __token__ --password $POETRY_PYPI_TOKEN_PYPI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
python-version: ['3.12'] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install poetry | ||
poetry install | ||
- name: Run tests | ||
env: | ||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} | ||
run: | | ||
poetry run pytest | ||
coverage: | ||
name: Run coverage and upload to Codecov | ||
runs-on: ubuntu-latest | ||
needs: test | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Set up Python 3.12 | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: 3.12 | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install poetry | ||
poetry install | ||
poetry add pytest-cov | ||
- name: Run tests and generate coverage report | ||
env: | ||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} | ||
run: | | ||
poetry run pytest --cov=./simpleaitranslator --cov-report=xml --cov-report=html | ||
- name: Upload results to Codecov | ||
uses: codecov/codecov-action@v4 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,159 @@ | ||
# llm-translate | ||
# SimpleAITranslator | ||
[](https://github.com/adam-pawelek/SimpleAITranslator/actions/workflows/test.yml) | ||
[](https://github.com/adam-pawelek/SimpleAITranslator/actions/workflows/publish.yml) | ||
[](https://www.python.org/) | ||
[](https://pypi.org/project/llm_translate/) | ||
[](https://opensource.org/licenses/MIT) | ||
[](https://codecov.io/github/adam-pawelek/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: | ||
|
||
```bash | ||
pip install llm_translate | ||
``` | ||
|
||
## 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. | ||
```python | ||
from llm_translate.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. | ||
```python | ||
from llm_translate.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: | ||
|
||
```python | ||
from llm_translate.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: | ||
|
||
```python | ||
from llm_translate.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: | ||
|
||
```python | ||
from llm_translate.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](https://localizely.com/iso-639-1-list/). | ||
|
||
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 | ||
|
||
- [PyPI page](https://pypi.org/project/llm_translate/) | ||
- [ISO 639-1 Codes](https://localizely.com/iso-639-1-list/) | ||
- [Github project repository](https://github.com/adam-pawelek/SimpleAITranslator) | ||
|
||
## Authors | ||
- Adam Pawełek | ||
- [LinkedIn](https://www.linkedin.com/in/adam-roman-pawelek/) | ||
- [Email](mailto:adam.pwk@outlook.com) | ||
|
||
|
||
|
||
## License | ||
|
||
SimpleAITranslator is licensed under the MIT License. See the LICENSE file for more details. | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from llm_translate.utils.enums import ModelForTranslator | ||
|
||
|
||
class MissingAPIKeyError(Exception): | ||
"""Exception raised for missing API key.""" | ||
def __init__(self, message="OpenAI API key is not set. Please set the API key using the instructions in the documentation. See: https://github.com/adam-pawelek/SimpleAITranslator?tab=readme-ov-file#setting-the-openai-api-key"): | ||
self.message = message | ||
super().__init__(self.message) | ||
|
||
|
||
class NoneAPIKeyProvidedError(Exception): | ||
"""Exception raised for missing API key.""" | ||
def __init__(self, message="Provide a valid API key. Right now you passed None value to this function"): | ||
self.message = message | ||
super().__init__(self.message) | ||
|
||
|
||
class InvalidModelName(Exception): | ||
"""Exception raised for invalid model name.""" | ||
def __init__(self, message="", invalid_model_name=None): | ||
valid_models = ", ".join(model.value for model in ModelForTranslator) | ||
self.message = f"Invalid model name '{invalid_model_name}'. Value must be one of: {valid_models}" | ||
super().__init__(self.message) | ||
|
||
|
Oops, something went wrong.