File size: 29,898 Bytes
0425de2
 
 
 
 
 
 
 
 
 
 
 
54215d8
 
 
 
 
 
 
0425de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4578029
 
 
 
 
 
 
 
 
 
 
 
 
 
ac4f54f
 
 
 
0425de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a63b94c
0425de2
 
 
 
 
 
 
 
 
 
 
 
 
0794289
0425de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e7d360
 
 
0425de2
5e7d360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0425de2
 
ac4f54f
 
 
 
 
 
 
 
 
 
 
 
5e7d360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0425de2
 
5e7d360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4578029
f6145b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae15bc9
f6145b0
 
 
 
 
 
 
ae15bc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6145b0
ae15bc9
 
 
 
f6145b0
ae15bc9
f6145b0
ae15bc9
 
 
f6145b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae15bc9
f6145b0
 
 
ae15bc9
 
 
 
 
 
 
 
 
 
 
 
 
 
f6145b0
 
 
 
 
 
 
ae15bc9
0425de2
 
 
 
 
ac4f54f
0425de2
 
 
 
 
 
 
 
4578029
0425de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e7d360
0425de2
 
 
54215d8
0794289
 
54215d8
 
 
 
 
 
 
 
 
 
0794289
54215d8
 
 
f6145b0
 
 
54215d8
0425de2
0794289
 
0425de2
 
54215d8
0425de2
54215d8
0425de2
54215d8
0425de2
 
0794289
0425de2
 
 
54215d8
 
 
0425de2
 
0794289
 
0425de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da0a6a4
0425de2
 
 
 
 
 
 
 
 
54215d8
0425de2
 
 
 
 
 
54215d8
0425de2
 
 
 
 
 
 
 
 
 
54215d8
0425de2
 
 
54215d8
 
 
 
 
 
0425de2
 
ae15bc9
 
 
54215d8
0425de2
54215d8
0425de2
f6145b0
ae15bc9
0425de2
 
0794289
 
 
f6145b0
ae15bc9
0794289
0425de2
54215d8
 
 
f6145b0
0425de2
 
54215d8
 
 
f6145b0
0425de2
 
54215d8
 
 
0425de2
54215d8
ac4f54f
54215d8
 
 
 
 
 
 
 
 
 
 
 
 
0425de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac4f54f
0425de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
import gradio as gr
import os
import base64

os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"

from handle_gradio import (
    process_video,
    process_video_stream,
    handle_file_upload,
    handle_remove_video,
    handle_select_example,
    open_record_modal,
    close_record_modal,
    save_record_direct,
    save_record_and_open_trim,
    open_preview_modal,
    close_preview_modal,
    save_preview_and_trim,
    open_keypoints_modal,
    close_keypoints_modal,
)

# Check and download the 1GB pre-trained model weights if not cached
print("Checking for large model file...")
local_model_path = os.path.join("Uni_Sign", "unisign_model", "best_checkpoint-wlasl.pth")
if os.path.exists(local_model_path):
    model_path = local_model_path
else:
    from huggingface_hub import hf_hub_download
    model_path = hf_hub_download(
        repo_id="plice13/sign-language-weights", 
        filename="best_checkpoint-wlasl.pth"                  
    )
print(f"File successfully loaded at: {model_path}")
os.environ["UNISIGN_WEIGHTS"] = model_path
# ====================

# Prepare base64-encoded logos for header badges from the 'logo' directory
logo_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logo")

def _get_b64_logo(filename):
    p = os.path.join(logo_dir, filename)
    if os.path.exists(p):
        with open(p, "rb") as f:
            return f"data:image/png;base64,{base64.b64encode(f.read()).decode('utf-8')}"
    return ""

logo_data = {
    "FAV_english_RGB": _get_b64_logo("FAV_english_RGB.png"),
    "fav_logo": _get_b64_logo("fav_logo.png"),
    "ZCU_logo_english_RGB": _get_b64_logo("ZCU_logo_english_RGB.png"),
    "zcu_logo": _get_b64_logo("zcu_logo.png"),
}

fav_logo_src = logo_data["FAV_english_RGB"]
zcu_light_logo_src = logo_data["ZCU_logo_english_RGB"]
zcu_dark_logo_src = logo_data["zcu_logo"]


import json

def load_examples_config():
    examples_json_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "examples.json")
    if os.path.exists(examples_json_path):
        try:
            with open(examples_json_path, "r", encoding="utf-8") as f:
                data = json.load(f)
                if isinstance(data, list):
                    return data
        except Exception:
            pass
    example_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "examples", "displayed")
    return [
        {"title": "Book", "video_path": os.path.join(example_dir, "book.mp4"), "keypoints_path": os.path.join(example_dir, "book_keypoints.json"), "keypoints_video_path": os.path.join(example_dir, "book_keypoints.mp4")},
        {"title": "Deaf", "video_path": os.path.join(example_dir, "deaf.mp4"), "keypoints_path": os.path.join(example_dir, "deaf_keypoints.json"), "keypoints_video_path": os.path.join(example_dir, "deaf_keypoints.mp4")},
        {"title": "Help", "video_path": os.path.join(example_dir, "help.mp4"), "keypoints_path": os.path.join(example_dir, "help_keypoints.json"), "keypoints_video_path": os.path.join(example_dir, "help_keypoints.mp4")},
        {"title": "Fine", "video_path": os.path.join(example_dir, "fine.mp4"), "keypoints_path": os.path.join(example_dir, "fine_keypoints.json"), "keypoints_video_path": os.path.join(example_dir, "fine_keypoints.mp4")},
        {"title": "Purple", "video_path": os.path.join(example_dir, "purple.mp4"), "keypoints_path": os.path.join(example_dir, "purple_keypoints.json"), "keypoints_video_path": os.path.join(example_dir, "purple_keypoints.mp4")},
        {"title": "No", "video_path": os.path.join(example_dir, "no.mp4"), "keypoints_path": os.path.join(example_dir, "no_keypoints.json"), "keypoints_video_path": os.path.join(example_dir, "no_keypoints.mp4")},
    ]

examples_list = load_examples_config()

# Load CSS from external style.css file
css_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "style.css")
with open(css_path, "r", encoding="utf-8") as f:
    custom_css = f.read()

# Information text hardcoded from DELETE/slt_demot.md
INFO_MARKDOWN = """## Sign Language Translation Demo



This interactive demo showcases **automatic sign language recognition from video to text**.



The current version demonstrates the processing of **American Sign Language (ASL)** using modern computer vision and deep learning methods.



## How to use the demo



1. **Upload a video** or select one of the provided examples.

2. Click **Submit** to process the video.

3. The predicted text will be displayed as the output.



For best results, the signer should be clearly visible, including the upper body, hands, and face.



> **Note:** This is a research demonstrator. Predictions may not always be accurate, especially for videos that differ significantly from the data used during training.



## How does it work?



The system consists of two main stages: **pose preprocessing** and **sign language translation**.



### Pose preprocessing



The input video is first converted into a structured pose representation. Keypoints describing the signer's **body, hands, and face** are extracted and normalized before being passed to the translation model.



Body keypoints are normalized globally, while hand and facial keypoints are normalized locally to preserve detailed information about their shape and movement.



More information about the preprocessing pipeline is available in the [PoseEstimation repository](https://github.com/JSALT2024/PoseEstimation).



### Sign language translation



The extracted pose sequence is processed using a model based on **Uni-Sign**, a unified framework for sign language understanding.



More information about the model and our implementation is available in the [Uni-Sign repository](https://github.com/zeleznyt/Uni-Sign).



## Resources



- [Demo source code](https://github.com/JSALT2024/slt-demo/tree/uni-sign-EP)

- [Pose estimation and preprocessing](https://github.com/JSALT2024/PoseEstimation)

- [Uni-Sign implementation](https://github.com/zeleznyt/Uni-Sign)

- **Uni-Sign:** *Uni-Sign: Toward Unified Sign Language Understanding at Scale*, Li et al., ICLR 2025



## Acknowledgements



This demonstrator was developed at the **University of West Bohemia (ZČU), Department of Cybernetics, Computer Vision group**.



Development was supported by the **2026 ZČU internal mini-project programme for the development and wider use of artificial intelligence**.



The system builds upon the **Uni-Sign** framework. We thank its authors and the open-source sign language research community for making their work publicly available.

"""



with gr.Blocks(title="Sign Language Translation", css=custom_css, theme=gr.themes.Default(primary_hue="amber", neutral_hue="neutral")) as app:

    current_video = gr.State("")
    keypoints_video_state = gr.State("")

    with gr.Column(elem_id="main-layout"):
        
        gr.HTML(f"""

        <style>{custom_css}</style>

        <script>

        (function() {{

            let isApplying = false;



            function getDesiredTheme() {{

                try {{

                    const urlParams = new URLSearchParams(window.location.search);

                    const themeParam = urlParams.get('__theme');

                    if (themeParam === 'dark') return 'dark';

                    if (themeParam === 'light') return 'light';

                }} catch(e) {{}}



                const appEl = document.querySelector('gradio-app');

                if (appEl && appEl.classList.contains('dark')) {{

                    return 'dark';

                }}



                if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) {{

                    return 'light';

                }}

                if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {{

                    return 'dark';

                }}



                return 'dark';

            }}



            function applyTheme() {{

                if (isApplying) return;

                isApplying = true;

                try {{

                    const isLight = (getDesiredTheme() === 'light');



                    document.documentElement.classList.toggle('light-theme', isLight);

                    document.body.classList.toggle('light-theme', isLight);



                    const appEl = document.querySelector('gradio-app');

                    if (appEl) {{

                        if (isLight) {{

                            if (!appEl.classList.contains('light-theme')) appEl.classList.add('light-theme');

                        }} else {{

                            if (appEl.classList.contains('light-theme')) appEl.classList.remove('light-theme');

                        }}

                    }}



                    const zcuImg = document.getElementById('zcu-header-logo');

                    const favImg = document.getElementById('fav-header-logo');

                    if (favImg && favImg.getAttribute('src') !== '{fav_logo_src}') {{

                        favImg.setAttribute('src', '{fav_logo_src}');

                    }}

                    if (zcuImg) {{

                        const targetZcu = isLight ? '{zcu_light_logo_src}' : '{zcu_dark_logo_src}';

                        if (zcuImg.getAttribute('src') !== targetZcu) {{

                            zcuImg.setAttribute('src', targetZcu);

                        }}

                    }}

                }} finally {{

                    setTimeout(function() {{ isApplying = false; }}, 50);

                }}

            }}



            try {{ localStorage.removeItem('slt_demo_theme'); }} catch(e) {{}}



            applyTheme();

            document.addEventListener('DOMContentLoaded', applyTheme);

            setTimeout(applyTheme, 150);

            setTimeout(applyTheme, 500);

            setTimeout(applyTheme, 1200);



            if (window.matchMedia) {{

                try {{

                    window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', applyTheme);

                    window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', applyTheme);

                }} catch(e) {{}}

            }}



            const observer = new MutationObserver(function(mutations) {{

                for (let i = 0; i < mutations.length; i++) {{

                    if (mutations[i].type === 'attributes' && mutations[i].attributeName === 'class') {{

                        applyTheme();

                        break;

                    }}

                }}

            }});



            function setupObserver() {{

                const appEl = document.querySelector('gradio-app');

                if (appEl) {{

                    observer.observe(appEl, {{ attributes: true, attributeFilter: ['class'] }});

                }} else {{

                    setTimeout(setupObserver, 200);

                }}

            }}

            setupObserver();



            // WebM Chromium Infinity duration bug workaround

            function fixWebmDuration(v) {{

                if (!v || v._durationFixAttempted) return;

                if (!isFinite(v.duration) || isNaN(v.duration) || v.duration === Infinity) {{

                    v._durationFixAttempted = true;

                    const onSeekEnd = function() {{

                        v.removeEventListener('timeupdate', onSeekEnd);

                        v.currentTime = 0;

                    }};

                    v.addEventListener('timeupdate', onSeekEnd);

                    v.currentTime = 1e101;

                }}

            }}



            // Sanitize any "Infinity:NaN:NaN" display text in video controls

            function sanitizeDurationText() {{

                const nodes = document.querySelectorAll('.controls, [class*="controls"], .time, [class*="time"], span');

                for (let i = 0; i < nodes.length; i++) {{

                    const el = nodes[i];

                    if (el.children.length === 0 && (el.textContent.includes('Infinity') || el.textContent.includes('NaN'))) {{

                        el.textContent = el.textContent.replace(/Infinity:NaN:NaN/g, '0:00').replace(/NaN:NaN/g, '0:00');

                    }}

                }}

            }}



            // Strictly detect when a recorded video is present (never live webcam)

            function checkRecordState() {{

                const recordFooter = document.getElementById('record-footer-row') || document.querySelector('.record-footer-row');

                if (!recordFooter) return;



                const recordModal = document.querySelector('.modal-dialog-card');

                if (!recordModal) return;



                const vids = recordModal.querySelectorAll('.modal-video-player video');

                let hasRecorded = false;

                let recordedVid = null;



                for (let i = 0; i < vids.length; i++) {{

                    const v = vids[i];

                    if (v && v.src && typeof v.src === 'string') {{

                        const s = v.src.trim();

                        // Live webcam uses srcObject; recorded video has blob:, http:, or file= URL

                        if (s.startsWith('blob:') || s.startsWith('http://') || s.startsWith('https://') || s.includes('/file=')) {{

                            if (s !== 'blob:' && !s.includes('null') && !s.includes('undefined')) {{

                                hasRecorded = true;

                                recordedVid = v;

                                break;

                            }}

                        }}

                    }}

                }}



                if (hasRecorded) {{

                    if (!recordFooter.classList.contains('has-recording')) {{

                        recordFooter.classList.add('has-recording');

                    }}

                    recordFooter.style.setProperty('display', 'flex', 'important');

                    if (recordedVid) fixWebmDuration(recordedVid);

                }} else {{

                    if (recordFooter.classList.contains('has-recording')) {{

                        recordFooter.classList.remove('has-recording');

                    }}

                    recordFooter.style.setProperty('display', 'none', 'important');

                }}

                sanitizeDurationText();

            }}



            document.addEventListener('loadedmetadata', function(e) {{

                if (e.target && e.target.tagName === 'VIDEO') {{

                    fixWebmDuration(e.target);

                    checkRecordState();

                }}

            }}, true);



            document.addEventListener('durationchange', function(e) {{

                if (e.target && e.target.tagName === 'VIDEO') {{

                    fixWebmDuration(e.target);

                    sanitizeDurationText();

                    checkRecordState();

                }}

            }}, true);



            const recordObserver = new MutationObserver(function() {{

                checkRecordState();

            }});



            function setupRecordObserver() {{

                const target = document.querySelector('.modal-overlay') || document.body;

                if (target) {{

                    recordObserver.observe(target, {{ childList: true, subtree: true, attributes: true, attributeFilter: ['src'] }});

                }} else {{

                    setTimeout(setupRecordObserver, 200);

                }}

            }}

            setupRecordObserver();



            setInterval(function() {{

                checkRecordState();

                sanitizeDurationText();

                const vids = document.querySelectorAll('video');

                vids.forEach(v => {{

                    if (v.duration === Infinity) fixWebmDuration(v);

                }});

            }}, 200);

        }})();

        </script>

        <div class="app-header-container">

            <div class="header-badge header-badge-left">

                <a href="https://www.zcu.cz" target="_blank" rel="noopener noreferrer" title="Západočeská univerzita v Plzni">

                    <img id="zcu-header-logo" src="{zcu_dark_logo_src}" alt="Západočeská univerzita v Plzni" class="header-badge-img" />

                </a>

            </div>

            <div class="header-titles">

                <h1 class="app-title">Sign Language to Text Translation</h1>

                <h2 class="app-subtitle">Upload an ASL video and get a text translation.</h2>

            </div>

            <div class="header-badge header-badge-right">

                <a href="https://fav.zcu.cz" target="_blank" rel="noopener noreferrer" title="Fakulta aplikovaných věd ZČU">

                    <img id="fav-header-logo" src="{fav_logo_src}" alt="Fakulta aplikovaných věd ZČU" class="header-badge-img" />

                </a>

            </div>

        </div>

        """, elem_classes=["app-header-html"])

        # Card 1: Upload Video Box (Compact Dropzone & Info Row)
        with gr.Column(elem_classes=["ui-card"]):
            with gr.Row():
                with gr.Column(scale=1, min_width=0):
                    gr.Markdown("<h3 class='card-title'>Upload video</h3>")
                with gr.Column(scale=0, min_width=160, elem_classes=["record-btn-col"]):
                    record_yourself_btn = gr.Button(
                        "📹 RECORD YOURSELF",
                        variant="secondary",
                        elem_classes=["record-yourself-btn"],
                        visible=True,
                    )
            
            # Compact file dropzone
            upload_file = gr.File(
                label="Upload Video",
                file_types=["video"],
                file_count="single",
                show_label=False,
                elem_classes=["compact-dropzone"]
            )
            
            # Active video status row: Video name on left, action buttons aligned to the right
            with gr.Row(visible=False, elem_classes=["video-info-row"]) as video_info_row:
                video_name_md = gr.Markdown("<div class='video-name-badge'><span>video.mp4</span></div>", elem_classes=["video-name-col"])
                preview_btn = gr.Button("🎬 Preview & Trim Video", variant="secondary", elem_classes=["preview-modal-btn"])
                change_video_btn = gr.Button("✕ Remove", variant="secondary", elem_classes=["change-vid-btn"])

        # Standalone Translate Action Button (hidden until a video is uploaded or selected)
        submit_btn = gr.Button("Translate", variant="primary", elem_classes=["translate-btn"], visible=False)
        
        # Card 2: Translation Result Box (hidden until Translate is clicked)
        with gr.Column(elem_classes=["ui-card", "translation-card"], elem_id="translation-card", visible=False) as translation_card:
            gr.Markdown("<h3 class='card-title'>Translation</h3>")
            translation_display = gr.HTML(value="", elem_id="translation-display", elem_classes=["translation-display-html"])
            show_keypoints_btn = gr.Button(
                "🎯 SHOW KEYPOINTS",
                variant="secondary",
                elem_classes=["show-keypoints-btn"],
                visible=False,
            )
        
        # Card 3: Examples Box (loaded dynamically from examples.json)
        example_buttons = []
        with gr.Column(elem_classes=["ui-card"]):
            gr.Markdown("<h3 class='card-title'>Examples</h3>")
            for i in range(0, len(examples_list), 3):
                row_items = examples_list[i:i+3]
                with gr.Row(elem_classes=["examples-row"] if i == 0 else ["examples-row", "examples-row-second"]):
                    for idx, ex in enumerate(row_items):
                        item_num = i + idx + 1
                        v_path = ex.get("video_path", "")
                        title = ex.get("title", f"Example {item_num}")
                        with gr.Column(scale=1):
                            gr.HTML(f"<div class='example-title'>{title}</div>")
                            gr.Video(value=v_path, interactive=False, show_label=False, autoplay=False, height=160, mirror_webcam=False)
                            btn = gr.Button(f"Use Example {item_num}", variant="secondary", elem_classes=["example-btn"])
                            example_buttons.append((btn, v_path))

        # Card 4: Information Box
        with gr.Column(elem_classes=["ui-card"]):
            gr.Markdown("<h3 class='card-title'>Information</h3>")
            gr.Markdown(INFO_MARKDOWN, elem_classes=["info-markdown"])

        # Subtle bottom control row (Restart session)
        with gr.Row(elem_classes=["restart-row"]):
            restart_btn = gr.Button("🔄 Restart session", variant="secondary", elem_classes=["restart-btn"], size="sm")
            
    # Fullscreen Floating Modal Window for Webcam Recording
    with gr.Column(elem_classes=["modal-overlay"], scale=0, visible=False) as record_modal:
        with gr.Column(elem_classes=["modal-dialog-card"], scale=0):
            with gr.Row(elem_classes=["modal-header-row"]):
                with gr.Column(scale=1, min_width=0):
                    gr.Markdown("<h3 class='modal-title'>📹 Webcam Video Recording</h3>")
                with gr.Column(scale=0, min_width=36, elem_classes=["modal-close-col"]):
                    record_close_top = gr.Button("✕", size="sm", min_width=36, elem_classes=["modal-close-icon"])
            
            record_video = gr.Video(
                interactive=True,
                show_label=False,
                sources=["webcam"],
                mirror_webcam=False,
                elem_classes=["modal-video-player"],
            )
            
            with gr.Row(elem_id="record-footer-row", elem_classes=["modal-footer-row", "record-footer-row"]):
                record_direct_btn = gr.Button("✓ Use Directly", variant="secondary", elem_classes=["modal-use-direct-btn"])
                record_trim_btn = gr.Button("✂️ Trim", variant="primary", elem_classes=["modal-done-btn"])

    # Fullscreen Floating Modal Window for Video Preview & Trimming
    with gr.Column(elem_classes=["modal-overlay"], scale=0, visible=False) as preview_modal:
        with gr.Column(elem_classes=["modal-dialog-card"], scale=0):
            with gr.Row(elem_classes=["modal-header-row"]):
                with gr.Column(scale=1, min_width=0):
                    gr.Markdown("<h3 class='modal-title'>🎬 Video Preview & Trimming</h3>")
                with gr.Column(scale=0, min_width=36, elem_classes=["modal-close-col"]):
                    preview_close_top = gr.Button("✕", size="sm", min_width=36, elem_classes=["modal-close-icon"])
            
            preview_video = gr.Video(
                interactive=True,
                show_label=False,
                mirror_webcam=False,
                elem_classes=["modal-video-player"],
            )
            
            with gr.Row(elem_classes=["modal-footer-row"]):
                preview_close_bottom = gr.Button("✕ Cancel", variant="secondary", elem_classes=["modal-cancel-btn"])
                preview_save_btn = gr.Button("✓ Save & Use Video", variant="primary", elem_classes=["modal-done-btn"])

    # Fullscreen Floating Modal Window for Keypoints Visualization
    with gr.Column(elem_classes=["modal-overlay", "keypoints-modal-overlay"], scale=0, visible=False) as keypoints_modal:
        with gr.Column(elem_classes=["modal-dialog-card"], scale=0):
            with gr.Row(elem_classes=["modal-header-row"]):
                with gr.Column(scale=1, min_width=0):
                    gr.Markdown("<h3 class='modal-title'>🎯 Extracted Pose & Keypoints</h3>")
                with gr.Column(scale=0, min_width=36, elem_classes=["modal-close-col"]):
                    keypoints_close_top = gr.Button("✕", size="sm", min_width=36, elem_classes=["modal-close-icon"])
            
            keypoints_modal_video = gr.Video(
                interactive=False,
                show_label=False,
                autoplay=True,
                loop=True,
                elem_classes=["modal-video-player", "keypoints-video-player"],
            )
            
            with gr.Column(elem_classes=["modal-footer-col"]):
                keypoints_close_bottom = gr.Button("Close", variant="secondary", elem_classes=["modal-done-btn"])

    def start_translating_ui():
        spinner_html = """<div class="translation-content-box"><div class="loading-container"><div class="pulse-spinner"></div><div class="queue-notice-text" style="margin-top: 14px; font-size: 14px; font-weight: 600; color: var(--gold); text-align: center; letter-spacing: 0.3px;">Please wait, you are in queue...</div></div></div>"""
        return gr.update(visible=True), spinner_html, gr.update(visible=False), ""

    def finish_translating(video_path):
        yield from process_video_stream(video_path)

    # Upload file event
    upload_file.upload(
        fn=handle_file_upload,
        inputs=upload_file,
        outputs=[current_video, upload_file, record_yourself_btn, video_info_row, video_name_md, submit_btn, translation_card],
    )

    # Remove / Change video event
    change_video_btn.click(
        fn=handle_remove_video,
        inputs=None,
        outputs=[current_video, upload_file, record_yourself_btn, video_info_row, video_name_md, submit_btn, translation_card],
    )

    # Smooth scroll to top JavaScript helper for example selections
    scroll_top_js = "() => { window.scrollTo({ top: 0, behavior: 'smooth' }); }"

    # Examples click events with smooth auto-scroll to top
    for btn_obj, v_path_val in example_buttons:
        btn_obj.click(
            fn=lambda p=v_path_val: handle_select_example(p),
            inputs=None,
            outputs=[current_video, upload_file, record_yourself_btn, video_info_row, video_name_md, submit_btn, translation_card],
            js=scroll_top_js,
        )

    # Restart / Reset Session event
    restart_btn.click(
        fn=handle_remove_video,
        inputs=None,
        outputs=[current_video, upload_file, record_yourself_btn, video_info_row, video_name_md, submit_btn, translation_card],
        js="() => { window.location.href = window.location.href.split('#')[0]; }"
    )

    open_record_modal_js = "() => { window.scrollTo({ top: 0, behavior: 'smooth' }); const rf = document.getElementById('record-footer-row') || document.querySelector('.record-footer-row'); if (rf) { rf.classList.remove('has-recording'); rf.style.setProperty('display', 'none', 'important'); } }"
    close_record_modal_js = "() => { const rf = document.getElementById('record-footer-row') || document.querySelector('.record-footer-row'); if (rf) { rf.classList.remove('has-recording'); rf.style.setProperty('display', 'none', 'important'); } }"

    # --- Modal 1: Webcam Recording Events ---
    record_yourself_btn.click(
        fn=open_record_modal,
        inputs=None,
        outputs=[record_modal, record_video],
        js=open_record_modal_js,
    )

    record_close_top.click(
        fn=close_record_modal,
        inputs=None,
        outputs=[record_modal, record_video],
        js=close_record_modal_js,
    )

    record_direct_btn.click(
        fn=save_record_direct,
        inputs=record_video,
        outputs=[record_modal, record_video, current_video, upload_file, record_yourself_btn, video_info_row, video_name_md, submit_btn, translation_card],
    )

    record_trim_btn.click(
        fn=save_record_and_open_trim,
        inputs=record_video,
        outputs=[record_modal, record_video, current_video, upload_file, record_yourself_btn, video_info_row, video_name_md, submit_btn, translation_card, preview_modal, preview_video],
    )

    # --- Modal 2: Preview & Trimming Events ---
    preview_btn.click(
        fn=open_preview_modal,
        inputs=current_video,
        outputs=[preview_modal, preview_video],
        js=scroll_top_js,
    )

    for btn in [preview_close_top, preview_close_bottom]:
        btn.click(
            fn=close_preview_modal,
            inputs=None,
            outputs=preview_modal,
        )

    preview_save_btn.click(
        fn=save_preview_and_trim,
        inputs=[preview_video, current_video],
        outputs=[preview_modal, current_video, video_name_md],
    )

    # Translate event
    submit_btn.click(
        fn=start_translating_ui,
        inputs=None,
        outputs=[translation_card, translation_display, show_keypoints_btn, keypoints_video_state],
    ).then(
        fn=finish_translating,
        inputs=current_video,
        outputs=[translation_display, show_keypoints_btn, keypoints_video_state],
    )

    # Open Keypoints Modal
    show_keypoints_btn.click(
        fn=open_keypoints_modal,
        inputs=keypoints_video_state,
        outputs=[keypoints_modal, keypoints_modal_video],
        js=scroll_top_js,
    )

    # Close Keypoints Modal
    keypoints_close_top.click(
        fn=close_keypoints_modal,
        inputs=None,
        outputs=[keypoints_modal, keypoints_modal_video],
    )
    keypoints_close_bottom.click(
        fn=close_keypoints_modal,
        inputs=None,
        outputs=[keypoints_modal, keypoints_modal_video],
    )



if __name__ == "__main__":
    app.queue(default_concurrency_limit=1).launch()