Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,54 +5,74 @@ import gradio as gr
|
|
5 |
# Install required packages if not already installed
|
6 |
try:
|
7 |
import sign_language_translator as slt
|
|
|
8 |
except ImportError:
|
9 |
-
print("Installing
|
10 |
-
os.system("pip install sign-language-translator --quiet")
|
11 |
import sign_language_translator as slt
|
|
|
12 |
|
13 |
-
TITLE = "English to Sign Language Translator"
|
14 |
-
DESCRIPTION = """
|
15 |
-
The text is preprocessed, tokenized and each token is mapped to a prerecorded video which are concatenated and returned.
|
16 |
|
17 |
**NOTE:**
|
18 |
-
-
|
19 |
- First-time loading may take a moment as the model downloads
|
20 |
-
- For best results, use simple sentences
|
21 |
"""
|
22 |
|
23 |
-
#
|
|
|
|
|
24 |
def get_model():
|
|
|
25 |
try:
|
26 |
-
return slt.models.ConcatenativeSynthesis("
|
27 |
except Exception as e:
|
28 |
print(f"Error initializing model: {str(e)}")
|
29 |
return None
|
30 |
|
31 |
-
# Global model variable
|
32 |
-
model = None
|
33 |
-
|
34 |
def initialize_model():
|
|
|
35 |
global model
|
36 |
if model is None:
|
37 |
model = get_model()
|
38 |
return model is not None
|
39 |
|
40 |
-
def
|
41 |
-
"""Translate English text to
|
42 |
if not text:
|
43 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Initialize model if not already done
|
46 |
if not initialize_model():
|
47 |
-
return None, "Failed to initialize the translation model
|
48 |
|
49 |
try:
|
50 |
-
# Format the text (lowercase first letter as required by model)
|
51 |
-
text = text[:1].lower() + text[1:] if text else ""
|
52 |
-
|
53 |
# Configure model
|
54 |
-
model.text_language = "
|
55 |
-
model.sign_language = "pk-sl"
|
56 |
model.sign_format = format_type
|
57 |
|
58 |
if format_type == "landmarks":
|
@@ -60,7 +80,7 @@ def translate_text(text, format_type):
|
|
60 |
|
61 |
# Translate
|
62 |
output_path = "output.mp4"
|
63 |
-
sign = model.translate(
|
64 |
|
65 |
# Save output
|
66 |
if isinstance(sign, slt.Landmarks):
|
@@ -71,12 +91,30 @@ def translate_text(text, format_type):
|
|
71 |
else:
|
72 |
sign.save(output_path, overwrite=True, codec="mp4v")
|
73 |
|
74 |
-
return output_path, f"Successfully
|
75 |
|
76 |
except Exception as e:
|
77 |
error_msg = str(e)
|
78 |
-
print(f"
|
79 |
-
return None, f"Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# Create the Gradio interface
|
82 |
with gr.Blocks(title=TITLE) as demo:
|
@@ -86,7 +124,7 @@ with gr.Blocks(title=TITLE) as demo:
|
|
86 |
with gr.Row():
|
87 |
with gr.Column():
|
88 |
# Input area
|
89 |
-
|
90 |
lines=4,
|
91 |
placeholder="Enter English text here...",
|
92 |
label="English Text"
|
@@ -102,6 +140,10 @@ with gr.Blocks(title=TITLE) as demo:
|
|
102 |
clear_btn = gr.Button("Clear")
|
103 |
translate_btn = gr.Button("Translate", variant="primary")
|
104 |
|
|
|
|
|
|
|
|
|
105 |
status_output = gr.Textbox(label="Status", interactive=False)
|
106 |
|
107 |
with gr.Column():
|
@@ -116,31 +158,33 @@ with gr.Blocks(title=TITLE) as demo:
|
|
116 |
# Examples
|
117 |
gr.Examples(
|
118 |
examples=[
|
119 |
-
["
|
120 |
["My name is John.", "video"],
|
121 |
-
["
|
122 |
["I want to learn sign language.", "video"]
|
123 |
],
|
124 |
-
inputs=[
|
125 |
-
outputs=[video_output, status_output],
|
126 |
-
fn=
|
127 |
)
|
128 |
|
129 |
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
130 |
translate_btn.click(
|
131 |
-
fn=
|
132 |
-
inputs=[
|
133 |
-
outputs=[video_output, status_output]
|
134 |
)
|
135 |
|
136 |
clear_btn.click(
|
137 |
-
fn=lambda: ("", "Input cleared"),
|
138 |
inputs=None,
|
139 |
-
outputs=[
|
140 |
)
|
141 |
-
|
142 |
-
# Initialize model on load (not blocking)
|
143 |
-
demo.load(lambda: None, None, None)
|
144 |
|
145 |
# Launch the app
|
146 |
if __name__ == "__main__":
|
|
|
5 |
# Install required packages if not already installed
|
6 |
try:
|
7 |
import sign_language_translator as slt
|
8 |
+
import requests
|
9 |
except ImportError:
|
10 |
+
print("Installing required packages...")
|
11 |
+
os.system("pip install sign-language-translator requests --quiet")
|
12 |
import sign_language_translator as slt
|
13 |
+
import requests
|
14 |
|
15 |
+
TITLE = "English to Pakistan Sign Language Translator"
|
16 |
+
DESCRIPTION = """This app translates English text to Pakistan Sign Language by first converting English to Urdu (which may improve vocabulary coverage), then using the Urdu text to generate sign language videos.
|
|
|
17 |
|
18 |
**NOTE:**
|
19 |
+
- The initial translation to Urdu uses a simple API-based translation
|
20 |
- First-time loading may take a moment as the model downloads
|
21 |
+
- For best results, use simple sentences
|
22 |
"""
|
23 |
|
24 |
+
# Global model variable
|
25 |
+
model = None
|
26 |
+
|
27 |
def get_model():
|
28 |
+
"""Initialize the sign language translation model"""
|
29 |
try:
|
30 |
+
return slt.models.ConcatenativeSynthesis("ur", "pk-sl", "video")
|
31 |
except Exception as e:
|
32 |
print(f"Error initializing model: {str(e)}")
|
33 |
return None
|
34 |
|
|
|
|
|
|
|
35 |
def initialize_model():
|
36 |
+
"""Ensure model is loaded"""
|
37 |
global model
|
38 |
if model is None:
|
39 |
model = get_model()
|
40 |
return model is not None
|
41 |
|
42 |
+
def english_to_urdu(text):
|
43 |
+
"""Translate English text to Urdu using a translation API"""
|
44 |
if not text:
|
45 |
+
return "", "Please enter text to translate"
|
46 |
+
|
47 |
+
try:
|
48 |
+
# Using MyMemory translation API (free and doesn't require auth)
|
49 |
+
url = f"https://api.mymemory.translated.net/get?q={text}&langpair=en|ur"
|
50 |
+
response = requests.get(url)
|
51 |
+
data = response.json()
|
52 |
+
|
53 |
+
if "responseData" in data and "translatedText" in data["responseData"]:
|
54 |
+
urdu_text = data["responseData"]["translatedText"]
|
55 |
+
return urdu_text, f"Translated to Urdu: {urdu_text}"
|
56 |
+
else:
|
57 |
+
return "", "Error: Could not translate to Urdu"
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Translation API error: {str(e)}")
|
61 |
+
return "", f"Error during Urdu translation: {str(e)}"
|
62 |
+
|
63 |
+
def urdu_to_sign(urdu_text, format_type):
|
64 |
+
"""Translate Urdu text to Pakistan Sign Language"""
|
65 |
+
if not urdu_text:
|
66 |
+
return None, "No Urdu text to translate"
|
67 |
|
68 |
# Initialize model if not already done
|
69 |
if not initialize_model():
|
70 |
+
return None, "Failed to initialize the translation model"
|
71 |
|
72 |
try:
|
|
|
|
|
|
|
73 |
# Configure model
|
74 |
+
model.text_language = "ur" # Urdu
|
75 |
+
model.sign_language = "pk-sl" # Pakistan Sign Language
|
76 |
model.sign_format = format_type
|
77 |
|
78 |
if format_type == "landmarks":
|
|
|
80 |
|
81 |
# Translate
|
82 |
output_path = "output.mp4"
|
83 |
+
sign = model.translate(urdu_text)
|
84 |
|
85 |
# Save output
|
86 |
if isinstance(sign, slt.Landmarks):
|
|
|
91 |
else:
|
92 |
sign.save(output_path, overwrite=True, codec="mp4v")
|
93 |
|
94 |
+
return output_path, f"Successfully created sign language video"
|
95 |
|
96 |
except Exception as e:
|
97 |
error_msg = str(e)
|
98 |
+
print(f"Sign generation error: {error_msg}")
|
99 |
+
return None, f"Error generating signs: {error_msg}"
|
100 |
+
|
101 |
+
def translate_english_to_sign(english_text, format_type):
|
102 |
+
"""Complete translation pipeline: English → Urdu → Sign Language"""
|
103 |
+
if not english_text:
|
104 |
+
return None, ""
|
105 |
+
|
106 |
+
# Step 1: Translate English to Urdu
|
107 |
+
urdu_text, urdu_status = english_to_urdu(english_text)
|
108 |
+
if not urdu_text:
|
109 |
+
return None, urdu_status
|
110 |
+
|
111 |
+
# Step 2: Translate Urdu to Sign Language
|
112 |
+
video, sign_status = urdu_to_sign(urdu_text, format_type)
|
113 |
+
|
114 |
+
# Combine status messages
|
115 |
+
status = f"English: \"{english_text}\"\nUrdu: \"{urdu_text}\"\n{sign_status}"
|
116 |
+
|
117 |
+
return video, status
|
118 |
|
119 |
# Create the Gradio interface
|
120 |
with gr.Blocks(title=TITLE) as demo:
|
|
|
124 |
with gr.Row():
|
125 |
with gr.Column():
|
126 |
# Input area
|
127 |
+
english_input = gr.Textbox(
|
128 |
lines=4,
|
129 |
placeholder="Enter English text here...",
|
130 |
label="English Text"
|
|
|
140 |
clear_btn = gr.Button("Clear")
|
141 |
translate_btn = gr.Button("Translate", variant="primary")
|
142 |
|
143 |
+
# Intermediate Urdu translation
|
144 |
+
urdu_output = gr.Textbox(label="Urdu Translation", interactive=False)
|
145 |
+
|
146 |
+
# Status area
|
147 |
status_output = gr.Textbox(label="Status", interactive=False)
|
148 |
|
149 |
with gr.Column():
|
|
|
158 |
# Examples
|
159 |
gr.Examples(
|
160 |
examples=[
|
161 |
+
["How are you?", "video"],
|
162 |
["My name is John.", "video"],
|
163 |
+
["Thank you for your help.", "video"],
|
164 |
["I want to learn sign language.", "video"]
|
165 |
],
|
166 |
+
inputs=[english_input, format_dropdown],
|
167 |
+
outputs=[video_output, urdu_output, status_output],
|
168 |
+
fn=lambda text, fmt: (*translate_english_to_sign(text, fmt), english_to_urdu(text)[0])
|
169 |
)
|
170 |
|
171 |
# Event handlers
|
172 |
+
def handle_translation(text, fmt):
|
173 |
+
video, status = translate_english_to_sign(text, fmt)
|
174 |
+
urdu = english_to_urdu(text)[0] if text else ""
|
175 |
+
return video, urdu, status
|
176 |
+
|
177 |
translate_btn.click(
|
178 |
+
fn=handle_translation,
|
179 |
+
inputs=[english_input, format_dropdown],
|
180 |
+
outputs=[video_output, urdu_output, status_output]
|
181 |
)
|
182 |
|
183 |
clear_btn.click(
|
184 |
+
fn=lambda: ("", "", "Input cleared"),
|
185 |
inputs=None,
|
186 |
+
outputs=[english_input, urdu_output, status_output]
|
187 |
)
|
|
|
|
|
|
|
188 |
|
189 |
# Launch the app
|
190 |
if __name__ == "__main__":
|