Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
import json
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# Load translation model
|
7 |
+
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
|
8 |
+
|
9 |
+
# Load language data
|
10 |
+
with open('language.json', 'r') as file:
|
11 |
+
language_data = json.load(file)
|
12 |
+
|
13 |
+
def get_FLORES_code_from_language(language):
|
14 |
+
for entry in language_data:
|
15 |
+
if entry['Language'].lower() == language.lower():
|
16 |
+
return entry['FLORES-200 code']
|
17 |
+
return None
|
18 |
+
|
19 |
+
def translate_text(text, destination_language):
|
20 |
+
dest_code = get_FLORES_code_from_language(destination_language)
|
21 |
+
if not dest_code:
|
22 |
+
return "Language code not found. Please try again."
|
23 |
+
translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)
|
24 |
+
return translation[0]["translation_text"]
|
25 |
+
|
26 |
+
# Gradio layout with default theme
|
27 |
+
description_text = """
|
28 |
+
# Multi-Language Translator
|
29 |
+
Translate English text into multiple languages easily and efficiently.
|
30 |
+
|
31 |
+
### Credits:
|
32 |
+
Made by Taizun S
|
33 |
+
"""
|
34 |
+
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown(description_text)
|
37 |
+
with gr.Row():
|
38 |
+
with gr.Column():
|
39 |
+
input_text = gr.Textbox(label="Input English Text", placeholder="Type your text here...", lines=6)
|
40 |
+
destination_language = gr.Dropdown(["German", "French", "Hindi", "Romanian", "Arabic"],
|
41 |
+
label="Select Language")
|
42 |
+
submit_button = gr.Button("Translate")
|
43 |
+
with gr.Column():
|
44 |
+
output_text = gr.Textbox(label="Translated Text", placeholder="Your translation appears here...", lines=4)
|
45 |
+
|
46 |
+
# Link the components
|
47 |
+
submit_button.click(fn=translate_text, inputs=[input_text, destination_language], outputs=[output_text])
|
48 |
+
|
49 |
+
demo.launch()
|