File size: 3,421 Bytes
9876bd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

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()