|
|
||
|---|---|---|
| .. | ||
| translations | ||
| README.md | ||
| main.py | ||
| requirements.txt | ||
| utils.py | ||
README.md
Parallel Batch Translation Process
This project demonstrates using PocketFlow's async and parallel features (AsyncFlow, AsyncParallelBatchNode) to translate a document into multiple languages concurrently.
- Check out the Substack Post Tutorial for more!
Goal
Translate ../../README.md into multiple languages (Chinese, Spanish, etc.) in parallel, saving each to a file in the translations/ directory. The main goal is to compare execution time against a sequential process.
Getting Started
- Install requirements:
pip install -r requirements.txt
-
Set API Key: Set the environment variable for your Anthropic API key.
export ANTHROPIC_API_KEY="your-api-key-here"(Replace
"your-api-key-here"with your actual key) (Alternatively, placeANTHROPIC_API_KEY=your-api-key-herein a.envfile) -
Verify API Key (Optional): Run a quick check using the utility script.
python utils.py(Note: This requires a valid API key to be set.)
-
Run the translation process:
python main.py
How It Works
The implementation uses an AsyncParallelBatchNode that processes translation requests concurrently. The TranslateTextNodeParallel:
-
Prepares batches, pairing the source text with each target language.
-
Executes translation calls to the LLM for all languages concurrently using
asyncoperations. -
Saves the translated content to individual files (
translations/README_LANGUAGE.md).
This approach leverages asyncio and parallel execution to speed up I/O-bound tasks like multiple API calls.
Example Output & Comparison
Running this parallel version significantly reduces the total time compared to a sequential approach:
# --- Sequential Run Output (from pocketflow-batch) ---
Starting sequential translation into 8 languages...
Translated Chinese text
...
Translated Korean text
Saved translation to translations/README_CHINESE.md
...
Saved translation to translations/README_KOREAN.md
Total sequential translation time: ~1136 seconds
=== Translation Complete ===
Translations saved to: translations
============================
# --- Parallel Run Output (this example) ---
Starting parallel translation into 8 languages...
Translated French text
Translated Portuguese text
... # Messages may appear interleaved
Translated Spanish text
Saved translation to translations/README_CHINESE.md
...
Saved translation to translations/README_KOREAN.md
Total parallel translation time: ~209 seconds
=== Translation Complete ===
Translations saved to: translations
============================
(Actual times will vary based on API response speed and system.)
Files
main.py: Implements the parallel batch translation node and flow.utils.py: Async wrapper for calling the Anthropic model.requirements.txt: Project dependencies (includesaiofiles).translations/: Output directory (created automatically).