natfil commited on
Commit
cff2c21
·
verified ·
1 Parent(s): edf6896

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ import json
4
+
5
+ # Use a pipeline as a high-level helper
6
+ from transformers import pipeline
7
+
8
+ text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M",
9
+ torch_dtype=torch.bfloat16)
10
+
11
+ # Load the JSON data from the file
12
+ with open('language.json', 'r') as file:
13
+ language_data = json.load(file)
14
+
15
+ def get_FLORES_code_from_language(language):
16
+ for entry in language_data:
17
+ if entry['Language'].lower() == language.lower():
18
+ return entry['FLORES-200 code']
19
+ return None
20
+
21
+
22
+ def translate_text(text, destination_language):
23
+ # text = "Hello Friends, How are you?"
24
+ dest_code= get_FLORES_code_from_language(destination_language)
25
+ translation = text_translator(text,
26
+ src_lang="eng_Latn",
27
+ tgt_lang=dest_code)
28
+ return translation[0]["translation_text"]
29
+
30
+ gr.close_all()
31
+
32
+ demo = gr.Interface(fn=translate_text,
33
+ inputs=[gr.Textbox(label="Zu übersetzenden Text eingeben",lines=6),
34
+ gr.Dropdown(["German","French", "Hindi", "Romanian "],
35
+ label="Zielsprache auswählen")],
36
+ outputs=[gr.Textbox(label="Übersetzter Text",lines=4)],
37
+ title="Projekt 4: Mehrsprachiger Übersetzer",
38
+ description="DIESE ANWENDUNG WIRD VERWENDET, UM EINEN BELIEBIGEN ENGLISCHEN TEXT IN MEHRERE SPRACHEN ZU ÜBERSETZEN",
39
+ allow_flagging="never",
40
+ submit_btn="Übermitteln",
41
+ clear_btn="Bereinigen")
42
+ demo.launch()