vishurdx commited on
Commit
ad7c122
·
verified ·
1 Parent(s): d40d071

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -35
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
- # pydantic model build to validate the input data
18
-
19
- class UserInput(BaseModel):
20
- age : Annotated[int,Field(...,ge = 18, lt = 120,description = 'Age of the delivery person')]
21
- rating : Annotated[float,Field(...,ge = 1, le = 6 ,description = 'Delivery person Ratings')]
22
- distance : Annotated[int,Field(...,gt = 0,description = 'Total Distance to be covered')]
23
-
24
-
25
- @app.post('/predict')
26
- def predict_time(data: UserInput):
27
- features = np.array([[data.age, data.rating, data.distance]])
28
- prediction = model.predict(features)
29
-
30
- prediction_value = math.ceil(float(prediction[0]))
31
-
32
- return JSONResponse(
33
- status_code=200,
34
- content={"Predicted Delivery Time in Minutes": prediction_value}
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
+ )