app.py Instantiation with hardcoded eng2swa translations
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
| 3 |
+
|
| 4 |
+
# Load the mBART model and tokenizer
|
| 5 |
+
model_name = "facebook/mbart-large-50-many-to-many-mmt"
|
| 6 |
+
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
| 7 |
+
tokenizer = MBart50TokenizerFast.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Hardcoded translation function (English -> Swahili)
|
| 10 |
+
def translate(text):
|
| 11 |
+
tokenizer.src_lang = "en_XX" # Set source language to English
|
| 12 |
+
encoded = tokenizer(text, return_tensors="pt")
|
| 13 |
+
generated_tokens = model.generate(**encoded, forced_bos_token_id=tokenizer.lang_code_to_id["sw_KE"]) # Translate to Swahili
|
| 14 |
+
return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
| 15 |
+
|
| 16 |
+
# Gradio UI
|
| 17 |
+
with gr.Blocks() as demo:
|
| 18 |
+
gr.Markdown("## English to Swahili Translator")
|
| 19 |
+
src_text = gr.Textbox(label="Enter English text")
|
| 20 |
+
output_text = gr.Textbox(label="Swahili Translation", interactive=False)
|
| 21 |
+
translate_btn = gr.Button("Translate")
|
| 22 |
+
|
| 23 |
+
translate_btn.click(translate, inputs=src_text, outputs=output_text)
|
| 24 |
+
|
| 25 |
+
demo.launch()
|