Real-time translation from Catalan to multiple languages
High-performance translation microservice powered by Projecte AINA CTranslate2 models.
Features • Quick Start • API Reference • Deployment • Performance
AINA Translator is a production-ready FastAPI microservice that provides real-time translation from Catalan to 6 target languages. Built with CTranslate2 for maximum performance, it achieves sub-second translation latency on GPU and supports parallel multi-language translation.
| Source | Targets |
|---|---|
Catalan (ca) |
English, Spanish, French, Portuguese, Italian, German |
en, es, fr, pt, it, de |
- 🚀 High Performance: Sub-second translation latency with CTranslate2 optimization
- 🔄 Parallel Translation: Translate to multiple languages simultaneously
- 🎮 GPU Accelerated: Automatic CUDA detection with float16 precision
- 🔌 REST API: Clean FastAPI interface with automatic OpenAPI docs
- 🐳 Docker Ready: Containerized deployment with GPU support
- ⚡ Async Architecture: ThreadPoolExecutor for concurrent translations
- 🎯 Production Ready: Health checks, validation, and comprehensive error handling
- 📊 Configurable: Beam search, max tokens, and worker pool customization
- Framework: FastAPI 0.104+
- Translation Engine: CTranslate2 4.0+
- Tokenization: PyOnmttok 1.37+ (SentencePiece)
- Models: Projecte AINA (Hugging Face)
- Server: Uvicorn with async workers
- Container: Docker with NVIDIA GPU support
- Python 3.10+
- CUDA 11+ (optional, for GPU acceleration)
- 8GB+ RAM (16GB+ recommended for all models)
- Docker (optional, for containerized deployment)
# Clone the repository
git clone https://github.com/yourusername/aina-translator.git
cd aina-translator
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Run the service
uvicorn app:app --host 0.0.0.0 --port 8000The service will download models on first startup (~2GB total). This takes 2-5 minutes depending on your connection.
# Build the image
docker build -t aina-translator .
# Run with GPU support
docker run --gpus all -p 8000:8000 aina-translator
# Run CPU-only
docker run -p 8000:8000 aina-translator# Check health
curl http://localhost:8000/health
# Test translation
curl -X POST http://localhost:8000/translate \
-H "Content-Type: application/json" \
-d '{
"text": "Bon dia, com estàs?",
"targets": ["en", "es"]
}'http://localhost:8000
Root endpoint with service information.
Response:
{
"service": "AINA Translator",
"version": "2.0.0",
"engine": "CTranslate2",
"docs": "/docs",
"health": "/health"
}Health check endpoint showing service status and loaded models.
Response:
{
"status": "ok",
"device": "cuda",
"models_loaded": ["en", "es", "fr", "pt", "it", "de"]
}Translate Catalan text to one or more target languages.
Request Body:
| Field | Type | Required | Description |
|---|---|---|---|
text |
string | Yes | Catalan source text (min 1 char) |
targets |
array | Yes | List of target language codes |
max_decoding_length |
integer | No | Max tokens to generate (default: 256, max: 1024) |
beam_size |
integer | No | Beam search width (default: 1, max: 10) |
Example Request:
{
"text": "La intel·ligència artificial està transformant el món",
"targets": ["en", "es", "fr"],
"beam_size": 1,
"max_decoding_length": 256
}Example Response:
{
"en": "Artificial intelligence is transforming the world",
"es": "La inteligencia artificial está transformando el mundo",
"fr": "L'intelligence artificielle transforme le monde"
}Notes:
- Unknown language codes return
null - All translations run in parallel
- Response time scales with number of targets
import requests
url = "http://localhost:8000/translate"
# Single language
response = requests.post(url, json={
"text": "Avui fa molt bon temps",
"targets": ["en"]
})
print(response.json())
# {"en": "Today the weather is very nice"}
# Multiple languages
response = requests.post(url, json={
"text": "M'agrada programar en Python",
"targets": ["en", "es", "fr"]
})
print(response.json())
# {
# "en": "I like programming in Python",
# "es": "Me gusta programar en Python",
# "fr": "J'aime programmer en Python"
# }
# With beam search for better quality
response = requests.post(url, json={
"text": "El president va anunciar noves mesures econòmiques",
"targets": ["en"],
"beam_size": 4
})
print(response.json())const response = await fetch('http://localhost:8000/translate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: 'Barcelona és una ciutat increïble',
targets: ['en', 'es']
})
});
const data = await response.json();
console.log(data);
// {
// "en": "Barcelona is an incredible city",
// "es": "Barcelona es una ciudad increíble"
// }# Translate to all languages
curl -X POST http://localhost:8000/translate \
-H "Content-Type: application/json" \
-d '{
"text": "Hola, necessito ajuda amb aquest projecte",
"targets": ["en", "es", "fr", "pt", "it", "de"]
}'-
Create RunPod Pod
- Select GPU: RTX 5090 / A100 / RTX 4090 recommended
- Template: PyTorch 2.0+ with CUDA 11.8+
- Disk: 20GB+ recommended
-
Deploy Container
# SSH into your pod
ssh root@<pod-ip>
# Pull and run
docker pull ghcr.io/yourusername/aina-translator:latest
docker run -d --gpus all -p 8000:8000 \
--name aina-translator \
ghcr.io/yourusername/aina-translator:latest- Expose Service
- Enable HTTP service on port 8000 in RunPod dashboard
- Note your public URL:
https://<pod-id>-8000.proxy.runpod.net
# Use the included test client
python test_client.py https://<pod-id>-8000.proxy.runpod.netThe test suite validates:
- ✅ Service health and model loading
- ✅ Single and multi-target translations
- ✅ All 6 language models
- ✅ Beam search functionality
- ✅ Input validation
- ✅ Latency benchmarks
| Test | Languages | Latency | Throughput |
|---|---|---|---|
| Single (en) | 1 | 0.15s | ~6.7 req/s |
| Triple (en, es, fr) | 3 | 0.28s | ~3.6 req/s |
| All languages | 6 | 0.45s | ~2.2 req/s |
GPU Configuration
# In app.py, adjust for your hardware
MAX_WORKERS = 6 # Increase for more parallel capacity
compute_type = "float16" # Use "int8" for faster inferenceEnvironment Variables
export TRANSLATE_WORKERS=8 # Concurrent translation threads
export CUDA_VISIBLE_DEVICES=0 # Select GPUBeam Search Trade-offs
beam_size=1: Fastest, greedy decodingbeam_size=4: Better quality, 2-3x slowerbeam_size=8: Best quality, 4-5x slower
| Variable | Default | Description |
|---|---|---|
TRANSLATE_WORKERS |
6 |
ThreadPoolExecutor max workers |
CUDA_VISIBLE_DEVICES |
- | GPU selection (e.g., 0,1) |
Models are defined in app.py:
MODEL_REGISTRY = {
"en": {"repo": "projecte-aina/aina-translator-ca-en", "spm": "spm.model"},
"es": {"repo": "projecte-aina/aina-translator-ca-es", "spm": "spm.model"},
# ... add more languages
}To add a new language pair:
- Add entry to
MODEL_REGISTRY - Ensure CTranslate2-compatible model on Hugging Face
- Restart service to download and load model
# Local
python test_client.py
# Remote
python test_client.py https://your-service-url.com
# With debugging
DEBUG=True python test_client.py- ✅ Health check endpoint
- ✅ Single target translation
- ✅ Multiple target translation (parallel)
- ✅ All 6 languages
- ✅ Unknown language handling
- ✅ Beam search parameter
- ✅ Custom max_decoding_length
- ✅ Input validation (empty text)
- ✅ Input validation (missing targets)
docker build -t aina-translator:latest .# GitHub Container Registry
docker tag aina-translator:latest ghcr.io/yourusername/aina-translator:latest
docker push ghcr.io/yourusername/aina-translator:latest
# Docker Hub
docker tag aina-translator:latest yourusername/aina-translator:latest
docker push yourusername/aina-translator:latestversion: '3.8'
services:
translator:
image: aina-translator:latest
ports:
- "8000:8000"
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [gpu]
environment:
- TRANSLATE_WORKERS=8Integrate with monitoring tools:
# Kubernetes liveness probe
livenessProbe:
httpGet:
path: /health
port: 8000
initialDelaySeconds: 300
periodSeconds: 30
# Docker healthcheck
HEALTHCHECK --interval=30s --timeout=10s --start-period=300s \
CMD curl -f http://localhost:8000/health || exit 1Structured logging with levels:
# Adjust log level
logging.basicConfig(level=logging.DEBUG) # DEBUG, INFO, WARNING, ERRORContributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project uses models from Projecte AINA, which are released under the Apache 2.0 license.
- Projecte AINA: For training and releasing high-quality Catalan translation models
- CTranslate2: For the efficient inference engine
- Hugging Face: For model hosting and distribution
- FastAPI: For the excellent web framework
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Projecte AINA: https://projecte-aina.github.io/
Made with ❤️ for real-time translation
⭐ Star this repo if you find it useful!