File size: 1,543 Bytes
55f664e
00b26e4
6669778
 
00b26e4
6669778
 
e588032
6669778
 
e588032
 
6669778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5420a19
e588032
6669778
00b26e4
6669778
 
 
 
 
 
bae8052
6dfb9dc
6669778
00b26e4
e588032
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()