tegarganang commited on
Commit
d7162e7
·
1 Parent(s): 9fe4242

initial commit

Browse files
Files changed (4) hide show
  1. .dockerfile +13 -0
  2. app.py +96 -0
  3. insurance_model.pkl +3 -0
  4. requirement.txt +6 -0
.dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ COPY --chown=user ./requirements.txt requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ COPY --chown=user . /app
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
96
+
insurance_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e3025a6bf31989a5271a2864b8bce71a9344eaf8b4bcc80d70eb5194f2fae885
3
+ size 634
requirement.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ pydantic
3
+ python-multipart
4
+ uvicorn
5
+ starlette
6
+ scikit-learn