Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AINA Translator Microservice

Real-time translation from Catalan to multiple languages

FastAPI CTranslate2 Docker GPU

High-performance translation microservice powered by Projecte AINA CTranslate2 models.

Features • Quick Start • API Reference • Deployment • Performance


🎯 Overview

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.

Supported Languages

Source Targets
Catalan (ca) English, Spanish, French, Portuguese, Italian, German
en, es, fr, pt, it, de

✨ Features

  • 🚀 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

🛠️ Tech Stack

  • 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

🚀 Quick Start

Prerequisites

  • Python 3.10+
  • CUDA 11+ (optional, for GPU acceleration)
  • 8GB+ RAM (16GB+ recommended for all models)
  • Docker (optional, for containerized deployment)

Local Installation

# 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 8000

The service will download models on first startup (~2GB total). This takes 2-5 minutes depending on your connection.

Docker Installation

# 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

Verify Installation

# 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"]
  }'

📚 API Reference

Base URL

http://localhost:8000

Endpoints

GET /

Root endpoint with service information.

Response:

{
  "service": "AINA Translator",
  "version": "2.0.0",
  "engine": "CTranslate2",
  "docs": "/docs",
  "health": "/health"
}

GET /health

Health check endpoint showing service status and loaded models.

Response:

{
  "status": "ok",
  "device": "cuda",
  "models_loaded": ["en", "es", "fr", "pt", "it", "de"]
}

POST /translate

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

🎨 Usage Examples

Python

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())

JavaScript

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"
// }

cURL

# 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"]
  }'

🚢 Deployment on RunPod

Setup

  1. Create RunPod Pod

    • Select GPU: RTX 5090 / A100 / RTX 4090 recommended
    • Template: PyTorch 2.0+ with CUDA 11.8+
    • Disk: 20GB+ recommended
  2. 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
  1. Expose Service
    • Enable HTTP service on port 8000 in RunPod dashboard
    • Note your public URL: https://<pod-id>-8000.proxy.runpod.net

Test Deployment

# Use the included test client
python test_client.py https://<pod-id>-8000.proxy.runpod.net

The test suite validates:

  • ✅ Service health and model loading
  • ✅ Single and multi-target translations
  • ✅ All 6 language models
  • ✅ Beam search functionality
  • ✅ Input validation
  • ✅ Latency benchmarks

⚡ Performance

Benchmark Results (RTX 5090)

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

Optimization Tips

GPU Configuration

# In app.py, adjust for your hardware
MAX_WORKERS = 6  # Increase for more parallel capacity
compute_type = "float16"  # Use "int8" for faster inference

Environment Variables

export TRANSLATE_WORKERS=8  # Concurrent translation threads
export CUDA_VISIBLE_DEVICES=0  # Select GPU

Beam Search Trade-offs

  • beam_size=1: Fastest, greedy decoding
  • beam_size=4: Better quality, 2-3x slower
  • beam_size=8: Best quality, 4-5x slower

🔧 Configuration

Environment Variables

Variable Default Description
TRANSLATE_WORKERS 6 ThreadPoolExecutor max workers
CUDA_VISIBLE_DEVICES - GPU selection (e.g., 0,1)

Model Registry

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:

  1. Add entry to MODEL_REGISTRY
  2. Ensure CTranslate2-compatible model on Hugging Face
  3. Restart service to download and load model

🧪 Testing

Run Test Suite

# Local
python test_client.py

# Remote
python test_client.py https://your-service-url.com

# With debugging
DEBUG=True python test_client.py

Test Coverage

  • ✅ 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

docker build -t aina-translator:latest .

Push to Registry

# 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:latest

Docker Compose

version: '3.8'

services:
  translator:
    image: aina-translator:latest
    ports:
      - "8000:8000"
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    environment:
      - TRANSLATE_WORKERS=8

📊 Monitoring

Health Check

Integrate 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 1

Logging

Structured logging with levels:

# Adjust log level
logging.basicConfig(level=logging.DEBUG)  # DEBUG, INFO, WARNING, ERROR

🤝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project uses models from Projecte AINA, which are released under the Apache 2.0 license.


🙏 Acknowledgments

  • 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

📞 Support


Made with ❤️ for real-time translation

⭐ Star this repo if you find it useful!

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages