Spaces:
Runtime error
Runtime error
NewtonKimathi
commited on
Commit
•
945748a
1
Parent(s):
4c31138
Sepsis Prediction API
Browse files- .dockerignore +10 -0
- Dockerfile +28 -0
- commands.txt +10 -0
- main.py +94 -0
- requirements.txt +50 -0
.dockerignore
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
venv/
|
2 |
+
__pycache__/
|
3 |
+
*.venv
|
4 |
+
*.venv.*
|
5 |
+
venv.*
|
6 |
+
*.pyc
|
7 |
+
.git
|
8 |
+
.vscode
|
9 |
+
.gitattributes
|
10 |
+
.gitkeep
|
Dockerfile
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Specifying the base image
|
2 |
+
FROM python:3.11
|
3 |
+
|
4 |
+
# Set the working directory to /Sepsis
|
5 |
+
WORKDIR /Sepsis
|
6 |
+
|
7 |
+
# Copy the current directory contents into the container at /Sepsis
|
8 |
+
COPY ./requirements.txt /Sepsis/requirements.txt
|
9 |
+
|
10 |
+
# Install requirements.txt
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r /Sepsis/requirements.txt
|
12 |
+
|
13 |
+
# Set up a new user named "user" with user ID 1000
|
14 |
+
RUN useradd -m -u 1000 user
|
15 |
+
# Switch to the "user" user
|
16 |
+
USER user
|
17 |
+
# Set home to the user's home directory
|
18 |
+
ENV HOME=/home/user \
|
19 |
+
PATH=/home/user/.local/bin:$PATH
|
20 |
+
|
21 |
+
# Set the working directory to the user's home directory
|
22 |
+
WORKDIR $HOME/app
|
23 |
+
|
24 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
25 |
+
COPY --chown=user . $HOME/app
|
26 |
+
|
27 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
28 |
+
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
commands.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
python -m venv venv; # To create a venv environment
|
2 |
+
venv\Scripts\activate; # To activate the environment
|
3 |
+
python -m pip install -q --upgrade pip;# To upgrade the pip
|
4 |
+
python -m pip install -qr requirements.txt # To install the requirements
|
5 |
+
|
6 |
+
# THe model of the input is useful to structure the input of the data (class)
|
7 |
+
#The variable for a function will be an object of the class defined
|
8 |
+
|
9 |
+
# View a summary of image vulnerabilities and recommendations
|
10 |
+
docker scout quickview
|
main.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
# Setup Section
|
4 |
+
|
5 |
+
# Create FastAPI instance
|
6 |
+
app = FastAPI(title="Sepsis Prediction API",description="API for Predicting Sespsis ")
|
7 |
+
## A function to load machine Learning components to re-use
|
8 |
+
def Ml_loading_components(fp):
|
9 |
+
with open(fp, "rb") as f:
|
10 |
+
object=pickle.load(f)
|
11 |
+
return(object)
|
12 |
+
# Loading the machine learning components
|
13 |
+
DIRPATH = os.path.dirname(os.path.realpath(__file__))
|
14 |
+
ml_core_fp = os.path.join(DIRPATH,"ML","ML_Model.pkl")
|
15 |
+
ml_components_dict = Ml_loading_components(fp=ml_core_fp)
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
# Defining the variables for each component
|
20 |
+
label_encoder = ml_components_dict['label_encoder'] # The label encoder
|
21 |
+
# Loaded scaler component
|
22 |
+
scaler = ml_components_dict['scaler']
|
23 |
+
#Loaded model
|
24 |
+
model = ml_components_dict['model']
|
25 |
+
# Defining our input variables
|
26 |
+
|
27 |
+
|
28 |
+
class InputData(BaseModel):
|
29 |
+
PRG:int
|
30 |
+
PL: int
|
31 |
+
BP: int
|
32 |
+
SK: int
|
33 |
+
TS: int
|
34 |
+
BMI: float
|
35 |
+
BD2: float
|
36 |
+
Age: int
|
37 |
+
|
38 |
+
"""
|
39 |
+
* PRG: Plasma glucose
|
40 |
+
|
41 |
+
* PL: Blood Work Result-1 (mu U/ml)
|
42 |
+
|
43 |
+
* PR: Blood Pressure (mmHg)
|
44 |
+
|
45 |
+
* SK: Blood Work Result-2(mm)
|
46 |
+
|
47 |
+
* TS: Blood Work Result-3 (muU/ml)
|
48 |
+
|
49 |
+
* M11: Body mass index (weight in kg/(height in m)^2
|
50 |
+
|
51 |
+
* BD2: Blood Work Result-4 (mu U/ml)
|
52 |
+
|
53 |
+
* Age: patients age(years)
|
54 |
+
|
55 |
+
"""
|
56 |
+
# Index route
|
57 |
+
@app.get("/")
|
58 |
+
def index():
|
59 |
+
return{'message':'Hello, Welcome to My Sepsis Prediction FastAPI'}
|
60 |
+
|
61 |
+
|
62 |
+
# Create prediction endpoint
|
63 |
+
@app.post("/predict")
|
64 |
+
def predict (df:InputData):
|
65 |
+
|
66 |
+
|
67 |
+
# Prepare the feature and structure them like in the notebook
|
68 |
+
df = pd.DataFrame([df.dict().values()],columns=df.dict().keys())
|
69 |
+
|
70 |
+
|
71 |
+
print(f"[Info] The inputed dataframe is : {df.to_markdown()}")
|
72 |
+
age = df['Age']
|
73 |
+
print(age)
|
74 |
+
# Scaling the inputs
|
75 |
+
df_scaled = scaler.transform(df)
|
76 |
+
|
77 |
+
|
78 |
+
# Prediction
|
79 |
+
raw_prediction = model.predict(df_scaled)
|
80 |
+
|
81 |
+
if raw_prediction == 0:
|
82 |
+
raise HTTPException(status_code=status.HTTP_200_OK, detail="The patient will Not Develop Sepsis")
|
83 |
+
elif raw_prediction == 1:
|
84 |
+
raise HTTPException(status_code=status.HTTP_200_OK, detail="The patient Will Develop Sepsis")
|
85 |
+
else:
|
86 |
+
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Prediction Error")
|
87 |
+
|
88 |
+
|
89 |
+
if __name__ == "__main__":
|
90 |
+
uvicorn.run("main:app",reload=True)
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
|
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
|