File size: 4,589 Bytes
c341525 |
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 |
from functools import lru_cache
from typing import Literal, Any
import gradio as gr
from transformers import pipeline
from indic_transliteration import sanscript
from indic_transliteration.sanscript import transliterate
import os
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0" # silences oneDNN
# Model identifiers (Helsinki-NLP MarianMT models)
HI_EN_MODEL = "Helsinki-NLP/opus-mt-hi-en"
EN_HI_MODEL = "Helsinki-NLP/opus-mt-en-hi"
TaskType = Literal[
"Hindi → English",
"English → Hindi",
"Hindi → Hinglish (transliteration)",
]
@lru_cache(maxsize=None)
def get_hi_en_pipe() -> pipeline:
"""Lazy-load Hindi → English translation pipeline"""
return pipeline("translation", model=HI_EN_MODEL)
@lru_cache(maxsize=None)
def get_en_hi_pipe() -> pipeline:
"""Lazy-load English → Hindi translation pipeline"""
return pipeline("translation", model=EN_HI_MODEL)
def translate(
text: str | None,
task: TaskType,
max_length: int = 256,
) -> str:
"""
Main translation function used by the Gradio interface.
Handles empty input gracefully and supports three tasks.
"""
text = (text or "").strip()
if not text:
return ""
try:
if task == "Hindi → English":
pipe = get_hi_en_pipe()
result = pipe(
text,
max_length=max_length,
num_beams=4,
early_stopping=True,
)
return result[0]["translation_text"]
if task == "English → Hindi":
pipe = get_en_hi_pipe()
result = pipe(
text,
max_length=max_length,
num_beams=4,
early_stopping=True,
)
return result[0]["translation_text"]
if task == "Hindi → Hinglish (transliteration)":
return transliterate(text, sanscript.DEVANAGARI, sanscript.ITRANS)
return "Unsupported task selected"
except (ValueError, RuntimeError, OSError) as e:
return f"Translation error: {str(e)}"
except Exception as e: # pylint: disable=broad-exception-caught
return f"Unexpected error during translation: {str(e)}"
def build_demo() -> gr.Blocks:
"""Build and configure the Gradio interface"""
with gr.Blocks(title="Hindi ↔ English + Hinglish Translator") as demo:
gr.Markdown(
"""
# Hindi ↔ English + Hinglish Translator
- **Hindi → English** & **English → Hindi**: Neural machine translation
- **Hindi → Hinglish**: Roman transliteration (Devanagari → ITRANS)
"""
)
with gr.Row():
task = gr.Dropdown(
choices=[
"Hindi → English",
"English → Hindi",
"Hindi → Hinglish (transliteration)",
],
value="Hindi → English",
label="Task",
interactive=True,
)
max_len = gr.Slider(
32, 512, value=256, step=16,
label="Max output length",
info="Higher values allow longer translations (slower)",
)
input_text = gr.Textbox(
label="Input text",
lines=5,
placeholder="नमस्ते दुनिया! या Hello world...",
)
translate_btn = gr.Button("Translate", variant="primary")
output_text = gr.Textbox(
label="Output",
lines=5,
interactive=False,
)
translate_btn.click(
fn=translate,
inputs=[input_text, task, max_len],
outputs=output_text,
)
gr.Examples(
examples=[
["नमस्ते! सब ठीक है?", "Hindi → English", 128],
["How are you today?", "English → Hindi", 128],
["नमस्ते भाई, क्या हाल है?", "Hindi → Hinglish (transliteration)", 128],
["खुश रहो और मुस्कुराते रहो", "Hindi → Hinglish (transliteration)", 128],
],
inputs=[input_text, task, max_len],
label="Quick examples",
)
return demo
demo = build_demo()
if __name__ == "__main__":
# pragma: no cover - UI launch not tested
demo.launch(
# share=False,
# inbrowser=False,
# server_name="0.0.0.0",
# server_port=7860,
) |