Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,34 +5,41 @@ import json
|
|
| 5 |
# Use a pipeline as a high-level helper
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 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 |
-
|
| 24 |
-
dest_code
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
gr.close_all()
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Use a pipeline as a high-level helper
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
+
# Initialize the translation pipeline
|
| 9 |
+
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", 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 |
+
# Extract language names from the JSON data
|
| 16 |
+
language_names = [entry['Language'] for entry in language_data]
|
| 17 |
+
|
| 18 |
def get_FLORES_code_from_language(language):
|
| 19 |
for entry in language_data:
|
| 20 |
if entry['Language'].lower() == language.lower():
|
| 21 |
return entry['FLORES-200 code']
|
| 22 |
return None
|
| 23 |
|
|
|
|
| 24 |
def translate_text(text, destination_language):
|
| 25 |
+
dest_code = get_FLORES_code_from_language(destination_language)
|
| 26 |
+
if dest_code:
|
| 27 |
+
translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)
|
| 28 |
+
return translation[0]["translation_text"]
|
| 29 |
+
else:
|
| 30 |
+
return "Destination language code not found."
|
| 31 |
+
|
| 32 |
+
# Create and launch the Gradio interface
|
| 33 |
gr.close_all()
|
| 34 |
+
demo = gr.Interface(
|
| 35 |
+
fn=translate_text,
|
| 36 |
+
inputs=[
|
| 37 |
+
gr.Textbox(label="Input text to translate", lines=6),
|
| 38 |
+
gr.Dropdown(language_names, label="Select Destination Language")
|
| 39 |
+
],
|
| 40 |
+
outputs=[gr.Textbox(label="Translated text", lines=4)],
|
| 41 |
+
title="Multi-language Translator",
|
| 42 |
+
description="This application translates any English text to multiple languages."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
demo.launch(auth=('user', 'test@123'))
|