File size: 7,541 Bytes
a7d24e3 86c9796 a7d24e3 86c9796 a7d24e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
"""
Pydantic models for request/response validation
"""
from typing import Optional, Dict
from pydantic import BaseModel, Field, validator
class TranslationRequest(BaseModel):
"""
Translation request model
Validates input for the translation endpoint with proper FLORES-200 language codes.
"""
text: str = Field(
...,
example="Habari ya asubuhi",
description="Text to translate (1-5000 characters)",
min_length=1,
max_length=5000,
title="Input Text"
)
target_language: str = Field(
...,
example="eng_Latn",
description="Target language in FLORES-200 format (e.g., eng_Latn for English)",
pattern=r"^[a-z]{3}_[A-Z][a-z]{3}$",
title="Target Language Code"
)
source_language: Optional[str] = Field(
None,
example="swh_Latn",
description="Source language in FLORES-200 format. If not provided, language will be auto-detected",
pattern=r"^[a-z]{3}_[A-Z][a-z]{3}$",
title="Source Language Code (Optional)"
)
class Config:
schema_extra = {
"examples": [
{
"summary": "Auto-detect source language",
"description": "Translate Swahili to English with automatic language detection",
"value": {
"text": "Habari ya asubuhi",
"target_language": "eng_Latn"
}
},
{
"summary": "Specify source language",
"description": "Translate English to Swahili with specified source language",
"value": {
"text": "Good morning",
"source_language": "eng_Latn",
"target_language": "swh_Latn"
}
},
{
"summary": "African language translation",
"description": "Translate Kikuyu to English",
"value": {
"text": "Wĩ mwega?",
"source_language": "kik_Latn",
"target_language": "eng_Latn"
}
}
]
}
@validator('text')
def validate_text(cls, v):
if not v.strip():
raise ValueError('Text cannot be empty or only whitespace')
return v.strip()
class TranslationResponse(BaseModel):
"""
Translation response model
Contains the translated text and metadata about the translation process.
"""
translated_text: str = Field(
...,
description="The translated text result",
example="Good morning",
title="Translated Text"
)
source_language: str = Field(
...,
description="Detected or provided source language code",
example="swh_Latn",
title="Source Language"
)
target_language: str = Field(
...,
description="Target language code as requested",
example="eng_Latn",
title="Target Language"
)
inference_time: float = Field(
...,
description="Time taken for translation in seconds",
example=0.234,
ge=0,
title="Inference Time (seconds)"
)
character_count: int = Field(
...,
description="Number of characters in the input text",
example=17,
ge=1,
title="Character Count"
)
timestamp: str = Field(
...,
description="Timestamp of the translation in Nairobi timezone",
example="Monday | 2024-06-21 | 14:30:25",
title="Timestamp"
)
request_id: str = Field(
...,
description="Unique request identifier for debugging and tracking",
example="550e8400-e29b-41d4-a716-446655440000",
title="Request ID"
)
class Config:
schema_extra = {
"example": {
"translated_text": "Good morning",
"source_language": "swh_Latn",
"target_language": "eng_Latn",
"inference_time": 0.234,
"character_count": 17,
"timestamp": "Monday | 2024-06-21 | 14:30:25",
"request_id": "550e8400-e29b-41d4-a716-446655440000"
}
}
class HealthResponse(BaseModel):
"""Response model for health check endpoints"""
status: str = Field(..., description="API health status")
version: str = Field(..., description="API version")
models_loaded: bool = Field(..., description="Whether models are loaded")
uptime: float = Field(..., description="API uptime in seconds")
timestamp: str = Field(..., description="Current timestamp")
class ErrorResponse(BaseModel):
"""Response model for error responses"""
error: str = Field(..., description="Error type")
message: str = Field(..., description="Error message")
request_id: str = Field(..., description="Request identifier")
timestamp: str = Field(..., description="Error timestamp")
class LanguageInfo(BaseModel):
"""
Language information model
Contains metadata about a supported language.
"""
name: str = Field(..., description="English name of the language", example="Swahili")
native_name: str = Field(..., description="Native name of the language", example="Kiswahili")
region: str = Field(..., description="Geographic region", example="Africa")
script: str = Field(..., description="Writing script", example="Latin")
class LanguagesResponse(BaseModel):
"""
Languages list response model
Contains a dictionary of supported languages with their metadata.
"""
languages: Dict[str, LanguageInfo] = Field(..., description="Dictionary of language codes to language info")
total_count: int = Field(..., description="Total number of languages")
class Config:
schema_extra = {
"example": {
"languages": {
"swh_Latn": {
"name": "Swahili",
"native_name": "Kiswahili",
"region": "Africa",
"script": "Latin"
},
"eng_Latn": {
"name": "English",
"native_name": "English",
"region": "Europe",
"script": "Latin"
}
},
"total_count": 2
}
}
class LanguageStatsResponse(BaseModel):
"""
Language statistics response model
Contains statistics about supported languages.
"""
total_languages: int = Field(..., description="Total number of supported languages")
regions: int = Field(..., description="Number of geographic regions covered")
scripts: int = Field(..., description="Number of writing scripts supported")
by_region: Dict[str, int] = Field(..., description="Language count by region")
class Config:
schema_extra = {
"example": {
"total_languages": 200,
"regions": 6,
"scripts": 15,
"by_region": {
"Africa": 25,
"Europe": 40,
"Asia": 80,
"Middle East": 15,
"Americas": 30,
"Oceania": 10
}
}
}
|