Spaces:
Sleeping
Sleeping
hackerbyhobby
commited on
fixed version
Browse files- app.py +15 -19
- trained_model.pkl +0 -3
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import joblib
|
3 |
import pandas as pd
|
4 |
-
import numpy as np
|
5 |
|
6 |
# Load the pre-trained model
|
7 |
model = joblib.load("tuned_model.pkl")
|
@@ -10,10 +9,13 @@ model = joblib.load("tuned_model.pkl")
|
|
10 |
features = pd.read_csv("features_used_in_model.csv")["Feature"].tolist()
|
11 |
|
12 |
# Prediction function
|
13 |
-
def predict_heart_failure(
|
14 |
try:
|
15 |
-
# Convert
|
16 |
-
|
|
|
|
|
|
|
17 |
|
18 |
# Predict probability for heart failure (class 1)
|
19 |
probability = model.predict_proba(input_df)[:, 1][0]
|
@@ -22,25 +24,21 @@ def predict_heart_failure(input_data):
|
|
22 |
prediction = "At Risk of Heart Failure" if probability >= 0.3 else "No Risk Detected"
|
23 |
|
24 |
# Return prediction, probability, and user inputs
|
25 |
-
return
|
26 |
-
"Prediction": prediction,
|
27 |
-
"Risk Probability": round(probability, 4),
|
28 |
-
"User Inputs": input_data
|
29 |
-
}
|
30 |
except Exception as e:
|
31 |
-
return {"error": str(e)}
|
32 |
|
33 |
# Gradio Interface
|
34 |
-
inputs = []
|
35 |
-
for feature in features:
|
36 |
-
inputs.append(gr.inputs.Textbox(label=feature, placeholder=f"Enter value for {feature}"))
|
37 |
-
|
38 |
-
output = gr.outputs.JSON(label="Heart Failure Prediction & Inputs")
|
39 |
|
40 |
interface = gr.Interface(
|
41 |
fn=predict_heart_failure,
|
42 |
inputs=inputs,
|
43 |
-
outputs=
|
|
|
|
|
|
|
|
|
44 |
title="Heart Failure Prediction Model",
|
45 |
description=(
|
46 |
"Predicts the likelihood of heart failure based on health features. "
|
@@ -48,6 +46,4 @@ interface = gr.Interface(
|
|
48 |
)
|
49 |
)
|
50 |
|
51 |
-
# Launch the interface for local testing or
|
52 |
-
if __name__ == "__main__":
|
53 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import joblib
|
3 |
import pandas as pd
|
|
|
4 |
|
5 |
# Load the pre-trained model
|
6 |
model = joblib.load("tuned_model.pkl")
|
|
|
9 |
features = pd.read_csv("features_used_in_model.csv")["Feature"].tolist()
|
10 |
|
11 |
# Prediction function
|
12 |
+
def predict_heart_failure(*input_values):
|
13 |
try:
|
14 |
+
# Convert inputs into a dictionary
|
15 |
+
input_data = dict(zip(features, input_values))
|
16 |
+
|
17 |
+
# Convert input dictionary to DataFrame
|
18 |
+
input_df = pd.DataFrame([input_data])
|
19 |
|
20 |
# Predict probability for heart failure (class 1)
|
21 |
probability = model.predict_proba(input_df)[:, 1][0]
|
|
|
24 |
prediction = "At Risk of Heart Failure" if probability >= 0.3 else "No Risk Detected"
|
25 |
|
26 |
# Return prediction, probability, and user inputs
|
27 |
+
return prediction, round(probability, 4), input_data
|
|
|
|
|
|
|
|
|
28 |
except Exception as e:
|
29 |
+
return "Error", 0, {"error": str(e)}
|
30 |
|
31 |
# Gradio Interface
|
32 |
+
inputs = [gr.Textbox(label=feature, placeholder=f"Enter value for {feature}") for feature in features]
|
|
|
|
|
|
|
|
|
33 |
|
34 |
interface = gr.Interface(
|
35 |
fn=predict_heart_failure,
|
36 |
inputs=inputs,
|
37 |
+
outputs=[
|
38 |
+
gr.Text(label="Prediction"),
|
39 |
+
gr.Number(label="Risk Probability"),
|
40 |
+
gr.JSON(label="User Inputs")
|
41 |
+
],
|
42 |
title="Heart Failure Prediction Model",
|
43 |
description=(
|
44 |
"Predicts the likelihood of heart failure based on health features. "
|
|
|
46 |
)
|
47 |
)
|
48 |
|
49 |
+
# Launch the interface for local testing or
|
|
|
|
trained_model.pkl
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:671e17b4726b0253e73d9a3b6af013302cdd2c32086e9b67357d44f145af01cd
|
3 |
-
size 7804185
|
|
|
|
|
|
|
|