HilmiZr commited on
Commit
d1e6108
·
1 Parent(s): 3c20b42

added: app.py

Browse files
Files changed (3) hide show
  1. app.py +91 -3
  2. insurance_model.pkl +3 -0
  3. requirements.txt +5 -1
app.py CHANGED
@@ -1,7 +1,95 @@
1
- from fastapi import FastAPI
 
 
 
 
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  @app.get("/")
6
- def greet_json():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Form, Depends, Request
2
+ from fastapi.encoders import jsonable_encoder
3
+ from fastapi.responses import JSONResponse
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ from pydantic import BaseModel
6
+ import pickle
7
 
8
  app = FastAPI()
9
 
10
+ # Add CORS middleware
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_credentials=True,
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
+ )
18
+
19
+ # Load the pre-trained model
20
+ with open('insurance_model.pkl', 'rb') as model_file:
21
+ model = pickle.load(model_file, encoding='bytes')
22
+
23
+ class Req(BaseModel):
24
+ age: int
25
+ sex: int
26
+ smoker: int
27
+ bmi: float
28
+ children: int
29
+ region: int
30
+
31
+ class Resp(BaseModel):
32
+ age: int
33
+ sex: str
34
+ smoker: str
35
+ bmi: float
36
+ children: int
37
+ region: str
38
+ insurance_cost: float
39
+
40
  @app.get("/")
41
+ async def root():
42
+ return {"message": "Hello World. Welcome to FastAPI!"}
43
+
44
+ def form_req(age: str = Form(...), sex: str = Form(...), smoker: str = Form(...),
45
+ bmi: str = Form(...), children: str = Form(...), region: str = Form(...)):
46
+ sBmi = bmi.replace(",", ".")
47
+ return Req(age=int(age), sex=int(sex), smoker=int(smoker), bmi=float(sBmi), children=int(children), region=int(region))
48
+
49
+ def get_region_name(region_code):
50
+ region_mapping = {
51
+ 0: "Northeast",
52
+ 1: "Northwest",
53
+ 2: "Southeast",
54
+ 3: "Southwest"
55
+ }
56
+ return region_mapping.get(region_code, "Unknown")
57
+
58
+ @app.post("/predict")
59
+ async def predict(request: Request, requess: Req = Depends(form_req)):
60
+ '''
61
+ Predict the insurance cost based on user inputs
62
+ and render the result to the html page
63
+ '''
64
+ age = requess.age
65
+ sex = requess.sex
66
+ smoker = requess.smoker
67
+ bmi = requess.bmi
68
+ children = requess.children
69
+ region = requess.region
70
+ data = []
71
+
72
+ data.append(int(age))
73
+ data.extend([int(sex)])
74
+ data.extend([float(bmi)])
75
+ data.extend([int(children)])
76
+ data.extend([int(smoker)])
77
+ data.extend([int(region)])
78
+
79
+ prediction = model.predict([data])
80
+ output = round(prediction[0], 2)
81
+
82
+ sex = "Male" if requess.sex == 1 else "Female"
83
+ smoker = "Yes" if requess.smoker == 1 else "No"
84
+
85
+ # Render index.html with prediction results
86
+ json_compatible_resp_data = jsonable_encoder(Resp(
87
+ age=requess.age,
88
+ sex=sex,
89
+ smoker=smoker,
90
+ bmi=requess.bmi,
91
+ children=requess.children,
92
+ region=get_region_name(requess.region),
93
+ insurance_cost=output
94
+ ))
95
+ return JSONResponse(content=json_compatible_resp_data)
insurance_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ce5baecaec7aa0475bc4f500227df4d42dadaa242c2c95ac1a930b8281b755d1
3
+ size 633
requirements.txt CHANGED
@@ -1,2 +1,6 @@
1
  fastapi
2
- uvicorn[standard]
 
 
 
 
 
1
  fastapi
2
+ pydantic
3
+ python-multipart
4
+ uvicorn
5
+ starlette
6
+ scikit-learn