AlbieCofie commited on
Commit
7bc4b4f
·
1 Parent(s): b589f1b

Delete main.py

Browse files
Files changed (1) hide show
  1. main.py +0 -87
main.py DELETED
@@ -1,87 +0,0 @@
1
- from fastapi import FastAPI
2
- from pydantic import BaseModel
3
- import pickle
4
- import pandas as pd
5
- import numpy as np
6
- import uvicorn
7
-
8
- # call the app
9
- app = FastAPI(title="API")
10
-
11
- # Load the model and scaler
12
- def load_model_and_scaler():
13
- with open("model.pkl", "rb") as f1, open("scaler.pkl", "rb") as f2:
14
- return pickle.load(f1), pickle.load(f2)
15
-
16
- model, scaler = load_model_and_scaler()
17
-
18
- def predict(df, endpoint="simple"):
19
- # Scaling
20
- scaled_df = scaler.transform(df) # Scale the input data using a pre-defined scaler
21
-
22
- # Prediction
23
- prediction = model.predict_proba(scaled_df) # Make predictions using a pre-trained model
24
-
25
- highest_proba = prediction.max(axis=1) # Get the highest probability for each prediction
26
-
27
- # Assign predicted labels based on the highest probabilities
28
- predicted_labels = ["Patient does not have sepsis" if i == 0 else "Patient has sepsis" for i in highest_proba]
29
- print(f"Predicted labels: {predicted_labels}") # Print the predicted labels to the terminal
30
- print(highest_proba) # Print the highest probabilities to the terminal
31
-
32
- response = []
33
- for label, proba in zip(predicted_labels, highest_proba):
34
- # Create a response for each prediction with the predicted label and probability
35
- output = {
36
- "prediction": label,
37
- "probability of prediction": str(round(proba * 100)) + '%' # Convert the probability to a percentage
38
- }
39
- response.append(output) # Add the response to the list of responses
40
-
41
- return response # Return the list of responses
42
-
43
-
44
- class Patient(BaseModel):
45
- Blood_Work_R1: int
46
- Blood_Pressure: int
47
- Blood_Work_R3: int
48
- BMI: float
49
- Blood_Work_R4: float
50
- Patient_age: int
51
-
52
- class Patients(BaseModel):
53
- all_patients: list[Patient]
54
-
55
- @classmethod
56
- def return_list_of_dict(cls, patients: "Patients"):
57
- patient_list = []
58
- for patient in patients.all_patients: #for each item in all_patients,
59
- patient_dict = patient.dict() #convert to a dictionary
60
- patient_list.append(patient_dict) #add it to the empty list called patient_list
61
- return patient_list
62
-
63
- # Endpoints
64
- # Root Endpoint
65
- @app.get("/")
66
- def root():
67
- return {"API": "This is an API for sepsis prediction."}
68
-
69
- # Prediction endpoint
70
- @app.post("/predict")
71
- def predict_sepsis(patient: Patient):
72
- # Make prediction
73
- data = pd.DataFrame(patient.dict(), index=[0])
74
- parsed = predict(df=data)
75
- return {"output": parsed}
76
-
77
- # Multiple Prediction Endpoint
78
- @app.post("/predict_multiple")
79
- def predict_sepsis_for_multiple_patients(patients: Patients):
80
- """Make prediction with the passed data"""
81
- data = pd.DataFrame(Patients.return_list_of_dict(patients))
82
- parsed = predict(df=data, endpoint="multi")
83
- return {"output": parsed}
84
-
85
- if __name__ == "__main__":
86
- uvicorn.run("main:app", reload=True)
87
-