File size: 11,481 Bytes
a7d24e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86c9796
a7d24e3
 
 
 
 
 
86c9796
a7d24e3
 
 
 
be6b137
a7d24e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d014389
 
 
 
 
 
 
a7d24e3
 
be6b137
a7d24e3
 
 
 
 
 
 
d014389
 
a7d24e3
 
 
 
 
 
 
 
 
 
 
 
 
 
06eadfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
700ea8e
06eadfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d014389
 
 
 
 
 
 
06eadfa
 
 
 
 
 
 
 
 
 
 
d014389
 
06eadfa
 
 
 
a7d24e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be6b137
a7d24e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be6b137
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""
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:
        json_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"
    )
    total_time: float = Field(
        ...,
        description="Total request processing time in seconds",
        example=1.234,
        ge=0,
        title="Total Processing Time (seconds)"
    )

    class Config:
        json_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",
                "total_time": 1.234
            }
        }


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 LanguageDetectionRequest(BaseModel):
    """
    Language detection request model

    For detecting the language of input text.
    """

    text: str = Field(
        ...,
        example="Habari ya asubuhi",
        description="Text to detect language for (1-1000 characters)",
        min_length=1,
        max_length=1000,
        title="Input Text"
    )

    class Config:
        json_schema_extra = {
            "examples": [
                {
                    "summary": "Swahili text detection",
                    "description": "Detect language for Swahili greeting",
                    "value": {
                        "text": "Habari ya asubuhi"
                    }
                },
                {
                    "summary": "English text detection",
                    "description": "Detect language for English text",
                    "value": {
                        "text": "Good morning, how are you?"
                    }
                },
                {
                    "summary": "French text detection",
                    "description": "Detect language for French text",
                    "value": {
                        "text": "Bonjour, comment allez-vous?"
                    }
                }
            ]
        }


class LanguageDetectionResponse(BaseModel):
    """
    Language detection response model

    Contains detected language information and confidence.
    """

    detected_language: str = Field(
        ...,
        description="Detected language code in FLORES-200 format",
        example="swh_Latn",
        title="Detected Language Code"
    )
    language_name: str = Field(
        ...,
        description="Human-readable name of detected language",
        example="Swahili",
        title="Language Name"
    )
    native_name: str = Field(
        ...,
        description="Native name of detected language",
        example="Kiswahili",
        title="Native Language Name"
    )
    confidence: float = Field(
        ...,
        description="Detection confidence score (0.0 to 1.0)",
        example=0.9876,
        ge=0.0,
        le=1.1,  # Allow slightly above 1.0 for FastText edge cases
        title="Confidence Score"
    )
    is_english: bool = Field(
        ...,
        description="Whether the detected language is English",
        example=False,
        title="Is English"
    )
    character_count: int = Field(
        ...,
        description="Number of characters in input text",
        example=17,
        ge=1,
        title="Character Count"
    )
    timestamp: str = Field(
        ...,
        description="Detection timestamp in Nairobi timezone",
        example="Monday | 2024-06-21 | 14:30:25",
        title="Timestamp"
    )
    request_id: str = Field(
        ...,
        description="Unique request identifier for debugging",
        example="550e8400-e29b-41d4-a716-446655440000",
        title="Request ID"
    )
    total_time: float = Field(
        ...,
        description="Total request processing time in seconds",
        example=0.045,
        ge=0,
        title="Total Processing Time (seconds)"
    )

    class Config:
        json_schema_extra = {
            "example": {
                "detected_language": "swh_Latn",
                "language_name": "Swahili",
                "native_name": "Kiswahili",
                "confidence": 0.9876,
                "is_english": False,
                "character_count": 17,
                "timestamp": "Monday | 2024-06-21 | 14:30:25",
                "request_id": "550e8400-e29b-41d4-a716-446655440000",
                "total_time": 0.045
            }
        }


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:
        json_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:
        json_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
                }
            }
        }