Spaces:
Sleeping
Sleeping
Upload indic_text.py with huggingface_hub
Browse files- indic_text.py +80 -0
indic_text.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""English -> Kannada translation via AI4Bharat IndicTrans2 — local GPU for HF Spaces."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
from config import TRANSLATION_MODEL
|
| 11 |
+
_TRANS_HUB_ID = TRANSLATION_MODEL.hub_id
|
| 12 |
+
|
| 13 |
+
_tok = None
|
| 14 |
+
_model = None
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _get_model():
|
| 18 |
+
global _tok, _model
|
| 19 |
+
if _tok is None or _model is None:
|
| 20 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 21 |
+
token = os.environ.get("HF_TOKEN") or None
|
| 22 |
+
_tok = AutoTokenizer.from_pretrained(
|
| 23 |
+
_TRANS_HUB_ID, trust_remote_code=True, token=token)
|
| 24 |
+
_model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 25 |
+
_TRANS_HUB_ID, trust_remote_code=True, token=token)
|
| 26 |
+
_model = _model.to("cuda")
|
| 27 |
+
_model.eval()
|
| 28 |
+
return _tok, _model
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Load at module level for ZeroGPU (CUDA emulation outside @spaces.GPU)
|
| 32 |
+
try:
|
| 33 |
+
_get_model()
|
| 34 |
+
except Exception:
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def _split_sentences(text: str, max_chars: int = 180):
|
| 39 |
+
parts = re.split(r"(?<=[.!?।])\s+|\n+", text.strip())
|
| 40 |
+
out = []
|
| 41 |
+
for p in parts:
|
| 42 |
+
p = p.strip()
|
| 43 |
+
if not p:
|
| 44 |
+
continue
|
| 45 |
+
while len(p) > max_chars:
|
| 46 |
+
cut = p.rfind(" ", 0, max_chars)
|
| 47 |
+
cut = cut if cut > 0 else max_chars
|
| 48 |
+
out.append(p[:cut].strip())
|
| 49 |
+
p = p[cut:].strip()
|
| 50 |
+
out.append(p)
|
| 51 |
+
return out or [text.strip()]
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def translate_to_kannada(en_text: str) -> str:
|
| 55 |
+
"""Translate English story text to Kannada (Kannada script)."""
|
| 56 |
+
text = (en_text or "").strip()
|
| 57 |
+
if not text:
|
| 58 |
+
raise ValueError("Nothing to translate.")
|
| 59 |
+
|
| 60 |
+
from IndicTransToolkit.processor import IndicProcessor
|
| 61 |
+
|
| 62 |
+
tok, model = _get_model()
|
| 63 |
+
ip = IndicProcessor(inference=True)
|
| 64 |
+
|
| 65 |
+
sents = _split_sentences(text, max_chars=180)
|
| 66 |
+
batch = ip.preprocess_batch(sents, src_lang="eng_Latn", tgt_lang="kan_Knda")
|
| 67 |
+
inputs = tok(batch, truncation=True, padding="longest", return_tensors="pt").to(model.device)
|
| 68 |
+
|
| 69 |
+
with torch.inference_mode():
|
| 70 |
+
generated = model.generate(
|
| 71 |
+
**inputs, max_length=512, num_beams=5,
|
| 72 |
+
num_return_sequences=1, length_penalty=1.0,
|
| 73 |
+
)
|
| 74 |
+
decoded = tok.batch_decode(generated, skip_special_tokens=True)
|
| 75 |
+
translations = ip.postprocess_batch(decoded, lang="kan_Knda")
|
| 76 |
+
|
| 77 |
+
kn = " ".join(t.strip() for t in translations if t.strip())
|
| 78 |
+
if not kn:
|
| 79 |
+
raise RuntimeError("Translation returned empty Kannada text.")
|
| 80 |
+
return kn
|