Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
import json
|
| 4 |
-
from transformers import pipeline
|
| 5 |
import datetime
|
|
|
|
| 6 |
|
|
|
|
| 7 |
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
|
| 8 |
|
|
|
|
| 9 |
try:
|
| 10 |
with open("Files/languages.json", "r", encoding="utf-8") as file:
|
| 11 |
data = json.load(file)
|
|
@@ -16,6 +18,7 @@ except json.JSONDecodeError:
|
|
| 16 |
print("Failed to decode JSON file.")
|
| 17 |
data = []
|
| 18 |
|
|
|
|
| 19 |
def FLORES_code(source_language, target_language):
|
| 20 |
src_code = tgt_code = None
|
| 21 |
for entry in data:
|
|
@@ -27,6 +30,7 @@ def FLORES_code(source_language, target_language):
|
|
| 27 |
return src_code, tgt_code
|
| 28 |
return "Language not found in the dataset.", None
|
| 29 |
|
|
|
|
| 30 |
def translate_text(text, source_language, target_language):
|
| 31 |
src_code, target_code = FLORES_code(source_language, target_language)
|
| 32 |
if src_code == "Language not found in the dataset." or not src_code or not target_code:
|
|
@@ -34,22 +38,30 @@ def translate_text(text, source_language, target_language):
|
|
| 34 |
translation = text_translator(text, src_lang=src_code, tgt_lang=target_code)
|
| 35 |
return translation[0]["translation_text"]
|
| 36 |
|
|
|
|
| 37 |
def save_translation(text):
|
| 38 |
filename = f"translation_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
|
| 39 |
with open(filename, "w", encoding="utf-8") as f:
|
| 40 |
f.write(text)
|
| 41 |
return filename
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
gr.close_all()
|
| 44 |
|
|
|
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
gr.Markdown("# 🌐 Multilingual Translator")
|
| 47 |
gr.Markdown("## Translate text between multiple languages.")
|
| 48 |
|
|
|
|
| 49 |
with gr.Row():
|
| 50 |
input_len_slider = gr.Slider(minimum=1, maximum=50, value=5, label="Input Textbox Height (Lines)")
|
| 51 |
output_len_slider = gr.Slider(minimum=1, maximum=50, value=5, label="Output Textbox Height (Lines)")
|
| 52 |
|
|
|
|
| 53 |
with gr.Row():
|
| 54 |
with gr.Column(scale=1):
|
| 55 |
src_dropdown = gr.Dropdown(
|
|
@@ -76,13 +88,17 @@ with gr.Blocks() as demo:
|
|
| 76 |
placeholder="Translation will appear here..."
|
| 77 |
)
|
| 78 |
|
|
|
|
| 79 |
with gr.Row():
|
| 80 |
save_btn = gr.Button("💾 Save Translation")
|
|
|
|
| 81 |
file_output = gr.File(label="Download File")
|
| 82 |
|
|
|
|
| 83 |
translate_btn.click(translate_text, inputs=[text_input, src_dropdown, tgt_dropdown], outputs=output_box)
|
| 84 |
input_len_slider.change(lambda n: gr.update(lines=n), inputs=input_len_slider, outputs=text_input)
|
| 85 |
output_len_slider.change(lambda n: gr.update(lines=n), inputs=output_len_slider, outputs=output_box)
|
| 86 |
save_btn.click(save_translation, inputs=output_box, outputs=file_output)
|
|
|
|
| 87 |
|
| 88 |
demo.launch()
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
import json
|
|
|
|
| 4 |
import datetime
|
| 5 |
+
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# Load model
|
| 8 |
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
|
| 9 |
|
| 10 |
+
# Load language list
|
| 11 |
try:
|
| 12 |
with open("Files/languages.json", "r", encoding="utf-8") as file:
|
| 13 |
data = json.load(file)
|
|
|
|
| 18 |
print("Failed to decode JSON file.")
|
| 19 |
data = []
|
| 20 |
|
| 21 |
+
# Get FLORES codes
|
| 22 |
def FLORES_code(source_language, target_language):
|
| 23 |
src_code = tgt_code = None
|
| 24 |
for entry in data:
|
|
|
|
| 30 |
return src_code, tgt_code
|
| 31 |
return "Language not found in the dataset.", None
|
| 32 |
|
| 33 |
+
# Translate function
|
| 34 |
def translate_text(text, source_language, target_language):
|
| 35 |
src_code, target_code = FLORES_code(source_language, target_language)
|
| 36 |
if src_code == "Language not found in the dataset." or not src_code or not target_code:
|
|
|
|
| 38 |
translation = text_translator(text, src_lang=src_code, tgt_lang=target_code)
|
| 39 |
return translation[0]["translation_text"]
|
| 40 |
|
| 41 |
+
# Save translation to file
|
| 42 |
def save_translation(text):
|
| 43 |
filename = f"translation_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
|
| 44 |
with open(filename, "w", encoding="utf-8") as f:
|
| 45 |
f.write(text)
|
| 46 |
return filename
|
| 47 |
|
| 48 |
+
# Clear all fields
|
| 49 |
+
def clear_fields():
|
| 50 |
+
return "", None, None, ""
|
| 51 |
+
|
| 52 |
gr.close_all()
|
| 53 |
|
| 54 |
+
# Interface
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
gr.Markdown("# 🌐 Multilingual Translator")
|
| 57 |
gr.Markdown("## Translate text between multiple languages.")
|
| 58 |
|
| 59 |
+
# Line height sliders
|
| 60 |
with gr.Row():
|
| 61 |
input_len_slider = gr.Slider(minimum=1, maximum=50, value=5, label="Input Textbox Height (Lines)")
|
| 62 |
output_len_slider = gr.Slider(minimum=1, maximum=50, value=5, label="Output Textbox Height (Lines)")
|
| 63 |
|
| 64 |
+
# Main translation row
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column(scale=1):
|
| 67 |
src_dropdown = gr.Dropdown(
|
|
|
|
| 88 |
placeholder="Translation will appear here..."
|
| 89 |
)
|
| 90 |
|
| 91 |
+
# Action buttons
|
| 92 |
with gr.Row():
|
| 93 |
save_btn = gr.Button("💾 Save Translation")
|
| 94 |
+
clear_btn = gr.Button("🧹 Clear All")
|
| 95 |
file_output = gr.File(label="Download File")
|
| 96 |
|
| 97 |
+
# Function bindings
|
| 98 |
translate_btn.click(translate_text, inputs=[text_input, src_dropdown, tgt_dropdown], outputs=output_box)
|
| 99 |
input_len_slider.change(lambda n: gr.update(lines=n), inputs=input_len_slider, outputs=text_input)
|
| 100 |
output_len_slider.change(lambda n: gr.update(lines=n), inputs=output_len_slider, outputs=output_box)
|
| 101 |
save_btn.click(save_translation, inputs=output_box, outputs=file_output)
|
| 102 |
+
clear_btn.click(clear_fields, outputs=[text_input, src_dropdown, tgt_dropdown, output_box])
|
| 103 |
|
| 104 |
demo.launch()
|