|
# Sema Translation API - Project Overview |
|
|
|
## ๐ฏ Project Summary |
|
|
|
Enterprise-grade translation API supporting 200+ languages with automatic language detection, built with FastAPI and powered by the consolidated `sematech/sema-utils` model repository. |
|
|
|
## ๐ Project Structure |
|
|
|
``` |
|
backend/sema-api/ |
|
โโโ app/ # Main application package |
|
โ โโโ main.py # Application entry point & FastAPI instance |
|
โ โโโ api/v1/endpoints.py # API route handlers (versioned) |
|
โ โโโ core/ # Core configuration & setup |
|
โ โ โโโ config.py # Settings management |
|
โ โ โโโ logging.py # Structured logging setup |
|
โ โ โโโ metrics.py # Prometheus metrics definitions |
|
โ โโโ middleware/ # Custom middleware |
|
โ โ โโโ request_middleware.py # Request tracking & metrics |
|
โ โโโ models/schemas.py # Pydantic data models |
|
โ โโโ services/translation.py # Business logic & model management |
|
โ โโโ utils/helpers.py # Utility functions |
|
โโโ tests/ # Test suite |
|
โ โโโ test_model_download.py # Model download & loading tests |
|
โ โโโ test_api_client.py # API endpoint tests |
|
โ โโโ README.md # Test documentation |
|
โโโ Dockerfile # Multi-stage Docker build |
|
โโโ requirements.txt # Python dependencies |
|
โโโ README.md # API documentation |
|
โโโ ARCHITECTURE.md # Technical architecture |
|
โโโ PROJECT_OVERVIEW.md # This file |
|
``` |
|
|
|
## ๐ Key Features |
|
|
|
### Core Translation |
|
- **200+ Language Support**: Full FLORES-200 language codes |
|
- **Automatic Language Detection**: Optional source language detection |
|
- **High Performance**: CTranslate2 optimized inference |
|
- **Character Tracking**: Usage monitoring for billing/analytics |
|
|
|
### Enterprise Features |
|
- **Rate Limiting**: 60 requests/minute per IP |
|
- **Request Tracking**: Unique request IDs for debugging |
|
- **Structured Logging**: JSON logs for easy parsing |
|
- **Prometheus Metrics**: Comprehensive monitoring |
|
- **Health Checks**: System status monitoring |
|
|
|
### API Quality |
|
- **Comprehensive Swagger UI**: Interactive documentation |
|
- **Type Safety**: Full Pydantic validation |
|
- **Versioned Endpoints**: `/api/v1/` for future compatibility |
|
- **Error Handling**: Graceful error responses |
|
- **CORS Support**: Cross-origin resource sharing |
|
|
|
## ๐ง Technical Stack |
|
|
|
### Core Technologies |
|
- **FastAPI**: Modern Python web framework |
|
- **CTranslate2**: Optimized neural machine translation |
|
- **SentencePiece**: Subword tokenization |
|
- **FastText**: Language detection |
|
- **HuggingFace Hub**: Model repository integration |
|
|
|
### Monitoring & Observability |
|
- **Prometheus**: Metrics collection |
|
- **Structlog**: Structured JSON logging |
|
- **SlowAPI**: Rate limiting |
|
- **Uvicorn**: ASGI server |
|
|
|
### Development & Deployment |
|
- **Docker**: Multi-stage containerization |
|
- **Pydantic**: Data validation and settings |
|
- **Pytest**: Testing framework (ready) |
|
- **HuggingFace Spaces**: Cloud deployment |
|
|
|
## ๐ API Endpoints |
|
|
|
### Health & Monitoring |
|
| Endpoint | Method | Description | |
|
|----------|--------|-------------| |
|
| `/` | GET | Basic health check | |
|
| `/health` | GET | Detailed health monitoring | |
|
| `/metrics` | GET | Prometheus metrics | |
|
| `/docs` | GET | Swagger UI documentation | |
|
| `/redoc` | GET | ReDoc documentation | |
|
|
|
### Translation |
|
| Endpoint | Method | Description | |
|
|----------|--------|-------------| |
|
| `/translate` | POST | Main translation endpoint | |
|
| `/api/v1/translate` | POST | Versioned translation endpoint | |
|
|
|
## ๐ Security & Reliability |
|
|
|
### Current Implementation |
|
- **Input Validation**: Comprehensive Pydantic validation |
|
- **Rate Limiting**: IP-based request limiting |
|
- **Error Handling**: Graceful error responses |
|
- **Request Tracking**: Unique IDs for debugging |
|
|
|
### Future-Ready Features |
|
- **Authentication Framework**: Ready for Supabase integration |
|
- **Usage Analytics**: Character count and request tracking |
|
- **Audit Logging**: Request/response logging |
|
|
|
## ๐ Performance & Scalability |
|
|
|
### Optimization Features |
|
- **Async/Await**: Full asynchronous processing |
|
- **Model Caching**: Models loaded once at startup |
|
- **Efficient Translation**: CTranslate2 optimization |
|
- **Connection Pooling**: Ready for database integration |
|
|
|
### Monitoring Metrics |
|
- Request count by endpoint and status |
|
- Request duration histograms |
|
- Translation count by language pair |
|
- Character count tracking |
|
- Error count by type |
|
|
|
## ๐งช Testing |
|
|
|
### Test Coverage |
|
- **Model Tests**: Download, loading, and translation pipeline |
|
- **API Tests**: All endpoints, error handling, performance |
|
- **Integration Tests**: End-to-end workflow validation |
|
|
|
### Test Commands |
|
```bash |
|
# Model download and loading tests |
|
cd tests && python test_model_download.py |
|
|
|
# API endpoint tests (local) |
|
cd tests && python test_api_client.py |
|
|
|
# API endpoint tests (production) |
|
cd tests && python test_api_client.py https://sematech-sema-api.hf.space |
|
``` |
|
|
|
## ๐ Deployment |
|
|
|
### Local Development |
|
```bash |
|
cd backend/sema-api |
|
pip install -r requirements.txt |
|
uvicorn app.main:app --reload |
|
``` |
|
|
|
### Docker Development |
|
```bash |
|
docker build -t sema-api . |
|
docker run -p 8000:8000 sema-api |
|
``` |
|
|
|
### HuggingFace Spaces |
|
- Automatic deployment from git push |
|
- Multi-stage Docker build for optimization |
|
- Model pre-downloading for faster startup |
|
|
|
## ๐ฎ Future Enhancements |
|
|
|
### Planned Features |
|
- **Supabase Authentication**: User management and API keys |
|
- **Database Integration**: Usage tracking and analytics |
|
- **Redis Caching**: Performance optimization |
|
- **Advanced Monitoring**: Grafana dashboards and alerting |
|
|
|
### Scaling Considerations |
|
- **Load Balancing**: Stateless design for horizontal scaling |
|
- **Database Sharding**: For high-volume usage tracking |
|
- **CDN Integration**: For global performance |
|
- **Auto-scaling**: Based on request volume |
|
|
|
## ๐ Development Guidelines |
|
|
|
### Code Organization |
|
- **Separation of Concerns**: Clear module boundaries |
|
- **Type Safety**: Full type hints and Pydantic validation |
|
- **Error Handling**: Comprehensive exception management |
|
- **Documentation**: Inline docs and comprehensive README |
|
|
|
### Adding New Features |
|
1. **Endpoints**: Add to `app/api/v1/endpoints.py` |
|
2. **Business Logic**: Add to appropriate service in `app/services/` |
|
3. **Data Models**: Add to `app/models/schemas.py` |
|
4. **Configuration**: Add to `app/core/config.py` |
|
5. **Tests**: Add to `tests/` directory |
|
|
|
## ๐ Support & Maintenance |
|
|
|
### Documentation |
|
- **API Docs**: Available at `/docs` endpoint |
|
- **Architecture**: See `ARCHITECTURE.md` |
|
- **Tests**: See `tests/README.md` |
|
|
|
### Monitoring |
|
- **Health**: Monitor `/health` endpoint |
|
- **Metrics**: Scrape `/metrics` for Prometheus |
|
- **Logs**: Structured JSON logs for analysis |
|
|
|
This project provides a solid foundation for a production-ready translation API that can scale and evolve with your needs. |
|
|