Kadigas commited on
Commit
6411ffa
·
1 Parent(s): 6db84c9

initial commit

Browse files
Files changed (4) hide show
  1. Dockerfile +20 -0
  2. main.py +44 -0
  3. requirements.txt +18 -0
  4. templates/index.html +136 -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"]
main.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("/welcomeMessage")
33
+ async def welcome():
34
+ return {"message": "Hello World. Welcome to FastAPI!"}
35
+
36
+ @app.get("/")
37
+ async def root(request: Request):
38
+ return templates.TemplateResponse(
39
+ "index.html",
40
+ {
41
+ "request": request,
42
+ "insurance_cost": 0,
43
+ }
44
+ )
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,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>
12
+ <div class="bg-black text-white flex flex-row justify-end py-1">
13
+ <button class="mx-8 font-bold tablink active rounded-2xl p-2 hover:bg-purple-500 duration-150" onclick="openModel(event, 'Model1')">Model 1</button>
14
+ <button class="mx-8 font-bold tablink rounded-2xl p-2 hover:bg-purple-500 duration-150" onclick="openModel(event, 'Model2')">Model 2</button>
15
+ </div>
16
+
17
+ <div id="Model1" class="Model bg-gradient-to-r from-purple-400 to-purple-700 text-gray-800">
18
+ <section class="flex flex-col justify-between item-center p-8 min-h-screen md:flex-row">
19
+ <main class="bg-white p-16 rounded-lg">
20
+ <h1 class="text-3xl font-bold mb-8">Prediksi Biaya Asuransi Anda:</h1>
21
+
22
+ <form action="https://alproits-cyberdas-lr.hf.space/predict" method="post" enctype="multipart/form-data" class="flex flex-col">
23
+ <label for="age" class="mb-2">Usia</label>
24
+ <input type="text" name="age" placeholder="Usia" required="required" class="p-4 bg-gray-100 rounded-md" />
25
+
26
+ <label for="sex" class="mt-4 mb-2">Jenis Kelamin:</label>
27
+ <select name="sex" id="sex" class="p-4 bg-gray-100 rounded-md">
28
+ <option value="1">Laki-laki</option>
29
+ <option value="0">Perempuan</option>
30
+ </select>
31
+
32
+ <label for="smoker" class="mt-4 mb-2">Perokok:</label>
33
+ <select name="smoker" id="smoker" class="p-4 bg-gray-100 rounded-md">
34
+ <option value="1">Ya</option>
35
+ <option value="0">Tidak</option>
36
+ </select>
37
+
38
+ <label for="bmi" class="mt-4 mb-2">BMI:</label>
39
+ <input type="text" name="bmi" placeholder="BMI" required="required" class="p-4 bg-gray-100 rounded-md" />
40
+
41
+ <label for="children" class="mt-4 mb-2">Jumlah Anak:</label>
42
+ <input type="text" name="children" placeholder="Jumlah Anak" required="required" class="p-4 bg-gray-100 rounded-md" />
43
+
44
+ <label for="region" class="mt-4 mb-2">Region:</label>
45
+ <select name="region" id="region" class="p-4 bg-gray-100 rounded-md">
46
+ <option value="3">Southwest</option>
47
+ <option value="2">Southeast</option>
48
+ <option value="1">Northwest</option>
49
+ <option value="0">Northeast</option>
50
+ </select>
51
+
52
+ <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">
53
+ 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">
54
+ <line x1="5" y1="12" x2="19" y2="12"></line>
55
+ <polyline points="12 5 19 12 12 19"></polyline>
56
+ </svg>
57
+ </button>
58
+ </form>
59
+ </main>
60
+ <section class="mt-8 h-full flex-auto rounded-lg md:mt-0 md:ml-8">
61
+ <div class="bg-white p-16 rounded-lg">
62
+ <h1 class="text-2xl mb-8">Estimasi biaya asuransi Anda adalah</h1>
63
+ <h2 class="text-5xl font-bold">${{ '{:,.2f}'.format(insurance_cost) }}</h2>
64
+ {% if insurance_cost != 0 %}
65
+ <div class="mt-8">
66
+ <p><strong>Usia:</strong> {{ age }} tahun</p>
67
+ <p><strong>Jenis kelamin:</strong> {{ sex }}</p>
68
+ <p><strong>Status perokok:</strong> {{ smoker }}</p>
69
+ <p><strong>BMI:</strong> {{ bmi }}</p>
70
+ <p><strong>Jumlah Anak:</strong> {{ children }}</p>
71
+ <p><strong>Region:</strong> {{ region }}</p>
72
+ </div>
73
+ {% endif %}
74
+ </div>
75
+ <div class="flex justify-around p-16 rounded-lg mt-8">
76
+ <img class="h-24 md:h-28" src="https://elib.its.ac.id/conf/cyberdas/public/assets/img/img_navbar.svg" alt="logo-dts" />
77
+ </div>
78
+ </section>
79
+ </section>
80
+ </div>
81
+
82
+ <div id="Model2" class="Model bg-gradient-to-r from-green-400 to-green-700 text-gray-800">
83
+ <section class="flex flex-col justify-between item-center p-8 min-h-screen md:flex-row">
84
+ <section class="mt-8 flex-auto rounded-lg md:mt-0 md:ml-8">
85
+ <div class="bg-white p-16 rounded-lg">
86
+ <h1 class="text-3xl font-bold mb-8">Prediksi :</h1>
87
+ <form action="https://alproits-cyberdas-lr.hf.space/predict" method="post" enctype="multipart/form-data" class="flex flex-col">
88
+ <label for="">
89
+
90
+ </label>
91
+ <button type="submit" class="flex justify-center align-center mt-8 p-4 bg-gradient-to-r from-green-400 to-green-700 text-white rounded-md">
92
+ 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">
93
+ <line x1="5" y1="12" x2="19" y2="12"></line>
94
+ <polyline points="12 5 19 12 12 19"></polyline>
95
+ </svg>
96
+ </button>
97
+ </form>
98
+ </div>
99
+ </section>
100
+ <section class="mt-8 h-full flex-auto rounded-lg md:mt-0 md:ml-8">
101
+ <div class="bg-white p-16 rounded-lg">
102
+ <h1 class="text-3xl font-bold mb-8"> Hasil Prediksi :</h1>
103
+ </div>
104
+ <div class="flex justify-around p-16 rounded-lg mt-8">
105
+ <img class="h-24 md:h-28" src="https://elib.its.ac.id/conf/cyberdas/public/assets/img/img_navbar.svg" alt="logo-dts" />
106
+ </div>
107
+ </section>
108
+ </section>
109
+ </div>
110
+
111
+ <style>
112
+ .active {
113
+ background-color: rgb(124 58 237);
114
+ }
115
+ #Model2 {
116
+ display: none;
117
+ }
118
+ </style>
119
+ </body>
120
+
121
+ <script>
122
+ function openModel(evt, modelName) {
123
+ var i, x, tablinks;
124
+ x = document.getElementsByClassName("Model");
125
+ for (i = 0; i < x.length; i++) {
126
+ x[i].style.display = "none";
127
+ }
128
+ tablinks = document.getElementsByClassName("tablink");
129
+ for (i = 0; i < x.length; i++) {
130
+ tablinks[i].className = tablinks[i].className.replace(" active", "");
131
+ }
132
+ document.getElementById(modelName).style.display = "block";
133
+ evt.currentTarget.className += " active";
134
+ }
135
+ </script>
136
+ </html>