-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
92 lines (75 loc) · 3.14 KB
/
Copy pathapp.py
File metadata and controls
92 lines (75 loc) · 3.14 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
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field, computed_field
from typing import Literal, Annotated
import pickle
import pandas as pd
import pickle
# import the ml model
with open('model.pkl', 'rb') as f:
model = pickle.load(f)
app = FastAPI()
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"
]
# pydantic model to validate incoming data
class UserInput(BaseModel):
age: Annotated[int, Field(..., gt=0, lt=120, description='Age of the user')]
weight: Annotated[float, Field(..., gt=0, description='Weight of the user')]
height: Annotated[float, Field(..., gt=0, lt=2.5, description='Height of the user')]
income_lpa: Annotated[float, Field(..., gt=0, description='Annual salary of the user in lpa')]
smoker: Annotated[bool, Field(..., description='Is user a smoker')]
city: Annotated[str, Field(..., description='The city that the user belongs to')]
occupation: Annotated[Literal['retired', 'freelancer', 'student', 'government_job',
'business_owner', 'unemployed', 'private_job'], Field(..., description='Occupation of the user')]
@computed_field
@property
def bmi(self) -> float:
return self.weight/(self.height**2)
@computed_field
@property
def lifestyle_risk(self) -> str:
if self.smoker and self.bmi > 30:
return "High"
elif self.smoker or self.bmi > 27:
return "Medium"
else:
return "Low"
@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"
@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
@app.post('/predict')
def predict_premium(data: UserInput):
input_df = pd.DataFrame([{
'bmi': data.bmi,
'age_group': data.age_group,
'lifestyle_risk': data.lifestyle_risk,
'city_tier': data.city_tier,
'income_lpa': data.income_lpa,
'occupation': data.occupation
}])
prediction = model.predict(input_df)[0]
return JSONResponse(status_code=200, content={'predicted_category': prediction})