File size: 1,660 Bytes
ddc27c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Use a pipeline as a high-level helper
from transformers import pipeline
import torch
import json
import gradio as gr

text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)

# Load the JSON table
with open('language.json') as f:
    language_data = json.load(f)

def get_flores_200_code(language):
    for code in language_data:
        if code['Language'] == language:
            return code['FLORES-200 code']
    return None

def translate_text(text, destination_language):
    
    # text = "Hello friends how are you?"
    dest_code = get_flores_200_code(destination_language)

    translation = text_translator(text,
                              src_lang="eng_Latn",
                              tgt_lang=dest_code)
    return translation[0]["translation_text"]

gr.close_all()

demo = gr.Interface(fn=translate_text,
                    inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["English", "Bulgarian",
"Croatian",
"Czech",
"Danish",
"Dutch",
"English",
"Estonian",
"Finnish",
"French",
"German",
"Greek",
"Hungarian",
"Irish",
"Italian",
"Latvian",
"Lithuanian",
"Maltese",
"Polish",
"Portuguese",
"Romanian",
"Slovak",
"Slovenian",
"Spanish", 
"Swedish"],label="Select destination language")],
                    outputs=[gr.Textbox(label="Translated text", lines=4)],
                    title="Multi Language translator - From European languages to English",
                    description="This application uses NLLB model from Facebook trained on 200 languages for machine translation research",
                    concurrency_limit=16)
demo.launch()