enasyazzs commited on
Commit
808454f
·
verified ·
1 Parent(s): cf54efa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ model_path = ("../.venv/Modal/models--facebook--nllb-200-distilled-600M/snapshots"
9
+ "/f8d333a098d19b4fd9a8b18f94170487ad3f821d")
10
+
11
+ text_translator = pipeline("translation", model="facebook--nllb-200-distilled-600M",
12
+ torch_dtype=torch.bfloat16)
13
+
14
+ # text_translator = pipeline("translation", model=model_path,
15
+ # torch_dtype=torch.bfloat16)
16
+
17
+ # Load the JSON data from the file
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
+ # text = "Hello friends, how are you?"
29
+ dest_code = get_FLORES_code_from_language(destination_language)
30
+ translation = text_translator(text,
31
+ src_lang="eng_Latn",
32
+ tgt_lang=dest_code)
33
+ return translation[0]["translation_text"]
34
+
35
+ gr.close_all()
36
+
37
+ # demo = gr.Interface(fn=summary, inputs="text", outputs="text")
38
+ demo = gr.Interface(fn=translate_text,
39
+ inputs=[gr.Textbox(label="Input text to translate", lines=6), gr.Dropdown(["German", "French", "Hindi", "Romanian" ], label="Select Destination Language")],
40
+ outputs=[gr.Textbox(label="Translated text", lines=4)],
41
+ title="Project 4: Multi language translator",
42
+ description="THIS APPLICATIONS WILL BE USED TO TRANSLATE ANY ENGLISH TEXT TO MULTIPLE LANGUAGE.")
43
+
44
+ demo.launch()