Spaces:
Sleeping
Sleeping
""" | |
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() | |