Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
|
| 5 |
+
# 1. Define paths
|
| 6 |
+
# We load the base configuration and weights from the official hub,
|
| 7 |
+
# but point the model to load your local folder ("./") for the PEFT adapter layers.
|
| 8 |
+
base_model_name = "facebook/nllb-200-distilled-600M"
|
| 9 |
+
adapter_path = "./"
|
| 10 |
+
|
| 11 |
+
print("Loading official NLLB-200 base model...")
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
| 13 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(base_model_name)
|
| 14 |
+
|
| 15 |
+
print("Merging your fine-tuned DialectGo adapter layers...")
|
| 16 |
+
# This automatically spots adapter_config.json in your root and snaps it on
|
| 17 |
+
model.load_adapter(adapter_path)
|
| 18 |
+
|
| 19 |
+
# 2. Map display names to NLLB-200 language codes
|
| 20 |
+
LANG_CODES = {
|
| 21 |
+
"English": "eng_Latn",
|
| 22 |
+
"Tagalog": "tgl_Latn",
|
| 23 |
+
"Cebuano": "ceb_Latn"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
def translate_text(text, target_lang_name):
|
| 27 |
+
if not text.strip():
|
| 28 |
+
return "Please enter text to translate."
|
| 29 |
+
|
| 30 |
+
target_code = LANG_CODES[target_lang_name]
|
| 31 |
+
|
| 32 |
+
# Run inference using CPU tokens
|
| 33 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 34 |
+
|
| 35 |
+
translated_tokens = model.generate(
|
| 36 |
+
**inputs,
|
| 37 |
+
forced_bos_token_id=tokenizer.convert_tokens_to_ids(target_code),
|
| 38 |
+
max_length=128
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
result = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
| 42 |
+
return result
|
| 43 |
+
|
| 44 |
+
# 3. Build the UI Layout
|
| 45 |
+
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 46 |
+
gr.Markdown("# 🇵🇭 DialectGo Translator (LoRA Fine-Tuned)")
|
| 47 |
+
gr.Markdown("Translate seamlessly between English, Tagalog, and Cebuano using your adapter-optimized NLLB model.")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column():
|
| 51 |
+
input_text = gr.Textbox(
|
| 52 |
+
label="Source Text",
|
| 53 |
+
placeholder="Type your text here...",
|
| 54 |
+
lines=4
|
| 55 |
+
)
|
| 56 |
+
target_lang = gr.Dropdown(
|
| 57 |
+
choices=["English", "Tagalog", "Cebuano"],
|
| 58 |
+
value="Tagalog",
|
| 59 |
+
label="Translate To"
|
| 60 |
+
)
|
| 61 |
+
submit_btn = gr.Button("Translate", variant="primary")
|
| 62 |
+
|
| 63 |
+
with gr.Column():
|
| 64 |
+
output_text = gr.Textbox(
|
| 65 |
+
label="Translation Output",
|
| 66 |
+
lines=4,
|
| 67 |
+
interactive=False
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
submit_btn.click(
|
| 71 |
+
fn=translate_text,
|
| 72 |
+
inputs=[input_text, target_lang],
|
| 73 |
+
outputs=output_text
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
demo.launch()
|