- Added optional 'model' parameter to translation request (default: m2m100)
- M2M100: 105 languages, Apache 2.0 License (commercial OK)
- NLLB-200: 200 languages, CC-BY-NC 4.0 License (non-commercial only)
- Updated /api/translate endpoint to accept model selection
- Updated /api/supported-languages to show languages per model
- Added comprehensive language name mappings for all NLLB-200 languages
- Both models can be used independently with automatic model loading
- Model information includes license and commercial use status
Example usage:
- Default (M2M100): {"text": "Hello", "source_lang": "en", "target_lang": "ko"}
- NLLB-200: {"text": "Hello", "source_lang": "en", "target_lang": "ko", "model": "nllb200"}
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
from pydantic import BaseModel, Field, field_validator
|
|
from typing import Optional, Literal
|
|
|
|
|
|
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)
|
|
model: Literal["m2m100", "nllb200"] = Field(
|
|
default="m2m100",
|
|
description="Translation model to use: 'm2m100' (105 langs, Apache 2.0, commercial OK) or 'nllb200' (200 langs, CC-BY-NC, non-commercial only)"
|
|
)
|
|
|
|
@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",
|
|
"model": "m2m100"
|
|
}
|
|
}
|
|
|
|
|
|
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
|