Kadigas commited on
Commit
d9c8574
·
1 Parent(s): 27a5b24

initial commit

Browse files
Files changed (9) hide show
  1. Dockerfile +20 -0
  2. LICENSE.md +21 -0
  3. Procfile +1 -0
  4. __pycache__/main.cpython-310.pyc +0 -0
  5. insurance_model.pkl +3 -0
  6. main.py +106 -0
  7. model.pkl +3 -0
  8. pyproject.toml +17 -0
  9. requirements.txt +18 -0
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ RUN useradd -m -u 1000 user
10
+
11
+ USER user
12
+
13
+ ENV HOME=/home/user \
14
+ PATH=/home/user/.local/bin:$PATH
15
+
16
+ WORKDIR $HOME/app
17
+
18
+ COPY --chown=user . $HOME/app
19
+
20
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
LICENSE.md ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Himesh Samarasekera
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
Procfile ADDED
@@ -0,0 +1 @@
 
 
1
+ web: uvicorn main:app --host 0.0.0.0 --port $PORT
__pycache__/main.cpython-310.pyc ADDED
Binary file (3.55 kB). View file
 
insurance_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ce5baecaec7aa0475bc4f500227df4d42dadaa242c2c95ac1a930b8281b755d1
3
+ size 633
main.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # encoding: utf-8
3
+
4
+ from fastapi import FastAPI, Form, Depends, Request
5
+ from fastapi.encoders import jsonable_encoder
6
+ from fastapi.responses import JSONResponse
7
+ from pydantic import BaseModel
8
+ import pickle
9
+ import json
10
+
11
+ app = FastAPI()
12
+
13
+ model_file = open('insurance_model.pkl', 'rb')
14
+ model = pickle.load(model_file, encoding='bytes')
15
+
16
+
17
+ class Msg(BaseModel):
18
+ msg: str
19
+
20
+
21
+ class Req(BaseModel):
22
+ age: int
23
+ sex: int
24
+ smoker: int
25
+ bmi: float
26
+ children: int
27
+ region: int
28
+
29
+ class Resp(BaseModel):
30
+ age: int
31
+ sex: str
32
+ smoker: str
33
+ bmi: float
34
+ children: int
35
+ region: str
36
+ insurance_cost: float
37
+
38
+
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
+
50
+ @app.get("/path")
51
+ async def demo_get():
52
+ return {"message": "This is /path endpoint, use a post request to transform the text to uppercase"}
53
+
54
+
55
+ @app.post("/path")
56
+ async def demo_post(inp: Msg):
57
+ return {"message": inp.msg.upper()}
58
+
59
+
60
+ @app.get("/path/{path_id}")
61
+ async def demo_get_path_id(path_id: int):
62
+ return {"message": f"This is /path/{path_id} endpoint, use post request to retrieve result"}
63
+
64
+
65
+ @app.get("/predict/{path_id}")
66
+ async def predict(path_id: int):
67
+ return {"message": f"This is /predict/{path_id} endpoint, use post request to retrieve result"}
68
+
69
+ def get_region_name(region_code):
70
+ region_mapping = {
71
+ 0: "Northeast",
72
+ 1: "Northwest",
73
+ 2: "Southeast",
74
+ 3: "Southwest"
75
+ }
76
+ return region_mapping.get(region_code, "Unknown")
77
+
78
+ @app.post("/predict")
79
+ async def predict(request: Request, requess: Req = Depends(form_req)):
80
+ '''
81
+ Predict the insurance cost based on user inputs
82
+ and render the result to the html page
83
+ '''
84
+ age = requess.age
85
+ sex = requess.sex
86
+ smoker = requess.smoker
87
+ bmi = requess.bmi
88
+ children = requess.children
89
+ region = requess.region
90
+ data = []
91
+
92
+ data.append(int(age))
93
+ data.extend([int(sex)])
94
+ data.extend([float(bmi)])
95
+ data.extend([int(children)])
96
+ data.extend([int(smoker)])
97
+ data.extend([int(region)])
98
+
99
+ prediction = model.predict([data])
100
+ output = round(prediction[0], 2)
101
+
102
+ # Merender index.html dengan data hasil prediksi
103
+ json_compatible_resp_data = jsonable_encoder(Resp(age=requess.age, sex=sex, smoker=int(smoker),
104
+ bmi=requess.bmi, children=requess.children, region=get_region_name(requess.region), insurance_cost=output))
105
+ return JSONResponse(content=json_compatible_resp_data)
106
+
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f0e2966701ef9bda29bde5d34b776789a0f614589c179d44bf33dbde8b3362f0
3
+ size 630
pyproject.toml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "railway-fastapi-template"
3
+ version = "0.1.0"
4
+ description = "This example starts up a FastAPI server"
5
+ authors = ["DeviousLab <deviouslab@gmail.com>"]
6
+ license = "MIT"
7
+
8
+ [tool.poetry.dependencies]
9
+ python = "^3.10"
10
+ fastapi = "^0.80.0"
11
+ uvicorn = "^0.18.3"
12
+
13
+ [tool.poetry.dev-dependencies]
14
+
15
+ [build-system]
16
+ requires = ["poetry-core>=1.0.0"]
17
+ build-backend = "poetry.core.masonry.api"
requirements.txt ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ anyio>=3.7.1
2
+ click==8.1.7
3
+ fastapi==0.103.2
4
+ h11==0.14.0
5
+ httptools==0.6.0
6
+ idna==3.4
7
+ pydantic==2.4.2
8
+ python-dotenv==1.0.0
9
+ python-multipart==0.0.6
10
+ PyYAML==6.0.1
11
+ sniffio==1.3.0
12
+ starlette>=0.27.0
13
+ typing_extensions==4.8.0
14
+ uvicorn==0.23.2
15
+ watchfiles==0.20.0
16
+ websockets==11.0.3
17
+ scikit-learn
18
+ jinja2