File size: 6,126 Bytes
c52255c
 
 
 
 
 
 
 
 
 
 
0a32234
 
 
 
 
 
 
 
 
 
 
 
 
 
3bb7d92
 
 
 
 
 
 
 
 
 
 
 
 
0a32234
 
 
 
 
 
 
 
 
 
 
3bb7d92
0a32234
3bb7d92
0a32234
 
3bb7d92
0a32234
3bb7d92
 
0a32234
3bb7d92
 
0a32234
3bb7d92
 
0a32234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
---
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 `/app` directory
- Simplified Dockerfile structure for better reliability

### Python Dependencies
- Removed conflicting `pathlib` dependency (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:
```bash
export OPENROUTER_API_KEY="your_api_key_here"
```

### Local Development

1. **Quick Setup** (recommended):
   ```bash
   python setup.py
   ```

2. **Manual Setup**:
   ```bash
   # 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

1. **Build the Image**:
   ```bash
   docker build -t document-translator .
   ```

2. **Run the Container**:
   ```bash
   docker run -p 7860:7860 -e OPENROUTER_API_KEY="your_api_key" document-translator
   ```

### Hugging Face Spaces Deployment

1. **Create a new Space** on Hugging Face
2. **Upload all files** from this repository
3. **Set the environment variable**:
   - Go to Settings β†’ Repository secrets
   - Add `OPENROUTER_API_KEY` with your API key
4. **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

1. **File Upload**: User uploads PDF or DOCX file through web interface
2. **Format Detection**: System identifies file type and validates
3. **Conversion** (if needed): PDF files are converted to DOCX using LibreOffice
4. **Text Extraction**: Document content is extracted paragraph by paragraph
5. **AI Translation**: Each paragraph is sent to OpenRouter API for translation
6. **Document Reconstruction**: Translated text replaces original while preserving formatting
7. **Final Conversion**: If original was PDF, translated DOCX is converted back to PDF
8. **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

1. **Sign up** at [OpenRouter](https://openrouter.ai)
2. **Get your API key** from the dashboard
3. **Add credits** to your account for model usage
4. **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:
1. Check the application logs for error details
2. Verify your OpenRouter API key is valid
3. Ensure LibreOffice is properly installed
4. Check network connectivity for API calls

## πŸ”„ Version History

- **v1.0.0**: Initial release with PDF/DOCX support, OpenRouter integration, and web interface