File size: 1,278 Bytes
55f664e
00b26e4
55f664e
00b26e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bae8052
6dfb9dc
bae8052
00b26e4
 
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
import gradio as gr
import joblib
import json
import numpy as np

# Load the trained model
model = joblib.load('trained_model.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()