Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 4 |
|
| 5 |
-
# Load model
|
| 6 |
model_path = "itsmeussa/AdabTranslate-Darija"
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
|
@@ -10,6 +10,7 @@ tokenizer = AutoTokenizer.from_pretrained(model_path)
|
|
| 10 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_path).to(device).eval()
|
| 11 |
|
| 12 |
def translate_darija_to_msa(text):
|
|
|
|
| 13 |
with torch.no_grad():
|
| 14 |
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 15 |
output_ids = model.generate(
|
|
@@ -22,10 +23,16 @@ def translate_darija_to_msa(text):
|
|
| 22 |
)
|
| 23 |
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 24 |
|
|
|
|
| 25 |
with gr.Blocks() as demo:
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
user_in = gr.Textbox(visible=False)
|
| 28 |
bot_out = gr.Textbox(visible=False)
|
|
|
|
|
|
|
| 29 |
user_in.submit(translate_darija_to_msa, inputs=user_in, outputs=bot_out)
|
| 30 |
|
| 31 |
demo.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 4 |
|
| 5 |
+
# --- Load the model ---
|
| 6 |
model_path = "itsmeussa/AdabTranslate-Darija"
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
|
|
|
| 10 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_path).to(device).eval()
|
| 11 |
|
| 12 |
def translate_darija_to_msa(text):
|
| 13 |
+
"""Takes Darija text and returns MSA translation"""
|
| 14 |
with torch.no_grad():
|
| 15 |
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 16 |
output_ids = model.generate(
|
|
|
|
| 23 |
)
|
| 24 |
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 25 |
|
| 26 |
+
# --- Build Gradio app ---
|
| 27 |
with gr.Blocks() as demo:
|
| 28 |
+
# Load your custom HTML file into the interface
|
| 29 |
+
gr.HTML(open("index.html", encoding="utf-8").read())
|
| 30 |
+
|
| 31 |
+
# Invisible components to "link" JS → Python
|
| 32 |
user_in = gr.Textbox(visible=False)
|
| 33 |
bot_out = gr.Textbox(visible=False)
|
| 34 |
+
|
| 35 |
+
# Connect backend function
|
| 36 |
user_in.submit(translate_darija_to_msa, inputs=user_in, outputs=bot_out)
|
| 37 |
|
| 38 |
demo.launch()
|