HilmiZr commited on
Commit
a244b20
·
1 Parent(s): 3036228

Add application file

Browse files
.vscode/settings.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "liveServer.settings.port": 5501
3
+ }
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.32 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,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # encoding: utf-8
3
+
4
+ from fastapi import FastAPI, Form, Depends, Request
5
+ from fastapi.templating import Jinja2Templates
6
+ from pydantic import BaseModel
7
+ import pickle
8
+ import json
9
+
10
+ app = FastAPI()
11
+
12
+ # Menentukan direktori templates
13
+ templates = Jinja2Templates(directory="templates")
14
+
15
+ model_file = open('insurance_model.pkl', 'rb')
16
+ model = pickle.load(model_file, encoding='bytes')
17
+
18
+
19
+ class Msg(BaseModel):
20
+ msg: str
21
+
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
+
32
+ @app.get("/")
33
+ async def root():
34
+ return {"message": "Hello World. Welcome to FastAPI!"}
35
+
36
+
37
+ def form_req(age: str = Form(...), sex: str = Form(...), smoker: str = Form(...),
38
+ bmi: str = Form(...), children: str = Form(...), region: str = Form(...)):
39
+ sBmi = bmi.replace(",", ".")
40
+ return Req(age=int(age), sex=int(sex), smoker=int(smoker), bmi=float(sBmi), children=int(children), region=int(region))
41
+
42
+
43
+ @app.get("/path")
44
+ async def demo_get():
45
+ return {"message": "This is /path endpoint, use a post request to transform the text to uppercase"}
46
+
47
+
48
+ @app.post("/path")
49
+ async def demo_post(inp: Msg):
50
+ return {"message": inp.msg.upper()}
51
+
52
+
53
+ @app.get("/path/{path_id}")
54
+ async def demo_get_path_id(path_id: int):
55
+ return {"message": f"This is /path/{path_id} endpoint, use post request to retrieve result"}
56
+
57
+
58
+ @app.get("/predict/{path_id}")
59
+ async def predict(path_id: int):
60
+ return {"message": f"This is /predict/{path_id} endpoint, use post request to retrieve result"}
61
+
62
+
63
+ @app.post("/predict")
64
+ async def predict(request: Request, requess: Req = Depends(form_req)):
65
+ '''
66
+ Predict the insurance cost based on user inputs
67
+ and render the result to the html page
68
+ '''
69
+ age = requess.age
70
+ sex = requess.sex
71
+ smoker = requess.smoker
72
+ bmi = requess.bmi
73
+ children = requess.children
74
+ region = requess.region
75
+ data = []
76
+
77
+ data.append(int(age))
78
+ data.extend([int(sex)])
79
+ data.extend([float(bmi)])
80
+ data.extend([int(children)])
81
+ data.extend([int(smoker)])
82
+ data.extend([int(region)])
83
+
84
+ prediction = model.predict([data])
85
+ output = round(prediction[0], 2)
86
+
87
+ # Merender index.html dengan data hasil prediksi
88
+ return templates.TemplateResponse(
89
+ "index.html",
90
+ {
91
+ "request": request,
92
+ "insurance_cost": output,
93
+ "age": requess.age,
94
+ "sex": "Laki-laki" if requess.sex == 1 else "Perempuan",
95
+ "smoker": "Ya" if requess.smoker == 1 else "Tidak",
96
+ "bmi": requess.bmi, # Menambahkan ini
97
+ "children": requess.children, # Menambahkan ini
98
+ "region": get_region_name(requess.region) # Menambahkan ini
99
+ }
100
+ )
101
+
102
+ def get_region_name(region_code):
103
+ region_mapping = {
104
+ 0: "Northeast",
105
+ 1: "Northwest",
106
+ 2: "Southeast",
107
+ 3: "Southwest"
108
+ }
109
+ return region_mapping.get(region_code, "Unknown")
110
+
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
templates/index.html ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />
8
+ <title>Aplikasi Prediksi Biaya Asuransi</title>
9
+ </head>
10
+
11
+ <body class="flex flex-col justify-between item-center p-8 min-h-screen bg-gradient-to-r from-purple-400 to-purple-700 text-gray-800 md:flex-row">
12
+ <main class="bg-white p-16 rounded-lg">
13
+ <h1 class="text-3xl font-bold mb-8">Prediksi Biaya Asuransi Anda:</h1>
14
+
15
+ <form action="http://127.0.0.1:8000/predict" method="post" enctype="multipart/form-data" class="flex flex-col">
16
+ <label for="age" class="mb-2">Usia</label>
17
+ <input type="text" name="age" placeholder="Usia" required="required" class="p-4 bg-gray-100 rounded-md" />
18
+
19
+ <label for="sex" class="mt-4 mb-2">Jenis Kelamin:</label>
20
+ <select name="sex" id="sex" class="p-4 bg-gray-100 rounded-md">
21
+ <option value="1">Laki-laki</option>
22
+ <option value="0">Perempuan</option>
23
+ </select>
24
+
25
+ <label for="smoker" class="mt-4 mb-2">Perokok:</label>
26
+ <select name="smoker" id="smoker" class="p-4 bg-gray-100 rounded-md">
27
+ <option value="1">Ya</option>
28
+ <option value="0">Tidak</option>
29
+ </select>
30
+
31
+ <label for="bmi" class="mt-4 mb-2">BMI:</label>
32
+ <input type="text" name="bmi" placeholder="BMI" required="required" class="p-4 bg-gray-100 rounded-md" />
33
+
34
+ <label for="children" class="mt-4 mb-2">Jumlah Anak:</label>
35
+ <input type="text" name="children" placeholder="Jumlah Anak" required="required" class="p-4 bg-gray-100 rounded-md" />
36
+
37
+ <label for="region" class="mt-4 mb-2">Region:</label>
38
+ <select name="region" id="region" class="p-4 bg-gray-100 rounded-md">
39
+ <option value="3">Southwest</option>
40
+ <option value="2">Southeast</option>
41
+ <option value="1">Northwest</option>
42
+ <option value="0">Northeast</option>
43
+ </select>
44
+
45
+ <button type="submit" class="flex justify-center align-center mt-8 p-4 bg-gradient-to-r from-purple-400 to-purple-700 text-white rounded-md">
46
+ PREDIKSI SEKARANG<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right ml-4">
47
+ <line x1="5" y1="12" x2="19" y2="12"></line>
48
+ <polyline points="12 5 19 12 12 19"></polyline>
49
+ </svg>
50
+ </button>
51
+ </form>
52
+ </main>
53
+ <section class="mt-8 h-full flex-auto rounded-lg md:mt-0 md:ml-8">
54
+ <div class="bg-white p-16 rounded-lg">
55
+ <h1 class="text-2xl mb-8">Estimasi biaya asuransi Anda adalah</h1>
56
+ <h2 class="text-5xl font-bold">${{ '{:,.2f}'.format(insurance_cost) }} USD</h2>
57
+ {% if insurance_cost != 0 %}
58
+ <div class="mt-8">
59
+ <p><strong>Usia:</strong> {{ age }} tahun</p>
60
+ <p><strong>Jenis kelamin:</strong> {{ sex }}</p>
61
+ <p><strong>Status perokok:</strong> {{ smoker }}</p>
62
+ <p><strong>BMI:</strong> {{ bmi }}</p>
63
+ <p><strong>Jumlah Anak:</strong> {{ children }}</p>
64
+ <p><strong>Region:</strong> {{ region }}</p>
65
+ </div>
66
+ {% endif %}
67
+ </div>
68
+ <div class="flex justify-around p-16 rounded-lg mt-8">
69
+ <img class="h-24 md:h-28" src="https://elib.its.ac.id/conf/cyberdas/public/assets/img/img_navbar.svg" alt="logo-dts" />
70
+ </div>
71
+ </section>
72
+ </body>
73
+ </html>