ikoghoemmanuell commited on
Commit
008fca6
1 Parent(s): 645687e

first attempt to improve my root endpoint

Browse files
Files changed (1) hide show
  1. app/main.py +27 -8
app/main.py CHANGED
@@ -1,9 +1,10 @@
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")
@@ -63,24 +64,42 @@ class Patients(BaseModel):
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)
 
1
+ from fastapi import FastAPI, Request
2
  from pydantic import BaseModel
3
  import pickle
4
  import pandas as pd
5
  import numpy as np
6
  import uvicorn
7
+ import json
8
 
9
  # call the app
10
  app = FastAPI(title="API")
 
64
  # Endpoints
65
  # Root Endpoint
66
  @app.get("/")
67
+ def root(request: Request):
68
+ content = await request.json()
69
+ response = {}
70
+
71
+ if "endpoint" in content:
72
+ endpoint = content["endpoint"]
73
+
74
+ if endpoint == "predict":
75
+ if "patient" in content:
76
+ patient_data = content["patient"]
77
+ patient = Patient(**patient_data)
78
+ prediction = predict_sepsis(patient)
79
+ response["prediction"] = prediction
80
+
81
+ elif endpoint == "predict_multiple":
82
+ if "patients" in content:
83
+ patients_data = content["patients"]
84
+ patients = Patients(**patients_data)
85
+ prediction = predict_sepsis_for_multiple_patients(patients)
86
+ response["prediction"] = prediction
87
+
88
+ return response
89
 
90
  # Prediction endpoint
 
91
  def predict_sepsis(patient: Patient):
92
  # Make prediction
93
  data = pd.DataFrame(patient.dict(), index=[0])
94
  parsed = predict(df=data)
95
+ return parsed
96
 
97
  # Multiple Prediction Endpoint
 
98
  def predict_sepsis_for_multiple_patients(patients: Patients):
99
  """Make prediction with the passed data"""
100
  data = pd.DataFrame(Patients.return_list_of_dict(patients))
101
  parsed = predict(df=data, endpoint="multi")
102
+ return parsed
103
 
104
  if __name__ == "__main__":
105
+ uvicorn.run("main:app", reload=True)