rasmodev commited on
Commit
9b95b1e
·
1 Parent(s): 7dba773

FastAPI, Dockerfile and Build File

Browse files
Files changed (3) hide show
  1. src/Dockerfile +21 -0
  2. src/app.py +71 -0
  3. src/build.sh +11 -0
src/Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python image as a parent image
2
+ FROM python:3.11.3-slim
3
+
4
+ # Set the working directory within the container
5
+ WORKDIR /app
6
+
7
+ # Copy your FastAPI application code into the container
8
+ COPY src/app.py /app
9
+
10
+ # Copy the requirements.txt file into the container
11
+ COPY requirements.txt /app
12
+
13
+ # Install the Python dependencies
14
+ RUN pip install -r /app/requirements.txt
15
+
16
+ # Expose port 8000 for the FastAPI application
17
+ EXPOSE 8000
18
+
19
+ # Define the command to run your FastAPI application
20
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
21
+
src/app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Query
2
+ from pydantic import BaseModel
3
+ import pickle
4
+ import pandas as pd
5
+
6
+ app = FastAPI(
7
+ title="Sepsis Prediction API",
8
+ description="This FastAPI application provides sepsis predictions using a machine learning model.",
9
+ version="1.0"
10
+ )
11
+
12
+ # Load the model and key components
13
+ with open('model_and_key_components.pkl', 'rb') as file:
14
+ loaded_components = pickle.load(file)
15
+
16
+ loaded_model = loaded_components['model']
17
+ loaded_encoder = loaded_components['encoder']
18
+ loaded_scaler = loaded_components['scaler']
19
+
20
+ # Define the input data structure using Pydantic BaseModel
21
+ class InputData(BaseModel):
22
+ PRG: int = Query(..., title="Patient's Pregnancy Count", description="Enter the number of pregnancies.", example=2)
23
+ PL: float = Query(..., title="Platelet Count", description="Enter the platelet count.", example=150.0)
24
+ PR: float = Query(..., title="Pulse Rate", description="Enter the pulse rate.", example=75.0)
25
+ SK: float = Query(..., title="Skin Thickness", description="Enter the skin thickness.", example=25.0)
26
+ TS: int = Query(..., title="Triceps Skin Fold Thickness", description="Enter the triceps skin fold thickness.", example=30)
27
+ M11: float = Query(..., title="Insulin Level", description="Enter the insulin level.", example=120.0)
28
+ BD2: float = Query(..., title="BMI", description="Enter the Body Mass Index (BMI).", example=32.0)
29
+ Age: int = Query(..., title="Age", description="Enter the patient's age.", example=35)
30
+
31
+ # Define the output data structure using Pydantic BaseModel
32
+ class OutputData(BaseModel):
33
+ Sepsis: str
34
+
35
+ # Define a function to preprocess input data
36
+ def preprocess_input_data(input_data: InputData):
37
+ # Encode Categorical Variables (if needed)
38
+ # All columns are numerical. No need for encoding
39
+
40
+ # Apply scaling to numerical data
41
+ numerical_cols = ['PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age']
42
+ input_data_scaled = loaded_scaler.transform([list(input_data.dict().values())])
43
+
44
+ return pd.DataFrame(input_data_scaled, columns=numerical_cols)
45
+
46
+ # Define a function to make predictions
47
+ def make_predictions(input_data_scaled_df: pd.DataFrame):
48
+ y_pred = loaded_model.predict(input_data_scaled_df)
49
+ sepsis_mapping = {0: 'Negative', 1: 'Positive'}
50
+ return sepsis_mapping[y_pred[0]]
51
+
52
+ @app.get("/")
53
+ async def root():
54
+ # Endpoint at the root URL ("/") returns a welcome message with a clickable link
55
+ message = "Welcome to your Sepsis Classification API! Click [here](/docs) to access the API documentation."
56
+ return {"message": message}
57
+
58
+ @app.post("/predict/", response_model=OutputData)
59
+ async def predict_sepsis(input_data: InputData):
60
+ try:
61
+ input_data_scaled_df = preprocess_input_data(input_data)
62
+ sepsis_status = make_predictions(input_data_scaled_df)
63
+ return {"Sepsis": sepsis_status}
64
+ except Exception as e:
65
+ # Handle exceptions and return an error response
66
+ raise HTTPException(status_code=500, detail=str(e))
67
+
68
+ if __name__ == "__main__":
69
+ import uvicorn
70
+ # Run the FastAPI application on the local host and port 8000
71
+ uvicorn.run(app, host="127.0.0.1", port=8000)
src/build.sh ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Build the Docker container for the FastAPI application
2
+ docker build -t sepsis_fastapi -f src/Dockerfile .
3
+
4
+ # List all Docker images
5
+ docker images
6
+
7
+ # Run the Docker container locally
8
+ docker run -p 8000:8000 --name sepsis_fastapi "generated docker image"
9
+
10
+ # List running Docker containers
11
+ docker ps