File size: 1,677 Bytes
55f664e
00b26e4
6669778
00b26e4
6669778
3573793
e588032
6669778
 
e588032
 
32e56b3
6669778
32e56b3
 
 
 
 
6669778
 
 
 
 
 
 
d384299
32e56b3
6669778
32e56b3
6669778
 
32e56b3
5420a19
e588032
6669778
00b26e4
32e56b3
 
 
 
 
6669778
 
 
 
 
bae8052
6dfb9dc
28d8ff1
 
03b8948
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

# 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_values):
    try:
        # Convert inputs into a dictionary
        input_data = dict(zip(features, input_values))
        
        # Convert input dictionary to DataFrame
        input_df = pd.DataFrame([input_data])
        
        # 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, probability, and user inputs
        return prediction, round(probability, 4), input_data
    except Exception as e:
        return "Error", 0, {"error": str(e)}

# Gradio Interface
inputs = [gr.Textbox(label=feature, placeholder=f"Enter value for {feature}") for feature in features]

interface = gr.Interface(
    fn=predict_heart_failure,
    inputs=inputs,
    outputs=[
        gr.Text(label="Prediction"),
        gr.Number(label="Risk Probability"),
        gr.JSON(label="User Inputs")
    ],
    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(share=True)