File size: 3,500 Bytes
7d3a794
55f664e
98a0238
7d3a794
b565f0a
6ed3230
 
7d3a794
6ed3230
d59529d
6ed3230
 
 
 
 
 
b565f0a
6ed3230
d59529d
6ed3230
7d3a794
c48f883
7d3a794
c48f883
98a0238
7d3a794
6ed3230
 
d59529d
fcf0bca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c48f883
7d3a794
 
c48f883
7d3a794
 
d59529d
fcf0bca
7d3a794
 
c48f883
 
7d3a794
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

import gradio as gr
import joblib
import pandas as pd

# Path to the trained model
MODEL_PATH = "tuned_model.pkl"

# Load the model
try:
    model = joblib.load(MODEL_PATH)
except FileNotFoundError:
    raise FileNotFoundError(f"Model file not found at {MODEL_PATH}.")

# Define prediction function
def predict_with_model(State: str, Sex: str, GeneralHealth: str, PhysicalHealthDays: str, MentalHealthDays: str, LastCheckupTime: str, PhysicalActivities: str, SleepHours: str, HadStroke: str, HadArthritis: str, HadDiabetes: str, SmokerStatus: str, ECigaretteUsage: str, RaceEthnicityCategory: str, AgeCategory: str, HeightInMeters: str, WeightInKilograms: str, BMI: str, AlcoholDrinkers: str, HighRiskLastYear: str):
    try:
        # Prepare input data as a DataFrame
        input_data = pd.DataFrame([[State, Sex, GeneralHealth, PhysicalHealthDays, MentalHealthDays, LastCheckupTime, PhysicalActivities, SleepHours, HadStroke, HadArthritis, HadDiabetes, SmokerStatus, ECigaretteUsage, RaceEthnicityCategory, AgeCategory, HeightInMeters, WeightInKilograms, BMI, AlcoholDrinkers, HighRiskLastYear]], columns=['State', 'Sex', 'GeneralHealth', 'PhysicalHealthDays', 'MentalHealthDays', 'LastCheckupTime', 'PhysicalActivities', 'SleepHours', 'HadStroke', 'HadArthritis', 'HadDiabetes', 'SmokerStatus', 'ECigaretteUsage', 'RaceEthnicityCategory', 'AgeCategory', 'HeightInMeters', 'WeightInKilograms', 'BMI', 'AlcoholDrinkers', 'HighRiskLastYear'])
        prediction = model.predict(input_data)
        return "Heart Disease Risk" if prediction[0] == 1 else "No Risk"
    except Exception as e:
        return f"Error during prediction: {e}"

# Define the Gradio interface
with gr.Blocks() as app:
    gr.Markdown("# Health Risk Prediction App")
    gr.Markdown("### Input the feature values below to predict health risks.")

    inputs = [
        gr.Dropdown(["Alabama", "Alaska", "Arizona", "Arkansas", "California"], label="State"),  # Example states
        gr.Radio(["Male", "Female"], label="Sex"),
        gr.Radio(["Excellent", "Very Good", "Good", "Fair", "Poor"], label="GeneralHealth"),
        gr.Slider(0, 30, step=1, label="PhysicalHealthDays"),
        gr.Slider(0, 30, step=1, label="MentalHealthDays"),
        gr.Radio(["Within last year", "1-2 years ago", "3-5 years ago", "5+ years ago"], label="LastCheckupTime"),
        gr.Radio(["Yes", "No"], label="PhysicalActivities"),
        gr.Slider(0, 24, step=1, label="SleepHours"),
        gr.Radio(["Yes", "No"], label="HadStroke"),
        gr.Radio(["Yes", "No"], label="HadArthritis"),
        gr.Radio(["Yes", "No"], label="HadDiabetes"),
        gr.Radio(["Smoker", "Non-Smoker"], label="SmokerStatus"),
        gr.Radio(["Yes", "No"], label="ECigaretteUsage"),
        gr.Dropdown(["White", "Black", "Asian", "Hispanic", "Other"], label="RaceEthnicityCategory"),
        gr.Dropdown(["18-24", "25-34", "35-44", "45-54", "55-64", "65+"], label="AgeCategory"),
        gr.Slider(1.0, 2.5, step=0.01, label="HeightInMeters"),
        gr.Slider(30, 200, step=1, label="WeightInKilograms"),
        gr.Slider(10, 50, step=0.1, label="BMI"),
        gr.Radio(["Yes", "No"], label="AlcoholDrinkers"),
        gr.Radio(["Yes", "No"], label="HighRiskLastYear"),
    ]

    predict_button = gr.Button("Predict")
    output = gr.Textbox(label="Prediction Result")

    # Connect prediction logic
    predict_button.click(
        fn=predict_with_model,
        inputs=inputs,
        outputs=output,
    )

# Launch the app
app.launch()