Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,23 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
#
|
|
|
|
| 6 |
model_name = "facebook/nllb-200-distilled-600M"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
-
def translate_dialectgo(text,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
tokenizer.src_lang = src
|
| 12 |
inputs = tokenizer(text, return_tensors="pt")
|
| 13 |
forced_target_id = tokenizer.convert_tokens_to_ids(tgt)
|
|
@@ -21,24 +32,21 @@ def translate_dialectgo(text, src, tgt):
|
|
| 21 |
|
| 22 |
return tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
],
|
| 40 |
-
outputs=gr.Textbox(label="Translation"),
|
| 41 |
-
title="DialectGo AI"
|
| 42 |
-
)
|
| 43 |
|
| 44 |
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Load the model
|
| 6 |
+
# Ginamit natin itong distilled version para mabilis mag-load sa Hugging Face
|
| 7 |
model_name = "facebook/nllb-200-distilled-600M"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
def translate_dialectgo(text, src_name, tgt_name):
|
| 12 |
+
# Mapping ng names to NLLB codes
|
| 13 |
+
codes = {
|
| 14 |
+
"Tagalog": "tgl_Latn",
|
| 15 |
+
"Cebuano": "ceb_Latn",
|
| 16 |
+
"English": "eng_Latn"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
src = codes[src_name]
|
| 20 |
+
tgt = codes[tgt_name]
|
| 21 |
+
|
| 22 |
tokenizer.src_lang = src
|
| 23 |
inputs = tokenizer(text, return_tensors="pt")
|
| 24 |
forced_target_id = tokenizer.convert_tokens_to_ids(tgt)
|
|
|
|
| 32 |
|
| 33 |
return tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
| 34 |
|
| 35 |
+
# UI Design
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("#DialectGo AI")
|
| 38 |
+
gr.Markdown("Trilingual Translation for Cebuano, Tagalog, and English.")
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
input_text = gr.Textbox(label="Type your message here...", placeholder="Kumain ka na ba?")
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
src_lang = gr.Dropdown(["Tagalog", "Cebuano", "English"], label="From", value="Tagalog")
|
| 45 |
+
tgt_lang = gr.Dropdown(["Tagalog", "Cebuano", "English"], label="To", value="Cebuano")
|
| 46 |
+
|
| 47 |
+
submit_btn = gr.Button("Translate")
|
| 48 |
+
output_text = gr.Textbox(label="Translation Result")
|
| 49 |
+
|
| 50 |
+
submit_btn.click(fn=translate_dialectgo, inputs=[input_text, src_lang, tgt_lang], outputs=output_text)
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
demo.launch()
|