Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,20 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 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", torch_dtype=torch.bfloat16)
|
| 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 |
-
#
|
| 28 |
-
|
| 29 |
-
# translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)
|
| 30 |
-
# return translation[0]["translation_text"]
|
| 31 |
|
| 32 |
-
#
|
| 33 |
|
| 34 |
-
#
|
| 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 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
import gradio as gr
|
| 45 |
-
import json
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
from transformers import pipeline
|
| 49 |
|
| 50 |
-
text_translator = pipeline("translation", model=model_path,
|
| 51 |
-
torch_dtype=torch.bfloat16)
|
| 52 |
-
# Load the JSON data from the file
|
| 53 |
with open('language.json', 'r') as file:
|
| 54 |
language_data = json.load(file)
|
| 55 |
|
|
@@ -59,25 +24,21 @@ def get_FLORES_code_from_language(language):
|
|
| 59 |
return entry['FLORES-200 code']
|
| 60 |
return None
|
| 61 |
|
| 62 |
-
|
| 63 |
def translate_text(text, destination_language):
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
translation = text_translator(text,
|
| 67 |
-
src_lang="eng_Latn",
|
| 68 |
-
tgt_lang=dest_code)
|
| 69 |
return translation[0]["translation_text"]
|
| 70 |
|
| 71 |
gr.close_all()
|
| 72 |
|
| 73 |
-
#
|
| 74 |
demo = gr.Interface(fn=translate_text,
|
| 75 |
-
inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["German","French", "Hindi", "Romanian
|
| 76 |
outputs=[gr.Textbox(label="Translated text",lines=4)],
|
| 77 |
title="@GenAILearniverse Project 4: Multi language translator",
|
| 78 |
description="THIS APPLICATION WILL BE USED TO TRNSLATE ANY ENGLIST TEXT TO MULTIPLE LANGUAGES.")
|
| 79 |
-
demo.launch()
|
| 80 |
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
|
|
|
|
| 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 |
+
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
|
| 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 |
|
|
|
|
| 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 |
|
| 43 |
|
| 44 |
|