mbarnig's picture
Update app.py
16bae1a
import gradio as gr
import torch
from huggingface_hub import hf_hub_download
from transformers import pipeline
model_checkpoint_nllb = "facebook/nllb-200-distilled-600M"
model_checkpoint_marian_en = "mbarnig/marianNMT-tatoeba-en-lb"
model_checkpoint_marian_lb = "mbarnig/marianNMT-tatoeba-lb-en"
model_checkpoint_t5_mt5 = "mbarnig/T5-mt5-tatoeba-en-lb"
my_title = "🇬🇧 Mir iwwersetzen vun an op Lëtzebuergesch ! 🇫🇷"
my_description = "English-Luxembourgish machine translation (MT) demo based on 3 open-source transformer models: Facebook-NLLB, Microsoft-MarianNMT & Google-T5/mt5."
my_article = "<h3>User guide</h3><p>1. Press the submit button to translate an english text with the default values. 2. Compare the result with the luxembourgish example. 3. Select a model and a translation direction and enter your own text. Have fun !</p><p>Go to <a href='https://www.web3.lu/'>Internet with a Brain</a> to read my french publication <a href='https://www.web3.lu/'>Das Küsschen und die Sonne stritten sich ...</a> about the history of machine translation in Luxembourg from 1975 until today.</p>"
default_input = "The North Wind and the Sun were disputing which was the stronger, when a traveler came along wrapped in a warm cloak."
TRANSLATION_MODELS = [
"NLLB",
"MarianNMT",
"T5-mt5"
]
TRANSLATION_DIRECTION = [
"en -> lb",
"lb -> en"
]
EXAMPLE = [
["An der Zäit hunn sech den Nordwand an d’Sonn gestridden, wie vun hinnen zwee wuel méi staark wier, wéi e Wanderer, deen an ee waarme Mantel agepak war, iwwert de Wee koum", "NLLB", "lb -> en"]
]
my_inputs = [
gr.Textbox(lines=5, label="Input", value=default_input),
gr.Radio(label="Translation Model", choices = TRANSLATION_MODELS, value = "NLLB"),
gr.Radio(label="Translation Direction", choices = TRANSLATION_DIRECTION, value = "en -> lb")
]
my_output = gr.Textbox(lines=5, label="Translation")
def iwwersetz(source_text, model, direc):
if model == "NLLB":
translator = pipeline("translation", model=model_checkpoint_nllb)
if direc == "en -> lb":
translation = translator(source_text, src_lang="eng_Latn", tgt_lang="ltz_Latn")
else:
translation = translator(source_text, src_lang="ltz_Latn", tgt_lang="eng_Latn")
elif model == "MarianNMT":
if direc == "en -> lb":
translator = pipeline("translation", model=model_checkpoint_marian_en)
translation = translator(source_text)
else:
translator = pipeline("translation", model=model_checkpoint_marian_lb)
translation = translator(source_text)
elif model == "T5-mt5":
translator = pipeline("translation", model=model_checkpoint_t5_mt5)
translation = translator(source_text)
else:
translation = "Please select a Translation Model !"
return translation
demo=gr.Interface(
fn=iwwersetz,
inputs=my_inputs,
outputs=my_output,
title=my_title,
description=my_description,
article=my_article,
examples=EXAMPLE,
allow_flagging=False)
demo.launch()