File size: 1,425 Bytes
63a82cf
1b41464
63a82cf
1b41464
d74e010
 
 
 
1b41464
63a82cf
1b41464
 
 
 
 
 
 
 
 
 
 
 
 
d74e010
1b41464
 
 
 
 
 
d74e010
1b41464
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import gradio as gr
from transformers import pipeline

available_models = {
    "Baseline": pipeline("text2text-generation", model="samzirbo/mt5.baseline"),
    "Genered": pipeline("text2text-generation", model="samzirbo/mt5.gendered"),
    "Balanced": pipeline("text2text-generation", model="samzirbo/mt5.balanced"),
    "Gendered and Balanced": pipeline("text2text-generation", model="samzirbo/mt5.gendered_balanced")
}

def translate_text(model_name, lang_dir, gender, input_text):

    model = available_models[model_name]

    src, tgt = lang_dir.split(" -> ")

    prompt = f"Translate {src} to {tgt} " + f"as a {gender} : " if gender and  "gendered" in model_name else f"Translate {src} to {tgt} : "
    inputs = prompt + input_text
    print(inputs)
    output_text = model(inputs, max_length=128)
    return output_text[0]['generated_text']


model_dropdown = gr.Dropdown(choices=list(available_models.keys()), label="Select Model", value="Baseline")
lang_dropdown = gr.Dropdown(choices=["English -> Spanish", "Spanish -> English"], label="Language Direction", value="English -> Spanish")
gender_dropdown = gr.Dropdown(choices=["female", "male"], label="Select Gender", value=None)

iface = gr.Interface(fn=translate_text, 
                     inputs=[model_dropdown, lang_dropdown, gender_dropdown, "text"], 
                     outputs="text", 
                     title="Translation Interface")

iface.launch()