Initial commit: Multilingual Translation API
- Implemented REST API for 105+ language translation - Used Facebook M2M100 model (Apache 2.0 License - Commercial use allowed) - Supports any-to-any translation between 105 languages - Major languages: English, Chinese, Spanish, Arabic, Russian, Japanese, Korean, etc. - Southeast Asian: Malay, Indonesian, Thai, Vietnamese, Tagalog, Burmese, Khmer, Lao - South Asian: Bengali, Hindi, Urdu, Tamil, Telugu, Marathi, Gujarati, etc. - European: German, French, Italian, Spanish, Portuguese, Russian, etc. - African: Swahili, Amharic, Hausa, Igbo, Yoruba, Zulu, Xhosa - And many more languages Tech Stack: - FastAPI for REST API - Transformers (Hugging Face) for ML model - PyTorch for inference - Docker for containerization - M2M100 418M parameter model Features: - Health check endpoint - Supported languages listing - Dynamic language validation - Model caching for performance - GPU support (auto-detection) - CORS enabled for web clients 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
51
app/models.py
Normal file
51
app/models.py
Normal file
@ -0,0 +1,51 @@
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class TranslationRequest(BaseModel):
|
||||
"""Translation request schema"""
|
||||
text: str = Field(..., description="Text to translate", min_length=1, max_length=5000)
|
||||
source_lang: str = Field(..., description="Source language code (e.g., 'en', 'ms', 'bn', etc.)", min_length=2, max_length=5)
|
||||
target_lang: str = Field(..., description="Target language code (e.g., 'en', 'ms', 'bn', etc.)", min_length=2, max_length=5)
|
||||
|
||||
@field_validator('source_lang', 'target_lang')
|
||||
@classmethod
|
||||
def validate_lang_code(cls, v: str) -> str:
|
||||
"""Validate language code format"""
|
||||
return v.lower().strip()
|
||||
|
||||
class Config:
|
||||
json_schema_extra = {
|
||||
"example": {
|
||||
"text": "Selamat pagi, apa khabar?",
|
||||
"source_lang": "ms",
|
||||
"target_lang": "en"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class TranslationResponse(BaseModel):
|
||||
"""Translation response schema"""
|
||||
original_text: str = Field(..., description="Original input text")
|
||||
translated_text: str = Field(..., description="Translated text")
|
||||
source_lang: str = Field(..., description="Source language code")
|
||||
target_lang: str = Field(..., description="Target language code")
|
||||
model_used: str = Field(..., description="Translation model identifier")
|
||||
|
||||
class Config:
|
||||
json_schema_extra = {
|
||||
"example": {
|
||||
"original_text": "Selamat pagi, apa khabar?",
|
||||
"translated_text": "Good morning, how are you?",
|
||||
"source_lang": "ms",
|
||||
"target_lang": "en",
|
||||
"model_used": "Helsinki-NLP/opus-mt-ms-en"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class HealthResponse(BaseModel):
|
||||
"""Health check response"""
|
||||
status: str
|
||||
message: str
|
||||
models_loaded: bool
|
||||
Reference in New Issue
Block a user