-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend.html
339 lines (298 loc) · 11.5 KB
/
frontend.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="./static/icon.jpg">
<title>Japanese Sensei</title>
<style>
body {
background-image: url('./static/background.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
display: flex;
justify-content: space-between; /* Distribute items evenly along the main axis */
align-items: flex-start;
}
.container {
width: 50%; /* 50% of the page width */
padding: 20px;
position: relative;
min-height: 100vh; /* Minimum height to fill the viewport */
}
.upload-section {
width: 50%; /* 50% of the page width */
padding: 20px;
position: relative;
min-height: 100vh; /* Minimum height to fill the viewport */
}
input[type="text"] {
width: calc(100% - 16px); /* Adjust for padding */
margin-bottom: 10px;
padding: 8px;
box-sizing: border-box;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
.progress-container {
width: 50%; /* 50% of the page width */
background-color: #f5f5f5;
border-radius: 5px;
margin-bottom: 20px;
}
.progress-bar {
height: 30px;
background-color: #4caf50;
width: 0%;
border-radius: 5px;
}
.loading {
height: 30px;
width: 30px;
display: none; /* Initially hide the loading GIF */
}
textarea {
width: 100%;
resize: none;
}
.upload-section {
margin-left: 20px;
}
.upload-form {
display: flex;
align-items: center;
}
.upload-form input[type="file"] {
margin-right: 10px;
}
canvas {
border: 2px solid black;
cursor: crosshair;
max-width: 100%;
height: auto;
}
#output {
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<br>
<h2>Quiz Options</h2>
<label for="question">Enter Question:</label>
<input type="text" id="question" placeholder="Enter your question...">
<label for="option1">Option 1:</label>
<input type="text" id="option1" placeholder="Option 1...">
<label for="option2">Option 2:</label>
<input type="text" id="option2" placeholder="Option 2...">
<label for="option3">Option 3:</label>
<input type="text" id="option3" placeholder="Option 3...">
<label for="option4">Option 4:</label>
<input type="text" id="option4" placeholder="Option 4...">
<button id="submitBtn" onclick="submitOptions()">Submit</button>
<br>
<label for="answer">Answer:</label>
<textarea id="answer" rows="1" cols="80" readonly></textarea>
<br>
<label for="explanation">Explanation:</label>
<textarea id="explanation" rows="4" cols="80" readonly></textarea>
<div class="progress-container">
<div class="progress-bar" id="progress-bar"></div>
</div>
<img src="./static/load.gif" alt="Loading..." class="loading" id="loadingGif">
</div>
<div class="upload-section">
<h2>Or Upload an Image of the Question!</h2>
<form id="uploadForm" enctype="multipart/form-data">
<label for="numOptions">Number of Options:</label>
<input type="number" id="numOptions" min="2" max="4" value="4" required>
<input type="file" id="imageUpload" accept="image/*" required>
<br>
<canvas id="imageCanvas"></canvas>
<br>
<button type="button" id="extractButton">Extract Text</button>
</form>
</div>
<script>
// WebSocket connection
var socket = new WebSocket("ws://" + window.location.host + "/ws");
socket.onmessage = function(event) {
var data = JSON.parse(event.data);
if ('progress' in data) {
updateProgress(data.progress);
}
if ('reset' in data) {
resetTextOutput();
}
};
function submitOptions() {
var loadgif = document.getElementById('loadingGif');
var submitBtn = document.getElementById("submitBtn");
loadgif.style.display = 'block';
submitBtn.disabled = "disabled";
var question = document.getElementById("question").value;
var option1 = document.getElementById("option1").value;
var option2 = document.getElementById("option2").value;
var option3 = document.getElementById("option3").value;
var option4 = document.getElementById("option4").value;
fetch('/reterieve_answer', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
question: question,
option1: option1,
option2: option2,
option3: option3,
option4: option4
}),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
document.getElementById("answer").value = JSON.stringify(data).replace('\n', '<br>').replace('"', '');
return retrieveExplanation(data);
})
.then(() => {
submitBtn.disabled = false;
loadgif.style.display = 'none';
})
.catch((error) => {
console.error('Error:', error);
submitBtn.disabled = false;
loadgif.style.display = 'none';
});
}
function retrieveExplanation(data) {
var question = document.getElementById("question").value;
var option1 = document.getElementById("option1").value;
var option2 = document.getElementById("option2").value;
var option3 = document.getElementById("option3").value;
var option4 = document.getElementById("option4").value;
request_body = {
question: question,
option1: option1,
option2: option2,
option3: option3,
option4: option4,
answer: data
}
return new Promise((resolve, reject) => {
fetch('/retrieve_explanation', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(request_body)
})
.then(response => response.json())
.then(explanationData => {
console.log('Explanation:', explanationData);
document.getElementById("explanation").value = explanationData;
resolve();
})
.catch((error) => {
console.error('Error retrieving explanation:', error);
reject(error); // Reject the promise with the error
});
});
}
function updateProgress(progress) {
document.getElementById('progress-bar').style.width = progress + '%';
}
function resetTextOutput() {
document.getElementById('answer').value = "";
document.getElementById('explanation').value = "";
}
let canvas, ctx, img;
let isDrawing = false;
let startX, startY, endX, endY;
document.getElementById('imageUpload').addEventListener('change', function(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function(event) {
displayImage(event.target.result);
};
reader.readAsDataURL(file);
});
function displayImage(imageData) {
canvas = document.getElementById('imageCanvas');
ctx = canvas.getContext('2d');
img = new Image();
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
};
img.src = imageData;
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mouseup', endDrawing);
canvas.addEventListener('mousemove', drawRectangle);
document.getElementById('extractButton').addEventListener('click',extractText);
}
function startDrawing(event) {
isDrawing = true;
startX = event.offsetX * canvas.width / canvas.clientWidth;
startY = event.offsetY * canvas.height / canvas.clientHeight;
}
function endDrawing() {
isDrawing = false;
}
function drawRectangle(event) {
if (!isDrawing) return;
endX = event.offsetX * canvas.width / canvas.clientWidth;
endY = event.offsetY * canvas.height / canvas.clientHeight;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
ctx.strokeStyle = 'red';
ctx.lineWidth = 3;
ctx.beginPath();
ctx.rect(startX, startY, endX - startX, endY - startY);
ctx.stroke();
}
function extractText() {
const bbox = [startX, startY, endX, endY];
const canvasData = canvas.toDataURL();
const numOptions = document.getElementById("numOptions").value;
const formData = new FormData();
formData.append('image_data', canvasData);
formData.append('x1', Math.round(startX));
formData.append('y1', Math.round(startY));
formData.append('x2', Math.round(endX));
formData.append('y2', Math.round(endY));
formData.append('num_options', numOptions);
fetch('/extract-text/', {
method: 'POST',
body: formData,
})
.then(response => response.json())
.then(data => {
document.getElementById("question").value = data.suggested_question;
// Fill up the suggested options in the respective input fields
data.suggested_options.forEach((option, index) => {
document.getElementById(`option${index + 1}`).value = option;
});
alert('Suggested question and options filled up.');
})
.catch(error => {
console.error('Error extracting text:', error);
alert('Error extracting text');
});
}
</script>
</body>
</html>