-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML_Model_API.py
More file actions
99 lines (83 loc) · 3.06 KB
/
ML_Model_API.py
File metadata and controls
99 lines (83 loc) · 3.06 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from fastapi import FastAPI , HTTPException
from fastapi.responses import JSONResponse
import pickle
from pydantic import BaseModel , Field , computed_field
from typing import Literal
import pandas as pd
app = FastAPI()
@app.get("/health_")
def health_cond():
try:
return JSONResponse(content= {"Message : This application is working fine"} , status_code= 200)
except:
raise HTTPException(status_code= 400 , detail="App is not working fine")
def get_model():
with open("model.pkl" , "rb") as f:
model = pickle.load(f)
return model
tier_1_cities = ["Mumbai", "Delhi", "Bangalore", "Chennai", "Kolkata", "Hyderabad", "Pune"]
tier_2_cities = [
"Jaipur", "Chandigarh", "Indore", "Lucknow", "Patna", "Ranchi", "Visakhapatnam", "Coimbatore",
"Bhopal", "Nagpur", "Vadodara", "Surat", "Rajkot", "Jodhpur", "Raipur", "Amritsar", "Varanasi",
"Agra", "Dehradun", "Mysore", "Jabalpur", "Guwahati", "Thiruvananthapuram", "Ludhiana", "Nashik",
"Allahabad", "Udaipur", "Aurangabad", "Hubli", "Belgaum", "Salem", "Vijayawada", "Tiruchirappalli",
"Bhavnagar", "Gwalior", "Dhanbad", "Bareilly", "Aligarh", "Gaya", "Kozhikode", "Warangal",
"Kolhapur", "Bilaspur", "Jalandhar", "Noida", "Guntur", "Asansol", "Siliguri"
]
class Customer(BaseModel):
age :int = Field(gt = 0 , lt = 150)
weight :float = Field(gt = 0 , lt = 200)
height :float = Field(gt = 0 , lt = 3)
income_lpa :float
smoker : bool
city : str
occupation : Literal['retired', 'freelancer', 'student', 'government_job','business_owner', 'unemployed', 'private_job']
@computed_field
@property
def bmi(self)->float:
return (self.weight / ((self.height)**2))
@computed_field
@property
def age_group(self)->str:
if self.age < 25:
return "young"
elif self.age < 45:
return "Adult"
elif self.age < 60:
return "middle_aged"
else:
return "senior_citizen"
@computed_field
@property
def city_tier(self)->int:
if self.city in tier_1_cities:
return 1
elif self.city in tier_2_cities:
return 2
else:
return 3
@computed_field
@property
def lifestyle_risk(self)->str:
if self.smoker and self.bmi > 30:
return "high"
elif self.smoker and self.bmi > 27:
return "medium"
else:
return "low"
@app.post("/predict")
def predict(customer : Customer):
try:
data = pd.DataFrame([{
"BMI" : customer.bmi,
"age_category" : customer.age_group,
'occupation' : customer.occupation,
"lifestyle" : customer.lifestyle_risk,
"city_tier" : customer.city_tier,
"income_lpa" : customer.income_lpa
}])
pipeline = get_model()
y_pred = pipeline.predict(data)
return JSONResponse(status_code= 200 , content= {"y_pred" : y_pred[0]})
except:
raise HTTPException(status_code= 400 , detail={"Something went wrong"})