Spaces:
Sleeping
Sleeping
| #!/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 | |
| ) | |