File size: 4,524 Bytes
3b1fac6
 
 
785835e
3b1fac6
 
 
 
 
785835e
3b1fac6
785835e
 
 
 
 
 
 
 
 
3b1fac6
 
 
 
 
 
 
 
785835e
3b1fac6
785835e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b1fac6
 
785835e
 
 
 
fdcc0cf
 
 
938d58f
 
 
 
fdcc0cf
 
785835e
 
 
 
 
fdcc0cf
 
785835e
 
 
 
 
 
 
 
 
 
fdcc0cf
 
 
 
 
785835e
 
 
 
 
3b1fac6
785835e
 
 
 
 
 
3b1fac6
 
 
785835e
 
 
 
 
 
 
 
 
 
 
 
 
 
3b1fac6
785835e
 
 
 
 
 
 
 
 
 
3b1fac6
785835e
 
 
 
 
 
 
 
 
3b1fac6
 
 
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
#!/usr/bin/env python3
"""
Startup script for Hugging Face Spaces deployment.
Handles model preloading and graceful fallbacks for containerized environments.
"""

import os
import subprocess
import sys
import logging

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

def create_directories():
    """Create necessary directories with proper permissions."""
    dirs = [
        'uploads', 'outputs', 'model_cache', 'temp_files', 
        'demo_results', '/tmp/matplotlib', '/tmp/fontconfig'
    ]
    
    for d in dirs:
        try:
            os.makedirs(d, mode=0o777, exist_ok=True)
            logger.info(f'βœ… Created directory: {d}')
        except Exception as e:
            logger.warning(f'⚠️ Failed to create directory {d}: {e}')

def check_dependencies():
    """Check if critical dependencies are available."""
    critical_deps = ['fastapi', 'uvicorn', 'torch', 'transformers']
    missing_deps = []
    
    for dep in critical_deps:
        try:
            __import__(dep)
            logger.info(f'βœ… {dep} available')
        except ImportError:
            missing_deps.append(dep)
            logger.warning(f'⚠️ {dep} not available')
    
    if missing_deps:
        logger.error(f'❌ Missing critical dependencies: {missing_deps}')
        return False
    
    return True

def preload_models():
    """Attempt to preload models with graceful fallback."""
    logger.info('πŸ”„ Attempting model preloading...')
    
    try:
        # Check if model_preloader.py exists and is importable
        import model_preloader
        logger.info('βœ… Model preloader module found')
        
        # Set environment variables to handle onnxruntime issues
        env = os.environ.copy()
        env.update({
            'ORT_DYLIB_DEFAULT_OPTIONS': 'DisableExecutablePageAllocator=1',
            'ONNXRUNTIME_EXECUTION_PROVIDERS': 'CPUExecutionProvider',
            'TF_ENABLE_ONEDNN_OPTS': '0',
            'OMP_NUM_THREADS': '1'
        })
        
        # Try to run the preloader
        result = subprocess.run(
            ['python', 'model_preloader.py'], 
            capture_output=True, 
            text=True, 
            timeout=300,  # 5 minute timeout
            env=env
        )
        
        if result.returncode == 0:
            logger.info('βœ… Models loaded successfully')
            if result.stdout:
                logger.info(f'Preloader output: {result.stdout[:500]}...')
            return True
        else:
            logger.warning(f'⚠️ Model preloading failed with return code {result.returncode}')
            if result.stderr:
                # Check if it's the onnxruntime issue
                if 'cannot enable executable stack' in result.stderr:
                    logger.warning('⚠️ ONNX Runtime executable stack issue detected - this is expected in containers')
                else:
                    logger.warning(f'Preloader stderr: {result.stderr[:500]}...')
            return False
            
    except subprocess.TimeoutExpired:
        logger.warning('⚠️ Model preloading timed out, continuing...')
        return False
    except Exception as e:
        logger.warning(f'⚠️ Model preloading failed: {e}')
        return False

def start_web_app():
    """Start the web application."""
    logger.info('🌐 Starting web application...')
    
    try:
        import uvicorn
        logger.info('βœ… Uvicorn imported successfully')
        
        # Start the server
        uvicorn.run(
            'web_app:app', 
            host='0.0.0.0', 
            port=7860, 
            workers=1, 
            log_level='info',
            access_log=True
        )
    except ImportError as e:
        logger.error(f'❌ Failed to import uvicorn: {e}')
        sys.exit(1)
    except Exception as e:
        logger.error(f'❌ Failed to start web application: {e}')
        sys.exit(1)

def main():
    """Main startup function."""
    logger.info('πŸš€ Starting Multilingual Audio Intelligence System...')
    
    # Check critical dependencies
    if not check_dependencies():
        logger.error('❌ Critical dependencies missing, exiting...')
        sys.exit(1)
    
    # Create necessary directories
    create_directories()
    
    # Attempt model preloading (non-blocking)
    preload_models()
    
    # Start the web application
    start_web_app()

if __name__ == '__main__':
    main()