Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 1. Install necessary libraries (if you haven't already)
|
2 |
+
pip install gradio transformers torch sentencepiece
|
3 |
+
|
4 |
+
# 2. Import libraries
|
5 |
+
import gradio as gr
|
6 |
+
from transformers import pipeline
|
7 |
+
import torch # PyTorch is needed as a backend for transformers
|
8 |
+
|
9 |
+
# 3. Load the translation pipeline
|
10 |
+
# Using the NLLB model which supports many languages including English and Telugu
|
11 |
+
# You might need to adjust device mapping based on your hardware (e.g., device=0 for GPU)
|
12 |
+
try:
|
13 |
+
# Try loading the specific model mentioned implicitly by language codes
|
14 |
+
translator = pipeline('translation', model='facebook/nllb-200-distilled-600M', device=-1) # Use -1 for CPU
|
15 |
+
print("Translator pipeline loaded successfully.")
|
16 |
+
except Exception as e:
|
17 |
+
print(f"Error loading translator pipeline: {e}")
|
18 |
+
# Define a dummy function if pipeline fails to load, so Gradio interface still runs
|
19 |
+
def translator(text, src_lang, tgt_lang):
|
20 |
+
return f"Error: Could not load translation model. {e}"
|
21 |
+
|
22 |
+
# 4. Define the translation function for Gradio
|
23 |
+
def translate_text(text_to_translate, source_language, target_language):
|
24 |
+
"""
|
25 |
+
Translates text using the loaded Hugging Face pipeline.
|
26 |
+
|
27 |
+
Args:
|
28 |
+
text_to_translate (str): The text to translate.
|
29 |
+
source_language (str): The source language code (e.g., 'eng_Latn').
|
30 |
+
target_language (str): The target language code (e.g., 'tel_Telu').
|
31 |
+
|
32 |
+
Returns:
|
33 |
+
str: The translated text or an error message.
|
34 |
+
"""
|
35 |
+
if not text_to_translate:
|
36 |
+
return "Please enter text to translate."
|
37 |
+
if not source_language or not target_language:
|
38 |
+
return "Please select both source and target languages."
|
39 |
+
|
40 |
+
try:
|
41 |
+
# Perform the translation using the pipeline
|
42 |
+
# Note: The pipeline function expects keyword arguments src_lang and tgt_lang
|
43 |
+
translated_output = translator(text_to_translate,
|
44 |
+
src_lang=source_language,
|
45 |
+
tgt_lang=target_language)
|
46 |
+
# The output is usually a list containing a dictionary
|
47 |
+
if translated_output and isinstance(translated_output, list):
|
48 |
+
return translated_output[0]['translation_text']
|
49 |
+
else:
|
50 |
+
# Handle unexpected output format
|
51 |
+
return f"Translation failed. Unexpected output: {translated_output}"
|
52 |
+
except Exception as e:
|
53 |
+
print(f"Translation error: {e}")
|
54 |
+
# Provide a user-friendly error message
|
55 |
+
return f"An error occurred during translation: {e}. Make sure the language codes are correct and supported by the model."
|
56 |
+
|
57 |
+
# 5. Define language choices for dropdowns (using NLLB codes)
|
58 |
+
# Add more languages as needed from the NLLB supported list
|
59 |
+
language_choices = [
|
60 |
+
("English", "eng_Latn"),
|
61 |
+
("Telugu", "tel_Telu"),
|
62 |
+
("Hindi", "hin_Deva"),
|
63 |
+
("Tamil", "tam_Taml"),
|
64 |
+
("Spanish", "spa_Latn"),
|
65 |
+
("French", "fra_Latn"),
|
66 |
+
("German", "deu_Latn"),
|
67 |
+
("Chinese (Simplified)", "zho_Hans"),
|
68 |
+
]
|
69 |
+
|
70 |
+
# 6. Create the Gradio interface
|
71 |
+
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
72 |
+
gr.Markdown("# Text Translator using NLLB Model")
|
73 |
+
gr.Markdown("Enter text and select the source and target languages.")
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
# Input text area
|
77 |
+
input_text = gr.Textbox(label="Text to Translate", placeholder="Enter text here...", lines=5)
|
78 |
+
# Output text area
|
79 |
+
output_text = gr.Textbox(label="Translated Text", placeholder="Translation will appear here...", lines=5, interactive=False)
|
80 |
+
|
81 |
+
with gr.Row():
|
82 |
+
# Source language dropdown
|
83 |
+
source_lang = gr.Dropdown(
|
84 |
+
label="Source Language",
|
85 |
+
choices=language_choices,
|
86 |
+
value="eng_Latn" # Default to English
|
87 |
+
)
|
88 |
+
# Target language dropdown
|
89 |
+
target_lang = gr.Dropdown(
|
90 |
+
label="Target Language",
|
91 |
+
choices=language_choices,
|
92 |
+
value="tel_Telu" # Default to Telugu
|
93 |
+
)
|
94 |
+
|
95 |
+
# Translate button
|
96 |
+
translate_button = gr.Button("Translate", variant="primary")
|
97 |
+
|
98 |
+
# Define the action when the button is clicked
|
99 |
+
translate_button.click(
|
100 |
+
fn=translate_text,
|
101 |
+
inputs=[input_text, source_lang, target_lang],
|
102 |
+
outputs=output_text,
|
103 |
+
api_name="translate" # Name for API endpoint if needed
|
104 |
+
)
|
105 |
+
|
106 |
+
gr.Markdown("Powered by Hugging Face Transformers and Gradio.")
|
107 |
+
|
108 |
+
# 7. Launch the Gradio app
|
109 |
+
# When running locally, this will provide a URL.
|
110 |
+
# On Hugging Face Spaces, this line makes the app run.
|
111 |
+
if __name__ == "__main__":
|
112 |
+
iface.launch()
|