Spaces:
Sleeping
Sleeping
import gradio as gr | |
import joblib | |
import json | |
import numpy as np | |
# Load the trained model | |
model = joblib.load('trained_model_selected.pkl') | |
# Load feature names | |
with open('selected_features.json', 'r') as f: | |
feature_names = json.load(f) | |
# Define prediction function | |
def predict_heart_failure(*args): | |
""" | |
Predict heart failure likelihood based on input features. | |
Args: | |
*args: Input values for each feature. | |
Returns: | |
str: Prediction result. | |
str: Prediction probability as a percentage. | |
""" | |
input_data = np.array(args).reshape(1, -1) | |
prediction = model.predict(input_data) | |
probability = model.predict_proba(input_data) | |
result = "Likely" if prediction[0] == 1 else "Unlikely" | |
return result, f"{probability[0][1] * 100:.2f}%" | |
# Create Gradio interface | |
inputs = [gr.inputs.Number(label=feature) for feature in feature_names] | |
outputs = [ | |
gr.outputs.Textbox(label="Prediction"), | |
gr.outputs.Textbox(label="Probability (%)") | |
] | |
app = gr.Interface( | |
fn=predict_heart_failure, | |
inputs=inputs, | |
outputs=outputs, | |
title="Heart Failure Prediction", | |
description="Enter the values for the features to predict the likelihood of heart failure." | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
app.launch() | |