Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,99 @@
|
|
| 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 |
-
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
# text = "Hello friends, How are you?"
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
with open('language.json', 'r') as file:
|
| 19 |
language_data = json.load(file)
|
| 20 |
|
|
@@ -24,19 +103,29 @@ def get_FLORES_code_from_language(language):
|
|
| 24 |
return entry['FLORES-200 code']
|
| 25 |
return None
|
| 26 |
|
|
|
|
| 27 |
def translate_text(text, destination_language):
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
return translation[0]["translation_text"]
|
| 31 |
|
| 32 |
gr.close_all()
|
| 33 |
|
| 34 |
-
#
|
| 35 |
demo = gr.Interface(fn=translate_text,
|
| 36 |
-
inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["German",
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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", 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 |
+
# 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 |
+
Hugging Face's logo
|
| 44 |
+
Hugging Face
|
| 45 |
+
Models
|
| 46 |
+
Datasets
|
| 47 |
+
Spaces
|
| 48 |
+
Posts
|
| 49 |
+
Docs
|
| 50 |
+
Enterprise
|
| 51 |
+
Pricing
|
| 52 |
|
|
|
|
|
|
|
| 53 |
|
|
|
|
| 54 |
|
| 55 |
+
Spaces:
|
| 56 |
|
| 57 |
+
GenAILearniverse
|
| 58 |
+
/
|
| 59 |
+
MultiLanguageTranslator
|
| 60 |
|
|
|
|
| 61 |
|
| 62 |
+
like
|
| 63 |
+
1
|
| 64 |
+
App
|
| 65 |
+
Files
|
| 66 |
+
Community
|
| 67 |
+
1
|
| 68 |
+
MultiLanguageTranslator
|
| 69 |
+
/
|
| 70 |
+
app.py
|
| 71 |
|
| 72 |
+
GenAILearniverse's picture
|
| 73 |
+
GenAILearniverse
|
| 74 |
+
Create app.py
|
| 75 |
+
92b225d
|
| 76 |
+
verified
|
| 77 |
+
11 months ago
|
| 78 |
+
raw
|
| 79 |
+
|
| 80 |
+
Copy download link
|
| 81 |
+
history
|
| 82 |
+
blame
|
| 83 |
+
contribute
|
| 84 |
+
delete
|
| 85 |
+
|
| 86 |
+
1.77 kB
|
| 87 |
+
import torch
|
| 88 |
+
import gradio as gr
|
| 89 |
+
import json
|
| 90 |
+
|
| 91 |
+
# Use a pipeline as a high-level helper
|
| 92 |
+
from transformers import pipeline
|
| 93 |
+
|
| 94 |
+
text_translator = pipeline("translation", model=model_path,
|
| 95 |
+
torch_dtype=torch.bfloat16)
|
| 96 |
+
# Load the JSON data from the file
|
| 97 |
with open('language.json', 'r') as file:
|
| 98 |
language_data = json.load(file)
|
| 99 |
|
|
|
|
| 103 |
return entry['FLORES-200 code']
|
| 104 |
return None
|
| 105 |
|
| 106 |
+
|
| 107 |
def translate_text(text, destination_language):
|
| 108 |
+
# text = "Hello Friends, How are you?"
|
| 109 |
+
dest_code= get_FLORES_code_from_language(destination_language)
|
| 110 |
+
translation = text_translator(text,
|
| 111 |
+
src_lang="eng_Latn",
|
| 112 |
+
tgt_lang=dest_code)
|
| 113 |
return translation[0]["translation_text"]
|
| 114 |
|
| 115 |
gr.close_all()
|
| 116 |
|
| 117 |
+
# demo = gr.Interface(fn=summary, inputs="text",outputs="text")
|
| 118 |
demo = gr.Interface(fn=translate_text,
|
| 119 |
+
inputs=[gr.Textbox(label="Input text to translate",lines=6), gr.Dropdown(["German","French", "Hindi", "Romanian "], label="Select Destination Language")],
|
| 120 |
outputs=[gr.Textbox(label="Translated text",lines=4)],
|
| 121 |
title="@GenAILearniverse Project 4: Multi language translator",
|
| 122 |
description="THIS APPLICATION WILL BE USED TO TRNSLATE ANY ENGLIST TEXT TO MULTIPLE LANGUAGES.")
|
|
|
|
| 123 |
demo.launch()
|
| 124 |
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|