Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pickle | |
import pandas as pd | |
# Load the trained model | |
model_path = "tuned_model.pkl" | |
def load_model(): | |
"""Load the model from the pickle file.""" | |
with open(model_path, "rb") as file: | |
return pickle.load(file) | |
# Prediction function | |
def predict_with_model(*inputs): | |
try: | |
model = load_model() # Load the model dynamically | |
# Create a DataFrame for prediction | |
input_data = pd.DataFrame([inputs], columns=features) | |
# Make prediction | |
prediction = model.predict(input_data) | |
return f"Prediction: {'Risk of Heart Failure' if prediction[0] == 1 else 'No Risk'}" | |
except Exception as e: | |
return f"Error during prediction: {str(e)}" | |
# Features derived from the CSV file | |
features = ["Feature1", "Feature2", "Feature3"] # Replace with actual feature names | |
# Create input sliders | |
input_sliders = [gr.Slider(0, 100, label=feature) for feature in features] | |
# Define Gradio interface | |
iface = gr.Interface( | |
fn=predict_with_model, | |
inputs=input_sliders, | |
outputs="text", | |
title="Heart Failure Prediction App", | |
description="Adjust the sliders to simulate feature values and predict heart failure risk.", | |
) | |
# Launch the app | |
iface.launch() |