Spaces:
Running
Running
layout
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ The text is preprocessed, tokenized and rearranged and then each token is mapped
|
|
| 18 |
TITLE = "Concatenative Synthesis: Rule Based Text to Sign Language Translator"
|
| 19 |
|
| 20 |
CUSTOM_JS = """<script>
|
| 21 |
-
const rtlLanguages = ["
|
| 22 |
|
| 23 |
function updateTextareaDir(language) {
|
| 24 |
const sourceTextarea = document.getElementById("source-textbox").querySelector("textarea");
|
|
@@ -31,8 +31,12 @@ function updateTextareaDir(language) {
|
|
| 31 |
}
|
| 32 |
</script>"""
|
| 33 |
# todo: add dropdown keyboard custom component with key mapping
|
|
|
|
| 34 |
|
| 35 |
CUSTOM_CSS = """
|
|
|
|
|
|
|
|
|
|
| 36 |
#auto-complete-button {
|
| 37 |
border-color: var(--button-primary-border-color-hover);
|
| 38 |
}
|
|
@@ -75,6 +79,13 @@ except Exception as e:
|
|
| 75 |
translation_model = slt.models.ConcatenativeSynthesis("ur", "pk-sl", "video")
|
| 76 |
language_models: Dict[str, slt.models.BeamSampling] = {}
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
def auto_complete_text(model_code: str, text: str):
|
| 80 |
if model_code not in language_models:
|
|
@@ -115,9 +126,9 @@ def text_to_video(
|
|
| 115 |
|
| 116 |
sign = translation_model.translate(text)
|
| 117 |
if isinstance(sign, slt.Landmarks):
|
| 118 |
-
sign.data[:, 33:
|
| 119 |
sign.data[:, 33:54, 0] += 0.25
|
| 120 |
-
sign.data[:, 54:
|
| 121 |
|
| 122 |
sign.save_animation(output_path, overwrite=True)
|
| 123 |
else:
|
|
@@ -127,6 +138,7 @@ def text_to_video(
|
|
| 127 |
|
| 128 |
|
| 129 |
def translate(text: str, text_lang: str, sign_lang: str, sign_format: str):
|
|
|
|
| 130 |
log = [
|
| 131 |
text,
|
| 132 |
text_lang,
|
|
@@ -160,13 +172,29 @@ with gradio.Blocks(title=TITLE, head=CUSTOM_JS, css=CUSTOM_CSS) as gradio_app:
|
|
| 160 |
if not isinstance(request_logger, gradio.HuggingFaceDatasetSaver)
|
| 161 |
else ""
|
| 162 |
)
|
| 163 |
-
with gradio.Row(): # Inputs and Outputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
with gradio.Column(): # Inputs
|
| 165 |
gradio.Markdown("## Select Languages")
|
| 166 |
with gradio.Row():
|
| 167 |
text_lang_dropdown = gradio.Dropdown(
|
| 168 |
-
choices=[
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
label="Text Language",
|
| 171 |
elem_id="text-lang-dropdown",
|
| 172 |
)
|
|
@@ -189,73 +217,62 @@ with gradio.Blocks(title=TITLE, head=CUSTOM_JS, css=CUSTOM_CSS) as gradio_app:
|
|
| 189 |
# todo: sign format: video/landmarks (tabs?)
|
| 190 |
|
| 191 |
gradio.Markdown("## Input Text")
|
| 192 |
-
with gradio.Row():
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
],
|
| 210 |
-
value=slt.ModelCodes.MIXER_LM_NGRAM_URDU.value,
|
| 211 |
-
label="Language Model for auto-complete",
|
| 212 |
-
)
|
| 213 |
-
with gradio.Row():
|
| 214 |
-
clear_button = gradio.ClearButton(
|
| 215 |
-
source_textbox, api_name=False
|
| 216 |
-
)
|
| 217 |
-
auto_complete_button = gradio.Button(
|
| 218 |
-
"Auto-Complete", elem_id="auto-complete-button"
|
| 219 |
-
)
|
| 220 |
-
auto_complete_button.click(
|
| 221 |
-
auto_complete_text,
|
| 222 |
-
inputs=[language_model_dropdown, source_textbox],
|
| 223 |
-
outputs=[source_textbox],
|
| 224 |
-
api_name=False,
|
| 225 |
-
)
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
|
| 251 |
gradio.Examples(
|
| 252 |
[
|
| 253 |
-
["یہ بہت اچھا ہے۔", "
|
| 254 |
-
["وہ کام آسان تھا۔", "
|
| 255 |
-
["पाँच घंटे।", "
|
| 256 |
-
["कैसे हैं आप?", "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
],
|
| 258 |
-
inputs=[source_textbox, text_lang_dropdown, sign_lang_dropdown, output_format_dropdown],
|
| 259 |
outputs=output_video,
|
| 260 |
)
|
| 261 |
request_logger.setup(
|
|
|
|
| 18 |
TITLE = "Concatenative Synthesis: Rule Based Text to Sign Language Translator"
|
| 19 |
|
| 20 |
CUSTOM_JS = """<script>
|
| 21 |
+
const rtlLanguages = ["urdu", "arabic"];
|
| 22 |
|
| 23 |
function updateTextareaDir(language) {
|
| 24 |
const sourceTextarea = document.getElementById("source-textbox").querySelector("textarea");
|
|
|
|
| 31 |
}
|
| 32 |
</script>"""
|
| 33 |
# todo: add dropdown keyboard custom component with key mapping
|
| 34 |
+
# todo: output full height
|
| 35 |
|
| 36 |
CUSTOM_CSS = """
|
| 37 |
+
.reverse-row {
|
| 38 |
+
flex-direction: row-reverse;
|
| 39 |
+
}
|
| 40 |
#auto-complete-button {
|
| 41 |
border-color: var(--button-primary-border-color-hover);
|
| 42 |
}
|
|
|
|
| 79 |
translation_model = slt.models.ConcatenativeSynthesis("ur", "pk-sl", "video")
|
| 80 |
language_models: Dict[str, slt.models.BeamSampling] = {}
|
| 81 |
|
| 82 |
+
full_to_short = {
|
| 83 |
+
"english": "en",
|
| 84 |
+
"urdu": "ur",
|
| 85 |
+
"hindi": "hi",
|
| 86 |
+
}
|
| 87 |
+
short_to_full = {s: f for f, s in full_to_short.items()}
|
| 88 |
+
|
| 89 |
|
| 90 |
def auto_complete_text(model_code: str, text: str):
|
| 91 |
if model_code not in language_models:
|
|
|
|
| 126 |
|
| 127 |
sign = translation_model.translate(text)
|
| 128 |
if isinstance(sign, slt.Landmarks):
|
| 129 |
+
sign.data[:, 33:] *= 2
|
| 130 |
sign.data[:, 33:54, 0] += 0.25
|
| 131 |
+
sign.data[:, 54:, 0] -= 0.25
|
| 132 |
|
| 133 |
sign.save_animation(output_path, overwrite=True)
|
| 134 |
else:
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
def translate(text: str, text_lang: str, sign_lang: str, sign_format: str):
|
| 141 |
+
text_lang = full_to_short.get(text_lang, text_lang)
|
| 142 |
log = [
|
| 143 |
text,
|
| 144 |
text_lang,
|
|
|
|
| 172 |
if not isinstance(request_logger, gradio.HuggingFaceDatasetSaver)
|
| 173 |
else ""
|
| 174 |
)
|
| 175 |
+
with gradio.Row(elem_classes=["reverse-row"]): # Inputs and Outputs
|
| 176 |
+
with gradio.Column(): # Outputs
|
| 177 |
+
gradio.Markdown("## Output Sign Language")
|
| 178 |
+
output_video = gradio.Video(
|
| 179 |
+
format="mp4",
|
| 180 |
+
label="Synthesized Sign Language Video",
|
| 181 |
+
autoplay=True,
|
| 182 |
+
show_download_button=True,
|
| 183 |
+
include_audio=False,
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
with gradio.Column(): # Inputs
|
| 187 |
gradio.Markdown("## Select Languages")
|
| 188 |
with gradio.Row():
|
| 189 |
text_lang_dropdown = gradio.Dropdown(
|
| 190 |
+
choices=[
|
| 191 |
+
short_to_full.get(code.value, code.value)
|
| 192 |
+
for code in slt.TextLanguageCodes
|
| 193 |
+
],
|
| 194 |
+
value=short_to_full.get(
|
| 195 |
+
slt.TextLanguageCodes.URDU.value,
|
| 196 |
+
slt.TextLanguageCodes.URDU.value,
|
| 197 |
+
),
|
| 198 |
label="Text Language",
|
| 199 |
elem_id="text-lang-dropdown",
|
| 200 |
)
|
|
|
|
| 217 |
# todo: sign format: video/landmarks (tabs?)
|
| 218 |
|
| 219 |
gradio.Markdown("## Input Text")
|
| 220 |
+
with gradio.Row(): # Source TextArea
|
| 221 |
+
source_textbox = gradio.Textbox(
|
| 222 |
+
lines=3,
|
| 223 |
+
placeholder="Enter Text Here...",
|
| 224 |
+
label="Spoken Language Sentence",
|
| 225 |
+
show_copy_button=True,
|
| 226 |
+
elem_id="source-textbox",
|
| 227 |
+
)
|
| 228 |
+
with gradio.Row(): # clear/auto-complete/Language Model
|
| 229 |
+
language_model_dropdown = gradio.Dropdown(
|
| 230 |
+
choices=[
|
| 231 |
+
slt.ModelCodes.MIXER_LM_NGRAM_URDU.value,
|
| 232 |
+
slt.ModelCodes.TRANSFORMER_LM_UR_SUPPORTED.value,
|
| 233 |
+
],
|
| 234 |
+
value=slt.ModelCodes.MIXER_LM_NGRAM_URDU.value,
|
| 235 |
+
label="Select language model to Generate sample text",
|
| 236 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
+
auto_complete_button = gradio.Button(
|
| 239 |
+
"Auto-Complete", elem_id="auto-complete-button"
|
| 240 |
+
)
|
| 241 |
+
auto_complete_button.click(
|
| 242 |
+
auto_complete_text,
|
| 243 |
+
inputs=[language_model_dropdown, source_textbox],
|
| 244 |
+
outputs=[source_textbox],
|
| 245 |
+
api_name=False,
|
| 246 |
+
)
|
| 247 |
+
clear_button = gradio.ClearButton(source_textbox, api_name=False)
|
| 248 |
|
| 249 |
+
with gradio.Row(): # Translate Button
|
| 250 |
+
translate_button = gradio.Button("Translate", variant="primary")
|
| 251 |
+
translate_button.click(
|
| 252 |
+
translate,
|
| 253 |
+
inputs=[
|
| 254 |
+
source_textbox,
|
| 255 |
+
text_lang_dropdown,
|
| 256 |
+
sign_lang_dropdown,
|
| 257 |
+
output_format_dropdown,
|
| 258 |
+
],
|
| 259 |
+
outputs=[output_video],
|
| 260 |
+
api_name="translate",
|
| 261 |
+
)
|
| 262 |
|
| 263 |
gradio.Examples(
|
| 264 |
[
|
| 265 |
+
["یہ بہت اچھا ہے۔", "urdu", "pakistan-sign-language", "video"],
|
| 266 |
+
["وہ کام آسان تھا۔", "urdu", "pakistan-sign-language", "landmarks"],
|
| 267 |
+
["पाँच घंटे।", "hindi", "pakistan-sign-language", "video"],
|
| 268 |
+
["कैसे हैं आप?", "hindi", "pakistan-sign-language", "landmarks"],
|
| 269 |
+
],
|
| 270 |
+
inputs=[
|
| 271 |
+
source_textbox,
|
| 272 |
+
text_lang_dropdown,
|
| 273 |
+
sign_lang_dropdown,
|
| 274 |
+
output_format_dropdown,
|
| 275 |
],
|
|
|
|
| 276 |
outputs=output_video,
|
| 277 |
)
|
| 278 |
request_logger.setup(
|