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|
+
import site
|
| 4 |
+
import torch
|
| 5 |
+
import transformers
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import re
|
| 8 |
+
|
| 9 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 10 |
+
|
| 11 |
+
print("=" * 80)
|
| 12 |
+
print("Vidyaapati Translator")
|
| 13 |
+
print("=" * 80)
|
| 14 |
+
|
| 15 |
+
print("Python :", sys.version)
|
| 16 |
+
print("Transformers :", transformers.__version__)
|
| 17 |
+
|
| 18 |
+
# -----------------------------------------------------------------------------
|
| 19 |
+
# Fix IndicTransToolkit import
|
| 20 |
+
# -----------------------------------------------------------------------------
|
| 21 |
+
|
| 22 |
+
def fix_collator():
|
| 23 |
+
try:
|
| 24 |
+
collator_path = None
|
| 25 |
+
|
| 26 |
+
for p in site.getsitepackages():
|
| 27 |
+
test = os.path.join(
|
| 28 |
+
p,
|
| 29 |
+
"IndicTransToolkit",
|
| 30 |
+
"collator.py"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if os.path.exists(test):
|
| 34 |
+
collator_path = test
|
| 35 |
+
break
|
| 36 |
+
|
| 37 |
+
if collator_path:
|
| 38 |
+
|
| 39 |
+
with open(collator_path, "r", encoding="utf-8") as f:
|
| 40 |
+
content = f.read()
|
| 41 |
+
|
| 42 |
+
content = content.replace(
|
| 43 |
+
"from transformers.tokenization_utils import PreTrainedTokenizerBase",
|
| 44 |
+
"from transformers import PreTrainedTokenizerBase",
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
with open(collator_path, "w", encoding="utf-8") as f:
|
| 48 |
+
f.write(content)
|
| 49 |
+
|
| 50 |
+
print("✓ IndicTransToolkit fixed")
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(e)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
fix_collator()
|
| 57 |
+
|
| 58 |
+
from IndicTransToolkit import IndicProcessor
|
| 59 |
+
|
| 60 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 61 |
+
|
| 62 |
+
print("Using device:", DEVICE)
|
| 63 |
+
|
| 64 |
+
if DEVICE == "cuda":
|
| 65 |
+
print(torch.cuda.get_device_name(0))
|
| 66 |
+
|
| 67 |
+
# -----------------------------------------------------------------------------
|
| 68 |
+
# Model IDs
|
| 69 |
+
# -----------------------------------------------------------------------------
|
| 70 |
+
|
| 71 |
+
HI_KO_MODEL = "shriyadiker/KonkaniTrans-Hi-Ko"
|
| 72 |
+
KO_HI_MODEL = "shriyadiker/KonkaniTrans-Ko-Hi"
|
| 73 |
+
|
| 74 |
+
print("\nLoading Hindi → Konkani model...")
|
| 75 |
+
|
| 76 |
+
hi_ko_tokenizer = AutoTokenizer.from_pretrained(
|
| 77 |
+
HI_KO_MODEL,
|
| 78 |
+
trust_remote_code=True,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
hi_ko_model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 82 |
+
HI_KO_MODEL,
|
| 83 |
+
trust_remote_code=True,
|
| 84 |
+
torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
|
| 85 |
+
).to(DEVICE)
|
| 86 |
+
|
| 87 |
+
print("✓ Hindi → Konkani loaded")
|
| 88 |
+
|
| 89 |
+
print("\nLoading Konkani → Hindi model...")
|
| 90 |
+
|
| 91 |
+
ko_hi_tokenizer = AutoTokenizer.from_pretrained(
|
| 92 |
+
KO_HI_MODEL,
|
| 93 |
+
trust_remote_code=True,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
ko_hi_model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 97 |
+
KO_HI_MODEL,
|
| 98 |
+
trust_remote_code=True,
|
| 99 |
+
torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
|
| 100 |
+
).to(DEVICE)
|
| 101 |
+
|
| 102 |
+
print("✓ Konkani → Hindi loaded")
|
| 103 |
+
|
| 104 |
+
ip = IndicProcessor(inference=True)
|
| 105 |
+
|
| 106 |
+
print("\nAll models loaded successfully.")
|
| 107 |
+
|
| 108 |
+
# -----------------------------------------------------------------------------
|
| 109 |
+
# Translation
|
| 110 |
+
# -----------------------------------------------------------------------------
|
| 111 |
+
|
| 112 |
+
def translate(text, direction):
|
| 113 |
+
|
| 114 |
+
if text is None or text.strip() == "":
|
| 115 |
+
return ""
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
|
| 119 |
+
if direction == "Hindi → Konkani":
|
| 120 |
+
|
| 121 |
+
tokenizer = hi_ko_tokenizer
|
| 122 |
+
model = hi_ko_model
|
| 123 |
+
|
| 124 |
+
src_lang = "hin_Deva"
|
| 125 |
+
tgt_lang = "gom_Deva"
|
| 126 |
+
|
| 127 |
+
else:
|
| 128 |
+
|
| 129 |
+
tokenizer = ko_hi_tokenizer
|
| 130 |
+
model = ko_hi_model
|
| 131 |
+
|
| 132 |
+
src_lang = "gom_Deva"
|
| 133 |
+
tgt_lang = "hin_Deva"
|
| 134 |
+
|
| 135 |
+
batch = ip.preprocess_batch(
|
| 136 |
+
[text],
|
| 137 |
+
src_lang=src_lang,
|
| 138 |
+
tgt_lang=tgt_lang,
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
inputs = tokenizer(
|
| 142 |
+
batch,
|
| 143 |
+
truncation=True,
|
| 144 |
+
padding=True,
|
| 145 |
+
return_tensors="pt",
|
| 146 |
+
return_attention_mask=True,
|
| 147 |
+
).to(DEVICE)
|
| 148 |
+
|
| 149 |
+
with torch.no_grad():
|
| 150 |
+
|
| 151 |
+
generated = model.generate(
|
| 152 |
+
**inputs,
|
| 153 |
+
max_length=256,
|
| 154 |
+
num_beams=5,
|
| 155 |
+
early_stopping=True,
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
decoded = tokenizer.batch_decode(
|
| 159 |
+
generated,
|
| 160 |
+
skip_special_tokens=True,
|
| 161 |
+
clean_up_tokenization_spaces=True,
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
output = ip.postprocess_batch(
|
| 165 |
+
decoded,
|
| 166 |
+
lang=tgt_lang,
|
| 167 |
+
)[0]
|
| 168 |
+
|
| 169 |
+
output = fix_escaped_unicode(output)
|
| 170 |
+
|
| 171 |
+
return output
|
| 172 |
+
|
| 173 |
+
except Exception as e:
|
| 174 |
+
|
| 175 |
+
return str(e)
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
UNICODE_ESCAPE_PATTERN = re.compile(r'\\u([0-9a-fA-F]{4})')
|
| 179 |
+
|
| 180 |
+
def fix_escaped_unicode(text):
|
| 181 |
+
|
| 182 |
+
if not isinstance(text, str):
|
| 183 |
+
return text
|
| 184 |
+
|
| 185 |
+
if "\\u" not in text:
|
| 186 |
+
return text
|
| 187 |
+
|
| 188 |
+
text = UNICODE_ESCAPE_PATTERN.sub(
|
| 189 |
+
lambda m: chr(int(m.group(1), 16)),
|
| 190 |
+
text
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
text = text.replace("ऱ", "ऱ")
|
| 194 |
+
text = text.replace("ऴ", "ऴ")
|
| 195 |
+
|
| 196 |
+
return text
|
| 197 |
+
|
| 198 |
+
# -----------------------------------------------------------------------------
|
| 199 |
+
# CSS
|
| 200 |
+
# -----------------------------------------------------------------------------
|
| 201 |
+
|
| 202 |
+
css = """
|
| 203 |
+
footer {
|
| 204 |
+
display: none !important;
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
.gradio-container {
|
| 208 |
+
max-width: 800px !important;
|
| 209 |
+
margin: auto !important;
|
| 210 |
+
padding: 30px 20px !important;
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
textarea {
|
| 214 |
+
font-size: 18px !important;
|
| 215 |
+
font-family: 'Segoe UI', 'Noto Sans Devanagari', Arial, sans-serif !important;
|
| 216 |
+
line-height: 1.8 !important;
|
| 217 |
+
padding: 15px !important;
|
| 218 |
+
min-height: 180px !important;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
button {
|
| 222 |
+
padding: 12px 40px !important;
|
| 223 |
+
font-size: 16px !important;
|
| 224 |
+
font-weight: 600 !important;
|
| 225 |
+
border-radius: 8px !important;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
.title {
|
| 229 |
+
text-align: center;
|
| 230 |
+
margin-bottom: 30px;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
.title h1 {
|
| 234 |
+
font-size: 28px;
|
| 235 |
+
font-weight: 600;
|
| 236 |
+
color: #1a1a1a;
|
| 237 |
+
margin-bottom: 5px;
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
.title p {
|
| 241 |
+
color: #666;
|
| 242 |
+
font-size: 14px;
|
| 243 |
+
}
|
| 244 |
+
"""
|
| 245 |
+
|
| 246 |
+
# -----------------------------------------------------------------------------
|
| 247 |
+
# Helper Functions
|
| 248 |
+
# -----------------------------------------------------------------------------
|
| 249 |
+
|
| 250 |
+
def clear_text():
|
| 251 |
+
return "", ""
|
| 252 |
+
|
| 253 |
+
# -----------------------------------------------------------------------------
|
| 254 |
+
# Gradio UI - Very Simple
|
| 255 |
+
# -----------------------------------------------------------------------------
|
| 256 |
+
|
| 257 |
+
with gr.Blocks(
|
| 258 |
+
title="VIDYAAPATI (Hindi-Konkani) | Goa University",
|
| 259 |
+
css=css,
|
| 260 |
+
) as demo:
|
| 261 |
+
|
| 262 |
+
# Updated Heading with Project Description & Affiliation
|
| 263 |
+
gr.HTML("""
|
| 264 |
+
<div style="text-align: center; margin-bottom: 25px;">
|
| 265 |
+
<h2 style="margin-bottom: 6px; color: #1a1a1a; font-size: 24px;">VIDYAAPATI (Hindi-Konkani)</h2>
|
| 266 |
+
<div style="font-weight: 600; color: #4a5568; margin-bottom: 10px;">Goa University</div>
|
| 267 |
+
<p style="font-size: 13px; color: #666; line-height: 1.5; max-width: 750px; margin: 0 auto;">
|
| 268 |
+
<b>Project:</b> “VIDYAAPATI: Bidirectional Machine Translation Involving Bengali, Konkani, Maithili, Marathi, and Hindi”.
|
| 269 |
+
Under the project titled, <b>“National Language Translation Mission (NLTM): BHASHINI”</b>
|
| 270 |
+
funded by the Ministry of Electronics and Information Technology (MeitY), Government of India.
|
| 271 |
+
</p>
|
| 272 |
+
</div>
|
| 273 |
+
""")
|
| 274 |
+
|
| 275 |
+
# Direction dropdown
|
| 276 |
+
direction = gr.Dropdown(
|
| 277 |
+
choices=[
|
| 278 |
+
"Hindi → Konkani",
|
| 279 |
+
"Konkani → Hindi",
|
| 280 |
+
],
|
| 281 |
+
value="Hindi → Konkani",
|
| 282 |
+
label="Translation Direction",
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
# Input textbox
|
| 286 |
+
input_text = gr.Textbox(
|
| 287 |
+
label="Input",
|
| 288 |
+
placeholder="Type or paste text here...",
|
| 289 |
+
lines=6,
|
| 290 |
+
max_lines=12,
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
# Translate button
|
| 294 |
+
translate_btn = gr.Button(
|
| 295 |
+
"Translate",
|
| 296 |
+
variant="primary",
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
# Output textbox
|
| 300 |
+
output_text = gr.Textbox(
|
| 301 |
+
label="Translation",
|
| 302 |
+
lines=6,
|
| 303 |
+
max_lines=12,
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
# Event handlers
|
| 307 |
+
translate_btn.click(
|
| 308 |
+
fn=translate,
|
| 309 |
+
inputs=[input_text, direction],
|
| 310 |
+
outputs=output_text,
|
| 311 |
+
)
|
| 312 |
+
|
| 313 |
+
input_text.submit(
|
| 314 |
+
fn=translate,
|
| 315 |
+
inputs=[input_text, direction],
|
| 316 |
+
outputs=output_text,
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
# -----------------------------------------------------------------------------
|
| 320 |
+
# Launch
|
| 321 |
+
# -----------------------------------------------------------------------------
|
| 322 |
+
|
| 323 |
+
if __name__ == "__main__":
|
| 324 |
+
demo.queue()
|
| 325 |
+
demo.launch(
|
| 326 |
+
server_name="0.0.0.0",
|
| 327 |
+
server_port=7860,
|
| 328 |
+
)
|