File size: 1,876 Bytes
5a58b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import subprocess
import webbrowser
import threading
import time

def install_dependencies():
    """Install dependencies from requirements.txt."""
    print("Checking and installing dependencies...")
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
        print("Dependencies installed successfully.")
    except subprocess.CalledProcessError as e:
        print(f"Error installing dependencies: {e}")
        sys.exit(1)

def run_app():
    """Run the FastAPI application."""
    print("Starting Heart Disease Prediction App...")
    
    # Set Environment Variable for Model Path if not set
    if "MODEL_PATH" not in os.environ:
        # Default to the correct path relative to this script
        os.environ["MODEL_PATH"] = "app/models/heart_disease_model.pkl"

    # Add current directory to sys.path
    sys.path.append(os.getcwd())

    # Open browser in a separate thread
    def open_browser():
        time.sleep(3) # Wait for server to start
        print("Opening browser at http://localhost:8000")
        webbrowser.open("http://localhost:8000")

    threading.Thread(target=open_browser, daemon=True).start()

    # Run Uvicorn
    try:
        import uvicorn
        uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=False)
    except ImportError:
        print("Error: uvicorn not found. Please run the script again after installation.")
    except Exception as e:
        print(f"Error running application: {e}")

if __name__ == "__main__":
    # Ensure we are in the script's directory
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    
    # Check if dependencies are installed
    try:
        import fastapi
        import uvicorn
        import joblib
        import jinja2
    except ImportError:
        install_dependencies()
    
    run_app()