KAITANY commited on
Commit
dff6457
·
1 Parent(s): 3fb4dec

push files to HuggingFace Hub

Browse files
Files changed (4) hide show
  1. .dockerignore +1 -0
  2. dockerfile +29 -0
  3. main.py +98 -0
  4. requirements.txt +51 -0
.dockerignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .venv
dockerfile ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use of an official Python runtime as a base image
2
+ FROM python:3.11.5
3
+
4
+ # #Set up of working directory in the container i.e /app
5
+ # WORKDIR /code
6
+
7
+ # # Copy current directory contents into the container at /app
8
+ # COPY . /app
9
+
10
+ # # Install needed packages specified in requirements.txt recursively
11
+ # RUN pip install -r requirements.txt
12
+
13
+ # # Expose the port number the app runs on
14
+ # EXPOSE 7860
15
+
16
+ # # Command to run the application
17
+ #CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "7860"]
18
+
19
+ FROM python:3.11.5
20
+
21
+ WORKDIR /code
22
+
23
+ COPY ./requirements.txt /code/requirements.txt
24
+
25
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
26
+
27
+ COPY . .
28
+
29
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ import uvicorn
3
+ #from typing import List, Literal
4
+ from pydantic import BaseModel, Field
5
+ import pandas as pd
6
+ import pickle, os
7
+
8
+ #setup
9
+ # Get the directory of the current file (FastAPI application file)
10
+ DIRPATH = os.path.dirname(os.path.realpath(__file__))
11
+
12
+ # Construct the path to ml.pkl relative to the current file using forward slashes
13
+ ml_core_fp = os.path.join(DIRPATH, "../model/ml.pkl")
14
+
15
+
16
+ #useful functions
17
+ def load_ml_components(fp):
18
+ "load the ml components to re-use in app"
19
+ with open(fp, 'rb') as file:
20
+ obj = pickle.load(file)
21
+ return obj
22
+
23
+
24
+ # Loading: Execute and instantiate ml components
25
+ ml_components_dict = load_ml_components(fp = ml_core_fp)
26
+
27
+ pipeline = ml_components_dict["pipeline"]
28
+
29
+ encoder = ml_components_dict["encoder"]
30
+
31
+
32
+ # API
33
+ app = FastAPI(
34
+ title= "Sepsis classification API"
35
+ )
36
+
37
+ # Input for Modelling
38
+ class Sepsis_Pred(BaseModel):
39
+
40
+ PRG: int = Field(..., description='Plasma glucose')
41
+ PL: int = Field(..., description='Blood Work Result-1 (mu U/ml)')
42
+ PR: int = Field(..., description='Blood Pressure (mm Hg)')
43
+ SK: int = Field(..., description='Blood Work Result-2 (mm)')
44
+ TS: int = Field(..., description='Blood Work Result-3 (mu U/ml)')
45
+ M11: float = Field(..., description='Body mass index (weight in kg/(height in m)^2)')
46
+ BD2: float = Field(..., description='Blood Work Result-4 (mu U/ml)')
47
+ Age: int = Field(..., description='Patient age (years)')
48
+ Insurance: int = Field(..., description='If a patient holds a valid insurance card')
49
+
50
+
51
+ @app.get("/")
52
+ def root():
53
+ return {
54
+ "Info": "Sepsis classification API : This API classifies whether a patient will develop sepsis based on various test results"
55
+ }
56
+
57
+
58
+ @app.post("/classify_patient")
59
+ def sepsis_classification(sepsis_pred: Sepsis_Pred):
60
+
61
+ try:
62
+
63
+ #Dataframe creation
64
+ df = pd.DataFrame([sepsis_pred.model_dump()])
65
+
66
+ print(f'df: {df}')
67
+
68
+ # ML prediction
69
+ prediction = pipeline.predict(df)
70
+
71
+ # Get the index of the predicted class (0 or 1 in binary classification)
72
+ predicted_class_index = prediction[0]
73
+
74
+ confidence_score = pipeline.predict_proba(df)
75
+
76
+ # Retrieve the confidence score for the predicted class
77
+
78
+ confidence_score_predicted_class = confidence_score[0][predicted_class_index]
79
+
80
+
81
+ print(f"confidence_score: {confidence_score}")
82
+
83
+ execution_message = "Execution successful"
84
+
85
+
86
+ # encoded prediction
87
+ decoded_prediction = encoder.inverse_transform([prediction])[0]
88
+
89
+
90
+ return {"execution message": execution_message, "patient_diagnosis": decoded_prediction, "confidence_score": confidence_score_predicted_class}
91
+
92
+
93
+ except Exception as e:
94
+ raise HTTPException(status_code=500, detail=f"An error occurred during prediction {str(e)}")
95
+
96
+
97
+ if __name__ == "__main__":
98
+ uvicorn.run("main:app", reload=True)
requirements.txt ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ annotated-types==0.6.0
2
+ anyio==3.7.1
3
+ certifi==2023.11.17
4
+ click==8.1.7
5
+ colorama==0.4.6
6
+ contourpy==1.2.0
7
+ cycler==0.12.1
8
+ dnspython==2.4.2
9
+ email-validator==2.1.0.post1
10
+ fastapi==0.105.0
11
+ fonttools==4.46.0
12
+ h11==0.14.0
13
+ httpcore==1.0.2
14
+ httptools==0.6.1
15
+ httpx==0.25.2
16
+ idna==3.6
17
+ imbalanced-learn==0.11.0
18
+ itsdangerous==2.1.2
19
+ Jinja2==3.1.2
20
+ joblib==1.3.2
21
+ kiwisolver==1.4.5
22
+ MarkupSafe==2.1.3
23
+ matplotlib==3.8.2
24
+ numpy==1.26.2
25
+ orjson==3.9.10
26
+ packaging==23.2
27
+ pandas==2.1.4
28
+ Pillow==10.1.0
29
+ pydantic==2.5.2
30
+ pydantic-extra-types==2.2.0
31
+ pydantic-settings==2.1.0
32
+ pydantic_core==2.14.5
33
+ pyparsing==3.1.1
34
+ python-dateutil==2.8.2
35
+ python-dotenv==1.0.0
36
+ python-multipart==0.0.6
37
+ pytz==2023.3.post1
38
+ PyYAML==6.0.1
39
+ scikit-learn==1.3.2
40
+ scipy==1.11.4
41
+ seaborn==0.13.0
42
+ six==1.16.0
43
+ sniffio==1.3.0
44
+ starlette==0.27.0
45
+ threadpoolctl==3.2.0
46
+ typing_extensions==4.9.0
47
+ tzdata==2023.3
48
+ ujson==5.9.0
49
+ uvicorn==0.24.0.post1
50
+ watchfiles==0.21.0
51
+ websockets==12.0