ikoghoemmanuell commited on
Commit
897f7f4
1 Parent(s): f0d14df

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +39 -37
app/main.py CHANGED
@@ -20,67 +20,69 @@ def predict(df, endpoint="simple"):
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 {"Welcome to the Sepsis Prediction API! This API provides endpoints for predicting sepsis based on patient data."}
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)
 
20
  scaled_df = scaler.transform(df) # Scale the input data using a pre-defined scaler
21
 
22
  # Prediction
23
+ prediction = model.predict(scaled_df) # Make predictions using a pre-trained XGBoost regressor model
 
 
 
 
 
 
 
24
 
25
  response = []
26
+ for eta in prediction:
27
+ # Create a response for each prediction with the predicted ETA
28
  output = {
29
+ "predicted_eta": eta
 
30
  }
31
  response.append(output) # Add the response to the list of responses
32
 
33
  return response # Return the list of responses
34
 
35
 
36
+ class Trip(BaseModel):
37
+ Origin_lat: float
38
+ Origin_lon: float
39
+ Destination_lat: float
40
+ Destination_lon: float
41
+ Trip_distance: int # Assuming this column represents an integer value
42
+ total_secs: int # Assuming this column represents an integer value
43
+ dewpoint_2m_temperature: float
44
+ maximum_2m_air_temperature: float
45
+ mean_2m_air_temperature: float
46
+ mean_sea_level_pressure: float
47
+ minimum_2m_air_temperature: float
48
+ surface_pressure: float
49
+ total_precipitation: float
50
+ u_component_of_wind_10m: float
51
+ v_component_of_wind_10m: float
52
+
53
+ class Trips(BaseModel):
54
+ all_trips: list[Trip]
55
 
56
  @classmethod
57
+ def return_list_of_dict(cls, trips: "Trips"):
58
+ trip_list = []
59
+ for trip in trips.all_trips: # for each item in all_trips
60
+ trip_dict = trip.dict() # convert to a dictionary
61
+ trip_list.append(trip_dict) # add it to the empty list called trip_list
62
+ return trip_list
63
+
64
 
65
  # Endpoints
66
  # Root Endpoint
67
  @app.get("/")
68
  def root():
69
+ return {"Welcome to the ETA Prediction API! This API provides endpoints for predicting ETA based on trip data."}
70
 
71
  # Prediction endpoint
72
  @app.post("/predict")
73
+ def predict_eta(trip: Trip):
74
  # Make prediction
75
+ data = pd.DataFrame(trip.dict(), index=[0])
76
+ predicted_eta = predict(df=data)
77
+ return {"predicted_eta": predicted_eta}
78
 
79
  # Multiple Prediction Endpoint
80
  @app.post("/predict_multiple")
81
+ def predict_eta_for_multiple_trips(trips: Trips):
82
  """Make prediction with the passed data"""
83
+ data = pd.DataFrame(Trips.return_list_of_dict(trips))
84
+ predicted_eta = predict(df=data, endpoint="multi")
85
+ return {"predicted_eta": predicted_eta}
86
 
87
  if __name__ == "__main__":
88
  uvicorn.run("main:app", reload=True)