Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,34 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
|
4 |
+
available_models = {
|
5 |
+
"mt5.baseline": pipeline("text2text-generation", model="samzirbo/mt5.baseline"),
|
6 |
+
"mt5.genered": pipeline("text2text-generation", model="samzirbo/mt5.gendered"),
|
7 |
+
"mt5.balanced": pipeline("text2text-generation", model="samzirbo/mt5.balanced"),
|
8 |
+
"mt5.gendered_balanced": pipeline("text2text-generation", model="samzirbo/mt5.gendered_balanced")
|
9 |
+
}
|
10 |
|
11 |
+
def translate_text(model_name, lang_dir, gender, input_text):
|
12 |
+
|
13 |
+
model = available_models[model_name]
|
14 |
+
|
15 |
+
src, tgt = lang_dir.split(" -> ")
|
16 |
+
|
17 |
+
prompt = f"Translate {src} to {tgt} " + f"as a {gender} : " if gender and "gendered" in model_name else f"Translate {src} to {tgt} : "
|
18 |
+
inputs = prompt + input_text
|
19 |
+
print(inputs)
|
20 |
+
output_text = model(inputs, max_length=128)
|
21 |
+
return output_text[0]['generated_text']
|
22 |
+
|
23 |
+
|
24 |
+
model_dropdown = gr.Dropdown(choices=list(available_models.keys()), label="Select Model", value="mt5.baseline")
|
25 |
+
lang_dropdown = gr.Dropdown(choices=["English -> Spanish", "Spanish -> English"], label="Language Direction", value="English -> Spanish")
|
26 |
+
gender_dropdown = gr.Dropdown(choices=["female", "male"], label="Select Gender", value=None)
|
27 |
+
|
28 |
+
iface = gr.Interface(fn=translate_text,
|
29 |
+
inputs=[model_dropdown, lang_dropdown, gender_dropdown, "text"],
|
30 |
+
outputs="text",
|
31 |
+
title="Translation Interface",
|
32 |
+
description="Select a model, language direction, and input text to translate.")
|
33 |
+
|
34 |
+
iface.launch()
|