uday210 commited on
Commit
7770d10
·
verified ·
1 Parent(s): 647c3e5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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", torch_dtype=torch.bfloat16)
9
+
10
+
11
+
12
+ with open('Language.json', 'r') as file:
13
+ language_data = json.load(file)
14
+
15
+ def get_languages():
16
+ languages = []
17
+ for item in language_data:
18
+ languages.append(item["Language"])
19
+ return languages
20
+
21
+
22
+ def get_FLORES_code_from_language(language):
23
+ for entry in language_data:
24
+ if entry['Language'].lower() == language.lower():
25
+ return entry['FLORES-200 code']
26
+ return None
27
+
28
+
29
+
30
+
31
+ def translate_text(text, destination_language):
32
+ # text = "Hello Friends, How are you?"
33
+ dest_code= get_FLORES_code_from_language(destination_language)
34
+ translation = text_translator(text,
35
+ src_lang="eng_Latn",
36
+ tgt_lang=dest_code)
37
+ return translation[0]["translation_text"]
38
+
39
+ gr.close_all()
40
+
41
+
42
+ # demo = gr.Interface(fn=summary, inputs="text",outputs="text")
43
+ demo = gr.Interface(fn=translate_text,
44
+ inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(get_languages(), label="Select Destination Language")],
45
+ outputs=[gr.Textbox(label="Translated text",lines=4)],
46
+ title="@GenAILearniverse Project 4: Multi language translator",
47
+ description="THIS APPLICATION WILL BE USED TO TRNSLATE ANY ENGLIST TEXT TO MULTIPLE LANGUAGES.")
48
+ demo.launch()