import gradio as gr import joblib import pandas as pd import numpy as np # Load the pre-trained model model = joblib.load("tuned_model.pkl") # Load the features used during training features = pd.read_csv("features_used_in_model.csv")["Feature"].tolist() # Prediction function def predict_heart_failure(input_data): try: # Convert input into a DataFrame input_df = pd.DataFrame([input_data], columns=features) # Predict probability for heart failure (class 1) probability = model.predict_proba(input_df)[:, 1][0] # Predict class (0 or 1) prediction = "At Risk of Heart Failure" if probability >= 0.3 else "No Risk Detected" return { "Prediction": prediction, "Risk Probability": round(probability, 4) } except Exception as e: return {"error": str(e)} # Gradio Interface inputs = [] for feature in features: inputs.append(gr.inputs.Textbox(label=feature, placeholder=f"Enter value for {feature}")) output = gr.outputs.JSON(label="Heart Failure Prediction") interface = gr.Interface( fn=predict_heart_failure, inputs=inputs, outputs=output, title="Heart Failure Prediction Model", description=( "Predicts the likelihood of heart failure based on health features. " "Enter the values for the features below and receive the prediction." ) ) # Launch the interface for local testing or Hugging Face Spaces deployment if __name__ == "__main__": interface.launch()