File size: 2,287 Bytes
bbd5f9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Railway startup script for organized codebase
"""
import os
import sys
import uvicorn

print("πŸ”§ Railway Startup Script")
print(f"🌍 Environment variables:")
print(f"  PORT = {os.environ.get('PORT', 'NOT SET')}")
print(f"  RAILWAY_ENVIRONMENT = {os.environ.get('RAILWAY_ENVIRONMENT', 'NOT SET')}")

# Ensure PORT is set
if 'PORT' not in os.environ:
    print("⚠️  PORT environment variable not set, using default 8000")
    os.environ['PORT'] = '8000'
else:
    print(f"βœ… PORT environment variable is set to {os.environ['PORT']}")

# Get port from environment
port = int(os.environ.get('PORT', 8000))
print(f"πŸš€ Starting server on 0.0.0.0:{port}")

# Add src to Python path
sys.path.insert(0, 'src')

# Set up proper module structure for joblib loading
try:
    # Import DataPreprocessor and make it available globally
    from models import DataPreprocessor
    
    # Make it available in multiple namespaces for joblib compatibility
    globals()['DataPreprocessor'] = DataPreprocessor
    
    # Create module aliases that joblib might look for
    import sys
    import types
    
    # Create a mock module for __main__ if needed
    if '__main__' not in sys.modules or not hasattr(sys.modules['__main__'], 'DataPreprocessor'):
        if '__main__' in sys.modules:
            sys.modules['__main__'].DataPreprocessor = DataPreprocessor
    
    # Also make it available under 'models' module path
    if 'models' not in sys.modules:
        models_module = types.ModuleType('models')
        models_module.DataPreprocessor = DataPreprocessor
        sys.modules['models'] = models_module
    
    print("βœ… DataPreprocessor class prepared for joblib loading")
    
except ImportError as e:
    print(f"Warning: Could not import DataPreprocessor: {e}")

if __name__ == "__main__":
    # Import the app from src directory
    from app import app
    
    print("πŸš€ Starting FastAPI application with uvicorn...")
    
    # Add a small delay to ensure everything is initialized
    import time
    print("⏳ Ensuring all models are ready...")
    time.sleep(2)
    print("βœ… Ready to serve requests!")
    
    uvicorn.run(
        app, 
        host="0.0.0.0", 
        port=port,
        log_level="info",
        access_log=True
    )