2seriescs commited on
Commit
ac2b8dc
1 Parent(s): 41c575a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ import gradio as gr
4
+ import torch
5
+ # Use a pipeline as a high-level helper
6
+ from transformers import pipeline
7
+
8
+ pipe = pipeline(
9
+ "translation",
10
+ model="facebook/nllb-200-distilled-600M",
11
+ torch_dtype=torch.bfloat16)
12
+
13
+ # Load the JSON data from the file
14
+ with open('language.json', 'r') as file:
15
+ language_data = json.load(file)
16
+
17
+ def get_FLORES_code_from_language(language):
18
+ for entry in language_data:
19
+ if entry['Language'].lower() == language.lower():
20
+ return entry['FLORES-200 code']
21
+ return None
22
+
23
+
24
+ def translate_text(text, destination_language):
25
+ dest_code = get_FLORES_code_from_language(destination_language)
26
+ if dest_code is None:
27
+ return "Invalid destination language."
28
+
29
+ translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)
30
+ return translation[0]["translation_text"]
31
+
32
+ gr.close_all()
33
+
34
+ # demo = gr.Interface(fn=summary, inputs="text",outputs="text")
35
+ demo = gr.Interface(fn=translate_text,
36
+ inputs=[gr.Textbox(label="Input text for translation",lines=6), gr.Dropdown(
37
+ ["Arabic", "Afrikaans", "Bengali", "Greek", "Estonian", "Portuguese", "Spanish"],
38
+ label="Select Destination Language")
39
+ ],
40
+ outputs=[gr.Textbox(label="Translated Text",lines=4)],
41
+ title="@caesar-2series: Multilingual Language Interpreter",
42
+ description="Translations from English into a few foreign languages")
43
+ demo.launch()