File size: 2,656 Bytes
7d3a794
d59529d
 
 
 
55f664e
98a0238
7d3a794
b565f0a
d59529d
7d3a794
d59529d
 
 
 
 
 
b565f0a
98a0238
d59529d
b565f0a
d59529d
 
 
7d3a794
c48f883
7d3a794
c48f883
98a0238
7d3a794
 
d59529d
 
 
 
 
c48f883
7d3a794
 
c48f883
7d3a794
 
d59529d
 
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

"""
Webapp Front End
"""

import gradio as gr
import joblib
import pandas as pd

MODEL_PATH = "Random_Foresttest_model.pkl"

try:
    rf_model = joblib.load(MODEL_PATH)
except FileNotFoundError as e:
    raise FileNotFoundError(
        f"Model file not found at {MODEL_PATH}. Please check the path."
    ) from e

# Define the prediction function
def predict_with_model(State: float, Sex: float, GeneralHealth: float, PhysicalHealthDays: float, MentalHealthDays: float, LastCheckupTime: float, PhysicalActivities: float, SleepHours: float, HadStroke: float, HadArthritis: float, HadDiabetes: float, SmokerStatus: float, ECigaretteUsage: float, RaceEthnicityCategory: float, AgeCategory: float, HeightInMeters: float, WeightInKilograms: float, BMI: float, AlcoholDrinkers: float, HighRiskLastYear: float):
    try:
        # Prepare input 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 = rf_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("# Heart Disease Prediction App")
    gr.Markdown("### Provide input values for the features below and get a prediction.")

    input_components = []
    for feature in ['State', 'Sex', 'GeneralHealth', 'PhysicalHealthDays', 'MentalHealthDays', 'LastCheckupTime', 'PhysicalActivities', 'SleepHours', 'HadStroke', 'HadArthritis', 'HadDiabetes', 'SmokerStatus', 'ECigaretteUsage', 'RaceEthnicityCategory', 'AgeCategory', 'HeightInMeters', 'WeightInKilograms', 'BMI', 'AlcoholDrinkers', 'HighRiskLastYear']:
        input_components.append(gr.Slider(0, 100, step=1, label=feature))

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

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

# Launch the app
app.launch()