Spaces:
Sleeping
Sleeping
QuophyDzifa
commited on
Commit
•
a58630e
1
Parent(s):
cadd0e7
Upload app files
Browse files- .dockerignore +8 -0
- .gitignore +2 -0
- Dockerfile +11 -0
- requirements.txt +50 -0
- src/ML/.gitkeep +0 -0
- src/ML/ML_Model.pkl +3 -0
- src/__pycache__/main.cpython-310.pyc +0 -0
- src/__pycache__/main.cpython-311.pyc +0 -0
- src/main.py +109 -0
.dockerignore
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
venv/
|
2 |
+
__pycache__/
|
3 |
+
*.venv
|
4 |
+
*.venv.*
|
5 |
+
venv.*
|
6 |
+
*.pyc
|
7 |
+
.git
|
8 |
+
.vscode
|
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
.venv*/
|
2 |
+
venv*/
|
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
COPY ./requirements.txt /app
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
|
8 |
+
|
9 |
+
COPY ./src /app/src
|
10 |
+
|
11 |
+
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
|
requirements.txt
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
annotated-types==0.6.0
|
2 |
+
anyio==3.7.1
|
3 |
+
black==23.9.1
|
4 |
+
certifi==2023.7.22
|
5 |
+
click==8.1.7
|
6 |
+
colorama==0.4.6
|
7 |
+
dnspython==2.4.2
|
8 |
+
email-validator==2.0.0.post2
|
9 |
+
fastapi==0.103.2
|
10 |
+
h11==0.14.0
|
11 |
+
httpcore==0.18.0
|
12 |
+
httptools==0.6.0
|
13 |
+
httpx==0.25.0
|
14 |
+
idna==3.4
|
15 |
+
imbalanced-learn==0.11.0
|
16 |
+
imblearn==0.0
|
17 |
+
itsdangerous==2.1.2
|
18 |
+
Jinja2==3.1.2
|
19 |
+
joblib==1.3.2
|
20 |
+
MarkupSafe==2.1.3
|
21 |
+
mypy-extensions==1.0.0
|
22 |
+
numpy==1.26.0
|
23 |
+
orjson==3.9.7
|
24 |
+
packaging==23.2
|
25 |
+
pandas==2.1.1
|
26 |
+
pathspec==0.11.2
|
27 |
+
platformdirs==3.11.0
|
28 |
+
pydantic==2.4.2
|
29 |
+
pydantic-extra-types==2.1.0
|
30 |
+
pydantic-settings==2.0.3
|
31 |
+
pydantic_core==2.10.1
|
32 |
+
python-dateutil==2.8.2
|
33 |
+
python-dotenv==1.0.0
|
34 |
+
python-multipart==0.0.6
|
35 |
+
pytz==2023.3.post1
|
36 |
+
PyYAML==6.0.1
|
37 |
+
scikit-learn==1.2.2
|
38 |
+
scipy==1.11.3
|
39 |
+
six==1.16.0
|
40 |
+
sniffio==1.3.0
|
41 |
+
starlette==0.27.0
|
42 |
+
threadpoolctl==3.2.0
|
43 |
+
typing_extensions==4.8.0
|
44 |
+
tzdata==2023.3
|
45 |
+
ujson==5.8.0
|
46 |
+
uvicorn==0.23.2
|
47 |
+
watchfiles==0.20.0
|
48 |
+
websockets==11.0.3
|
49 |
+
uvicorn
|
50 |
+
tabulate
|
src/ML/.gitkeep
ADDED
File without changes
|
src/ML/ML_Model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fb78be0c3676764b43302cced034af8dad919d64949bf02501ed4b2baf16cefd
|
3 |
+
size 3653916
|
src/__pycache__/main.cpython-310.pyc
ADDED
Binary file (2.3 kB). View file
|
|
src/__pycache__/main.cpython-311.pyc
ADDED
Binary file (3.94 kB). View file
|
|
src/main.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# Importations
|
3 |
+
|
4 |
+
from typing import Union
|
5 |
+
from fastapi import FastAPI
|
6 |
+
import pickle
|
7 |
+
from pydantic import BaseModel
|
8 |
+
import pandas as pd
|
9 |
+
import os
|
10 |
+
import uvicorn
|
11 |
+
from fastapi import HTTPException, status
|
12 |
+
from sklearn.preprocessing import StandardScaler
|
13 |
+
from sklearn.preprocessing import LabelEncoder
|
14 |
+
|
15 |
+
# Setup Section
|
16 |
+
|
17 |
+
# Create FastAPI instance
|
18 |
+
app = FastAPI(title="Sepsis Prediction API",
|
19 |
+
description="API for Predicting Sespsis ")
|
20 |
+
# A function to load machine Learning components to re-use
|
21 |
+
|
22 |
+
|
23 |
+
def Ml_loading_components(fp):
|
24 |
+
with open(fp, "rb") as f:
|
25 |
+
object = pickle.load(f)
|
26 |
+
return (object)
|
27 |
+
|
28 |
+
|
29 |
+
# Loading the machine learning components
|
30 |
+
DIRPATH = os.path.dirname(os.path.realpath(__file__))
|
31 |
+
ml_core_fp = os.path.join(DIRPATH, "ML", "ML_Model.pkl")
|
32 |
+
ml_components_dict = Ml_loading_components(fp=ml_core_fp)
|
33 |
+
|
34 |
+
|
35 |
+
# Defining the variables for each component
|
36 |
+
label_encoder = ml_components_dict['label_encoder'] # The label encoder
|
37 |
+
# Loaded scaler component
|
38 |
+
scaler = ml_components_dict['scaler']
|
39 |
+
# Loaded model
|
40 |
+
model = ml_components_dict['model']
|
41 |
+
# Defining our input variables
|
42 |
+
|
43 |
+
|
44 |
+
class InputData(BaseModel):
|
45 |
+
PRG: int
|
46 |
+
PL: int
|
47 |
+
BP: int
|
48 |
+
SK: int
|
49 |
+
TS: int
|
50 |
+
BMI: float
|
51 |
+
BD2: float
|
52 |
+
Age: int
|
53 |
+
|
54 |
+
|
55 |
+
"""
|
56 |
+
* PRG: Plasma glucose
|
57 |
+
|
58 |
+
* PL: Blood Work Result-1 (mu U/ml)
|
59 |
+
|
60 |
+
* PR: Blood Pressure (mmHg)
|
61 |
+
|
62 |
+
* SK: Blood Work Result-2(mm)
|
63 |
+
|
64 |
+
* TS: Blood Work Result-3 (muU/ml)
|
65 |
+
|
66 |
+
* M11: Body mass index (weight in kg/(height in m)^2
|
67 |
+
|
68 |
+
* BD2: Blood Work Result-4 (mu U/ml)
|
69 |
+
|
70 |
+
* Age: patients age(years)
|
71 |
+
|
72 |
+
"""
|
73 |
+
# Index route
|
74 |
+
|
75 |
+
|
76 |
+
@app.get("/")
|
77 |
+
def index():
|
78 |
+
return {'message': 'Hello, Welcome to My Sepsis Prediction FastAPI'}
|
79 |
+
|
80 |
+
|
81 |
+
# Create prediction endpoint
|
82 |
+
@app.post("/predict")
|
83 |
+
def predict(df: InputData):
|
84 |
+
|
85 |
+
# Prepare the feature and structure them like in the notebook
|
86 |
+
df = pd.DataFrame([df.dict().values()], columns=df.dict().keys())
|
87 |
+
|
88 |
+
print(f"[Info] The inputed dataframe is : {df.to_markdown()}")
|
89 |
+
age = df['Age']
|
90 |
+
print(age)
|
91 |
+
# Scaling the inputs
|
92 |
+
df_scaled = scaler.transform(df)
|
93 |
+
|
94 |
+
# Prediction
|
95 |
+
raw_prediction = model.predict(df_scaled)
|
96 |
+
|
97 |
+
if raw_prediction == 0:
|
98 |
+
raise HTTPException(status_code=status.HTTP_200_OK,
|
99 |
+
detail="The patient will Not Develop Sepsis")
|
100 |
+
elif raw_prediction == 1:
|
101 |
+
raise HTTPException(status_code=status.HTTP_200_OK,
|
102 |
+
detail="The patient Will Develop Sepsis")
|
103 |
+
else:
|
104 |
+
raise HTTPException(
|
105 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Prediction Error")
|
106 |
+
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
uvicorn.run("main:app", reload=True)
|