Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,43 @@
|
|
| 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= ("../Models/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 |
-
# Load the JSON data from the file
|
| 17 |
-
with open('language.json', 'r') as file:
|
| 18 |
-
language_data = json.load(file)
|
| 19 |
-
|
| 20 |
-
def get_FLORES_code_from_language(language):
|
| 21 |
-
for entry in language_data:
|
| 22 |
-
if entry['Language'].lower() == language.lower():
|
| 23 |
-
return entry['FLORES-200 code']
|
| 24 |
-
return None
|
| 25 |
-
|
| 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="
|
| 42 |
-
description="THIS APPLICATION WILL BE USED TO TRNSLATE ANY ENGLIST TEXT TO MULTIPLE LANGUAGES.")
|
| 43 |
demo.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 |
+
#model_path= ("../Models/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 |
+
# Load the JSON data from the file
|
| 17 |
+
with open('language.json', 'r') as file:
|
| 18 |
+
language_data = json.load(file)
|
| 19 |
+
|
| 20 |
+
def get_FLORES_code_from_language(language):
|
| 21 |
+
for entry in language_data:
|
| 22 |
+
if entry['Language'].lower() == language.lower():
|
| 23 |
+
return entry['FLORES-200 code']
|
| 24 |
+
return None
|
| 25 |
+
|
| 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="language translator",
|
| 42 |
+
description="THIS APPLICATION WILL BE USED TO TRNSLATE ANY ENGLIST TEXT TO MULTIPLE LANGUAGES.")
|
| 43 |
demo.launch()
|