File size: 35,106 Bytes
cf2f0cc
6854601
ceeabec
 
 
 
 
 
 
6854601
18062ed
ceeabec
 
 
6854601
ceeabec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77a509b
ceeabec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77a509b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dac15d6
 
77a509b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ceeabec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6854601
ceeabec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bc581b
 
 
 
 
ced046a
 
4bc581b
ced046a
4bc581b
 
 
 
 
 
 
 
 
 
ced046a
4bc581b
 
ced046a
77a509b
4bc581b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ace917e
4bc581b
 
 
 
 
 
8bedffe
4bc581b
 
94bdc6a
4bc581b
 
 
 
 
 
 
 
 
 
 
 
 
94bdc6a
4bc581b
 
 
 
 
 
 
 
 
 
 
 
 
ced046a
 
 
4bc581b
 
 
 
 
 
 
 
 
 
 
ced046a
4bc581b
 
 
 
 
 
ced046a
4bc581b
 
 
 
69d3d77
 
4bc581b
 
69d3d77
 
 
 
 
 
 
 
 
 
4bc581b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ceeabec
 
 
6854601
ceeabec
 
 
 
 
 
 
 
 
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
import spaces
import gradio as gr
import os
import tempfile
import huggingface_hub
import shutil
import logging
import traceback
from features import SHuBERTProcessor


# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# Set writable cache directories
def setup_cache_directories():
    """Set up cache directories with proper error handling"""
    try:
        cache_dirs = {
            'MPLCONFIGDIR': '/tmp/matplotlib',
            'TRANSFORMERS_CACHE': '/tmp/huggingface',
            'HF_HOME': '/tmp/huggingface',
            'FONTCONFIG_PATH': '/tmp/fontconfig',
            'TORCH_HOME': '/tmp/torch',  # PyTorch cache directory
        }
        
        for env_var, path in cache_dirs.items():
            os.environ[env_var] = path
            os.makedirs(path, exist_ok=True, mode=0o777)
            logger.info(f"Cache directory created: {env_var} = {path}")
        
        # Also set XDG_CACHE_HOME to override default .cache location
        os.environ['XDG_CACHE_HOME'] = '/tmp/cache'
        os.makedirs('/tmp/cache', exist_ok=True, mode=0o777)
        logger.info(f"Cache directory created: XDG_CACHE_HOME = /tmp/cache")
        
        # Clear any existing PyTorch Hub cache to avoid corruption issues
        torch_hub_dir = '/tmp/torch/hub'
        if os.path.exists(torch_hub_dir):
            shutil.rmtree(torch_hub_dir)
            logger.info("Cleared existing PyTorch Hub cache")
        os.makedirs(torch_hub_dir, exist_ok=True, mode=0o777)
        logger.info(f"Created clean PyTorch Hub cache directory: {torch_hub_dir}")
        
        # Copy updated DINOv2 files to torch cache after clearing
        # This ensures they're available when PyTorch Hub downloads the repo
        try:
            src_dir = os.path.dirname(os.path.abspath(__file__))
            target_dir = '/tmp/torch/hub/facebookresearch_dinov2_main/dinov2/layers'
            
            for filename in ['attention.py', 'block.py']:
                src_path = os.path.join(src_dir, filename)
                if os.path.exists(src_path):
                    # We'll copy these after the initial hub download
                    logger.info(f"Found {filename} in project directory - will copy after hub download")
                else:
                    logger.warning(f"Could not find {filename} in project directory")
        except Exception as e:
            logger.warning(f"Error preparing DINOv2 files: {e}")
        
        return True
    except Exception as e:
        logger.error(f"Error creating cache directories: {str(e)}")
        return False

# Configuration for Hugging Face Spaces
MODEL_REPO = "ShesterG/SHuBERT"
TOKEN = os.environ.get('HF_TOKEN')

def validate_environment():
    """Validate required environment variables and setup"""
    if not TOKEN:
        raise ValueError("HF_TOKEN environment variable not set. This is required to access private model repository.")
    
    # Check available disk space
    free_space = shutil.disk_usage('/').free / (1024*1024*1024)  # GB
    logger.info(f"Available disk space: {free_space:.2f} GB")
    
    if free_space < 2:  # Less than 2GB
        logger.warning("Low disk space available. This may cause issues.")
    
    return True

def download_models():
    """Download all required models from Hugging Face Hub with enhanced error handling"""
    logger.info("Starting model download process...")
    
    try:
        # Validate environment first
        validate_environment()
        


        logger.info("Downloading entire models folder...")

        # Download the entire models folder
        models_path = huggingface_hub.snapshot_download(
            repo_id=MODEL_REPO,
            allow_patterns="models/*",  # Download everything in models folder
            token=TOKEN,
            cache_dir=os.environ['TRANSFORMERS_CACHE']
        )
        

        # Build config dict with expected file paths
        config = {
            'yolov8_model_path': os.path.join(models_path, "models/yolov8n.pt"),
            'dino_face_model_path': os.path.join(models_path, "models/dinov2face.pth"),
            'dino_hands_model_path': os.path.join(models_path, "models/dinov2hand.pth"),
            'mediapipe_face_model_path': os.path.join(models_path, "models/face_landmarker_v2_with_blendshapes.task"),
            'mediapipe_hands_model_path': os.path.join(models_path, "models/hand_landmarker.task"),
            'shubert_model_path': os.path.join(models_path, "models/checkpoint_836_400000.pt"),
            'slt_model_config': os.path.join(models_path, "models/byt5_base/config.json"),
            'slt_model_checkpoint': os.path.join(models_path, "models/checkpoint-11625"),
            'slt_tokenizer_checkpoint': os.path.join(models_path, "models/byt5_base"),
            'temp_dir': 'temp'
        }

        # Verify all required files and folders exist
        logger.info("Verifying downloaded files...")
        missing_files = []

        for key, path in config.items():
            if key == 'temp_dir':  # Skip temp_dir check
                continue
            
            if not os.path.exists(path):
                missing_files.append(f"{key}: {path}")
                logger.error(f"Missing: {path}")
            else:
                logger.info(f"✓ Found: {path}")

        if missing_files:
            logger.error(f"Missing {len(missing_files)} required files/folders:")
            for missing in missing_files:
                logger.error(f"  - {missing}")
            raise FileNotFoundError(f"Required files not found: {missing_files}")

        logger.info("All models downloaded and verified successfully!")
        logger.info(f"Models root path: {models_path}")

        return config
        
    except Exception as e:
        logger.error(f"Error downloading models: {str(e)}")
        logger.error(f"Traceback: {traceback.format_exc()}")
        
        # Additional debugging info
        try:
            cache_contents = os.listdir(os.environ['TRANSFORMERS_CACHE'])
            logger.info(f"Cache directory contents: {cache_contents}")
        except:
            logger.error("Cannot access cache directory")
            
        return None



def download_example_videos():
    """Download example videos from Hugging Face Hub with enhanced error handling"""

    # Download the entire example_videos folder
    example_video_path = huggingface_hub.snapshot_download(
        repo_id=MODEL_REPO,
        allow_patterns="example_video/*",  # Download everything in example_videos folder
        token=TOKEN,
        cache_dir=os.environ['TRANSFORMERS_CACHE']
    )

    example_video_path_list = [
        os.path.join(example_video_path, "rDUefZVPfmU_crop_1.mp4"),
        os.path.join(example_video_path, "rDUefZVPfmU_crop_2.mp4"),
        os.path.join(example_video_path, "rDUefZVPfmU_crop_3.mp4"),
        os.path.join(example_video_path, "rDUefZVPfmU_crop_4.mp4"),
        os.path.join(example_video_path, "rDUefZVPfmU_crop_5.mp4"),
        os.path.join(example_video_path, "rDUefZVPfmU_crop_6.mp4"),
        os.path.join(example_video_path, "rDUefZVPfmU_crop_7.mp4"),
        os.path.join(example_video_path, "rDUefZVPfmU_crop_8.mp4"),
        os.path.join(example_video_path, "rDUefZVPfmU_crop_9.mp4"),
        os.path.join(example_video_path, "rDUefZVPfmU_crop_10.mp4"),
        os.path.join(example_video_path, "L5hUxT5YbnY_crop_1.mp4"),
        os.path.join(example_video_path, "L5hUxT5YbnY_crop_2.mp4"),
        os.path.join(example_video_path, "L5hUxT5YbnY_crop_3.mp4"),
        os.path.join(example_video_path, "L5hUxT5YbnY_crop_4.mp4"),
        os.path.join(example_video_path, "L5hUxT5YbnY_crop_5.mp4"),
        os.path.join(example_video_path, "L5hUxT5YbnY_crop_6.mp4"),
        os.path.join(example_video_path, "L5hUxT5YbnY_crop_7.mp4"),
    ]

    return example_video_path_list
        


def initialize_processor(config):
    """Initialize SHuBERT processor with error handling"""
    try:
        logger.info("Initializing SHuBERT processor...")
        processor = SHuBERTProcessor(config)
        logger.info("SHuBERT processor initialized successfully!")
        return processor
    except Exception as e:
        logger.error(f"Error initializing SHuBERT processor: {str(e)}")
        logger.error(f"Traceback: {traceback.format_exc()}")
        return None

# Initialize the application
def initialize_app():
    """Initialize the entire application with comprehensive error handling"""
    try:
        # Setup cache directories
        if not setup_cache_directories():
            raise RuntimeError("Failed to setup cache directories")
        
        # Download models
        config = download_models()
        if config is None:
            raise RuntimeError("Failed to download models")
        
        # Initialize processor
        processor = initialize_processor(config)
        if processor is None:
            raise RuntimeError("Failed to initialize SHuBERT processor")
        
        logger.info("Application initialized successfully!")
        return config, processor
        
    except Exception as e:
        error_msg = f"Application initialization failed: {str(e)}"
        logger.error(error_msg)
        logger.error(f"Full traceback: {traceback.format_exc()}")
        raise RuntimeError(error_msg)

# Global variables for application state
config = None
processor = None
initialization_error = None

try:
    config, processor = initialize_app()
except Exception as e:
    initialization_error = str(e)
    logger.error(f"Startup failed: {initialization_error}")

def copy_dinov2_files_if_needed():
    """Copy updated DINOv2 files after PyTorch Hub download if needed"""
    try:
        src_dir = os.path.dirname(os.path.abspath(__file__))
        target_dir = '/tmp/torch/hub/facebookresearch_dinov2_main/dinov2/layers'
        
        # Check if PyTorch Hub has downloaded the repository
        hub_main_dir = '/tmp/torch/hub/facebookresearch_dinov2_main'
        
        if os.path.exists(hub_main_dir):
            # Ensure the target directory exists
            os.makedirs(target_dir, exist_ok=True)
            
            files_copied = 0
            for filename in ['attention.py', 'block.py']:
                src_path = os.path.join(src_dir, filename)
                target_path = os.path.join(target_dir, filename)
                
                if os.path.exists(src_path):
                    # Always overwrite with our robust versions
                    shutil.copy2(src_path, target_path)
                    # Make sure it's readable
                    os.chmod(target_path, 0o644)
                    logger.info(f"Replaced {filename} with robust version (numpy/Python 3.8 compatible)")
                    files_copied += 1
                else:
                    logger.error(f"Source file not found: {src_path}")
            
            if files_copied > 0:
                # Clear Python's import cache to ensure new files are used
                import importlib
                import sys
                
                # Remove any cached imports of dinov2 modules
                modules_to_remove = [key for key in sys.modules.keys() if 'dinov2' in key]
                for module in modules_to_remove:
                    del sys.modules[module]
                    logger.info(f"Cleared cached import: {module}")
                
                logger.info(f"Successfully replaced {files_copied} DINOv2 files with robust versions")
                return True
        else:
            logger.info("PyTorch Hub repository not yet downloaded")
            return False
            
    except Exception as e:
        logger.error(f"Error copying DINOv2 files: {e}")
        logger.error(f"Traceback: {traceback.format_exc()}")
        return False
    
@spaces.GPU
def process_video(video_file):
    """Process uploaded video file with enhanced error handling"""
    # Check if initialization was successful
    if initialization_error:
        return f"Application initialization failed: {initialization_error}\n\nPlease check the logs for more details."
    
    if processor is None:
        return "Error: Model not initialized properly. Please check the logs."
    
    if video_file is None:
        return "Please upload a video file."
    
    logger.info(f"=== Starting video processing ===")
    logger.info(f"Video file input: {video_file}")
    logger.info(f"Video file type: {type(video_file)}")
    
    try:
        # Create temp directory with proper permissions
        temp_dir = config['temp_dir']
        os.makedirs(temp_dir, exist_ok=True, mode=0o777)
        logger.info(f"Temp directory: {temp_dir}")
        
        # Generate unique filename to avoid conflicts
        import time
        timestamp = str(int(time.time() * 1000))
        file_extension = '.mp4'  # Default extension
        
        # Try to get original extension if available
        try:
            if hasattr(video_file, 'name') and video_file.name:
                file_extension = os.path.splitext(video_file.name)[1] or '.mp4'
            elif isinstance(video_file, str):
                file_extension = os.path.splitext(video_file)[1] or '.mp4'
        except:
            pass
            
        temp_video_path = os.path.join(temp_dir, f"video_{timestamp}{file_extension}")
        logger.info(f"Target temp video path: {temp_video_path}")
        
        # Handle Gradio file upload - video_file is typically a string path to temp file
        logger.info(f"Processing video file: {video_file} (type: {type(video_file)})")
        
        if isinstance(video_file, str):
            # Gradio provides a file path string
            source_path = video_file
            
            # Handle both absolute and relative paths
            if not os.path.isabs(source_path):
                # Try current working directory first
                abs_source_path = os.path.abspath(source_path)
                logger.info(f"Converting relative path {source_path} to absolute: {abs_source_path}")
                if os.path.exists(abs_source_path):
                    source_path = abs_source_path
                else:
                    # Try looking in common Gradio temp directories
                    possible_paths = [
                        source_path,
                        os.path.join('/tmp', os.path.basename(source_path)),
                        os.path.join('/tmp/gradio', os.path.basename(source_path)),
                        abs_source_path
                    ]
                    
                    found_path = None
                    for path in possible_paths:
                        logger.info(f"Checking path: {path}")
                        if os.path.exists(path):
                            found_path = path
                            logger.info(f"Found file at: {path}")
                            break
                    
                    if found_path:
                        source_path = found_path
                    else:
                        logger.error(f"Could not find source file in any expected location")
                        logger.error(f"Tried paths: {possible_paths}")
                        raise FileNotFoundError(f"Source video file not found in any expected location: {video_file}")
            
            logger.info(f"Final source file path: {source_path}")
            logger.info(f"Source file exists: {os.path.exists(source_path)}")
            
            if os.path.exists(source_path):
                try:
                    # Check source file permissions and size
                    stat_info = os.stat(source_path)
                    logger.info(f"Source file size: {stat_info.st_size} bytes, mode: {oct(stat_info.st_mode)}")
                    
                    # Try to read the file content
                    with open(source_path, 'rb') as src:
                        content = src.read()
                        logger.info(f"Successfully read {len(content)} bytes from source")
                    
                    # Write to destination (with a different name to avoid conflicts)
                    final_temp_path = os.path.join(temp_dir, f"processed_{timestamp}{file_extension}")
                    with open(final_temp_path, 'wb') as dst:
                        dst.write(content)
                        logger.info(f"Successfully wrote to destination: {final_temp_path}")
                    
                    # Update temp_video_path to the final location
                    temp_video_path = final_temp_path
                        
                except PermissionError as e:
                    logger.error(f"Permission error reading source file: {e}")
                    # Try alternative approach - use a completely different temp location
                    try:
                        import tempfile
                        # Create a new temporary file in system temp directory
                        with tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) as tmp:
                            alternative_temp_path = tmp.name
                        
                        logger.info(f"Trying alternative temp path: {alternative_temp_path}")
                        
                        # Try to copy using system copy command as fallback
                        import subprocess
                        result = subprocess.run(['cp', source_path, alternative_temp_path], 
                                              capture_output=True, text=True)
                        
                        if result.returncode == 0:
                            logger.info("Successfully copied using system cp command")
                            temp_video_path = alternative_temp_path
                        else:
                            logger.error(f"System cp failed: {result.stderr}")
                            raise PermissionError(f"Cannot read video file due to permission restrictions: {e}")
                            
                    except Exception as e2:
                        logger.error(f"Alternative copy method also failed: {e2}")
                        raise PermissionError(f"Cannot read video file due to permission restrictions: {e}")
            else:
                raise FileNotFoundError(f"Source video file not found: {source_path}")
                
        elif hasattr(video_file, 'read'):
            # If it's a file-like object with read method
            try:
                content = video_file.read()
                with open(temp_video_path, 'wb') as f:
                    f.write(content)
                logger.info(f"Saved video from file object: {temp_video_path} ({len(content)} bytes)")
            except Exception as e:
                logger.error(f"Error reading from file object: {e}")
                raise ValueError(f"Cannot read from file object: {e}")
        else:
            # Handle other cases - try to extract file path or content
            logger.info(f"Attempting to handle unknown file type: {type(video_file)}")
            try:
                # Check if it has a name attribute (common for file objects)
                if hasattr(video_file, 'name'):
                    source_path = video_file.name
                    logger.info(f"Found name attribute: {source_path}")
                    
                    if os.path.exists(source_path):
                        with open(source_path, 'rb') as src:
                            content = src.read()
                        with open(temp_video_path, 'wb') as dst:
                            dst.write(content)
                        logger.info(f"Successfully copied from name attribute")
                    else:
                        raise FileNotFoundError(f"File from name attribute not found: {source_path}")
                else:
                    logger.error(f"Unsupported video file type: {type(video_file)}")
                    raise ValueError(f"Unsupported video file type: {type(video_file)}")
            except Exception as e:
                logger.error(f"Failed to handle unknown file type: {e}")
                raise ValueError(f"Cannot process video file: {e}")
        
        # Set proper permissions on the saved file
        os.chmod(temp_video_path, 0o666)
        
        # Verify file exists and has content
        if not os.path.exists(temp_video_path) or os.path.getsize(temp_video_path) == 0:
            raise ValueError("Video file is empty or could not be saved")
        
        # Copy DINOv2 files if needed before processing
        # This needs to happen right after PyTorch Hub downloads but before model loading
        logger.info("Ensuring DINOv2 files are ready for processing...")
        copy_dinov2_files_if_needed()
        
        # Set up a monitoring patch for torch.hub.load to replace files immediately after download
        original_torch_hub_load = None
        try:
            import torch.hub
            original_torch_hub_load = torch.hub.load
            
            def patched_torch_hub_load(*args, **kwargs):
                logger.info(f"PyTorch Hub load called with: {args[0] if args else 'unknown'}")
                
                # Call the original function first
                result = original_torch_hub_load(*args, **kwargs)
                
                # If this was a DINOv2 call, immediately replace the files
                if args and 'dinov2' in str(args[0]):
                    logger.info("DINOv2 downloaded! Immediately replacing with robust versions...")
                    
                    # Try multiple times to ensure files are replaced
                    import time
                    for attempt in range(5):
                        if copy_dinov2_files_if_needed():
                            logger.info("Successfully replaced DINOv2 files!")
                            break
                        else:
                            logger.info(f"Attempt {attempt + 1} failed, retrying in 1 second...")
                            time.sleep(1)
                
                return result
            
            # Temporarily patch torch.hub.load
            torch.hub.load = patched_torch_hub_load
            logger.info("Patched torch.hub.load to replace DINOv2 files after download")
        except Exception as e:
            logger.warning(f"Could not patch torch.hub.load: {e}")
        
        logger.info(f"Processing video: {temp_video_path}")
        try:
            output_text = processor.process_video(temp_video_path)
        finally:
            # Restore original function
            if original_torch_hub_load:
                try:
                    import torch.hub
                    torch.hub.load = original_torch_hub_load
                    logger.info("Restored original torch.hub.load")
                except:
                    pass
        
        logger.info(f"Video processed successfully. Output: {output_text[:100]}...")
        
        # Clean up temp file
        if os.path.exists(temp_video_path):
            os.remove(temp_video_path)
            logger.info("Temporary video file cleaned up")
        
        return output_text
        
    except Exception as e:
        logger.error(f"Error processing video: {str(e)}")
        logger.error(f"Traceback: {traceback.format_exc()}")
        return f"Error processing video: {str(e)}\n\nPlease check that your video is a valid ASL video under 10 seconds."

# # Create Gradio interface
# def create_interface():
#    """Create the Gradio interface"""
#    description = """
#    Upload an ASL* video to get an English translation.  *Sign languages belonging to the same sign language family as ASL (e.g. Ghanaian Sign Language, as well as others listed in Table 7, Row 1 of https://aclanthology.org/2023.findings-emnlp.664.pdf) might also have non-trivial performance, although the model is trained only on ASL data.


# This app uses TTIC's foundation model SHuBERT (introduced in an ACL 2025 paper, see http://shubert.pals.ttic.edu).
     
#    **Requirements:**
#    - We recommend that videos be under 20 seconds.  Performance for longer videos has not been tested.
#    - The signer should be the main part (e.g. 90% of the area) of the video. Videos recorded from a phone camera, tablet, or personal computer should work well. Studio recordings where the signer is farther from the camera may not work as well.
#    - Supported formats: MP4, MOV

#    **Note:**
#    - This is just a demo of a research project, and should NOT be used to replace an interpreter in any way.
#    - Videos will be deleted after the output is generated.
#    - Inquires or feedback? Please email us at shesterg@ttic.edu
#    """
  
#    if initialization_error:
#        description += f"\n\n:warning: **Initialization Error:** {initialization_error}"
  
    
# #    dailymoth_pathlist = download_example_videos()

#    src_dir = os.path.dirname(os.path.abspath(__file__))
#    dailymoth_pathlist = []
#    L5hUxT5YbnY_crop_1 = "dailymoth_examples/L5hUxT5YbnY_crop_1.mp4"
#    L5hUxT5YbnY_crop_2 = "dailymoth_examples/L5hUxT5YbnY_crop_2.mp4"
#    L5hUxT5YbnY_crop_3 = "dailymoth_examples/L5hUxT5YbnY_crop_3.mp4"
#    L5hUxT5YbnY_crop_4 = "dailymoth_examples/L5hUxT5YbnY_crop_4.mp4"
#    L5hUxT5YbnY_crop_5 = "dailymoth_examples/L5hUxT5YbnY_crop_5.mp4"
#    L5hUxT5YbnY_crop_6 = "dailymoth_examples/L5hUxT5YbnY_crop_6.mp4"
#    L5hUxT5YbnY_crop_7 = "dailymoth_examples/L5hUxT5YbnY_crop_7.mp4"
#    rDUefZVPfmU_crop_1 = "dailymoth_examples/rDUefZVPfmU_crop_1.mp4"
#    rDUefZVPfmU_crop_2 = "dailymoth_examples/rDUefZVPfmU_crop_2.mp4"
#    rDUefZVPfmU_crop_3 = "dailymoth_examples/rDUefZVPfmU_crop_3.mp4"
#    rDUefZVPfmU_crop_4 = "dailymoth_examples/rDUefZVPfmU_crop_4.mp4"
#    rDUefZVPfmU_crop_5 = "dailymoth_examples/rDUefZVPfmU_crop_5.mp4"
#    rDUefZVPfmU_crop_6 = "dailymoth_examples/rDUefZVPfmU_crop_6.mp4"
#    rDUefZVPfmU_crop_7 = "dailymoth_examples/rDUefZVPfmU_crop_7.mp4"
#    rDUefZVPfmU_crop_8 = "dailymoth_examples/rDUefZVPfmU_crop_8.mp4"
#    rDUefZVPfmU_crop_9 = "dailymoth_examples/rDUefZVPfmU_crop_9.mp4"
#    rDUefZVPfmU_crop_10 = "dailymoth_examples/rDUefZVPfmU_crop_10.mp4"
#    dailymoth_filenames = [L5hUxT5YbnY_crop_1, L5hUxT5YbnY_crop_2, L5hUxT5YbnY_crop_3, L5hUxT5YbnY_crop_4, L5hUxT5YbnY_crop_5, L5hUxT5YbnY_crop_6, L5hUxT5YbnY_crop_7, rDUefZVPfmU_crop_1, rDUefZVPfmU_crop_2, rDUefZVPfmU_crop_3, rDUefZVPfmU_crop_4, rDUefZVPfmU_crop_5, rDUefZVPfmU_crop_6, rDUefZVPfmU_crop_7, rDUefZVPfmU_crop_8, rDUefZVPfmU_crop_9, rDUefZVPfmU_crop_10]
   
#    for filename in dailymoth_filenames:
#        src_path = os.path.join(src_dir, filename)
#        if os.path.exists(src_path):
#            dailymoth_pathlist.append(src_path)
#        else:
#            print(f"Warning: File not found at {src_path}")
       
#    with gr.Blocks(title="ASL Video to English Text Translation") as interface:
#        gr.Markdown(f"# ASL Video to English Text Translation\n\n{description}")
    
#    with gr.Row():
#        with gr.Column():
#            video_input = gr.Video(label="ASL Video (under 20 seconds)", format="mp4", height=480, width=640)
#            submit_btn = gr.Button("Translate", variant="primary")
#        with gr.Column():
#            output_text = gr.Textbox(label="English Translation", lines=3)
#            # Add examples section
#            if dailymoth_pathlist:  # Only show examples if we have valid files
#                gr.Examples(
#                    examples=dailymoth_pathlist,
#                    inputs=video_input,
#                    label="Click a video to try an example"
#                )
                
#                # Add attribution note for the examples
#                gr.Markdown("""
#                ---
#                **Example Videos Attribution:**
#                The example videos used in this demo are from [The Daily Moth](https://www.youtube.com/@TheDailyMoth), 
#                a popular ASL news channel made by deaf creators. Specifically, they are from the Previews of [July 10](https://www.youtube.com/watch?v=rDUefZVPfmU) and [July 11](https://www.youtube.com/watch?v=L5hUxT5YbnY) 2025 Top Stories.
#                The videos are only used for illustrative purposes, and all rights to the content belong to The Daily Moth. In this light, we encourage to subscribe to their [channel](https://members.dailymoth.com/about).
#                """)
#            else:
#                gr.Markdown("*No example videos available at this time.*")

# #    video_input = gr.Video(label="ASL Video (under 20 seconds)", format="mp4", height=480, width=640)
# #    text_output = gr.Textbox(label="English Translation", lines=5)
  
  
  
# #    interface = gr.Interface(
# #        fn=process_video,
# #        inputs=video_input,
# #        outputs=text_output,
# #        title="ASL Video to English Text Translation",
# #        description=description,
# #        article="",
# #     #    examples=dailymoth_pathlist,  
# #     #    example_labels=["Officials with an EU force said they are searching for the missing."], 
# #        allow_flagging="never",    
# #    )
   
# #    gr.Examples(
# #         examples=dailymoth_pathlist,
# #         inputs=video_input,
# #         label="Click a video to try an example"
# #     )
#    submit_btn.click(fn=process_video, inputs=video_input, outputs=output_text)
   
#    return interface


def create_interface():
    """Create the Gradio interface"""
    description = """Upload an ASL* video to get an English translation.  *Sign languages belonging to the same sign language family as ASL (e.g. Ghanaian Sign Language, as well as others listed in Table 7, Row 1 of https://aclanthology.org/2023.findings-emnlp.664.pdf) might also have non-trivial performance, although the model is trained only on ASL data.

This app uses TTIC's foundation model SHuBERT (introduced in an ACL 2025 paper, see http://shubert.pals.ttic.edu).

**Requirements:**
- We recommend that videos be under 20 seconds.  Performance for longer videos has not been tested.
- The signer should be the main part (e.g. 90% of the area) of the video. Videos recorded from a phone camera, tablet, or personal computer should work well. Studio recordings where the signer is farther from the camera may not work as well.
- Supported formats: MP4, MOV

**Note:**
- This is just a demo of a research project, and should NOT be used to replace an interpreter in any way.
- Videos will be deleted after the output is generated.
- Inquires or feedback? Please email us at shesterg@ttic.edu"""
   
    if initialization_error:
        description += f"\n\n:warning: **Initialization Error:** {initialization_error}"
   
    src_dir = os.path.dirname(os.path.abspath(__file__))
    dailymoth_pathlist = []
    L5hUxT5YbnY_crop_1 = "dailymoth_examples/L5hUxT5YbnY_crop_1.mp4"
    L5hUxT5YbnY_crop_2 = "dailymoth_examples/L5hUxT5YbnY_crop_2.mp4"
    L5hUxT5YbnY_crop_3 = "dailymoth_examples/L5hUxT5YbnY_crop_3.mp4"
    L5hUxT5YbnY_crop_4 = "dailymoth_examples/L5hUxT5YbnY_crop_4.mp4"
    L5hUxT5YbnY_crop_5 = "dailymoth_examples/L5hUxT5YbnY_crop_5.mp4"
    L5hUxT5YbnY_crop_6 = "dailymoth_examples/L5hUxT5YbnY_crop_6.mp4"
    L5hUxT5YbnY_crop_7 = "dailymoth_examples/L5hUxT5YbnY_crop_7.mp4"
    rDUefZVPfmU_crop_1 = "dailymoth_examples/rDUefZVPfmU_crop_1.mp4"
    rDUefZVPfmU_crop_2 = "dailymoth_examples/rDUefZVPfmU_crop_2.mp4"
    rDUefZVPfmU_crop_3 = "dailymoth_examples/rDUefZVPfmU_crop_3.mp4"
    rDUefZVPfmU_crop_4 = "dailymoth_examples/rDUefZVPfmU_crop_4.mp4"
    rDUefZVPfmU_crop_5 = "dailymoth_examples/rDUefZVPfmU_crop_5.mp4"
    rDUefZVPfmU_crop_6 = "dailymoth_examples/rDUefZVPfmU_crop_6.mp4"
    rDUefZVPfmU_crop_7 = "dailymoth_examples/rDUefZVPfmU_crop_7.mp4"
    rDUefZVPfmU_crop_8 = "dailymoth_examples/rDUefZVPfmU_crop_8.mp4"
    rDUefZVPfmU_crop_9 = "dailymoth_examples/rDUefZVPfmU_crop_9.mp4"
    rDUefZVPfmU_crop_10 = "dailymoth_examples/rDUefZVPfmU_crop_10.mp4"
    dailymoth_filenames = [L5hUxT5YbnY_crop_1, L5hUxT5YbnY_crop_2, L5hUxT5YbnY_crop_3, L5hUxT5YbnY_crop_4, L5hUxT5YbnY_crop_5, L5hUxT5YbnY_crop_6, L5hUxT5YbnY_crop_7, rDUefZVPfmU_crop_1, rDUefZVPfmU_crop_2, rDUefZVPfmU_crop_3, rDUefZVPfmU_crop_4, rDUefZVPfmU_crop_5, rDUefZVPfmU_crop_6, rDUefZVPfmU_crop_7, rDUefZVPfmU_crop_8, rDUefZVPfmU_crop_9, rDUefZVPfmU_crop_10]
    
    for filename in dailymoth_filenames:
        src_path = os.path.join(src_dir, filename)
        if os.path.exists(src_path):
            dailymoth_pathlist.append(src_path)
        else:
            print(f"Warning: File not found at {src_path}")
        
    with gr.Blocks(title="ASL Video to English Text Translation") as interface:
        gr.Markdown(f"# ASL Video to English Text Translation\n\n{description}")
     
        with gr.Row():
            with gr.Column():
                video_input = gr.Video(label="ASL Video (under 20 seconds)", format="mp4", height=480, width=640)
                submit_btn = gr.Button("Translate", variant="primary")
            with gr.Column():
                output_text = gr.Textbox(label="English Translation", lines=3)
                
                # Add examples section in the right column
                if dailymoth_pathlist:  # Only show examples if we have valid files
                    gr.Examples(
                        examples=dailymoth_pathlist,
                        inputs=video_input,
                        label="Click a video to try an example"
                    )
                     
                    # Add attribution note for the examples
                    gr.Markdown("""
                    ---
                    **Example Videos Attribution:**
                    The example videos used in this demo are from [The Daily Moth](https://www.youtube.com/@TheDailyMoth), 
                    a popular ASL news channel made by deaf creators. Specifically, they are from the Previews of [July 10](https://www.youtube.com/watch?v=rDUefZVPfmU) and [July 11](https://www.youtube.com/watch?v=L5hUxT5YbnY) 2025 Top Stories.
                    The videos are only used for illustrative purposes, and all rights to the content belong to The Daily Moth. In this light, we encourage to subscribe to their [channel](https://members.dailymoth.com/about).
                    """)
                else:
                    gr.Markdown("*No example videos available at this time.*")
                
        # Set up the button click handler AFTER both input and output are defined
        submit_btn.click(fn=process_video, inputs=video_input, outputs=output_text)

    return interface


# Create the demo
demo = create_interface()

if __name__ == "__main__":
    # Launch with better configuration for Hugging Face Spaces
    logger.info("Launching Gradio interface...")
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        show_error=True
    )