Spaces:
Sleeping
Sleeping
Henamen21
commited on
Commit
·
794ba5c
1
Parent(s):
51221a6
Adding files
Browse files- Dockerfile +11 -0
- app.py +101 -0
- requirements.txt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["sepsis", "serve", "--address", "0.0.0.0", "--port", "7860" , "--allow-websocket-origin" , "Henok21-Sepsis-Prediction-in-Docker.hf.space"]
|
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# For app development
|
2 |
+
from typing import Annotated
|
3 |
+
from fastapi import FastAPI, Form, Depends
|
4 |
+
import pandas as pd
|
5 |
+
import uvicorn
|
6 |
+
from pydantic import BaseModel
|
7 |
+
# For data frame
|
8 |
+
import pandas as pd
|
9 |
+
# For loading pipeline
|
10 |
+
import pickle
|
11 |
+
# For controlling warnings
|
12 |
+
import warnings
|
13 |
+
warnings.filterwarnings('ignore')
|
14 |
+
|
15 |
+
# Pieline loading
|
16 |
+
with open("..\\notebook\\pipeline.pkl", "rb") as f:
|
17 |
+
pipe = pickle.load(f)
|
18 |
+
|
19 |
+
app = FastAPI( title = "The classification API for predicting Sepsis positve / negative") # instantiating fastAPI object
|
20 |
+
|
21 |
+
@app.get("/")
|
22 |
+
async def root():
|
23 |
+
return {
|
24 |
+
"Info" : "The classification API for predicting Sepsis positve / Negative"
|
25 |
+
}
|
26 |
+
|
27 |
+
# Class inherits from BaseModel to be used as pydantic model
|
28 |
+
class Sepssis(BaseModel):
|
29 |
+
# Input features
|
30 |
+
plasma_glucose : float
|
31 |
+
Blood_work_result_1 : float
|
32 |
+
Blood_pressure : float
|
33 |
+
Blood_work_result_2 : float
|
34 |
+
Blood_work_result_3 : float
|
35 |
+
Body_mass_index : float
|
36 |
+
Blood_work_result_4 : float
|
37 |
+
Age : int
|
38 |
+
Insurance : int
|
39 |
+
|
40 |
+
@classmethod
|
41 |
+
def as_form(
|
42 |
+
cls,
|
43 |
+
plasma_glucose: float = Form(...), # "..." means the form is required
|
44 |
+
Blood_work_result_1: float = Form(...),
|
45 |
+
Blood_pressure: float = Form(...),
|
46 |
+
Blood_work_result_2: float = Form(...),
|
47 |
+
Blood_work_result_3: float = Form(...),
|
48 |
+
Body_mass_index: float = Form(...),
|
49 |
+
Blood_work_result_4: float = Form(...),
|
50 |
+
Age: float = Form(...),
|
51 |
+
Insurance: float = Form(...)
|
52 |
+
) -> "Sepssis": # Forward reference
|
53 |
+
return cls(
|
54 |
+
plasma_glucose=plasma_glucose,
|
55 |
+
Blood_work_result_1=Blood_work_result_1,
|
56 |
+
Blood_pressure=Blood_pressure,
|
57 |
+
Blood_work_result_2=Blood_work_result_2,
|
58 |
+
Blood_work_result_3=Blood_work_result_3,
|
59 |
+
Body_mass_index=Body_mass_index,
|
60 |
+
Blood_work_result_4=Blood_work_result_4,
|
61 |
+
Age=Age,
|
62 |
+
Insurance=Insurance
|
63 |
+
)
|
64 |
+
|
65 |
+
@app.post("/dataframe/")
|
66 |
+
async def create_dataframe(form_data: Sepssis = Depends(Sepssis.as_form)):
|
67 |
+
try:
|
68 |
+
# Convert the form data to a data frame
|
69 |
+
df = pd.DataFrame(form_data.dict(), index=[0])
|
70 |
+
|
71 |
+
# Predicting...
|
72 |
+
output = pipe.predict_proba(df)
|
73 |
+
|
74 |
+
df["predicted_label"] = output.argmax(axis = -1)
|
75 |
+
mapping = {0: "Sepsis Negative", 1: "Sepsis Positive"}
|
76 |
+
df["predicted_label"] = [mapping[x] for x in df["predicted_label"]]
|
77 |
+
|
78 |
+
# Calculating confidence score
|
79 |
+
confidence_score = output.max(axis= -1)
|
80 |
+
df["confidence_score"] = f"{round( ( confidence_score[0] * 100 ) , 2) }%"
|
81 |
+
|
82 |
+
# Creating a display output
|
83 |
+
msg = "execution went fine"
|
84 |
+
code = 1
|
85 |
+
pred = df.to_dict("records")
|
86 |
+
|
87 |
+
result = { "Execution Message " : msg , "Execution Code " : code , "Prediction" : pred }
|
88 |
+
|
89 |
+
except Exception as e:
|
90 |
+
# If there is an error...
|
91 |
+
msg = "execution went wrong"
|
92 |
+
code = 0
|
93 |
+
pred = None
|
94 |
+
|
95 |
+
result = { "Error" : str(e) , "Execution Message " : msg , "Execution Code " : code , "Prediction" : pred }
|
96 |
+
|
97 |
+
return result
|
98 |
+
|
99 |
+
# Running automaticaly when there is a change
|
100 |
+
if __name__ == "__main__":
|
101 |
+
uvicorn.run("app:app" , reload = True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
pandas
|
3 |
+
pickle
|