SIH-Crop-Yield-API / start.py
AshrafGalibSk's picture
Upload folder using huggingface_hub
bbd5f9c verified
Raw
History Blame Contribute Delete
2.29 kB
#!/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
)