# πŸ“„ Document Translation API A powerful SaaS-ready 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 | | **OpenAI** | Cloud | GPT-4o/4o-mini with vision support | ### πŸ“Š 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/OpenAI) - βœ… **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, gpt-4o) ### 🏒 SaaS-Ready Features - 🚦 **Rate Limiting**: Per-client IP with token bucket and sliding window algorithms - πŸ”’ **Security Headers**: CSP, XSS protection, HSTS support - 🧹 **Auto Cleanup**: Automatic file cleanup with TTL tracking - πŸ“Š **Monitoring**: Health checks, metrics, and system status - πŸ” **Admin Dashboard**: Secure admin panel with authentication - πŸ“ **Request Logging**: Structured logging with unique request IDs ## πŸš€ 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` ### Frontend Setup ```powershell cd frontend npm install npm run dev ``` Frontend runs on `http://localhost:3000` ## πŸ“š API Documentation - **Swagger UI**: http://localhost:8000/docs - **ReDoc**: http://localhost:8000/redoc ## πŸ”§ API Endpoints ### Translation #### 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." ``` ### Monitoring #### GET /health Comprehensive health check with system status. ```json { "status": "healthy", "translation_service": "google", "memory": {"system_percent": 34.1, "system_available_gb": 61.7}, "disk": {"total_files": 0, "total_size_mb": 0}, "cleanup_service": {"is_running": true} } ``` #### GET /metrics System metrics and statistics. #### GET /rate-limit/status Current rate limit status for the requesting client. ### Admin Endpoints (Authentication Required) #### POST /admin/login Login to admin dashboard. ```bash curl -X POST "http://localhost:8000/admin/login" \ -F "username=admin" \ -F "password=your_password" ``` Response: ```json { "status": "success", "token": "your_bearer_token", "expires_in": 86400 } ``` #### GET /admin/dashboard Get comprehensive dashboard data (requires Bearer token). ```bash curl "http://localhost:8000/admin/dashboard" \ -H "Authorization: Bearer your_token" ``` #### POST /admin/cleanup/trigger Manually trigger file cleanup. #### GET /admin/files/tracked List currently tracked files. ## 🌐 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 Services ============== TRANSLATION_SERVICE=google DEEPL_API_KEY=your_deepl_api_key_here # Ollama Configuration OLLAMA_BASE_URL=http://localhost:11434 OLLAMA_MODEL=llama3 OLLAMA_VISION_MODEL=llava # ============== File Limits ============== MAX_FILE_SIZE_MB=50 # ============== Rate Limiting (SaaS) ============== RATE_LIMIT_ENABLED=true RATE_LIMIT_PER_MINUTE=30 RATE_LIMIT_PER_HOUR=200 TRANSLATIONS_PER_MINUTE=10 TRANSLATIONS_PER_HOUR=50 MAX_CONCURRENT_TRANSLATIONS=5 # ============== Cleanup Service ============== CLEANUP_ENABLED=true CLEANUP_INTERVAL_MINUTES=15 FILE_TTL_MINUTES=60 INPUT_FILE_TTL_MINUTES=30 OUTPUT_FILE_TTL_MINUTES=120 # ============== Security ============== ENABLE_HSTS=false CORS_ORIGINS=* # ============== Admin Authentication ============== ADMIN_USERNAME=admin ADMIN_PASSWORD=changeme123 # Change in production! # Or use a SHA256 hash: # ADMIN_PASSWORD_HASH=your_sha256_hash # ============== Monitoring ============== LOG_LEVEL=INFO ENABLE_REQUEST_LOGGING=true MAX_MEMORY_PERCENT=80 ``` ### 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 ## οΏ½ Admin Dashboard Access the admin dashboard at `/admin` in the frontend. Features: - **System Status**: Health, uptime, and issues - **Memory & Disk Monitoring**: Real-time usage stats - **Translation Statistics**: Total translations, success rate - **Rate Limit Management**: View active clients and limits - **Cleanup Service**: Monitor and trigger manual cleanup ### Default Credentials - **Username**: admin - **Password**: changeme123 ⚠️ **Change the default password in production!** ## πŸ—οΈ Project Structure ``` Translate/ β”œβ”€β”€ main.py # FastAPI application with SaaS features β”œβ”€β”€ config.py # Configuration with SaaS settings β”œβ”€β”€ requirements.txt # Dependencies β”œβ”€β”€ mcp_server.py # MCP server implementation β”œβ”€β”€ middleware/ # SaaS middleware β”‚ β”œβ”€β”€ __init__.py β”‚ β”œβ”€β”€ rate_limiting.py # Rate limiting with token bucket β”‚ β”œβ”€β”€ validation.py # Input validation β”‚ β”œβ”€β”€ security.py # Security headers & logging β”‚ └── cleanup.py # Auto cleanup service β”œβ”€β”€ 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 β”œβ”€β”€ frontend/ # Next.js frontend β”‚ β”œβ”€β”€ src/ β”‚ β”‚ β”œβ”€β”€ app/ β”‚ β”‚ β”‚ β”œβ”€β”€ page.tsx # Main translation page β”‚ β”‚ β”‚ β”œβ”€β”€ admin/ # Admin dashboard β”‚ β”‚ β”‚ └── settings/ # Settings pages β”‚ β”‚ └── components/ β”‚ └── package.json β”œβ”€β”€ static/ β”‚ └── webllm.html # WebLLM standalone interface β”œβ”€β”€ uploads/ # Temporary uploads (auto-cleaned) └── outputs/ # Translated files (auto-cleaned) ``` ## πŸ› οΈ Tech Stack ### Backend - **FastAPI**: Modern async web framework - **openpyxl**: Excel manipulation - **python-docx**: Word documents - **python-pptx**: PowerPoint presentations - **deep-translator**: Google/DeepL/Libre translation - **psutil**: System monitoring - **python-magic**: File type validation ### Frontend - **Next.js 15**: React framework - **Tailwind CSS**: Styling - **Lucide Icons**: Icon library - **WebLLM**: Browser-based LLM ## πŸ”Œ 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" } } } } ``` ## πŸš€ Production Deployment ### Security Checklist - [ ] Change `ADMIN_PASSWORD` or set `ADMIN_PASSWORD_HASH` - [ ] Set `CORS_ORIGINS` to your frontend domain - [ ] Enable `ENABLE_HSTS=true` if using HTTPS - [ ] Configure rate limits appropriately - [ ] Set up log rotation for `logs/` directory - [ ] Use a reverse proxy (nginx/traefik) for HTTPS ### Docker Deployment (Coming Soon) ```dockerfile FROM python:3.11-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . EXPOSE 8000 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] ``` ## πŸ“ License MIT License ## 🀝 Contributing Contributions welcome! Please submit a Pull Request. --- **Built with ❀️ using Python, FastAPI, Next.js, and Ollama**