Spaces:
Sleeping
Sleeping
readme update
Browse files
README.md
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# IndicTrans2 Language Server
|
2 |
---
|
3 |
title: IndicTrans2 Translator API
|
4 |
emoji: 🌐
|
@@ -9,9 +8,9 @@ sdk_version: "1.0"
|
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
tags:
|
|
|
12 |
- fastapi
|
13 |
- docker
|
14 |
-
- translation
|
15 |
- indic
|
16 |
- multilingual
|
17 |
license: apache-2.0
|
@@ -19,379 +18,16 @@ license: apache-2.0
|
|
19 |
|
20 |
# IndicTrans2 Translator API
|
21 |
|
22 |
-
A multilingual
|
23 |
-
|
24 |
-
|
25 |
-
## 🌟 Features
|
26 |
-
|
27 |
-
- **Multilingual Support**: Translate between 12 major Indian languages and English
|
28 |
-
- **High-Quality Translations**: Powered by state-of-the-art IndicTrans2 models
|
29 |
-
- **REST API**: Simple HTTP API for easy integration
|
30 |
-
- **GPU Acceleration**: CUDA support for faster inference
|
31 |
-
- **Memory Optimization**: Efficient model loading and GPU memory management
|
32 |
-
- **Graceful Shutdown**: Proper cleanup of resources on server termination
|
33 |
-
|
34 |
-
## 🚀 Quick Start
|
35 |
-
|
36 |
-
### Prerequisites
|
37 |
-
|
38 |
-
- Python 3.7+
|
39 |
-
- CUDA-compatible GPU (recommended)
|
40 |
-
- At least 8GB GPU memory for optimal performance
|
41 |
-
|
42 |
-
### Installation
|
43 |
-
|
44 |
-
1. **Clone the repository**
|
45 |
-
```bash
|
46 |
-
git clone https://github.com/AI4Bharat/IndicTrans2
|
47 |
-
cd IndicTrans2
|
48 |
-
```
|
49 |
-
|
50 |
-
2. **Install dependencies**
|
51 |
-
```bash
|
52 |
-
# Install IndicTrans2 dependencies
|
53 |
-
source install.sh
|
54 |
-
|
55 |
-
# Install additional requirements for the server
|
56 |
-
pip install fastapi uvicorn torch transformers
|
57 |
-
```
|
58 |
-
|
59 |
-
3. **Install IndicTransToolkit**
|
60 |
-
```bash
|
61 |
-
cd huggingface_interface/IndicTransToolkit
|
62 |
-
pip install -e .
|
63 |
-
cd ../..
|
64 |
-
```
|
65 |
-
|
66 |
-
4. **Run the server**
|
67 |
-
```bash
|
68 |
-
python lang_server.py
|
69 |
-
```
|
70 |
-
|
71 |
-
The server will start on `http://0.0.0.0:9000`
|
72 |
|
73 |
-
##
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
| Language | Code | Script |
|
78 |
-
|----------|------|--------|
|
79 |
-
| English | `eng_Latn` | Latin |
|
80 |
-
| Bengali | `ben_Beng` | Bengali |
|
81 |
-
| Punjabi | `pan_Guru` | Gurmukhi |
|
82 |
-
| Assamese | `asm_Beng` | Bengali |
|
83 |
-
| Konkani | `gom_Deva` | Devanagari |
|
84 |
-
| Gujarati | `guj_Gujr` | Gujarati |
|
85 |
-
| Hindi | `hin_Deva` | Devanagari |
|
86 |
-
| Kannada | `kan_Knda` | Kannada |
|
87 |
-
| Malayalam | `mal_Mlym` | Malayalam |
|
88 |
-
| Odia | `ory_Orya` | Odia |
|
89 |
-
| Tamil | `tam_Taml` | Tamil |
|
90 |
-
| Telugu | `tel_Telu` | Telugu |
|
91 |
-
|
92 |
-
## 🔧 API Usage
|
93 |
-
|
94 |
-
### Translation Endpoint
|
95 |
-
|
96 |
-
**POST** `/language-server/translate`
|
97 |
-
|
98 |
-
#### Request Body
|
99 |
|
100 |
```json
|
101 |
{
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
}
|
106 |
-
```
|
107 |
-
|
108 |
-
#### Response
|
109 |
-
|
110 |
-
```json
|
111 |
-
{
|
112 |
-
"translation": "नमस्ते, आप कैसे हैं?"
|
113 |
-
}
|
114 |
-
```
|
115 |
-
|
116 |
-
#### Error Response
|
117 |
-
|
118 |
-
```json
|
119 |
-
{
|
120 |
-
"message": "Not a valid dialect"
|
121 |
-
}
|
122 |
-
```
|
123 |
-
|
124 |
-
### Example Usage
|
125 |
-
|
126 |
-
#### cURL
|
127 |
-
```bash
|
128 |
-
curl -X POST "http://localhost:9000/language-server/translate" \
|
129 |
-
-H "Content-Type: application/json" \
|
130 |
-
-d '{
|
131 |
-
"input_sentence": "Good morning!",
|
132 |
-
"source_lan": "eng_Latn",
|
133 |
-
"target_lang": "hin_Deva"
|
134 |
-
}'
|
135 |
-
```
|
136 |
-
|
137 |
-
#### Python
|
138 |
-
```python
|
139 |
-
import requests
|
140 |
-
|
141 |
-
url = "http://localhost:9000/language-server/translate"
|
142 |
-
data = {
|
143 |
-
"input_sentence": "Good morning!",
|
144 |
-
"source_lan": "eng_Latn",
|
145 |
-
"target_lang": "hin_Deva"
|
146 |
-
}
|
147 |
-
|
148 |
-
response = requests.post(url, json=data)
|
149 |
-
print(response.json())
|
150 |
-
```
|
151 |
-
|
152 |
-
#### JavaScript
|
153 |
-
```javascript
|
154 |
-
const response = await fetch('http://localhost:9000/language-server/translate', {
|
155 |
-
method: 'POST',
|
156 |
-
headers: {
|
157 |
-
'Content-Type': 'application/json',
|
158 |
-
},
|
159 |
-
body: JSON.stringify({
|
160 |
-
input_sentence: 'Good morning!',
|
161 |
-
source_lan: 'eng_Latn',
|
162 |
-
target_lang: 'hin_Deva'
|
163 |
-
})
|
164 |
-
});
|
165 |
-
|
166 |
-
const result = await response.json();
|
167 |
-
console.log(result);
|
168 |
-
```
|
169 |
-
|
170 |
-
## ⚡ Performance Optimization
|
171 |
-
|
172 |
-
The server is optimized for production use with several performance features:
|
173 |
-
|
174 |
-
### Model Configuration
|
175 |
-
- **Distilled Models**: Uses 200M parameter distilled models for faster inference
|
176 |
-
- **Memory Efficient**: Automatic GPU memory cleanup after each request
|
177 |
-
- **Batch Processing**: Supports batch translation for multiple sentences
|
178 |
-
|
179 |
-
### Recommended Settings for Speed
|
180 |
-
To optimize performance, you can modify the following in `lang_server.py`:
|
181 |
-
|
182 |
-
```python
|
183 |
-
# Enable quantization for faster inference
|
184 |
-
quantization = "4-bit" # or "8-bit"
|
185 |
-
|
186 |
-
# Reduce generation parameters for speed
|
187 |
-
max_length = 128 # Reduced from 256
|
188 |
-
num_beams = 1 # Greedy decoding for fastest results
|
189 |
-
```
|
190 |
-
|
191 |
-
## 🏗️ Architecture
|
192 |
-
|
193 |
-
The server uses a dual-model architecture:
|
194 |
-
|
195 |
-
1. **English → Indic Model**: `ai4bharat/indictrans2-en-indic-dist-200M`
|
196 |
-
2. **Indic → English Model**: `ai4bharat/indictrans2-indic-en-dist-200M`
|
197 |
-
|
198 |
-
The appropriate model is automatically selected based on the target language:
|
199 |
-
- If target is English (`eng_Latn`): Uses Indic→English model
|
200 |
-
- If target is any Indic language: Uses English→Indic model
|
201 |
-
|
202 |
-
## 🔧 Configuration
|
203 |
-
|
204 |
-
### Environment Variables
|
205 |
-
|
206 |
-
| Variable | Default | Description |
|
207 |
-
|----------|---------|-------------|
|
208 |
-
| `BATCH_SIZE` | `4` | Batch size for translation |
|
209 |
-
| `DEVICE` | `cuda` | Device for model inference |
|
210 |
-
|
211 |
-
### Model Selection
|
212 |
-
|
213 |
-
You can switch between different model variants by modifying the checkpoint directories:
|
214 |
-
|
215 |
-
```python
|
216 |
-
# For base models (higher quality, slower)
|
217 |
-
en_indic_ckpt_dir = "ai4bharat/indictrans2-en-indic-1B"
|
218 |
-
|
219 |
-
# For distilled models (faster, good quality)
|
220 |
-
en_indic_ckpt_dir = "ai4bharat/indictrans2-en-indic-dist-200M"
|
221 |
-
```
|
222 |
-
|
223 |
-
## 🐳 Docker Deployment
|
224 |
-
|
225 |
-
Create a `Dockerfile`:
|
226 |
-
|
227 |
-
```dockerfile
|
228 |
-
FROM nvidia/cuda:11.8-devel-ubuntu20.04
|
229 |
-
|
230 |
-
# Install Python and dependencies
|
231 |
-
RUN apt-get update && apt-get install -y python3 python3-pip git
|
232 |
-
WORKDIR /app
|
233 |
-
|
234 |
-
# Clone and setup IndicTrans2
|
235 |
-
RUN git clone https://github.com/AI4Bharat/IndicTrans2 .
|
236 |
-
RUN source install.sh
|
237 |
-
RUN pip install fastapi uvicorn
|
238 |
-
|
239 |
-
# Install IndicTransToolkit
|
240 |
-
WORKDIR /app/huggingface_interface/IndicTransToolkit
|
241 |
-
RUN pip install -e .
|
242 |
-
WORKDIR /app
|
243 |
-
|
244 |
-
# Copy your server file
|
245 |
-
COPY lang_server.py .
|
246 |
-
|
247 |
-
# Expose port
|
248 |
-
EXPOSE 9000
|
249 |
-
|
250 |
-
# Run the server
|
251 |
-
CMD ["python3", "lang_server.py"]
|
252 |
-
```
|
253 |
-
|
254 |
-
Build and run:
|
255 |
-
```bash
|
256 |
-
docker build -t indictrans2-server .
|
257 |
-
docker run --gpus all -p 9000:9000 indictrans2-server
|
258 |
-
```
|
259 |
-
|
260 |
-
## 📊 Benchmarks
|
261 |
-
|
262 |
-
The IndicTrans2 models achieve state-of-the-art performance on various benchmarks:
|
263 |
-
|
264 |
-
- **FLORES-22**: Comprehensive evaluation across 22 languages
|
265 |
-
- **IN22**: New benchmark with 1024 sentences across multiple domains
|
266 |
-
- **chrF++**: Primary evaluation metric for translation quality
|
267 |
-
|
268 |
-
For detailed benchmark results, refer to the [IndicTrans2 paper](https://arxiv.org/abs/2305.16307).
|
269 |
-
|
270 |
-
## 🛠️ Development
|
271 |
-
|
272 |
-
### Running in Development Mode
|
273 |
-
|
274 |
-
```bash
|
275 |
-
# Install development dependencies
|
276 |
-
pip install fastapi[all] uvicorn[standard]
|
277 |
-
|
278 |
-
# Run with auto-reload
|
279 |
-
uvicorn lang_server:app --host 0.0.0.0 --port 9000 --reload
|
280 |
-
```
|
281 |
-
|
282 |
-
### Testing
|
283 |
-
|
284 |
-
```bash
|
285 |
-
# Test the translation endpoint
|
286 |
-
python -c "
|
287 |
-
import requests
|
288 |
-
response = requests.post('http://localhost:9000/language-server/translate',
|
289 |
-
json={'input_sentence': 'Hello', 'source_lan': 'eng_Latn', 'target_lang': 'hin_Deva'})
|
290 |
-
print(response.json())
|
291 |
-
"
|
292 |
-
```
|
293 |
-
|
294 |
-
## 🚦 Production Deployment
|
295 |
-
|
296 |
-
### Using Gunicorn
|
297 |
-
|
298 |
-
```bash
|
299 |
-
pip install gunicorn
|
300 |
-
|
301 |
-
# Run with multiple workers
|
302 |
-
gunicorn lang_server:app -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:9000
|
303 |
-
```
|
304 |
-
|
305 |
-
### Nginx Configuration
|
306 |
-
|
307 |
-
```nginx
|
308 |
-
server {
|
309 |
-
listen 80;
|
310 |
-
server_name your-domain.com;
|
311 |
-
|
312 |
-
location / {
|
313 |
-
proxy_pass http://127.0.0.1:9000;
|
314 |
-
proxy_set_header Host $host;
|
315 |
-
proxy_set_header X-Real-IP $remote_addr;
|
316 |
-
}
|
317 |
}
|
318 |
-
```
|
319 |
-
|
320 |
-
## 🔍 Troubleshooting
|
321 |
-
|
322 |
-
### Common Issues
|
323 |
-
|
324 |
-
1. **CUDA Out of Memory**
|
325 |
-
- Reduce `BATCH_SIZE` in the code
|
326 |
-
- Enable quantization: `quantization = "4-bit"`
|
327 |
-
- Use smaller model variant
|
328 |
-
|
329 |
-
2. **Slow Performance**
|
330 |
-
- Ensure GPU is available and being used
|
331 |
-
- Enable quantization for faster inference
|
332 |
-
- Reduce `max_length` and `num_beams` parameters
|
333 |
-
|
334 |
-
3. **Model Loading Issues**
|
335 |
-
- Check internet connection for model downloading
|
336 |
-
- Verify sufficient disk space (models are ~2GB each)
|
337 |
-
- Ensure proper CUDA installation
|
338 |
-
|
339 |
-
### Monitoring
|
340 |
-
|
341 |
-
```python
|
342 |
-
# Add to your server for monitoring
|
343 |
-
import psutil
|
344 |
-
import GPUtil
|
345 |
-
|
346 |
-
@app.get("/health")
|
347 |
-
def health_check():
|
348 |
-
gpu = GPUtil.getGPUs()[0] if GPUtil.getGPUs() else None
|
349 |
-
return {
|
350 |
-
"status": "healthy",
|
351 |
-
"gpu_memory": f"{gpu.memoryUsed}/{gpu.memoryTotal}MB" if gpu else "No GPU",
|
352 |
-
"cpu_percent": psutil.cpu_percent(),
|
353 |
-
"memory_percent": psutil.virtual_memory().percent
|
354 |
-
}
|
355 |
-
```
|
356 |
-
|
357 |
-
## 📄 License
|
358 |
-
|
359 |
-
This project uses the IndicTrans2 models which are released under the MIT License. See the [LICENSE](https://github.com/AI4Bharat/IndicTrans2/blob/main/LICENSE) file for details.
|
360 |
-
|
361 |
-
## 🤝 Contributing
|
362 |
-
|
363 |
-
1. Fork the repository
|
364 |
-
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
365 |
-
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
366 |
-
4. Push to the branch (`git push origin feature/amazing-feature`)
|
367 |
-
5. Open a Pull Request
|
368 |
-
|
369 |
-
## 🙏 Acknowledgments
|
370 |
-
|
371 |
-
- [AI4Bharat](https://ai4bharat.iitm.ac.in/) for the IndicTrans2 models
|
372 |
-
- [Hugging Face](https://huggingface.co/) for model hosting and transformers library
|
373 |
-
- [FastAPI](https://fastapi.tiangolo.com/) for the excellent web framework
|
374 |
-
|
375 |
-
## 📚 Citation
|
376 |
-
|
377 |
-
If you use this server in your research, please cite the IndicTrans2 paper:
|
378 |
-
|
379 |
-
```bibtex
|
380 |
-
@article{gala2023indictrans,
|
381 |
-
title={IndicTrans2: Towards High-Quality and Accessible Machine Translation Models for all 22 Scheduled Indian Languages},
|
382 |
-
author={Jay Gala and Pranjal A Chitale and A K Raghavan and Varun Gumma and Sumanth Doddapaneni and Aswanth Kumar M and Janki Atul Nawale and Anupama Sujatha and Ratish Puduppully and Vivek Raghavan and Pratyush Kumar and Mitesh M Khapra and Raj Dabre and Anoop Kunchukuttan},
|
383 |
-
journal={Transactions on Machine Learning Research},
|
384 |
-
issn={2835-8856},
|
385 |
-
year={2023},
|
386 |
-
url={https://openreview.net/forum?id=vfT4YuzAYA},
|
387 |
-
}
|
388 |
-
```
|
389 |
-
|
390 |
-
## 🔗 Links
|
391 |
-
|
392 |
-
- [IndicTrans2 GitHub](https://github.com/AI4Bharat/IndicTrans2)
|
393 |
-
- [IndicTrans2 Paper](https://arxiv.org/abs/2305.16307)
|
394 |
-
- [AI4Bharat Website](https://ai4bharat.iitm.ac.in/)
|
395 |
-
- [Demo](https://models.ai4bharat.org/#/nmt/v2)
|
396 |
-
- [Colab Notebook](https://colab.research.google.com/github/AI4Bharat/IndicTrans2/blob/main/huggingface_interface/colab_inference.ipynb)
|
397 |
-
|
|
|
|
|
1 |
---
|
2 |
title: IndicTrans2 Translator API
|
3 |
emoji: 🌐
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
tags:
|
11 |
+
- translation
|
12 |
- fastapi
|
13 |
- docker
|
|
|
14 |
- indic
|
15 |
- multilingual
|
16 |
license: apache-2.0
|
|
|
18 |
|
19 |
# IndicTrans2 Translator API
|
20 |
|
21 |
+
🌐 A multilingual translation API powered by AI4Bharat's IndicTrans2.
|
22 |
+
Supports English ↔ Indian languages (Hindi, Kannada, Tamil, etc.)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
## 🛠 API Endpoint
|
25 |
|
26 |
+
**POST** `/api/v1/translate`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
```json
|
29 |
{
|
30 |
+
"input_sentence": "Hello",
|
31 |
+
"source_lan": "eng_Latn",
|
32 |
+
"target_lang": "hin_Deva"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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