rasmodev commited on
Commit
94dce9d
1 Parent(s): 62d11b7

Delete src

Browse files
Files changed (1) hide show
  1. src/app.py +0 -75
src/app.py DELETED
@@ -1,75 +0,0 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
- import uvicorn
4
- import pickle
5
- import pandas as pd
6
-
7
- # Instantiate The Fast API instance
8
- app = FastAPI(
9
- title="Sepsis Prediction API",
10
- description="This FastAPI application provides sepsis predictions using a machine learning model.",
11
- version="1.0"
12
- )
13
-
14
- # Load the model and key components
15
- with open('model_and_key_components.pkl', 'rb') as file:
16
- loaded_components = pickle.load(file)
17
-
18
- loaded_model = loaded_components['model']
19
- loaded_encoder = loaded_components['encoder']
20
- loaded_scaler = loaded_components['scaler']
21
-
22
- # Define the input data structure using Pydantic BaseModel
23
- class InputData(BaseModel):
24
- PRG: int
25
- PL: float
26
- PR: float
27
- SK: float
28
- TS: int
29
- M11: float
30
- BD2: float
31
- Age: int
32
-
33
- # Define the output data structure using Pydantic BaseModel
34
- class OutputData(BaseModel):
35
- Sepsis: str
36
-
37
- # Define a function to preprocess input data
38
- def preprocess_input_data(input_data: InputData):
39
- # Encode Categorical Variables (if needed)
40
- # All columns are numerical. No need for encoding
41
-
42
- # Apply scaling to numerical data
43
- numerical_cols = ['PRG', 'PL', 'PR', 'SK', 'TS', 'M11', 'BD2', 'Age']
44
- input_data_scaled = loaded_scaler.transform([list(input_data.dict().values())])
45
-
46
- return pd.DataFrame(input_data_scaled, columns=numerical_cols)
47
-
48
- # Define a function to make predictions
49
- def make_predictions(input_data_scaled_df: pd.DataFrame):
50
- y_pred = loaded_model.predict(input_data_scaled_df)
51
- sepsis_mapping = {0: 'Negative', 1: 'Positive'}
52
- return sepsis_mapping[y_pred[0]]
53
-
54
- @app.get("/")
55
- async def root():
56
- # Endpoint at the root URL ("/") returns a welcome message with a clickable link
57
- message = "Welcome to your Sepsis Classification API! Click [here](/docs) to access the API documentation."
58
- return {"message": message}
59
-
60
-
61
- @app.post("/predict/", response_model=OutputData)
62
- async def predict_sepsis(input_data: InputData):
63
- try:
64
- input_data_scaled_df = preprocess_input_data(input_data)
65
- sepsis_status = make_predictions(input_data_scaled_df)
66
- return {"Sepsis": sepsis_status}
67
- except Exception as e:
68
-
69
- # Handle exceptions and return an error response
70
- raise HTTPException(status_code=500, detail=str(e))
71
-
72
- if __name__ == "__main__":
73
-
74
- # Run the FastAPI application
75
- uvicorn.run("app:app", reload=True)