sema-api / docs /PROJECT_OVERVIEW.md
kamau1's picture
update: Fastapi codebase structure with api endpoints
a7d24e3
|
raw
history blame
7.01 kB
# 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.