HilmiZr commited on
Commit
e5ae16b
·
1 Parent(s): 6334909

added: base files

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. app.py +17 -0
  3. requirements.txt +7 -0
  4. templates/index.html +128 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ COPY --chown=user . /app
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.templating import Jinja2Templates
3
+
4
+ app = FastAPI()
5
+
6
+ # Define the directory for templates
7
+ templates = Jinja2Templates(directory="templates")
8
+
9
+ @app.get("/")
10
+ async def root(request: Request):
11
+ return templates.TemplateResponse(
12
+ "index.html",
13
+ {
14
+ "request": request,
15
+ "insurance_cost": 0,
16
+ }
17
+ )
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ pydantic
3
+ python-multipart
4
+ uvicorn
5
+ starlette
6
+ scikit-learn
7
+ jinja2
templates/index.html ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ <!-- Navigation Bar -->
14
+ <div class="bg-black text-white flex flex-row justify-end py-1">
15
+ <button class="mx-8 font-bold rounded-2xl p-2 hover:bg-purple-500 duration-150">Model 1</button>
16
+ </div>
17
+
18
+ <!-- Model 1 Section -->
19
+ <div id="Model1" class="Model bg-gradient-to-r from-purple-400 to-purple-700 text-gray-800 block">
20
+ <section class="flex flex-col justify-between item-center p-8 min-h-screen md:flex-row">
21
+ <!-- Prediction Form -->
22
+ <main class="bg-white p-16 rounded-lg">
23
+ <h1 class="text-3xl font-bold mb-8">Predict Your Insurance Cost:</h1>
24
+
25
+ <form enctype="multipart/form-data" class="flex flex-col" id="upload_form">
26
+ <label for="age" class="mb-2">Age</label>
27
+ <input type="text" name="age" placeholder="Age" required="required" class="p-4 bg-gray-100 rounded-md" />
28
+
29
+ <label for="sex" class="mt-4 mb-2">Gender:</label>
30
+ <select name="sex" id="sex" class="p-4 bg-gray-100 rounded-md">
31
+ <option value="1">Male</option>
32
+ <option value="0">Female</option>
33
+ </select>
34
+
35
+ <label for="smoker" class="mt-4 mb-2">Smoker:</label>
36
+ <select name="smoker" id="smoker" class="p-4 bg-gray-100 rounded-md">
37
+ <option value="1">Yes</option>
38
+ <option value="0">No</option>
39
+ </select>
40
+
41
+ <label for="bmi" class="mt-4 mb-2">BMI:</label>
42
+ <input type="text" name="bmi" placeholder="BMI" required="required" class="p-4 bg-gray-100 rounded-md" />
43
+
44
+ <label for="children" class="mt-4 mb-2">Children:</label>
45
+ <input type="text" name="children" placeholder="Children" required="required" class="p-4 bg-gray-100 rounded-md" />
46
+
47
+ <label for="region" class="mt-4 mb-2">Region:</label>
48
+ <select name="region" id="region" class="p-4 bg-gray-100 rounded-md">
49
+ <option value="3">Southwest</option>
50
+ <option value="2">Southeast</option>
51
+ <option value="1">Northwest</option>
52
+ <option value="0">Northeast</option>
53
+ </select>
54
+
55
+ <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">
56
+ PREDICT NOW
57
+ <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">
58
+ <line x1="5" y1="12" x2="19" y2="12"></line>
59
+ <polyline points="12 5 19 12 12 19"></polyline>
60
+ </svg>
61
+ </button>
62
+ </form>
63
+ </main>
64
+
65
+ <!-- Response Display Section -->
66
+ <section class="mt-8 h-full flex-auto rounded-lg md:mt-0 md:ml-8">
67
+ <div class="bg-white p-16 rounded-lg">
68
+ <h1 class="text-2xl mb-8">Your estimated insurance cost:</h1>
69
+ <h2 class="text-5xl font-bold">$<span id="insurance_cost">0.00 USD</span></h2>
70
+ <div class="mt-8">
71
+ <p id="respAge"></p>
72
+ <p id="respSex"></p>
73
+ <p id="respSmoker"></p>
74
+ <p id="respBmi"></p>
75
+ <p id="respChildren"></p>
76
+ <p id="respRegion"></p>
77
+ </div>
78
+ </div>
79
+ <div class="flex justify-around p-16 rounded-lg mt-8">
80
+ <img class="h-24 md:h-28" src="https://elib.its.ac.id/conf/cyberdas/public/assets/img/img_navbar.svg" alt="logo-dts" />
81
+ </div>
82
+ </section>
83
+ </section>
84
+ </div>
85
+
86
+ <!-- Styling -->
87
+ <style>
88
+ .active {
89
+ background-color: rgb(124 58 237);
90
+ }
91
+ </style>
92
+ </body>
93
+
94
+ <!-- JavaScript for Form Submission and AJAX -->
95
+ <script>
96
+ $(document).ready(function () {
97
+ $("#upload_form").submit(function (event) {
98
+ event.preventDefault();
99
+
100
+ // Get the form data
101
+ var formData = new FormData(this);
102
+
103
+ // Make the Ajax request
104
+ $.ajax({
105
+ type: "POST",
106
+ url: "https://hilmizr-pdst-regression-be-v1.hf.space/predict",
107
+ data: formData,
108
+ processData: false,
109
+ contentType: false,
110
+ success: function (response) {
111
+ // Handle the response data
112
+ $('#respAge').html("<strong>Age:</strong> " + response.age + " tahun");
113
+ $('#respSex').html("<strong>Gender:</strong> " + response.sex);
114
+ $('#respSmoker').html("<strong>Smoker:</strong> " + response.smoker);
115
+ $('#respBmi').html("<strong>BMI:</strong> " + response.bmi);
116
+ $('#respChildren').html("<strong>Children:</strong> " + response.children);
117
+ $('#respRegion').html("<strong>Region:</strong> " + response.region);
118
+ $('#insurance_cost').html(parseFloat(response.insurance_cost).toFixed(2) + ' USD');
119
+ console.log(response);
120
+ },
121
+ error: function (error) {
122
+ console.error("Error:", error);
123
+ }
124
+ });
125
+ });
126
+ });
127
+ </script>
128
+ </html>