Skip to content

Commit

Permalink
Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
pescheck-bram committed Sep 9, 2024
1 parent 17d635d commit ffc2ac9
Showing 1 changed file with 59 additions and 48 deletions.
107 changes: 59 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,27 @@
# Python GPT-4 PO File Translator

This Python script provides a comprehensive tool for translating `.po` files using OpenAI's GPT-4 model. It is designed to accommodate both bulk and individual translation modes, making it suitable for a wide range of project sizes and `.po` file structures.
This Python script provides a robust and flexible tool for translating `.po` files using OpenAI's GPT-4 model. It accommodates various translation modes, handles fuzzy entries, and integrates batch processing for larger projects, making it suitable for diverse `.po` file structures and sizes.

## Features

- **Bulk Translation Mode**: Enhances efficiency by translating multiple text entries simultaneously. Ideal for large `.po` files.
- **Individual Translation Mode**: Offers flexibility to translate entries one at a time, ensuring precise translation. Useful for complex or nuanced content.
- **Configurable Batch Size**: Users can set the number of entries to be translated per batch during bulk translation, allowing for optimized API usage.
- **Fuzzy Entry Management**: Automatically handles 'fuzzy' entries by removing fuzzy flags, ensuring that only verified translations are processed.
- **Language Inference from Folder Structure**: Optionally infers the target language based on the folder structure of the `.po` files, streamlining the translation process.
- **Translation Validation and Retry Logic**: Built-in mechanisms validate translations, retrying where necessary to avoid incorrect or overly verbose translations.
- **Comprehensive Logging**: Detailed logging for progress monitoring, debugging, and ensuring transparency in the translation process.
- **Flexible API Key Configuration**: Supports providing the OpenAI API key via command-line arguments or environment variables for enhanced security and ease of use.
- **Retry Mechanism for Failed Translations**: Automatically retries failed translations up to three times, reducing the chance of incomplete or incorrect outputs.
- **Post-Processing for Translations**: Translations are reviewed to ensure they are concise and free from unnecessary explanations or repeated content.
- **Bulk and Individual Translation Modes**: Allows efficient bulk translation or precise, entry-by-entry translations for nuanced content.
- **Detailed Language Option (`--detail-lang`)**: Supports using full language names (e.g., "Netherlands, German") alongside shortcodes (e.g., `nl, de`), ensuring clarity in translation prompts.
- **Configurable Batch Size**: Set the number of entries to translate per batch during bulk translation, optimizing API usage.
- **Fuzzy Entry Management**: Automatically removes fuzzy flags and entries, ensuring only valid translations are processed.
- **Language Inference from Folder Structure**: Infers the target language from the folder structure, reducing the need for explicit language specifications.
- **Translation Validation and Retry Logic**: Built-in mechanisms validate translations and automatically retry to avoid incorrect or verbose translations.
- **Logging for Transparency**: Detailed logging for monitoring, debugging, and ensuring progress throughout the translation process.
- **OpenAI API Key Management**: Supports environment variables or command-line arguments for securely providing OpenAI API credentials.
- **Retry Mechanism for Failed Translations**: Retries failed translations up to three times, reducing incomplete or incorrect outputs.
- **Post-Processing for Concise Translations**: Automatically reviews translations to ensure they are concise and free of unnecessary explanations or repetitions.

## Requirements

- Python 3.x
- `polib` library
- `openai` Python package
- `polib` library (for `.po` file handling)
- `openai` Python package (for integration with OpenAI GPT models)
- `tenacity` library (for retry mechanisms)
- `python-dotenv` (for environment variable management)

## API Key Configuration

The `gpt-po-translator` supports two methods for providing OpenAI API credentials:

1. **Environment Variable**: Set your OpenAI API key as an environment variable named `OPENAI_API_KEY`. This method is recommended for security and ease of API key management.

```bash
export OPENAI_API_KEY='your_api_key_here'
```

2. **Command-Line Argument**: Pass the API key as a command-line argument using the `--api_key` option.

```bash
gpt-po-translator --folder ./locales --lang de,fr --api_key 'your_api_key_here' --bulk --bulksize 100 --folder-language
```

Ensure your API key is kept secure and not exposed in shared or public spaces.
- `python-dotenv` (for managing environment variables)

## Installation

Expand All @@ -53,7 +35,7 @@ pip install gpt-po-translator

### Manual Installation

For manual installation or to work with the latest code from the repository:
For manual installation or working with the latest code from the repository:

1. Clone the repository:
```bash
Expand All @@ -64,45 +46,74 @@ For manual installation or to work with the latest code from the repository:
pip install .
```

## API Key Configuration

The `gpt-po-translator` supports two methods for providing OpenAI API credentials:

1. **Environment Variable**: Set your OpenAI API key as an environment variable named `OPENAI_API_KEY`. This method is recommended for security and ease of API key management.

```bash
export OPENAI_API_KEY='your_api_key_here'
```

2. **Command-Line Argument**: Pass the API key as a command-line argument using the `--api_key` option.

```bash
gpt-po-translator --folder ./locales --lang de,fr --api_key 'your_api_key_here' --bulk --bulksize 100 --folder-language
```

Make sure your API key is securely stored and not exposed in public spaces or repositories.

## Usage

Use `gpt-po-translator` as a command-line tool:
Use `gpt-po-translator` as a command-line tool for translating `.po` files:

```
gpt-po-translator --folder [path_to_po_files] --lang [language_codes] [--api_key [your_openai_api_key]] [--fuzzy] [--bulk] [--bulksize [batch_size]] [--folder-language]
```bash
gpt-po-translator --folder [path_to_po_files] --lang [language_codes] [--api_key [your_openai_api_key]] [--fuzzy] [--bulk] [--bulksize [batch_size]] [--folder-language] [--detail-lang [full_language_names]]
```
### Example
```
gpt-po-translator --folder ./locales --lang de,fr --api_key 'your_api_key_here' --bulk --bulksize 40 --folder-language
```bash
gpt-po-translator --folder ./locales --lang de,fr --api_key 'your_api_key_here' --bulk --bulksize 40 --folder-language --detail-lang "German,French"
```
This command translates `.po` files in the `./locales` folder to German and French, using the provided OpenAI API key, and processes 40 translations per batch in bulk mode. It also infers the language from the folder structure.
This command translates `.po` files in the `./locales` folder to German and French, using the provided OpenAI API key and processing 40 translations per batch in bulk mode. It also infers the language from the folder structure.
### Command-Line Options
- `--folder`: Specifies the input folder containing `.po` files.
- `--lang`: Comma-separated language codes to filter `.po` files.
- `--fuzzy`: Remove fuzzy entries before processing.
- `--lang`: Comma-separated language codes to filter `.po` files (e.g., `de,fr`).
- `--detail-lang`: Optional argument for full language names, matching the order of `--lang` (e.g., "German,French").
- `--fuzzy`: Removes fuzzy entries before processing.
- `--bulk`: Enables bulk translation mode for faster processing.
- `--bulksize`: Sets the batch size for bulk translation. Default is 50.
- `--model`: Specifies the OpenAI model to use for translations. Default is `gpt-3.5-turbo-0125`.
- `--bulksize`: Sets the batch size for bulk translation (default is 50).
- `--model`: Specifies the OpenAI model to use for translations (default is `gpt-3.5-turbo-0125`).
- `--api_key`: OpenAI API key. Can be provided through the command line or as an environment variable.
- `--folder-language`: Infers the target language from the folder structure.
## Detailed Language Names and Shortcodes
The `--detail-lang` option complements `--lang` by allowing you to specify full language names (e.g., `Netherlands,German`) instead of language shortcodes. The full names are then used in the context of OpenAI prompts, improving clarity for the GPT model.
Example usage:
```bash
gpt-po-translator --folder ./locales --lang nl,de --detail-lang "Netherlands,German"
```
## Logging
The script logs detailed information about the files being processed, the number of translations, and batch details in bulk mode. The logs are essential for monitoring the progress, debugging issues, and ensuring all translations are handled correctly.
The script logs detailed information about the files being processed, the number of translations, and batch details in bulk mode. Logs are essential for monitoring progress, debugging issues, and ensuring transparency throughout the translation process.
## Error Handling and Retries
The translation process includes robust error handling and retries:
The script includes robust error handling and retries to ensure reliable translation:
- **Failed Translations**: If a translation fails, the script will automatically retry up to three times.
- **Empty Translations**: If an empty translation is returned, the script will attempt to translate the text again using a different approach.
- **Failed Translations**: Automatically retries failed translations up to three times.
- **Empty Translations**: If an empty translation is returned, the script will attempt to translate the text again using an alternative approach.
- **Lengthy or Incorrect Translations**: Translations that are too long or contain explanations instead of direct translations are flagged and retried.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

0 comments on commit ffc2ac9

Please sign in to comment.