Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,42 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
-
from pydantic import BaseModel, Field, computed_field
|
| 4 |
-
import numpy as np
|
| 5 |
-
from typing import Literal, Annotated
|
| 6 |
-
import pickle
|
| 7 |
-
import math
|
| 8 |
-
import pandas as pd
|
| 9 |
-
|
| 10 |
-
# import the ML model
|
| 11 |
-
with open('delivery_time_model.pkl','rb') as f:
|
| 12 |
-
model = pickle.load(f)
|
| 13 |
-
|
| 14 |
-
app = FastAPI()
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from pydantic import BaseModel, Field, computed_field
|
| 4 |
+
import numpy as np
|
| 5 |
+
from typing import Literal, Annotated
|
| 6 |
+
import pickle
|
| 7 |
+
import math
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
# import the ML model
|
| 11 |
+
with open('delivery_time_model.pkl','rb') as f:
|
| 12 |
+
model = pickle.load(f)
|
| 13 |
+
|
| 14 |
+
app = FastAPI()
|
| 15 |
+
|
| 16 |
+
@app.get('/')
|
| 17 |
+
def home():
|
| 18 |
+
return {'message' : 'Delivery time estimation API '}
|
| 19 |
+
|
| 20 |
+
@app.get('/health')
|
| 21 |
+
def healthcheck():
|
| 22 |
+
return {'status' : 'OK'}
|
| 23 |
+
|
| 24 |
+
# pydantic model build to validate the input data
|
| 25 |
+
|
| 26 |
+
class UserInput(BaseModel):
|
| 27 |
+
age : Annotated[int,Field(...,ge = 18, lt = 120,description = 'Age of the delivery person')]
|
| 28 |
+
rating : Annotated[float,Field(...,ge = 1, le = 6 ,description = 'Delivery person Ratings')]
|
| 29 |
+
distance : Annotated[int,Field(...,gt = 0,description = 'Total Distance to be covered')]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@app.post('/predict')
|
| 33 |
+
def predict_time(data: UserInput):
|
| 34 |
+
features = np.array([[data.age, data.rating, data.distance]])
|
| 35 |
+
prediction = model.predict(features)
|
| 36 |
+
|
| 37 |
+
prediction_value = math.ceil(float(prediction[0]))
|
| 38 |
+
|
| 39 |
+
return JSONResponse(
|
| 40 |
+
status_code=200,
|
| 41 |
+
content={"Predicted Delivery Time in Minutes": prediction_value}
|
| 42 |
+
)
|