Spaces:
Sleeping
Sleeping
File size: 3,578 Bytes
808643c d5a48a3 808643c c73808e a3c1eaa 808643c f935fd8 808643c 0447374 808643c 742149a 808643c 742149a 808643c 0447374 808643c 5d2853d 808643c 3e82c51 808643c 5d2853d c73808e c528ca2 808643c c528ca2 c73808e 2c46846 808643c 2c46846 c73808e 3e82c51 2c46846 c528ca2 2f96148 f935fd8 2f96148 c528ca2 3e82c51 f935fd8 c528ca2 c73808e 2c46846 c73808e c528ca2 808643c c528ca2 0447374 2c46846 0447374 c73808e 3e82c51 2c46846 c528ca2 f935fd8 f3206d4 c528ca2 3e82c51 f935fd8 c528ca2 c73808e | 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 | # src/translate.py
import logging
from transformers import pipeline
log = logging.getLogger("translate")
# Language codes
ENG = "eng_Latn"
HINDI = "hin_Deva"
# Globals
_local_hi2en = None
_local_en2hi = None
GEN_KW = dict(max_new_tokens=128, num_beams=4) # safe on CPU, avoids truncation
def _ensure_pipes():
"""Lazy init of IndicTrans2 pipelines (distilled)."""
global _local_hi2en, _local_en2hi
log.info("_ensure_pipes: start")
if _local_hi2en is None or _local_en2hi is None:
log.info("Loading IndicTrans2 distilled pipelines...")
try:
_local_hi2en = pipeline(
"translation",
model="ai4bharat/indictrans2-indic-en-dist-200M",
trust_remote_code=True
)
_local_en2hi = pipeline(
"translation",
model="ai4bharat/indictrans2-en-indic-dist-200M",
trust_remote_code=True
)
log.info("IndicTrans2 pipelines ready")
except Exception as e:
log.exception("Could not load IndicTrans2 pipelines: %s", e)
raise
log.info("_ensure_pipes: End")
def detect_lang(text: str) -> str:
"""Super-light language detector (hacky)."""
# if contains Devanagari → assume Hindi
log.info("Inside: detect_lang")
if any("\u0900" <= ch <= "\u097F" for ch in text):
return HINDI
return ENG
def ensure_english(text: str) -> str:
"""
Return English text. If input is Hindi, translate hi->en using IndicTrans2.
Prefer the 'prefix' format; fall back to kwargs if needed.
"""
_ensure_pipes()
if not isinstance(text, str):
text = str(text or "")
text = " ".join(text.split())
if detect_lang(text) == HINDI:
log.info("ensure_english: translating HI->EN")
prefixed = f"{HINDI} {ENG} {text}"
try:
log.info("ensure_english:calling _local_hi2en ")
out = _local_hi2en(prefixed, **GEN_KW)
log.info(f" ensure_english-> _local_hi2en -> returning value: {out[0]['translation_text']}")
return out[0]["translation_text"]
except Exception as e :
log.exception("Exception occured in _local_hi2en. Exception is : %s", e)
out = _local_hi2en(text, src_lang=HINDI, tgt_lang=ENG, **GEN_KW)
return out[0]["translation_text"]
return text
def en_to_lang(text: str, tgt_lang: str = HINDI) -> str:
"""
Translate English -> target Indic. Prefer the 'prefix' format; fallback to kwargs.
"""
try:
_ensure_pipes()
except Exception as e:
log.exception("en_to_lang --> Exception occured in _ensure_pipes. Exception is : %s", e)
log.info("en_to_lang: post _ensure_pipes")
if not isinstance(text, str):
text = str(text or "")
text = " ".join(text.split())
log.info(f"en_to_lang: Proceeding to conversion Tgt_lang = {tgt_lang} ")
if tgt_lang == HINDI and detect_lang(text) != HINDI:
log.info("en_to_lang: translating EN->HI")
prefixed = f"{ENG} {HINDI} {text}"
try:
out = _local_en2hi(prefixed, **GEN_KW)
log.info(f"en_to_lang returning value: {out[0]['translation_text']}")
return out[0]["translation_text"]
except Exception as e:
log.exception("Exception occured in en_to_lang. Exception is : %s", e)
out = _local_en2hi(text, src_lang=ENG, tgt_lang=HINDI, **GEN_KW)
return out[0]["translation_text"]
return text
|