generated from shaloy-lewis/ds_template_repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
72 lines (61 loc) · 2.59 KB
/
api.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from datetime import date, timedelta
from src.pipeline.prediction_pipeline import PredictPipeline, CustomData
app = FastAPI(
title='Flight Price Prediction',
description="Predicts price of flight using the travel details input by the user"
)
@app.get("/")
async def root():
return {"message": "Flight price prediction api"}
@app.get("/ping", summary='Health check')
def healthcheck():
return {"message": "Health check successful!"}
class FlightData(BaseModel):
airline: str = "AirAsia"
from_: str = "Mumbai"
to_: str = "Delhi"
class_: str = "economy"
ch_code: str = "SG"
dep_time: str = "06:20"
arr_time: str = "08:40"
date: str = (date.today() + timedelta(days=4)).strftime("%d-%m-%Y")
time_taken: str = "02h 20m"
stop: str = "non-stop"
@app.post("/predict")
def predict_default(data: FlightData):
try:
custom_data = CustomData(
airline=data.airline,
from_=data.from_,
to_=data.to_,
class_=data.class_,
ch_code=data.ch_code,
dep_time=data.dep_time,
arr_time=data.arr_time,
date=data.date,
time_taken=data.time_taken,
stop=data.stop
)
# Convert to DataFrame
features_df = custom_data.get_data_as_dataframe()
# Initialize the prediction pipeline and make predictions
pipeline = PredictPipeline()
prediction = pipeline.predict(features_df)
# Compute global feature importance
global_importance = pipeline.get_global_feature_importance()
global_importance_dict = {col: imp for col, imp in zip(pipeline.preprocessor.get_feature_names_out(), global_importance)}
global_importance_dict = dict(sorted(global_importance_dict.items(), reverse=True, key=lambda item: item[1]))
# Compute instance-specific feature importance using SHAP
shap_values = pipeline.get_instance_feature_importance(features_df)
instance_importance_dict = {col: imp for col, imp in zip(features_df.columns, shap_values[0])}
instance_importance_dict = dict(sorted(instance_importance_dict.items(), reverse=True, key=lambda item: item[1]))
# Return the prediction result
return {
"flight_price_prediction": round(prediction[0],2),
"instance_feature_importance": instance_importance_dict,
"global_feature_importance": global_importance_dict
}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))