""" Production deployment script for HealthGuard-AI application. This script helps with deploying the application in a production environment. """ import os import sys import subprocess import argparse from pathlib import Path def check_environment(): """Check if all required environment variables are set.""" required_vars = ['MONGODB_URI', 'SECRET_KEY'] missing_vars = [] for var in required_vars: if not os.getenv(var): missing_vars.append(var) if missing_vars: print(f"Missing required environment variables: {', '.join(missing_vars)}") print("Please set these variables before running the application.") return False return True def install_dependencies(): """Install required dependencies.""" try: subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt']) print("Dependencies installed successfully.") return True except subprocess.CalledProcessError as e: print(f"Failed to install dependencies: {e}") return False def check_models(): """Check if all required ML models are present.""" models_dir = Path('saved models') required_models = [ 'diabetes_model.sav', 'heart_disease_model.sav', 'parkinsons_model.sav' ] missing_models = [] for model in required_models: model_path = models_dir / model if not model_path.exists(): missing_models.append(model) if missing_models: print(f"Missing required models: {', '.join(missing_models)}") print("Please ensure all models are present in the 'saved models' directory.") return False return True def start_application(host='0.0.0.0', port=7860): """Start the Flask application.""" try: # Import and run the app from app import app app.run(host=host, port=port, debug=False) except Exception as e: print(f"Failed to start application: {e}") return False return True def main(): """Main deployment function.""" parser = argparse.ArgumentParser(description='Deploy HealthGuard-AI application') parser.add_argument('--install-deps', action='store_true', help='Install dependencies before starting') parser.add_argument('--host', default='0.0.0.0', help='Host to run the application on') parser.add_argument('--port', type=int, default=7860, help='Port to run the application on') args = parser.parse_args() print("HealthGuard-AI Production Deployment Script") print("=" * 50) # Check environment print("Checking environment variables...") if not check_environment(): sys.exit(1) # Install dependencies if requested if args.install_deps: print("Installing dependencies...") if not install_dependencies(): sys.exit(1) # Check models print("Checking ML models...") if not check_models(): sys.exit(1) # Start application print(f"Starting application on {args.host}:{args.port}...") if not start_application(args.host, args.port): sys.exit(1) if __name__ == '__main__': main()