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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# models/translation/translation_utils.py

"""

Translation Model Utilities for PENNY Project

Handles multilingual translation using NLLB-200 for civic engagement accessibility.

Provides async translation with structured error handling and language code normalization.

"""

import asyncio
import time
import os
import httpx
from typing import Dict, Any, Optional, List

# --- Logging Imports ---
from app.logging_utils import log_interaction, sanitize_for_logging

# --- Hugging Face API Configuration ---
HF_API_URL = "https://api-inference.huggingface.co/models/facebook/nllb-200-distilled-600M"
HF_TOKEN = os.getenv("HF_TOKEN")

AGENT_NAME = "penny-translate-agent"
SERVICE_AVAILABLE = True  # Assume available since we're using API

# NLLB-200 Language Code Mapping (Common languages for civic engagement)
LANGUAGE_CODES = {
    # English variants
    "english": "eng_Latn",
    "en": "eng_Latn",
    
    # Spanish variants
    "spanish": "spa_Latn",
    "es": "spa_Latn",
    "español": "spa_Latn",
    
    # French
    "french": "fra_Latn",
    "fr": "fra_Latn",
    "français": "fra_Latn",
    
    # Mandarin Chinese
    "chinese": "zho_Hans",
    "mandarin": "zho_Hans",
    "zh": "zho_Hans",
    
    # Arabic
    "arabic": "arb_Arab",
    "ar": "arb_Arab",
    
    # Hindi
    "hindi": "hin_Deva",
    "hi": "hin_Deva",
    
    # Portuguese
    "portuguese": "por_Latn",
    "pt": "por_Latn",
    
    # Russian
    "russian": "rus_Cyrl",
    "ru": "rus_Cyrl",
    
    # German
    "german": "deu_Latn",
    "de": "deu_Latn",
    
    # Vietnamese
    "vietnamese": "vie_Latn",
    "vi": "vie_Latn",
    
    # Tagalog
    "tagalog": "tgl_Latn",
    "tl": "tgl_Latn",
    
    # Urdu
    "urdu": "urd_Arab",
    "ur": "urd_Arab",
    
    # Swahili
    "swahili": "swh_Latn",
    "sw": "swh_Latn",
}

# Pre-translated civic phrases for common queries
CIVIC_PHRASES = {
    "eng_Latn": {
        "voting_location": "Where is my polling place?",
        "voter_registration": "How do I register to vote?",
        "city_services": "What city services are available?",
        "report_issue": "I want to report a problem.",
        "contact_city": "How do I contact city hall?",
    },
    "spa_Latn": {
        "voting_location": "¿Dónde está mi lugar de votación?",
        "voter_registration": "¿Cómo me registro para votar?",
        "city_services": "¿Qué servicios de la ciudad están disponibles?",
        "report_issue": "Quiero reportar un problema.",
        "contact_city": "¿Cómo contacto al ayuntamiento?",
    }
}


def is_translation_available() -> bool:
    """

    Check if translation service is available.

    

    Returns:

        bool: True if translation API is configured and ready.

    """
    return HF_TOKEN is not None and len(HF_TOKEN) > 0


def normalize_language_code(lang: str) -> str:
    """

    Converts common language names/codes to NLLB-200 format.

    

    Args:

        lang: Language name or code (e.g., "spanish", "es", "español")

        

    Returns:

        NLLB-200 language code (e.g., "spa_Latn")

    """
    if not lang or not isinstance(lang, str):
        return "eng_Latn"  # Default to English
    
    lang_lower = lang.lower().strip()
    
    # Check if it's already in NLLB format (contains underscore)
    if "_" in lang_lower:
        return lang_lower
    
    # Look up in mapping
    return LANGUAGE_CODES.get(lang_lower, lang_lower)


def get_supported_languages() -> List[str]:
    """

    Get list of supported language codes.

    

    Returns:

        List of NLLB-200 language codes supported by PENNY.

    """
    return list(set(LANGUAGE_CODES.values()))


async def translate_text(

    text: str,

    source_language: str = "eng_Latn",

    target_language: str = "spa_Latn",

    tenant_id: Optional[str] = None

) -> Dict[str, Any]:
    """

    Translates text from source language to target language using NLLB-200.



    Args:

        text: The text to translate.

        source_language: Source language code (e.g., "eng_Latn", "spanish", "es")

        target_language: Target language code (e.g., "spa_Latn", "french", "fr")

        tenant_id: Optional tenant identifier for logging.



    Returns:

        A dictionary containing:

            - translated_text (str): The translated text

            - source_lang (str): Normalized source language code

            - target_lang (str): Normalized target language code

            - original_text (str): The input text

            - available (bool): Whether the service was available

            - error (str, optional): Error message if translation failed

            - response_time_ms (int, optional): Translation time in milliseconds

    """
    start_time = time.time()

    # Check availability
    if not is_translation_available():
        log_interaction(
            intent="translation",
            tenant_id=tenant_id,
            success=False,
            error="Translation API not configured (missing HF_TOKEN)",
            fallback_used=True
        )
        return {
            "translated_text": text,  # Return original text as fallback
            "source_lang": source_language,
            "target_lang": target_language,
            "original_text": text,
            "available": False,
            "error": "Translation service is temporarily unavailable."
        }

    # Validate input
    if not text or not isinstance(text, str):
        log_interaction(
            intent="translation",
            tenant_id=tenant_id,
            success=False,
            error="Invalid text input"
        )
        return {
            "translated_text": "",
            "source_lang": source_language,
            "target_lang": target_language,
            "original_text": text if isinstance(text, str) else "",
            "available": True,
            "error": "Invalid text input provided."
        }
    
    # Check text length (prevent processing extremely long texts)
    if len(text) > 5000:  # 5k character limit for translation
        log_interaction(
            intent="translation",
            tenant_id=tenant_id,
            success=False,
            error=f"Text too long: {len(text)} characters",
            text_preview=sanitize_for_logging(text[:100])
        )
        return {
            "translated_text": text,
            "source_lang": source_language,
            "target_lang": target_language,
            "original_text": text,
            "available": True,
            "error": "Text is too long for translation (max 5,000 characters)."
        }

    # Normalize language codes
    src_lang = normalize_language_code(source_language)
    tgt_lang = normalize_language_code(target_language)
    
    # Skip translation if source and target are the same
    if src_lang == tgt_lang:
        log_interaction(
            intent="translation_skipped",
            tenant_id=tenant_id,
            success=True,
            details="Source and target languages are identical"
        )
        return {
            "translated_text": text,
            "source_lang": src_lang,
            "target_lang": tgt_lang,
            "original_text": text,
            "available": True,
            "skipped": True
        }

    try:
        # Prepare API request
        headers = {"Authorization": f"Bearer {HF_TOKEN}"}
        payload = {
            "inputs": text,
            "parameters": {
                "src_lang": src_lang,
                "tgt_lang": tgt_lang
            }
        }
        
        # Call Hugging Face Inference API
        async with httpx.AsyncClient(timeout=30.0) as client:
            response = await client.post(HF_API_URL, json=payload, headers=headers)
            
            response_time_ms = int((time.time() - start_time) * 1000)
            
            if response.status_code != 200:
                log_interaction(
                    intent="translation",
                    tenant_id=tenant_id,
                    success=False,
                    error=f"API returned status {response.status_code}",
                    response_time_ms=response_time_ms,
                    source_lang=src_lang,
                    target_lang=tgt_lang,
                    fallback_used=True
                )
                return {
                    "translated_text": text,  # Fallback to original
                    "source_lang": src_lang,
                    "target_lang": tgt_lang,
                    "original_text": text,
                    "available": False,
                    "error": f"Translation API error: {response.status_code}",
                    "response_time_ms": response_time_ms
                }
            
            results = response.json()
        
        # Validate results
        if not results or not isinstance(results, list) or len(results) == 0:
            log_interaction(
                intent="translation",
                tenant_id=tenant_id,
                success=False,
                error="Empty or invalid model output",
                response_time_ms=response_time_ms,
                source_lang=src_lang,
                target_lang=tgt_lang
            )
            return {
                "translated_text": text,  # Fallback to original
                "source_lang": src_lang,
                "target_lang": tgt_lang,
                "original_text": text,
                "available": True,
                "error": "Translation returned unexpected format."
            }
        
        # NLLB returns format: [{'translation_text': '...'}]
        translated = results[0].get('translation_text', '').strip()
        
        if not translated:
            log_interaction(
                intent="translation",
                tenant_id=tenant_id,
                success=False,
                error="Empty translation result",
                response_time_ms=response_time_ms,
                source_lang=src_lang,
                target_lang=tgt_lang
            )
            return {
                "translated_text": text,  # Fallback to original
                "source_lang": src_lang,
                "target_lang": tgt_lang,
                "original_text": text,
                "available": True,
                "error": "Translation produced empty result."
            }
        
        # Log slow translations
        if response_time_ms > 5000:  # 5 seconds
            log_interaction(
                intent="translation_slow",
                tenant_id=tenant_id,
                success=True,
                response_time_ms=response_time_ms,
                details="Slow translation detected",
                source_lang=src_lang,
                target_lang=tgt_lang,
                text_length=len(text)
            )
        
        log_interaction(
            intent="translation",
            tenant_id=tenant_id,
            success=True,
            response_time_ms=response_time_ms,
            source_lang=src_lang,
            target_lang=tgt_lang,
            text_length=len(text)
        )
        
        return {
            "translated_text": translated,
            "source_lang": src_lang,
            "target_lang": tgt_lang,
            "original_text": text,
            "available": True,
            "response_time_ms": response_time_ms
        }

    except httpx.TimeoutException:
        response_time_ms = int((time.time() - start_time) * 1000)
        log_interaction(
            intent="translation",
            tenant_id=tenant_id,
            success=False,
            error="Translation request timed out",
            response_time_ms=response_time_ms,
            source_lang=src_lang,
            target_lang=tgt_lang,
            fallback_used=True
        )
        return {
            "translated_text": text,  # Fallback to original
            "source_lang": src_lang,
            "target_lang": tgt_lang,
            "original_text": text,
            "available": False,
            "error": "Translation request timed out.",
            "response_time_ms": response_time_ms
        }

    except asyncio.CancelledError:
        log_interaction(
            intent="translation",
            tenant_id=tenant_id,
            success=False,
            error="Translation cancelled",
            source_lang=src_lang,
            target_lang=tgt_lang
        )
        raise
        
    except Exception as e:
        response_time_ms = int((time.time() - start_time) * 1000)
        
        log_interaction(
            intent="translation",
            tenant_id=tenant_id,
            success=False,
            error=str(e),
            response_time_ms=response_time_ms,
            source_lang=src_lang,
            target_lang=tgt_lang,
            text_preview=sanitize_for_logging(text[:100]),
            fallback_used=True
        )
        
        return {
            "translated_text": text,  # Fallback to original
            "source_lang": src_lang,
            "target_lang": tgt_lang,
            "original_text": text,
            "available": False,
            "error": str(e),
            "response_time_ms": response_time_ms
        }


async def detect_and_translate(

    text: str,

    target_language: str = "eng_Latn",

    tenant_id: Optional[str] = None

) -> Dict[str, Any]:
    """

    Attempts to detect the source language and translate to target.

    

    Note: This is a simplified heuristic-based detection. For production,

    consider integrating a dedicated language detection model.

    

    Args:

        text: The text to translate

        target_language: Target language code

        tenant_id: Optional tenant identifier for logging

        

    Returns:

        Translation result dictionary

    """
    if not text or not isinstance(text, str):
        return {
            "translated_text": "",
            "detected_lang": "unknown",
            "target_lang": target_language,
            "original_text": text if isinstance(text, str) else "",
            "available": True,
            "error": "Invalid text input."
        }
    
    # Simple heuristic: check for common non-English characters
    detected_lang = "eng_Latn"  # Default assumption
    
    # Check for Spanish characters
    if any(char in text for char in ['¿', '¡', 'ñ', 'á', 'é', 'í', 'ó', 'ú']):
        detected_lang = "spa_Latn"
    # Check for Chinese characters
    elif any('\u4e00' <= char <= '\u9fff' for char in text):
        detected_lang = "zho_Hans"
    # Check for Arabic script
    elif any('\u0600' <= char <= '\u06ff' for char in text):
        detected_lang = "arb_Arab"
    # Check for Cyrillic (Russian)
    elif any('\u0400' <= char <= '\u04ff' for char in text):
        detected_lang = "rus_Cyrl"
    # Check for Devanagari (Hindi)
    elif any('\u0900' <= char <= '\u097f' for char in text):
        detected_lang = "hin_Deva"
    
    log_interaction(
        intent="language_detection",
        tenant_id=tenant_id,
        success=True,
        detected_lang=detected_lang,
        text_preview=sanitize_for_logging(text[:50])
    )
    
    result = await translate_text(text, detected_lang, target_language, tenant_id)
    result["detected_lang"] = detected_lang
    
    return result


async def batch_translate(

    texts: List[str],

    source_language: str = "eng_Latn",

    target_language: str = "spa_Latn",

    tenant_id: Optional[str] = None

) -> List[Dict[str, Any]]:
    """

    Translate multiple texts at once.

    

    Args:

        texts: List of strings to translate

        source_language: Source language code

        target_language: Target language code

        tenant_id: Optional tenant identifier for logging

        

    Returns:

        List of translation result dictionaries

    """
    if not texts or not isinstance(texts, list):
        log_interaction(
            intent="batch_translation",
            tenant_id=tenant_id,
            success=False,
            error="Invalid texts input"
        )
        return []
    
    # Filter valid texts and limit batch size
    valid_texts = [t for t in texts if isinstance(t, str) and t.strip()]
    if len(valid_texts) > 50:  # Batch size limit
        valid_texts = valid_texts[:50]
        log_interaction(
            intent="batch_translation",
            tenant_id=tenant_id,
            success=None,
            details=f"Batch size limited to 50 texts"
        )
    
    if not valid_texts:
        log_interaction(
            intent="batch_translation",
            tenant_id=tenant_id,
            success=False,
            error="No valid texts in batch"
        )
        return []
    
    start_time = time.time()
    results = []
    
    for text in valid_texts:
        result = await translate_text(text, source_language, target_language, tenant_id)
        results.append(result)
    
    response_time_ms = int((time.time() - start_time) * 1000)
    
    log_interaction(
        intent="batch_translation",
        tenant_id=tenant_id,
        success=True,
        response_time_ms=response_time_ms,
        batch_size=len(valid_texts),
        source_lang=normalize_language_code(source_language),
        target_lang=normalize_language_code(target_language)
    )
    
    return results


def get_civic_phrase(

    phrase_key: str,

    language: str = "eng_Latn"

) -> str:
    """

    Get a pre-translated civic phrase for common queries.

    

    Args:

        phrase_key: Key for the civic phrase (e.g., "voting_location")

        language: Target language code

        

    Returns:

        Translated phrase or empty string if not found

    """
    if not phrase_key or not isinstance(phrase_key, str):
        return ""
    
    lang_code = normalize_language_code(language)
    phrase = CIVIC_PHRASES.get(lang_code, {}).get(phrase_key, "")
    
    if phrase:
        log_interaction(
            intent="civic_phrase_lookup",
            success=True,
            phrase_key=phrase_key,
            language=lang_code
        )
    
    return phrase