Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import joblib | |
| import numpy as np | |
| import pandas as pd | |
| # Load the trained model | |
| model_path = "trained_model.pkl" | |
| def load_model(path): | |
| try: | |
| return joblib.load(path) | |
| except Exception as e: | |
| raise ValueError(f"Error loading model: {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: | |
| model = load_model(model_path) | |
| # Combine inputs into a DataFrame for prediction | |
| 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("# Heart Disease Prediction App") | |
| gr.Markdown("### Provide input values for the features below and get a prediction.") | |
| input_sliders = [] | |
| for feature in ['State', 'Sex', 'GeneralHealth', 'PhysicalHealthDays', 'MentalHealthDays', 'LastCheckupTime', 'PhysicalActivities', 'SleepHours', 'HadStroke', 'HadArthritis', 'HadDiabetes', 'SmokerStatus', 'ECigaretteUsage', 'RaceEthnicityCategory', 'AgeCategory', 'HeightInMeters', 'WeightInKilograms', 'BMI', 'AlcoholDrinkers', 'HighRiskLastYear']: | |
| input_sliders.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_sliders, | |
| outputs=output, | |
| ) | |
| # Launch the app | |
| app.launch() | |