How xablin/hf_en-ru-04_me_cli works
Purpose and Entry Points
This is a local EN↔RU machine-translation application built on Hugging Face Marian/OPUS models. It supports an interactive console workflow and two desktop GUI implementations. main.py is the primary launcher, selecting mode and translation direction; direct UI entry points also exist in translate_cli.py, translate_gui_tk.py, and translate_gui_qt.py.
Startup and Dependency Gate
main.py coordinates startup: parses launch options, invokes dependency validation, initializes settings, verifies/downloads models, then starts the selected UI. dependency_checker.py checks required Python packages and Tkinter availability before application execution. Runtime dependencies are declared in requirements.txt.
Translation Core
translator.py is the central application service. It owns direction-aware model selection, Hugging Face tokenizer/model loading, device selection, and text translation. The supported model boundary is Hugging Face model IDs for Helsinki-NLP OPUS Marian EN→RU and RU→EN models, cached locally by the Hugging Face stack.
Model Lifecycle and External Services
The translator checks whether required models are locally available and can interactively download missing artifacts from Hugging Face Hub. Authentication is optional but supported through the HF_TOKEN environment variable. Model files are stored in the standard Hugging Face cache (~/.cache/huggingface/); inference uses PyTorch and may use CUDA when available, otherwise CPU.
Interface Adapters
The UI layer is separated by runtime:
• Console interactive translation loop: translate_cli.py
• Tkinter desktop UI: translate_gui_tk.py
• Qt desktop UI: translate_gui_qt.py
• Shared/help-oriented dialog surface: help_dialog.py
These adapters collect source text and direction choices, invoke the translator service, and present results. They should not own model lifecycle or inference policy.
Configuration and Persistent User State
config.py defines application-level defaults and constants, including translation directions and likely model/UI settings. settings_manager.py manages persisted user preferences and translation-history-related state, providing a boundary between UI interactions and local storage behavior. Relevant paths: config.py, settings_manager.py, translator.py.
Primary Control Flow
1. Launch through main.py with optional direction/UI arguments.
2. Validate local dependencies through dependency_checker.py.
3. Resolve settings and translation direction via settings_manager.py and config.py.
4. Check/download and load the selected Marian model in translator.py.
5. Run a CLI or GUI adapter, repeatedly passing text to the loaded translator.
6. Return translated text to the active interface; retain user state/history through the settings layer where enabled.
Components 15
Startup
- Application launcher · Selects mode and translation direction before starting an interface.
main.py - Dependency checker · Validates required packages and Tkinter availability.
dependency_checker.py - Runtime dependencies
requirements.txt
Interface adapters
- Interactive console
translate_cli.py - Tkinter desktop UI
translate_gui_tk.py - Qt desktop UI
translate_gui_qt.py - Help dialog
help_dialog.py
Translation core
- Application defaults · Defines translation directions and application defaults.
config.py - Settings manager · Persists user preferences and translation-related history.
settings_manager.py - Translator service · Selects direction-aware Marian models and performs inference.
translator.py - Model selection · Maps EN↔RU directions to supported OPUS Marian model IDs.
translator.py - Tokenizer and inference · Loads Hugging Face models and runs PyTorch on CUDA or CPU.
translator.py
Model services
- Hugging Face Hub · Supplies missing Helsinki-NLP OPUS Marian artifacts.
- Hugging Face cache · Stores downloaded model artifacts locally.
- HF_TOKEN · Environment variable used for optional Hub authentication.
Connections 22
- Application launcher → Dependency checker: validates before launch
- Dependency checker → Runtime dependencies: checks packages
- Application launcher → Application defaults: reads defaults
- Application launcher → Settings manager: initializes
- Application launcher → Translator service: prepares selected direction
- Application launcher → Interactive console: launches selected UI
- Application launcher → Tkinter desktop UI: launches selected UI
- Application launcher → Qt desktop UI: launches selected UI
- Settings manager → Application defaults: uses defaults
- Translator service → Model selection: selects model
- Model selection → Tokenizer and inference: provides model ID
- Tokenizer and inference → Hugging Face cache: loads cached artifacts
- Translator service → Hugging Face Hub: downloads missing models
- Hugging Face Hub → Hugging Face cache: stores artifacts
- HF_TOKEN → Hugging Face Hub: optional authentication
- Interactive console → Translator service: translates entered text
- Tkinter desktop UI → Translator service: translates entered text
- Qt desktop UI → Translator service: translates entered text
- Tkinter desktop UI → Settings manager: persists UI state
- Qt desktop UI → Settings manager: persists UI state
- Help dialog → Tkinter desktop UI: shared help surface
- Help dialog → Qt desktop UI: shared help surface