# πŸ“„ Document Translation API A powerful Python API for translating complex structured documents (Excel, Word, PowerPoint) while **strictly preserving** the original formatting, layout, and embedded media. ## ✨ Features ### πŸ”„ Multiple Translation Providers | Provider | Type | Description | |----------|------|-------------| | **Google Translate** | Cloud | Free, fast, reliable | | **Ollama** | Local LLM | Privacy-focused, customizable with system prompts | | **WebLLM** | Browser | Runs entirely in browser using WebGPU | | **DeepL** | Cloud | High-quality translations (API key required) | | **LibreTranslate** | Self-hosted | Open-source alternative | ### πŸ“Š Excel Translation (.xlsx) - βœ… Translates all cell content and sheet names - βœ… Preserves cell merging, formulas, and styles - βœ… Maintains font styles, colors, and borders - βœ… Image text extraction with vision models - βœ… Adds translated image text as comments ### πŸ“ Word Translation (.docx) - βœ… Translates body text, headers, footers, and tables - βœ… Preserves heading styles and paragraph formatting - βœ… Maintains lists, images, charts, and SmartArt - βœ… Image text extraction and translation ### πŸ“½οΈ PowerPoint Translation (.pptx) - βœ… Translates slide titles, body text, and speaker notes - βœ… Preserves slide layouts, transitions, and animations - βœ… Image text extraction with text boxes added below images - βœ… Keeps layering order and positions ### 🧠 LLM Features (Ollama/WebLLM) - βœ… **Custom System Prompts**: Provide context for better translations - βœ… **Technical Glossary**: Define term mappings (e.g., `batterie=coil`) - βœ… **Presets**: HVAC, IT, Legal, Medical terminology - βœ… **Vision Models**: Translate text within images (gemma3, qwen3-vl, llava) ## πŸš€ Quick Start ### Installation ```powershell # Clone the repository git clone https://gitea.parsanet.org/sepehr/office_translator.git cd office_translator # Create virtual environment python -m venv venv .\venv\Scripts\Activate.ps1 # Install dependencies pip install -r requirements.txt # Run the API python main.py ``` The API starts on `http://localhost:8000` ### Web Interface Open `http://localhost:8000/static/index.html` for the full-featured web interface. ## πŸ“š API Documentation - **Swagger UI**: http://localhost:8000/docs - **ReDoc**: http://localhost:8000/redoc ## πŸ”§ API Endpoints ### POST /translate Translate a document with full customization. ```bash curl -X POST "http://localhost:8000/translate" \ -F "file=@document.xlsx" \ -F "target_language=en" \ -F "provider=ollama" \ -F "ollama_model=gemma3:12b" \ -F "translate_images=true" \ -F "system_prompt=You are translating HVAC documents. Use: batterie=coil, CTA=AHU" ``` ### Parameters | Parameter | Type | Default | Description | |-----------|------|---------|-------------| | `file` | File | required | Document to translate (.xlsx, .docx, .pptx) | | `target_language` | string | required | Target language code (en, fr, es, fa, etc.) | | `provider` | string | google | Translation provider (google, ollama, webllm, deepl, libre) | | `ollama_model` | string | llama3.2 | Ollama model name | | `translate_images` | bool | false | Extract and translate image text with vision | | `system_prompt` | string | "" | Custom instructions and glossary for LLM | ### GET /ollama/models List available Ollama models. ### POST /ollama/configure Configure Ollama settings. ### GET /health Health check endpoint. ## 🌐 Supported Languages | Code | Language | Code | Language | |------|----------|------|----------| | en | English | fr | French | | fa | Persian/Farsi | es | Spanish | | de | German | it | Italian | | pt | Portuguese | ru | Russian | | zh | Chinese | ja | Japanese | | ko | Korean | ar | Arabic | ## βš™οΈ Configuration ### Environment Variables (.env) ```env # Translation Service TRANSLATION_SERVICE=google # Ollama Configuration OLLAMA_BASE_URL=http://localhost:11434 OLLAMA_MODEL=llama3.2 # DeepL API Key (optional) DEEPL_API_KEY=your_api_key_here # File Limits MAX_FILE_SIZE_MB=50 # Directories UPLOAD_DIR=./uploads OUTPUT_DIR=./outputs ``` ### Ollama Setup ```bash # Install Ollama (Windows) winget install Ollama.Ollama # Pull a model ollama pull llama3.2 # For vision/image translation ollama pull gemma3:12b # or ollama pull qwen3-vl:8b ``` ## 🎯 Using System Prompts & Glossary ### Example: HVAC Translation **System Prompt:** ``` You are translating HVAC technical documents. Use precise technical terminology. Keep unit measurements (kW, mΒ³/h, Pa) unchanged. ``` **Glossary:** ``` batterie=coil groupe froid=chiller CTA=AHU (Air Handling Unit) Γ©changeur=heat exchanger vanne 3 voies=3-way valve ``` ### Presets Available - πŸ”§ **HVAC**: Heating, Ventilation, Air Conditioning - πŸ’» **IT**: Software and technology - βš–οΈ **Legal**: Legal documents - πŸ₯ **Medical**: Healthcare terminology ## πŸ”Œ MCP Integration This API can be used as an MCP (Model Context Protocol) server for AI assistants. ### VS Code Configuration Add to your VS Code `settings.json` or `.vscode/mcp.json`: ```json { "servers": { "document-translator": { "type": "stdio", "command": "python", "args": ["mcp_server.py"], "cwd": "D:/Translate", "env": { "PYTHONPATH": "D:/Translate" } } } } ``` ### MCP Tools Available | Tool | Description | |------|-------------| | `translate_document` | Translate a document file | | `list_ollama_models` | Get available Ollama models | | `get_supported_languages` | List supported language codes | | `configure_translation` | Set translation provider and options | ## πŸ—οΈ Project Structure ``` Translate/ β”œβ”€β”€ main.py # FastAPI application β”œβ”€β”€ config.py # Configuration β”œβ”€β”€ requirements.txt # Dependencies β”œβ”€β”€ mcp_server.py # MCP server implementation β”œβ”€β”€ services/ β”‚ └── translation_service.py # Translation providers β”œβ”€β”€ translators/ β”‚ β”œβ”€β”€ excel_translator.py # Excel with image support β”‚ β”œβ”€β”€ word_translator.py # Word with image support β”‚ └── pptx_translator.py # PowerPoint with image support β”œβ”€β”€ utils/ β”‚ β”œβ”€β”€ file_handler.py # File operations β”‚ └── exceptions.py # Custom exceptions β”œβ”€β”€ static/ β”‚ └── index.html # Web interface β”œβ”€β”€ uploads/ # Temporary uploads └── outputs/ # Translated files ``` ## πŸ§ͺ Testing 1. Start the API: `python main.py` 2. Open: http://localhost:8000/static/index.html 3. Configure Ollama model 4. Upload a document 5. Select target language and provider 6. Click "Translate Document" ## πŸ› οΈ Tech Stack - **FastAPI**: Modern async web framework - **openpyxl**: Excel manipulation - **python-docx**: Word documents - **python-pptx**: PowerPoint presentations - **deep-translator**: Google/DeepL/Libre translation - **requests**: Ollama API communication - **Uvicorn**: ASGI server ## πŸ“ License MIT License ## 🀝 Contributing Contributions welcome! Please submit a Pull Request. --- **Built with ❀️ using Python, FastAPI, and Ollama**