File size: 1,229 Bytes
ad7c122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field, computed_field
import numpy as np
from typing import Literal, Annotated
import pickle 
import math
import pandas as pd

# import the ML model 
with open('delivery_time_model.pkl','rb') as f:
    model = pickle.load(f)
    
app = FastAPI()

@app.get('/')
def home():
    return {'message' : 'Delivery time estimation API '}

@app.get('/health')
def healthcheck():
    return {'status' : 'OK'}

# pydantic model build to validate the input data

class UserInput(BaseModel):
    age : Annotated[int,Field(...,ge = 18, lt = 120,description = 'Age of the delivery person')]
    rating : Annotated[float,Field(...,ge = 1, le = 6 ,description = 'Delivery person Ratings')]
    distance : Annotated[int,Field(...,gt = 0,description = 'Total Distance to be covered')]
    
    
@app.post('/predict')
def predict_time(data: UserInput):
    features = np.array([[data.age, data.rating, data.distance]])
    prediction = model.predict(features)

    prediction_value = math.ceil(float(prediction[0]))

    return JSONResponse(
        status_code=200,
        content={"Predicted Delivery Time in Minutes": prediction_value}
    )