miwytt's picture
Add paper link
1fe741e
from nmtscore import NMTScorer
import gradio as gr
from translation_direction_detection import TranslationDirectionDetector
detector = TranslationDirectionDetector(NMTScorer("m2m100_418M"))
buttons_html = """
<div style="display: flex; justify-content: start; align-items: center; gap: 10px;">
<a href="https://github.com/ZurichNLP/translation-direction-detection" target="_blank">
<img src="https://img.shields.io/badge/GitHub-View%20Source-blue?logo=github" alt="GitHub Repo"/>
</a>
<a href="http://arxiv.org/abs/2401.06769" target="_blank">
<img src="https://img.shields.io/badge/arXiv-Read%20Paper-brightgreen?logo=arxiv" alt="arXiv Paper"/>
</a>
</div>
""" # TODO: Update the arxiv link
description_text = """
This demo exemplifies a massively multilingual machine translation model's zero-shot capability to detect translation direction given two parallel texts as described in the paper "Machine Translation Models are Zero-Shot Detectors of Translation Direction" (linked above).
Instructions:
Simply input the parallel text in the respective languages and select the corresponding language for each text. The model will predict the direction of translation using the M2M100 418M translation model.
"""
description = buttons_html + description_text
def translate_direction(text1, lang1, text2, lang2):
lang_to_code = {"English": 'en',
"German": 'de',
"French": 'fr',
"Czech": 'cs',
"Ukrainian": 'uk',
"Chinese": 'zh',
"Russian": 'ru',
"Bengali": 'bn',
"Hindi": 'hi',
"Xhosa": 'xh',
"Zulu": 'zu',
}
if "\n" in text1 or "\n" in text2:
sentence1 = text1.split("\n")
sentence2 = text2.split("\n")
else:
sentence1 = text1
sentence2 = text2
result = detector.detect(sentence1, sentence2, lang_to_code[lang1], lang_to_code[lang2])
return result
iface = gr.Interface(
fn=translate_direction,
inputs=[
gr.Textbox(placeholder="Enter a single sentence or multiple sentences separated by a line break (Shift+Enter) in language 1 here.", label="Text 1"),
gr.Dropdown(choices=["English", "German", "French", "Czech", "Ukranian", "Chinese", "Russian", "Bengali", "Hindi", "Xhosa", "Zulu"], label="Language of Text 1"),
gr.Textbox(placeholder="Enter a single sentence or multiple sentences separated by a line break (Shift+Enter) in language 2 here.", label="Text 2"),
gr.Dropdown(choices=["English", "German", "French", "Czech", "Ukranian", "Chinese", "Russian", "Bengali", "Hindi", "Xhosa", "Zulu"], label="Language of Text 2")
],
outputs=gr.Textbox(label="Result"),
title="Translation Direction Detector",
description=description)
iface.launch()