prograk commited on
Commit
3f8fc2d
·
verified ·
1 Parent(s): 5f85d18

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 json
3
+ import gradio as gr
4
+
5
+ # Use a pipeline as a high-level helper
6
+ from transformers import pipeline
7
+
8
+ # model_path = "../Models/models--facebook--nllb-200-distilled-600M/snapshots/f8d333a098d19b4fd9a8b18f94170487ad3f821d"
9
+
10
+ # text_translator = pipeline("translation", model=model_path, torch_dtype=torch.bfloat16)
11
+
12
+ pipe = pipeline("translation", model="facebook/nllb-200-distilled-600M")
13
+
14
+ # text = "Hello friends, How are you?"
15
+
16
+ # translation = text_translator(text, src_lang="eng_Latn", tgt_lang="deu_Latn")
17
+
18
+ with open('language.json', 'r') as file:
19
+ language_data = json.load(file)
20
+
21
+ def get_FLORES_code_from_language(language):
22
+ for entry in language_data:
23
+ if entry['Language'].lower() == language.lower():
24
+ return entry['FLORES-200 code']
25
+ return None
26
+
27
+ def translate_text(text, destination_language):
28
+ dest_code = get_FLORES_code_from_language(destination_language)
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
+ # print(translate_text(text, "Hindi"))
35
+ demo = gr.Interface(fn=translate_text,
36
+ inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["German", "French", "Hindi", "Romanian", "Marathi"], label="Select Destination Language")],
37
+ outputs=[gr.Textbox(label="Translated text",lines=4)],
38
+ title="@GenAILearniverse Project 4: Multi language translator",
39
+ description="THIS APPLICATION WILL BE USED TO TRNSLATE ANY ENGLIST TEXT TO MULTIPLE LANGUAGES.")
40
+
41
+ demo.launch()
42
+