title: Document Translator
emoji: π
colorFrom: blue
colorTo: purple
sdk: docker
app_file: app.py
pinned: false
license: mit
π Document Translator - Hugging Face Space
A powerful document translation service that converts PDF and DOCX files using AI models from OpenRouter. Built with FastAPI and a simple HTML interface (no Gradio dependency).
β¨ Features
- File Support: Upload PDF or DOCX documents
- AI Translation: Uses Google Gemini 2.5 Pro model from OpenRouter
- Format Preservation: Maintains original document formatting
- Batch Processing: Translates documents paragraph by paragraph
- Multiple Languages: Support for 12+ languages including Arabic, English, Spanish, French, German, etc.
- Download Options: Get both original and translated files
- Translation Reports: Detailed statistics on processed content
π§ Fixed Issues
Docker Build Permissions
The original Docker build was failing due to permission issues when creating directories as a non-root user. This has been fixed by:
- Creating directories before switching to non-root user
- Properly setting ownership of the
/appdirectory - Simplified Dockerfile structure for better reliability
Python Dependencies
- Removed conflicting
pathlibdependency (built into Python 3.11) - Updated requirements.txt with compatible versions
- Added proper Python path handling in main.py
π Quick Start
Environment Variables
Set your OpenRouter API key:
export OPENROUTER_API_KEY="your_api_key_here"
Local Development
Quick Setup (recommended):
python setup.pyManual Setup:
# Set your API key export OPENROUTER_API_KEY="your_key_here" # Install dependencies pip install -r requirements.txt # Run the app python app.py
Docker Deployment
Build the Image:
docker build -t document-translator .Run the Container:
docker run -p 7860:7860 -e OPENROUTER_API_KEY="your_api_key" document-translator
Hugging Face Spaces Deployment
- Create a new Space on Hugging Face
- Upload all files from this repository
- Set the environment variable:
- Go to Settings β Repository secrets
- Add
OPENROUTER_API_KEYwith your API key
- Your Space will be automatically built and deployed
π Project Structure
.
βββ app/
β βββ main.py # FastAPI backend application
βββ web/
β βββ index.html # Frontend interface
β βββ style.css # Styling
β βββ app.js # Frontend logic
βββ translator.py # Translation logic and OpenRouter integration
βββ Dockerfile # Container configuration
βββ requirements.txt # Python dependencies
βββ README.md # This file
π§ How It Works
Translation Process
- File Upload: User uploads PDF or DOCX file through web interface
- Format Detection: System identifies file type and validates
- Conversion (if needed): PDF files are converted to DOCX using LibreOffice
- Text Extraction: Document content is extracted paragraph by paragraph
- AI Translation: Each paragraph is sent to OpenRouter API for translation
- Document Reconstruction: Translated text replaces original while preserving formatting
- Final Conversion: If original was PDF, translated DOCX is converted back to PDF
- Download: User receives both original and translated files plus translation report
Translation Model
The application uses Google Gemini 2.5 Pro (google/gemini-2.5-pro-exp-03-25) as the default translation model through OpenRouter. This model provides:
- High-quality translation accuracy
- Support for multiple languages
- Advanced context understanding
- Optimized performance for document translation
Supported Languages
- Auto-detect source language
- Target languages: English, Arabic, Spanish, French, German, Italian, Portuguese, Russian, Japanese, Korean, Chinese, Hindi
π OpenRouter Setup
- Sign up at OpenRouter
- Get your API key from the dashboard
- Add credits to your account for model usage
- Set the environment variable
OPENROUTER_API_KEY
π API Endpoints
GET /
Serves the main HTML interface
GET /models
Returns available translation models
POST /translate
Translates a document
- file: PDF or DOCX file
- model: Model ID from OpenRouter
- source_language: Source language code (default: "auto")
- target_language: Target language code (default: "en")
GET /download/{file_path}
Downloads processed files
GET /health
Health check endpoint
π Security & Limits
- File size limit: 50MB maximum
- Supported formats: PDF, DOCX only
- Rate limiting: Built-in delays to respect API limits
- File cleanup: Temporary files are automatically removed
- Error handling: Comprehensive error handling and user feedback
π Development
Adding New Languages
Edit the language options in web/index.html and ensure the language codes are supported by your chosen translation models.
Custom Models
Add new models in the _get_default_models() method in translator.py or they will be automatically fetched from OpenRouter.
Styling
Modify web/style.css to customize the appearance. The current theme uses a gradient background with a clean, modern interface.
π License
This project is open source and available under the MIT License.
π€ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
π Support
For issues and questions:
- Check the application logs for error details
- Verify your OpenRouter API key is valid
- Ensure LibreOffice is properly installed
- Check network connectivity for API calls
π Version History
- v1.0.0: Initial release with PDF/DOCX support, OpenRouter integration, and web interface