Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ model_name = "Mudasir692/mbart-eng-ur"
|
|
| 9 |
|
| 10 |
# Fix config issue
|
| 11 |
config = AutoConfig.from_pretrained(model_name)
|
| 12 |
-
if config
|
| 13 |
config.early_stopping = True
|
| 14 |
|
| 15 |
tokenizer = MBart50TokenizerFast.from_pretrained(model_name)
|
|
@@ -20,56 +20,96 @@ LANG_CODES = {
|
|
| 20 |
"Urdu": "ur_PK",
|
| 21 |
"Arabic": "ar_AR",
|
| 22 |
"Hindi": "hi_IN",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
# ---- Translation function ----
|
| 26 |
-
def translate_text(text, target_lang):
|
| 27 |
if not text.strip():
|
| 28 |
-
return "Please enter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
tgt_lang_code = LANG_CODES.get(target_lang, "ur_PK")
|
| 31 |
-
|
|
|
|
| 32 |
tokenizer.tgt_lang = tgt_lang_code
|
| 33 |
|
| 34 |
-
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
| 35 |
-
translated_tokens = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
output = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 37 |
return output
|
| 38 |
|
| 39 |
# ---- Examples ----
|
| 40 |
examples = [
|
| 41 |
-
["How are you?", "Urdu"],
|
| 42 |
-
["Where are you going?", "Arabic"],
|
| 43 |
-
["This is my new project.", "Hindi"],
|
|
|
|
|
|
|
| 44 |
]
|
| 45 |
|
| 46 |
# ---- Gradio Interface ----
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
outputs=gr.Textbox(label="Translation", lines=2),
|
| 54 |
-
examples=examples,
|
| 55 |
-
title="π Multi-Language Translator",
|
| 56 |
-
description="""
|
| 57 |
<div style='text-align:center;'>
|
| 58 |
-
<
|
| 59 |
-
<p
|
| 60 |
-
<p><
|
| 61 |
</div>
|
| 62 |
-
"""
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Fix config issue
|
| 11 |
config = AutoConfig.from_pretrained(model_name)
|
| 12 |
+
if getattr(config, "early_stopping", None) is None:
|
| 13 |
config.early_stopping = True
|
| 14 |
|
| 15 |
tokenizer = MBart50TokenizerFast.from_pretrained(model_name)
|
|
|
|
| 20 |
"Urdu": "ur_PK",
|
| 21 |
"Arabic": "ar_AR",
|
| 22 |
"Hindi": "hi_IN",
|
| 23 |
+
"French": "fr_XX",
|
| 24 |
+
"German": "de_DE",
|
| 25 |
+
"Spanish": "es_XX",
|
| 26 |
+
"Chinese": "zh_CN",
|
| 27 |
+
"Italian": "it_IT",
|
| 28 |
+
"Portuguese": "pt_XX",
|
| 29 |
+
"Russian": "ru_RU",
|
| 30 |
+
"Japanese": "ja_XX",
|
| 31 |
+
"Korean": "ko_KR",
|
| 32 |
+
"Turkish": "tr_TR",
|
| 33 |
+
"Persian": "fa_IR",
|
| 34 |
+
"Bengali": "bn_IN",
|
| 35 |
+
"Punjabi": "pa_IN",
|
| 36 |
+
"Pashto": "ps_AF",
|
| 37 |
+
"Malay": "ms_MY",
|
| 38 |
+
"Indonesian": "id_ID",
|
| 39 |
+
"Tamil": "ta_IN"
|
| 40 |
}
|
| 41 |
|
| 42 |
# ---- Translation function ----
|
| 43 |
+
def translate_text(text, target_lang, auto_detect):
|
| 44 |
if not text.strip():
|
| 45 |
+
return "β οΈ Please enter text to translate."
|
| 46 |
+
|
| 47 |
+
# Source language
|
| 48 |
+
if auto_detect:
|
| 49 |
+
# Very simple heuristic-based detection
|
| 50 |
+
if any("\u0600" <= ch <= "\u06FF" for ch in text):
|
| 51 |
+
src_lang = "ur_PK"
|
| 52 |
+
elif any("\u0900" <= ch <= "\u097F" for ch in text):
|
| 53 |
+
src_lang = "hi_IN"
|
| 54 |
+
else:
|
| 55 |
+
src_lang = "en_XX"
|
| 56 |
+
else:
|
| 57 |
+
src_lang = "en_XX"
|
| 58 |
|
| 59 |
tgt_lang_code = LANG_CODES.get(target_lang, "ur_PK")
|
| 60 |
+
|
| 61 |
+
tokenizer.src_lang = src_lang
|
| 62 |
tokenizer.tgt_lang = tgt_lang_code
|
| 63 |
|
| 64 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 65 |
+
translated_tokens = model.generate(
|
| 66 |
+
**inputs,
|
| 67 |
+
max_length=256,
|
| 68 |
+
num_beams=5,
|
| 69 |
+
early_stopping=True
|
| 70 |
+
)
|
| 71 |
output = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 72 |
return output
|
| 73 |
|
| 74 |
# ---- Examples ----
|
| 75 |
examples = [
|
| 76 |
+
["How are you?", "Urdu", False],
|
| 77 |
+
["Where are you going?", "Arabic", False],
|
| 78 |
+
["This is my new project.", "Hindi", False],
|
| 79 |
+
["I love learning new languages.", "French", False],
|
| 80 |
+
["Can you help me?", "Spanish", False],
|
| 81 |
]
|
| 82 |
|
| 83 |
# ---- Gradio Interface ----
|
| 84 |
+
with gr.Blocks(css="""
|
| 85 |
+
body {background: linear-gradient(to bottom right, #f7f9fb, #e0f7fa);}
|
| 86 |
+
.gr-button-primary {background-color: #1e3799 !important; color: white !important;}
|
| 87 |
+
""") as app:
|
| 88 |
+
|
| 89 |
+
gr.Markdown("""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
<div style='text-align:center;'>
|
| 91 |
+
<h2>π Multi-Language Translator (mBART)</h2>
|
| 92 |
+
<p>Translate between English and 20+ languages using a fine-tuned mBART model.</p>
|
| 93 |
+
<p style='color:gray;'>Built by <b>Khurram Basharat</b> β powered by Hugging Face & Gradio.</p>
|
| 94 |
</div>
|
| 95 |
+
""")
|
| 96 |
+
|
| 97 |
+
with gr.Row():
|
| 98 |
+
with gr.Column(scale=1):
|
| 99 |
+
text_input = gr.Textbox(label="Enter Text", placeholder="Type your sentence here...", lines=4)
|
| 100 |
+
target_lang = gr.Dropdown(sorted(LANG_CODES.keys()), label="Select Target Language", value="Urdu")
|
| 101 |
+
auto_detect = gr.Checkbox(label="Auto-detect Source Language", value=False)
|
| 102 |
+
translate_btn = gr.Button("π Translate")
|
| 103 |
+
|
| 104 |
+
with gr.Column(scale=1):
|
| 105 |
+
result_output = gr.Textbox(label="Translation", lines=4)
|
| 106 |
+
copy_btn = gr.Button("π Copy Translation")
|
| 107 |
+
|
| 108 |
+
gr.Examples(examples, inputs=[text_input, target_lang, auto_detect])
|
| 109 |
+
|
| 110 |
+
# ---- Actions ----
|
| 111 |
+
translate_btn.click(translate_text, inputs=[text_input, target_lang, auto_detect], outputs=result_output)
|
| 112 |
+
copy_btn.click(None, inputs=result_output, outputs=None, _js="(text) => navigator.clipboard.writeText(text)")
|
| 113 |
+
|
| 114 |
+
# ---- Launch app ----
|
| 115 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|