Spaces:
Sleeping
Sleeping
Akhil Koduri
commited on
Commit
•
f73eb0f
1
Parent(s):
babfdb2
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
import json
|
|
|
4 |
|
5 |
# Initialize the translation pipeline with the specific model
|
6 |
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
|
@@ -24,17 +25,25 @@ def get_flores_200_code(language):
|
|
24 |
return code['FLORES-200 code']
|
25 |
return None
|
26 |
|
27 |
-
def translate_text(text, destination_language):
|
28 |
"""
|
29 |
-
Translates text from
|
30 |
|
31 |
Args:
|
32 |
text (str): The text to translate.
|
33 |
destination_language (str): The target language for translation.
|
|
|
34 |
|
35 |
Returns:
|
36 |
str: The translated text.
|
37 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
dest_code = get_flores_200_code(destination_language)
|
39 |
|
40 |
if not dest_code:
|
@@ -43,7 +52,7 @@ def translate_text(text, destination_language):
|
|
43 |
try:
|
44 |
# Perform translation using T5 model pipeline
|
45 |
translation = text_translator(text,
|
46 |
-
src_lang=
|
47 |
tgt_lang=dest_code)
|
48 |
translated_text = translation[0]["translation_text"]
|
49 |
return translated_text
|
@@ -55,12 +64,13 @@ def main():
|
|
55 |
interface = gr.Interface(
|
56 |
fn=translate_text,
|
57 |
inputs=[
|
58 |
-
gr.Textbox(label="Input text to translate", lines=6, placeholder="Enter text
|
|
|
59 |
gr.Dropdown([code['Language'] for code in language_data], label="Select destination language")
|
60 |
],
|
61 |
outputs=[gr.Textbox(label="Translated text", lines=4)],
|
62 |
title="Multi-Language Translator",
|
63 |
-
description="Translate
|
64 |
theme=gr.themes.Soft(),
|
65 |
live=True # Enable live updates
|
66 |
)
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
import json
|
4 |
+
from langdetect import detect
|
5 |
|
6 |
# Initialize the translation pipeline with the specific model
|
7 |
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
|
|
|
25 |
return code['FLORES-200 code']
|
26 |
return None
|
27 |
|
28 |
+
def translate_text(text, destination_language, src_lang=None):
|
29 |
"""
|
30 |
+
Translates text from the source language to the selected destination language using the T5 model.
|
31 |
|
32 |
Args:
|
33 |
text (str): The text to translate.
|
34 |
destination_language (str): The target language for translation.
|
35 |
+
src_lang (str, optional): The source language of the text. If not provided, it will be detected automatically.
|
36 |
|
37 |
Returns:
|
38 |
str: The translated text.
|
39 |
"""
|
40 |
+
# Detect source language if not provided
|
41 |
+
if src_lang is None:
|
42 |
+
try:
|
43 |
+
src_lang = detect(text)
|
44 |
+
except Exception as e:
|
45 |
+
return f"Language detection error: {str(e)}"
|
46 |
+
|
47 |
dest_code = get_flores_200_code(destination_language)
|
48 |
|
49 |
if not dest_code:
|
|
|
52 |
try:
|
53 |
# Perform translation using T5 model pipeline
|
54 |
translation = text_translator(text,
|
55 |
+
src_lang=src_lang,
|
56 |
tgt_lang=dest_code)
|
57 |
translated_text = translation[0]["translation_text"]
|
58 |
return translated_text
|
|
|
64 |
interface = gr.Interface(
|
65 |
fn=translate_text,
|
66 |
inputs=[
|
67 |
+
gr.Textbox(label="Input text to translate", lines=6, placeholder="Enter text..."),
|
68 |
+
gr.Dropdown([code['Language'] for code in language_data], label="Select input language", default="English"),
|
69 |
gr.Dropdown([code['Language'] for code in language_data], label="Select destination language")
|
70 |
],
|
71 |
outputs=[gr.Textbox(label="Translated text", lines=4)],
|
72 |
title="Multi-Language Translator",
|
73 |
+
description="Translate text between multiple languages using the T5 model. Select input and destination languages from the dropdown menus and enter the text to translate.",
|
74 |
theme=gr.themes.Soft(),
|
75 |
live=True # Enable live updates
|
76 |
)
|