Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import numpy as np
|
4 |
+
from fastapi import FastAPI,Response
|
5 |
+
from sklearn.metrics import accuracy_score, f1_score
|
6 |
+
import prometheus_client as prom
|
7 |
+
import pandas as pd
|
8 |
+
# from transformers import pipeline
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
#model
|
13 |
+
save_file_name="xgboost-model.pkl"
|
14 |
+
loaded_model = pickle.load(open(save_file_name, 'rb'))
|
15 |
+
|
16 |
+
app=FastAPI()
|
17 |
+
|
18 |
+
# username="ashwml"
|
19 |
+
# repo_name="prometheus_model"
|
20 |
+
# model=username+'/'+repo_name
|
21 |
+
test_data=pd.read_csv("test.csv")
|
22 |
+
|
23 |
+
|
24 |
+
f1_metric = prom.Gauge('death_f1_score', 'F1 score for test samples')
|
25 |
+
|
26 |
+
# Function for updating metrics
|
27 |
+
def update_metrics():
|
28 |
+
test = test_data.sample(20)
|
29 |
+
X = test.iloc[:, :-1].values
|
30 |
+
y = test['DEATH_EVENT'].values
|
31 |
+
|
32 |
+
# test_text = test['Text'].values
|
33 |
+
test_pred = loaded_model.predict(X)
|
34 |
+
#pred_labels = [int(pred['label'].split("_")[1]) for pred in test_pred]
|
35 |
+
|
36 |
+
f1 = f1_score( y , test_pred).round(3)
|
37 |
+
|
38 |
+
#f1 = f1_score(test['labels'], pred_labels).round(3)
|
39 |
+
|
40 |
+
f1_metric.set(f1)
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
def predict_death_event(age, anaemia, creatinine_phosphokinase ,diabetes ,ejection_fraction, high_blood_pressure ,platelets ,serum_creatinine, serum_sodium, sex ,smoking ,time):
|
45 |
+
input=[[age, anaemia, creatinine_phosphokinase ,diabetes ,ejection_fraction, high_blood_pressure ,platelets ,serum_creatinine, serum_sodium, sex ,smoking ,time]]
|
46 |
+
result=loaded_model.predict(input)
|
47 |
+
|
48 |
+
if result[0]==1:
|
49 |
+
return 'Positive'
|
50 |
+
else:
|
51 |
+
return 'Negative'
|
52 |
+
return result
|
53 |
+
|
54 |
+
|
55 |
+
@app.get("/metrics")
|
56 |
+
async def get_metrics():
|
57 |
+
update_metrics()
|
58 |
+
return Response(media_type="text/plain", content= prom.generate_latest())
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
title = "Patient Survival Prediction"
|
63 |
+
description = "Predict survival of patient with heart failure, given their clinical record"
|
64 |
+
|
65 |
+
out_response = gr.components.Textbox(type="text", label='Death_event')
|
66 |
+
|
67 |
+
iface = gr.Interface(fn=predict_death_event,
|
68 |
+
inputs=[
|
69 |
+
gr.Slider(18, 100, value=20, label="Age"),
|
70 |
+
gr.Slider(0, 1, value=1, label="anaemia"),
|
71 |
+
gr.Slider(100, 2000, value=20, label="creatinine_phosphokinase"),
|
72 |
+
gr.Slider(0, 1, value=1, label="diabetes"),
|
73 |
+
gr.Slider(18, 100, value=20, label="ejection_fraction"),
|
74 |
+
gr.Slider(0, 1, value=1, label="high_blood_pressure"),
|
75 |
+
gr.Slider(18, 400000, value=20, label="platelets"),
|
76 |
+
gr.Slider(1, 10, value=20, label="serum_creatinine"),
|
77 |
+
gr.Slider(100, 200, value=20, label="serum_sodium"),
|
78 |
+
gr.Slider(0, 1, value=1, label="sex"),
|
79 |
+
gr.Slider(0, 1, value=1, label="smoking"),
|
80 |
+
gr.Slider(1, 10, value=20, label="time"),
|
81 |
+
],
|
82 |
+
outputs = [out_response])
|
83 |
+
|
84 |
+
|
85 |
+
app = gr.mount_gradio_app(app, iface, path="/")
|
86 |
+
|
87 |
+
# iface.launch(server_name = "0.0.0.0", server_port = 8001)
|
88 |
+
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
# Use this for debugging purposes only
|
92 |
+
import uvicorn
|
93 |
+
uvicorn.run(app, host="0.0.0.0", port=8001)
|