Spaces:
Sleeping
Sleeping
Update src/translate.py
Browse files- src/translate.py +12 -1
src/translate.py
CHANGED
|
@@ -18,6 +18,7 @@ GEN_KW = dict(max_new_tokens=128, num_beams=4) # safe on CPU, avoids truncation
|
|
| 18 |
def _ensure_pipes():
|
| 19 |
"""Lazy init of IndicTrans2 pipelines (distilled)."""
|
| 20 |
global _local_hi2en, _local_en2hi
|
|
|
|
| 21 |
if _local_hi2en is None or _local_en2hi is None:
|
| 22 |
log.info("Loading IndicTrans2 distilled pipelines...")
|
| 23 |
try:
|
|
@@ -35,6 +36,8 @@ def _ensure_pipes():
|
|
| 35 |
except Exception as e:
|
| 36 |
log.exception("Could not load IndicTrans2 pipelines: %s", e)
|
| 37 |
raise
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def detect_lang(text: str) -> str:
|
|
@@ -73,11 +76,19 @@ def en_to_lang(text: str, tgt_lang: str = HINDI) -> str:
|
|
| 73 |
"""
|
| 74 |
Translate English -> target Indic. Prefer the 'prefix' format; fallback to kwargs.
|
| 75 |
"""
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
if not isinstance(text, str):
|
| 78 |
text = str(text or "")
|
| 79 |
text = " ".join(text.split())
|
| 80 |
|
|
|
|
|
|
|
|
|
|
| 81 |
if tgt_lang == HINDI and detect_lang(text) != HINDI:
|
| 82 |
log.info("en_to_lang: translating EN->HI")
|
| 83 |
prefixed = f"{ENG} {HINDI} {text}"
|
|
|
|
| 18 |
def _ensure_pipes():
|
| 19 |
"""Lazy init of IndicTrans2 pipelines (distilled)."""
|
| 20 |
global _local_hi2en, _local_en2hi
|
| 21 |
+
log.info("_ensure_pipes: start")
|
| 22 |
if _local_hi2en is None or _local_en2hi is None:
|
| 23 |
log.info("Loading IndicTrans2 distilled pipelines...")
|
| 24 |
try:
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
log.exception("Could not load IndicTrans2 pipelines: %s", e)
|
| 38 |
raise
|
| 39 |
+
|
| 40 |
+
log.info("_ensure_pipes: End")
|
| 41 |
|
| 42 |
|
| 43 |
def detect_lang(text: str) -> str:
|
|
|
|
| 76 |
"""
|
| 77 |
Translate English -> target Indic. Prefer the 'prefix' format; fallback to kwargs.
|
| 78 |
"""
|
| 79 |
+
try:
|
| 80 |
+
_ensure_pipes()
|
| 81 |
+
except Exception as e:
|
| 82 |
+
log.exception("en_to_lang --> Exception occured in _ensure_pipes. Exception is : %s", e)
|
| 83 |
+
|
| 84 |
+
log.info("en_to_lang: post _ensure_pipes")
|
| 85 |
if not isinstance(text, str):
|
| 86 |
text = str(text or "")
|
| 87 |
text = " ".join(text.split())
|
| 88 |
|
| 89 |
+
|
| 90 |
+
log.info(f"en_to_lang: Proceeding to conversion Tgt_lang = {tgt_lang} ")
|
| 91 |
+
|
| 92 |
if tgt_lang == HINDI and detect_lang(text) != HINDI:
|
| 93 |
log.info("en_to_lang: translating EN->HI")
|
| 94 |
prefixed = f"{ENG} {HINDI} {text}"
|