Spaces:
Sleeping
Sleeping
kasper-boy
commited on
Commit
•
25a7c2f
1
Parent(s):
ebe4b40
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,38 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
return
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
description="Translate text from one language to another using a T5 model."
|
39 |
-
)
|
40 |
-
|
41 |
-
if __name__ == "__main__":
|
42 |
-
iface.launch()
|
|
|
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=summary, inputs="text",outputs="text")
|
33 |
+
demo = gr.Interface(fn=translate_text,
|
34 |
+
inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["German","French", "Hindi", "Romanian "], label="Select Destination Language")],
|
35 |
+
outputs=[gr.Textbox(label="Translated text",lines=4)],
|
36 |
+
title="@GenAILearniverse Project 4: Multi language translator",
|
37 |
+
description="THIS APPLICATION WILL BE USED TO TRNSLATE ANY ENGLIST TEXT TO MULTIPLE LANGUAGES.")
|
38 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|