Spaces:
Runtime error
Runtime error
Update app/main.py
Browse files- 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.
|
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
|
34 |
-
# Create a response for each prediction with the predicted
|
35 |
output = {
|
36 |
-
"
|
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
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
@classmethod
|
56 |
-
def return_list_of_dict(cls,
|
57 |
-
|
58 |
-
for
|
59 |
-
|
60 |
-
|
61 |
-
return
|
|
|
62 |
|
63 |
# Endpoints
|
64 |
# Root Endpoint
|
65 |
@app.get("/")
|
66 |
def root():
|
67 |
-
return {"Welcome to the
|
68 |
|
69 |
# Prediction endpoint
|
70 |
@app.post("/predict")
|
71 |
-
def
|
72 |
# Make prediction
|
73 |
-
data = pd.DataFrame(
|
74 |
-
|
75 |
-
return {"
|
76 |
|
77 |
# Multiple Prediction Endpoint
|
78 |
@app.post("/predict_multiple")
|
79 |
-
def
|
80 |
"""Make prediction with the passed data"""
|
81 |
-
data = pd.DataFrame(
|
82 |
-
|
83 |
-
return {"
|
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)
|