albin commited on
Commit
b790a6c
·
1 Parent(s): afa5871

add front files and connect to the back

Browse files
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"]
__pycache__/main.cpython-310.pyc ADDED
Binary file (1.27 kB). View file
 
main.cpython-310.pyc ADDED
Binary file (1.27 kB). View file
 
main.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ class Msg(BaseModel):
16
+ msg: str
17
+
18
+
19
+ class Req(BaseModel):
20
+ age: int
21
+ sex: int
22
+ smoker: int
23
+ bmi: float
24
+ children: int
25
+ region: int
26
+
27
+
28
+ @app.get("/welcomeMessage")
29
+ async def welcome():
30
+ return {"message": "Hello World. Welcome to FastAPI!"}
31
+
32
+ @app.get("/")
33
+ async def root(request: Request):
34
+ return templates.TemplateResponse(
35
+ "index.html",
36
+ {
37
+ "request": request,
38
+ "insurance_cost": 0,
39
+ }
40
+ )
templates/index.html ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
9
+ <title>Insurance Cost Prediction Application</title>
10
+ </head>
11
+
12
+ <body>
13
+ <div class="bg-black text-white flex flex-row justify-end py-1">
14
+ <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>
15
+ <button class="mx-8 font-bold tablink rounded-2xl p-2 hover:bg-purple-500 duration-150" onclick="openModel(event, 'Model2')">Model 2</button>
16
+ <button class="mx-8 font-bold tablink rounded-2xl p-2 hover:bg-purple-500 duration-150" onclick="openModel(event, 'Model3')">Model 3</button>
17
+ </div>
18
+
19
+ <div id="Model1" class="Model bg-gradient-to-r from-purple-400 to-purple-700 text-gray-800">
20
+ <section class="flex flex-col justify-between item-center p-8 min-h-screen md:flex-row">
21
+ <main class="bg-white p-16 rounded-lg">
22
+ <h1 class="text-3xl font-bold mb-8">Predict Your Insurance Cost:</h1>
23
+
24
+ <form enctype="multipart/form-data" class="flex flex-col" id="upload_form">
25
+ <label for="age" class="mb-2">Age</label>
26
+ <input type="text" name="age" placeholder="Age" required="required" class="p-4 bg-gray-100 rounded-md" />
27
+
28
+ <label for="sex" class="mt-4 mb-2">Gender:</label>
29
+ <select name="sex" id="sex" class="p-4 bg-gray-100 rounded-md">
30
+ <option value="1">Male</option>
31
+ <option value="0">Female</option>
32
+ </select>
33
+
34
+ <label for="smoker" class="mt-4 mb-2">Smoker:</label>
35
+ <select name="smoker" id="smoker" class="p-4 bg-gray-100 rounded-md">
36
+ <option value="1">Yes</option>
37
+ <option value="0">No</option>
38
+ </select>
39
+
40
+ <label for="bmi" class="mt-4 mb-2">BMI:</label>
41
+ <input type="text" name="bmi" placeholder="BMI" required="required" class="p-4 bg-gray-100 rounded-md" />
42
+
43
+ <label for="children" class="mt-4 mb-2">Children:</label>
44
+ <input type="text" name="children" placeholder="Children" required="required" class="p-4 bg-gray-100 rounded-md" />
45
+
46
+ <label for="region" class="mt-4 mb-2">Region:</label>
47
+ <select name="region" id="region" class="p-4 bg-gray-100 rounded-md">
48
+ <option value="3">Southwest</option>
49
+ <option value="2">Southeast</option>
50
+ <option value="1">Northwest</option>
51
+ <option value="0">Northeast</option>
52
+ </select>
53
+
54
+ <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">
55
+ PREDICT NOW<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">
56
+ <line x1="5" y1="12" x2="19" y2="12"></line>
57
+ <polyline points="12 5 19 12 12 19"></polyline>
58
+ </svg>
59
+ </button>
60
+ </form>
61
+ </main>
62
+ <section class="mt-8 h-full flex-auto rounded-lg md:mt-0 md:ml-8">
63
+ <div class="bg-white p-16 rounded-lg">
64
+ <h1 class="text-2xl mb-8">Your estimate insurance cost:</h1>
65
+ <h2 class="text-5xl font-bold">$<span id="insurance_cost">0.00 USD</span></h2>
66
+ <div class="mt-8">
67
+ <p id="respAge"></p>
68
+ <p id="respSex"></p>
69
+ <p id="respSmoker"></p>
70
+ <p id="respBmi"></p>
71
+ <p id="respChildren"></p>
72
+ <p id="respRegion"></p>
73
+ </div>
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
+ <style>
83
+ .active {
84
+ background-color: rgb(124 58 237);
85
+ }
86
+ #Model2 {
87
+ display: none;
88
+ }
89
+ </style>
90
+ </body>
91
+
92
+ <script>
93
+ function openModel(evt, modelName) {
94
+ var i, x, tablinks;
95
+ x = document.getElementsByClassName("Model");
96
+ for (i = 0; i < x.length; i++) {
97
+ x[i].style.display = "none";
98
+ }
99
+ tablinks = document.getElementsByClassName("tablink");
100
+ for (i = 0; i < x.length; i++) {
101
+ tablinks[i].className = tablinks[i].className.replace(" active", "");
102
+ }
103
+ document.getElementById(modelName).style.display = "block";
104
+ evt.currentTarget.className += " active";
105
+ }
106
+ </script>
107
+ <script>
108
+ $(document).ready(function () {
109
+ $("#upload_form").submit(function (event) {
110
+ event.preventDefault(); // Prevent the default form submission
111
+
112
+ // Get the form data
113
+ var formData = new FormData(this);
114
+
115
+ // Make the Ajax request
116
+ $.ajax({
117
+ type: "POST",
118
+ url: "https://binoubinks-ads-back.hf.space/predict",
119
+ data: formData,
120
+ processData: false,
121
+ contentType: false,
122
+ success: function (response) {
123
+ // Handle the response data
124
+ $('#respAge').html("<strong>Age:</strong> " + response.age + " tahun");
125
+ $('#respSex').html("<strong>Gender:</strong> " + response.sex);
126
+ $('#respSmoker').html("<strong>Smoker:</strong> "+ response.smoker);
127
+ $('#respBmi').html("<strong>BMI:</strong> " + response.bmi);
128
+ $('#respChildren').html("<strong>Children:</strong> " + response.children);
129
+ $('#respRegion').html("<strong>Region:</strong> " + response.region);
130
+ $('#insurance_cost').html(parseFloat(response.insurance_cost).toFixed(2) + ' USD');
131
+ console.log(response);
132
+ },
133
+ error: function (error) {
134
+ console.error("Error:", error);
135
+ }
136
+ });
137
+ });
138
+ });
139
+ </script>
140
+ </body>
141
+ </html>