Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- Dockerfile +23 -0
- app.py +106 -0
- requirements.txt +14 -0
- test_data.csv +91 -0
- xgboost-model.pkl +3 -0
Dockerfile
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# pull python base image
|
2 |
+
FROM python:3.10
|
3 |
+
|
4 |
+
COPY requirements.txt requirements.txt
|
5 |
+
|
6 |
+
# update pip
|
7 |
+
RUN pip install --upgrade pip
|
8 |
+
|
9 |
+
# install dependencies
|
10 |
+
RUN pip install -r requirements.txt
|
11 |
+
|
12 |
+
RUN useradd -m -u 1000 myuser
|
13 |
+
|
14 |
+
USER myuser
|
15 |
+
|
16 |
+
# copy application files
|
17 |
+
COPY --chown=myuser . .
|
18 |
+
|
19 |
+
# expose port for application
|
20 |
+
EXPOSE 8001
|
21 |
+
|
22 |
+
# start fastapi application
|
23 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request, Response
|
2 |
+
|
3 |
+
import gradio
|
4 |
+
import joblib
|
5 |
+
from xgboost import XGBClassifier
|
6 |
+
import pandas as pd
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
from sklearn.metrics import f1_score, precision_score, recall_score
|
10 |
+
import prometheus_client as prom
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
username = "runaksh"
|
15 |
+
repo_name = "Patientsurvival-model"
|
16 |
+
repo_path = username+ '/' + repo_name
|
17 |
+
sentiment_model = pipeline(model= repo_path)
|
18 |
+
|
19 |
+
import pandas as pd
|
20 |
+
|
21 |
+
test_data = pd.read_csv("test_data.csv")
|
22 |
+
|
23 |
+
f1_metric = prom.Gauge('sentiment_f1_score', 'F1 score for random 100 test samples')
|
24 |
+
precision_metric = prom.Gauge('sentiment_precision_score', 'Precision score for random 100 test samples')
|
25 |
+
recall_metric = prom.Gauge('sentiment_recall_score', 'Recall score for random 100 test samples')
|
26 |
+
|
27 |
+
|
28 |
+
# Function for response generation
|
29 |
+
# Load your trained model
|
30 |
+
xgb_model_loaded = joblib.load("xgboost-model.pkl")
|
31 |
+
|
32 |
+
def bol_to_int(bol):
|
33 |
+
if bol==True:
|
34 |
+
return 1
|
35 |
+
else:
|
36 |
+
return 0
|
37 |
+
|
38 |
+
# Function for prediction
|
39 |
+
def predict_death_event(feature1, feature2, feature3,feature4, feature5, feature6, feature7, feature8, feature9, feature10, feature11, feature12):
|
40 |
+
data = {'age' : [feature1], 'anaemia' : [bol_to_int(feature2)],
|
41 |
+
'creatinine_phosphokinase' : [feature3],
|
42 |
+
'diabetes' : [bol_to_int(feature4)],
|
43 |
+
'ejection_fraction' : [feature5],
|
44 |
+
'high_blood_pressure' : [bol_to_int(feature6)],
|
45 |
+
'platelets' : [feature7],
|
46 |
+
'serum_creatinine' : [feature8],
|
47 |
+
'serum_sodium' : [feature9],
|
48 |
+
'sex' : [bol_to_int(feature10)],
|
49 |
+
'smoking' : [bol_to_int(feature11)],
|
50 |
+
'time' : [feature12]}
|
51 |
+
df = pd.DataFrame(data)
|
52 |
+
y_pred = xgb_model_loaded.predict(df)[0]
|
53 |
+
return y_pred
|
54 |
+
|
55 |
+
# Function for updating metrics
|
56 |
+
def update_metrics():
|
57 |
+
test = test_data.sample(100)
|
58 |
+
test_text = test['Text'].values
|
59 |
+
test_pred = sentiment_model(list(test_text))
|
60 |
+
pred_labels = [int(pred['label'].split("_")[1]) for pred in test_pred]
|
61 |
+
f1 = f1_score(test['labels'], pred_labels).round(3)
|
62 |
+
precision = precision_score(test['labels'], pred_labels).round(3)
|
63 |
+
recall = recall_score(test['labels'], pred_labels).round(3)
|
64 |
+
|
65 |
+
f1_metric.set(f1)
|
66 |
+
precision_metric.set(precision)
|
67 |
+
recall_metric.set(recall)
|
68 |
+
|
69 |
+
|
70 |
+
@app.get("/metrics")
|
71 |
+
async def get_metrics():
|
72 |
+
update_metrics()
|
73 |
+
return Response(media_type="text/plain", content= prom.generate_latest())
|
74 |
+
|
75 |
+
|
76 |
+
# Gradio interface to generate UI link
|
77 |
+
# Gradio interface to generate UI link
|
78 |
+
title = "Patient Survival Prediction"
|
79 |
+
description = "Predict survival of patient with heart failure, given their clinical record"
|
80 |
+
|
81 |
+
iface = gradio.Interface(fn = predict_death_event,
|
82 |
+
inputs=[
|
83 |
+
gradio.components.Slider(30, 100, step=1, label= 'age'),
|
84 |
+
gradio.components.Radio(["0","1"], label= 'anaemia'),
|
85 |
+
gradio.components.Slider(1, 10000, step=1, label= 'creatinine_phosphokinase'),
|
86 |
+
gradio.components.Radio(["0","1"], label= 'diabetes'),
|
87 |
+
gradio.components.Slider(1, 100, step=1, label= 'ejection_fraction'),
|
88 |
+
gradio.components.Radio(["0","1"], label= 'high_blood_pressure'),
|
89 |
+
gradio.components.Number(label= 'platelets'),
|
90 |
+
gradio.components.Slider(0.1, 10.0, step=0.1, label= 'serum_creatinine'),
|
91 |
+
gradio.components.Slider(100, 150, step=1, label= 'serum_sodium'),
|
92 |
+
gradio.components.Radio(["0","1"], label= 'sex'),
|
93 |
+
gradio.components.Radio(["0","1"], label= 'smoking'),
|
94 |
+
gradio.components.Slider(1, 300, step=1, label= 'time')],
|
95 |
+
outputs = [gradio.components.Textbox (label ='DeathEvent')],
|
96 |
+
title = title,
|
97 |
+
description = description)
|
98 |
+
|
99 |
+
app = gradio.mount_gradio_app(app, iface, path="/")
|
100 |
+
|
101 |
+
#iface.launch(server_name = "0.0.0.0", server_port = 8001) # Ref. for parameters: https://www.gradio.app/docs/interface
|
102 |
+
|
103 |
+
if __name__ == "__main__":
|
104 |
+
# Use this for debugging purposes only
|
105 |
+
import uvicorn
|
106 |
+
uvicorn.run(app, host="0.0.0.0", port=8001)
|
requirements.txt
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
gradio
|
3 |
+
joblib
|
4 |
+
numpy
|
5 |
+
pandas
|
6 |
+
xgboost
|
7 |
+
|
8 |
+
uvicorn
|
9 |
+
fastapi
|
10 |
+
python-multipart
|
11 |
+
pydantic
|
12 |
+
|
13 |
+
scikit-learn
|
14 |
+
prometheus-client
|
test_data.csv
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
age,anaemia,creatinine_phosphokinase,diabetes,ejection_fraction,high_blood_pressure,platelets,serum_creatinine,serum_sodium,sex,smoking,time,DEATH_EVENT
|
2 |
+
70,0,161,0,25,0,244000,1.2,142,0,0,66,1
|
3 |
+
53,1,270,1,35,0,227000,2.15,145,1,0,105,0
|
4 |
+
60,1,588,1,60,0,194000,1.1,142,0,0,33,1
|
5 |
+
53,1,1280.25,0,60,1,249000,0.7,138,1,1,106,0
|
6 |
+
65,0,118,0,50,0,194000,1.1,145,1,1,200,0
|
7 |
+
70,0,93,0,35,0,185000,1.1,134,1,1,208,0
|
8 |
+
50,0,482,1,30,0,329000,0.9,132,0,0,109,0
|
9 |
+
60,1,96,1,60,1,271000,0.7,136,0,0,94,0
|
10 |
+
65,1,68,1,60,1,304000,0.8,140,1,0,79,0
|
11 |
+
60,0,68,0,20,0,119000,2.15,127,1,1,64,1
|
12 |
+
90,1,47,0,40,1,204000,2.1,132,1,1,8,1
|
13 |
+
90,1,337,0,38,0,390000,0.9,144,0,0,256,0
|
14 |
+
65,1,59,1,60,0,172000,0.9,137,0,0,107,0
|
15 |
+
60,0,1280.25,1,25,0,365000,2.1,144,0,0,172,1
|
16 |
+
70,0,69,0,40,0,293000,1.7,136,0,0,75,0
|
17 |
+
65,1,335,0,35,1,235000,0.8,136,0,0,120,0
|
18 |
+
60,0,253,0,35,0,279000,1.7,140,1,0,250,0
|
19 |
+
61,1,84,0,40,1,229000,0.9,141,0,0,110,0
|
20 |
+
75,1,81,0,38,1,368000,2.15,131,1,1,10,1
|
21 |
+
63,1,103,1,35,0,179000,0.9,136,1,1,270,0
|
22 |
+
40,0,244,0,45,1,275000,0.9,140,0,0,174,0
|
23 |
+
70,1,125,0,25,1,237000,1,140,0,0,15,1
|
24 |
+
49,1,69,0,50,0,132000,1,140,0,0,147,0
|
25 |
+
75,0,582,1,30,1,263358.03,1.83,134,0,0,23,1
|
26 |
+
73,1,231,1,30,0,160000,1.18,142,1,1,180,0
|
27 |
+
65,0,157,0,65,0,263358.03,1.5,138,0,0,10,1
|
28 |
+
60,1,315,1,60,0,440000,1.1,131,1,1,10,1
|
29 |
+
66,1,72,0,40,1,242000,1.2,134,1,0,121,0
|
30 |
+
53,0,63,1,60,0,368000,0.8,135,1,0,22,0
|
31 |
+
45,0,1280.25,0,38,0,140000,1.4,140,1,1,280,0
|
32 |
+
42,0,102,1,40,0,237000,1.2,140,1,0,74,0
|
33 |
+
50,0,115,0,45,1,184000,0.9,134,1,1,118,0
|
34 |
+
55,0,748,0,45,0,263000,1.3,137,1,0,88,0
|
35 |
+
70,0,97,0,60,1,220000,0.9,138,1,0,186,0
|
36 |
+
53,1,91,0,20,1,418000,1.4,139,0,0,43,1
|
37 |
+
60,0,582,0,40,0,217000,2.15,134,1,0,96,1
|
38 |
+
60,1,154,0,25,0,210000,1.7,135,1,0,82,1
|
39 |
+
44,0,582,1,30,1,263358.03,1.6,130,1,1,244,0
|
40 |
+
55,1,180,0,45,0,263358.03,1.18,137,1,1,211,0
|
41 |
+
63,1,122,1,60,0,267000,1.2,145,1,0,147,0
|
42 |
+
58,1,57,0,25,0,189000,1.3,132,1,1,205,0
|
43 |
+
58,1,200,1,60,0,300000,0.8,137,0,0,104,0
|
44 |
+
64,0,143,0,25,0,246000,2.15,135,1,0,214,0
|
45 |
+
85,0,129,0,60,0,306000,1.2,132,1,1,90,1
|
46 |
+
78,1,64,0,40,0,277000,0.7,137,1,1,187,0
|
47 |
+
52,0,190,1,38,0,382000,1,140,1,1,258,0
|
48 |
+
70,0,1202,0,50,1,358000,0.9,141,0,0,196,0
|
49 |
+
80,1,123,0,35,1,388000,2.15,133,1,1,10,1
|
50 |
+
95,1,112,0,40,1,196000,1,138,0,0,24,1
|
51 |
+
63,1,1280.25,0,45,0,76000,0.7,137,1,0,186,0
|
52 |
+
67,0,213,0,38,0,215000,1.2,133,0,0,245,0
|
53 |
+
58,1,60,0,38,0,153000,2.15,134,1,0,26,1
|
54 |
+
60,0,235,1,38,0,329000,2.15,142,0,0,30,1
|
55 |
+
59,1,129,0,45,1,362000,1.1,139,1,1,121,0
|
56 |
+
78,0,224,0,50,0,440000,1.4,138,1,1,192,0
|
57 |
+
70,0,571,1,45,1,185000,1.2,139,1,1,33,1
|
58 |
+
65,1,113,1,60,1,203000,0.9,140,0,0,94,0
|
59 |
+
50,0,582,1,38,0,310000,1.9,135,1,1,35,1
|
60 |
+
55,0,1199,0,20,0,263358.03,1.83,134,1,1,241,1
|
61 |
+
62,0,281,1,35,0,221000,1,136,0,0,108,0
|
62 |
+
45,1,66,1,25,0,233000,0.8,135,1,0,230,0
|
63 |
+
46,1,291,0,35,0,348000,0.9,140,0,0,109,0
|
64 |
+
64,0,1280.25,0,60,0,242000,1,137,1,0,113,0
|
65 |
+
55,1,1280.25,0,35,1,141000,1,140,1,0,206,0
|
66 |
+
95,1,371,0,30,0,440000,2,132,1,0,50,1
|
67 |
+
65,1,305,0,25,0,298000,1.1,141,1,0,87,0
|
68 |
+
50,1,121,1,40,0,260000,0.7,130,1,0,175,0
|
69 |
+
60,1,260,1,38,0,255000,2.15,132,0,1,45,1
|
70 |
+
65,0,198,1,35,1,281000,0.9,137,1,1,146,0
|
71 |
+
80,0,582,1,35,0,350000,2.1,134,1,0,174,0
|
72 |
+
55,0,835,0,40,0,279000,0.7,140,1,1,147,0
|
73 |
+
50,0,196,0,45,0,395000,1.6,136,1,1,285,0
|
74 |
+
50,0,582,0,50,0,153000,0.6,134,0,0,172,1
|
75 |
+
62,0,30,1,60,1,244000,0.9,139,1,0,117,0
|
76 |
+
51,0,1280.25,0,25,1,271000,0.9,130,1,0,38,1
|
77 |
+
69,0,1280.25,0,40,0,105000,1,135,1,1,147,0
|
78 |
+
70,1,143,0,60,0,351000,1.3,137,0,0,90,1
|
79 |
+
65,1,135,0,35,1,290000,0.8,134,1,0,194,0
|
80 |
+
50,1,582,1,20,1,279000,1,134,0,0,186,0
|
81 |
+
53,0,207,1,40,0,223000,1.2,130,0,0,214,0
|
82 |
+
60,1,156,1,25,1,318000,1.2,137,0,0,85,0
|
83 |
+
45,0,1280.25,1,60,0,440000,0.8,138,0,0,278,0
|
84 |
+
85,0,212,0,38,0,186000,0.9,136,1,0,187,0
|
85 |
+
55,0,60,0,35,0,228000,1.2,135,1,1,90,0
|
86 |
+
47,0,582,0,25,0,130000,0.8,134,1,0,201,0
|
87 |
+
72,1,328,0,30,1,440000,1.7,138,0,1,88,1
|
88 |
+
70,1,171,0,60,1,176000,1.1,145,1,1,146,0
|
89 |
+
75,1,203,1,38,1,283000,0.6,131,1,1,74,0
|
90 |
+
50,1,111,0,20,0,210000,1.9,137,1,0,7,1
|
91 |
+
73,0,582,0,20,0,263358.03,1.83,134,1,0,198,1
|
xgboost-model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:14c0d2599174830ec5a9052af2a416a00ef52c7e61b44844bf32c833cdb6de7f
|
3 |
+
size 189168
|