File size: 2,775 Bytes
92472dd b5bb8a8 1a0cfff 92472dd 875d8ff 92472dd 875d8ff 92472dd |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
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="https://arxiv.org/abs/PAPER_ID" 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() |