Docfile commited on
Commit
4d35288
·
verified ·
1 Parent(s): b326523

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +160 -153
templates/index.html CHANGED
@@ -166,186 +166,193 @@
166
  </div>
167
 
168
  <script>
169
- document.addEventListener('DOMContentLoaded', () => {
170
- const form = document.getElementById('problemForm');
171
- const imageInput = document.getElementById('imageInput');
172
- const loader = document.getElementById('loader');
173
- const solutionDiv = document.getElementById('solution');
174
- const thoughtsContent = document.getElementById('thoughtsContent');
175
- const answerContent = document.getElementById('answerContent');
176
- const thoughtsToggle = document.getElementById('thoughtsToggle');
177
- const thoughtsBox = document.getElementById('thoughtsBox');
178
- const imagePreview = document.getElementById('imagePreview');
179
- const previewImage = document.getElementById('previewImage');
180
- const timestamp = document.getElementById('timestamp');
181
-
182
- let startTime = null;
183
- let timerInterval = null;
184
-
185
- function updateTimestamp() {
186
- if (startTime) {
187
- const elapsed = Math.floor((Date.now() - startTime) / 1000);
188
- timestamp.textContent = elapsed + "s";
189
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  }
 
 
 
 
 
 
191
 
192
- function startTimer() {
193
- startTime = Date.now();
194
- timerInterval = setInterval(updateTimestamp, 1000);
195
- updateTimestamp();
196
- }
197
 
198
- function stopTimer() {
199
- if (timerInterval) {
200
- clearInterval(timerInterval);
201
- timerInterval = null;
202
- }
203
- startTime = null;
204
- timestamp.textContent = "";
205
- }
206
 
207
- thoughtsToggle.addEventListener('click', () => {
208
- thoughtsBox.classList.toggle('open');
209
- thoughtsToggle.querySelector('svg').classList.toggle('rotate-180');
210
- });
211
 
212
- imageInput.addEventListener('change', function(e) {
213
- const file = this.files[0];
214
- if (file) {
215
- const reader = new FileReader();
216
- reader.onload = function(e) {
217
- previewImage.src = e.target.result;
218
- imagePreview.classList.remove('hidden');
219
- }
220
- reader.readAsDataURL(file);
221
- } else {
222
- previewImage.src = "";
223
- imagePreview.classList.add('hidden');
224
- }
225
- });
226
 
227
- const dropZone = document.querySelector('.uploadArea');
 
 
228
 
229
- ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
230
- dropZone.addEventListener(eventName, preventDefaults, false);
231
- });
232
 
233
- function preventDefaults(e) {
234
- e.preventDefault();
235
- e.stopPropagation();
236
- }
237
 
238
- ['dragenter', 'dragover'].forEach(eventName => {
239
- dropZone.addEventListener(eventName, highlight, false);
240
- });
241
 
242
- ['dragleave', 'drop'].forEach(eventName => {
243
- dropZone.addEventListener(eventName, unhighlight, false);
244
- });
245
 
246
- function highlight(e) {
247
- dropZone.classList.add('border-blue-400');
 
 
 
 
 
248
  }
 
 
 
 
 
 
 
 
 
 
 
249
 
250
- function unhighlight(e) {
251
- dropZone.classList.remove('border-blue-400');
252
- }
253
 
254
- dropZone.addEventListener('drop', handleDrop, false);
 
 
 
 
255
 
256
- function handleDrop(e) {
257
- const dt = e.dataTransfer;
258
- const files = dt.files;
259
 
260
- if (files.length) {
261
- imageInput.files = files;
262
- const file = files[0];
263
- const reader = new FileReader();
264
- reader.onload = function(e) {
265
- previewImage.src = e.target.result;
266
- imagePreview.classList.remove('hidden');
267
- }
268
- reader.readAsDataURL(file);
269
- }
270
- }
271
 
272
- form.addEventListener('submit', async (event) => {
273
- event.preventDefault();
274
- const file = imageInput.files[0];
275
- if (!file) {
276
- alert('Veuillez sélectionner une image.');
277
- return;
278
- }
279
 
280
- startTimer();
281
-
282
- loader.classList.remove('hidden');
283
- solutionDiv.classList.add('hidden');
284
- thoughtsContent.innerHTML = '';
285
- answerContent.innerHTML = '';
286
- thoughtsBox.classList.add('open');
287
-
288
- const formData = new FormData();
289
- formData.append('image', file);
290
-
291
- try {
292
- let currentMode = null;
293
- const response = await fetch('/solve', {
294
- method: 'POST',
295
- body: formData
296
- });
297
-
298
- const reader = response.body.getReader();
299
- const decoder = new TextDecoder();
300
- let buffer = '';
301
-
302
- while (true) {
303
- const { done, value } = await reader.read();
304
- if (done) {
305
- stopTimer();
306
- break;
307
- }
308
 
309
- buffer += decoder.decode(value, { stream: true });
310
- let eolIndex;
311
 
312
- while ((eolIndex = buffer.indexOf('\n\n')) >= 0) {
313
- const line = buffer.slice(0, eolIndex).trim();
314
- buffer = buffer.slice(eolIndex + 2);
315
 
316
- if (line.startsWith('data:')) {
317
- const data = JSON.parse(line.slice(5));
318
-
319
- if (data.mode) {
320
- currentMode = data.mode;
321
- loader.classList.add('hidden');
322
- solutionDiv.classList.remove('hidden');
 
 
 
 
 
 
 
 
 
 
323
  }
324
-
325
- if (data.content) {
326
- const content = data.content;
327
- if (currentMode === 'thinking') {
328
- thoughtsContent.innerHTML += `<p>${content}</p>`;
329
- thoughtsContent.scrollTop = thoughtsContent.scrollHeight;
330
- } else if (currentMode === 'answering') {
331
- answerContent.innerHTML += content;
332
- if (window.MathJax) {
333
- MathJax.typesetPromise([answerContent]).catch((err) => console.log('MathJax error:', err));
334
- }
335
- }
336
  }
337
  }
338
  }
339
  }
340
-
341
- } catch (error) {
342
- console.error('Erreur:', error);
343
- alert('Une erreur est survenue lors du traitement de la requête.');
344
- loader.classList.add('hidden');
345
- stopTimer();
346
  }
347
- });
348
- });
 
 
 
 
 
 
 
349
  </script>
350
  </body>
351
  </html>
 
166
  </div>
167
 
168
  <script>
169
+
170
+ document.addEventListener('DOMContentLoaded', () => {
171
+ const form = document.getElementById('problemForm');
172
+ const imageInput = document.getElementById('imageInput');
173
+ const loader = document.getElementById('loader');
174
+ const solutionDiv = document.getElementById('solution');
175
+ const thoughtsContent = document.getElementById('thoughtsContent');
176
+ const answerContent = document.getElementById('answerContent');
177
+ const thoughtsToggle = document.getElementById('thoughtsToggle');
178
+ const thoughtsBox = document.getElementById('thoughtsBox');
179
+ const imagePreview = document.getElementById('imagePreview');
180
+ const previewImage = document.getElementById('previewImage');
181
+ const timestamp = document.getElementById('timestamp');
182
+
183
+ let startTime = null;
184
+ let timerInterval = null;
185
+
186
+ function updateTimestamp() {
187
+ if (startTime) {
188
+ const elapsed = Math.floor((Date.now() - startTime) / 1000);
189
+ timestamp.textContent = elapsed + "s";
190
+ }
191
+ }
192
+
193
+ function startTimer() {
194
+ startTime = Date.now();
195
+ timerInterval = setInterval(updateTimestamp, 1000);
196
+ updateTimestamp();
197
+ }
198
+
199
+ function stopTimer() {
200
+ if (timerInterval) {
201
+ clearInterval(timerInterval);
202
+ timerInterval = null;
203
+ }
204
+ startTime = null;
205
+ timestamp.textContent = "";
206
+ }
207
+
208
+ thoughtsToggle.addEventListener('click', () => {
209
+ thoughtsBox.classList.toggle('open');
210
+ thoughtsToggle.querySelector('svg').classList.toggle('rotate-180');
211
+ });
212
+
213
+ imageInput.addEventListener('change', function(e) {
214
+ const file = this.files[0];
215
+ if (file) {
216
+ const reader = new FileReader();
217
+ reader.onload = function(e) {
218
+ previewImage.src = e.target.result;
219
+ imagePreview.classList.remove('hidden');
220
  }
221
+ reader.readAsDataURL(file);
222
+ } else {
223
+ previewImage.src = "";
224
+ imagePreview.classList.add('hidden');
225
+ }
226
+ });
227
 
228
+ const dropZone = document.querySelector('.uploadArea');
 
 
 
 
229
 
230
+ ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
231
+ dropZone.addEventListener(eventName, preventDefaults, false);
232
+ });
 
 
 
 
 
233
 
234
+ function preventDefaults(e) {
235
+ e.preventDefault();
236
+ e.stopPropagation();
237
+ }
238
 
239
+ ['dragenter', 'dragover'].forEach(eventName => {
240
+ dropZone.addEventListener(eventName, highlight, false);
241
+ });
 
 
 
 
 
 
 
 
 
 
 
242
 
243
+ ['dragleave', 'drop'].forEach(eventName => {
244
+ dropZone.addEventListener(eventName, unhighlight, false);
245
+ });
246
 
247
+ function highlight(e) {
248
+ dropZone.classList.add('border-blue-400');
249
+ }
250
 
251
+ function unhighlight(e) {
252
+ dropZone.classList.remove('border-blue-400');
253
+ }
 
254
 
255
+ dropZone.addEventListener('drop', handleDrop, false);
 
 
256
 
257
+ function handleDrop(e) {
258
+ const dt = e.dataTransfer;
259
+ const files = dt.files;
260
 
261
+ if (files.length) {
262
+ imageInput.files = files;
263
+ const file = files[0];
264
+ const reader = new FileReader();
265
+ reader.onload = function(e) {
266
+ previewImage.src = e.target.result;
267
+ imagePreview.classList.remove('hidden');
268
  }
269
+ reader.readAsDataURL(file);
270
+ }
271
+ }
272
+
273
+ form.addEventListener('submit', async (event) => {
274
+ event.preventDefault();
275
+ const file = imageInput.files[0];
276
+ if (!file) {
277
+ alert('Veuillez sélectionner une image.');
278
+ return;
279
+ }
280
 
281
+ startTimer();
 
 
282
 
283
+ loader.classList.remove('hidden');
284
+ solutionDiv.classList.add('hidden');
285
+ thoughtsContent.innerHTML = '';
286
+ answerContent.innerHTML = '';
287
+ thoughtsBox.classList.add('open');
288
 
289
+ const formData = new FormData();
290
+ formData.append('image', file);
 
291
 
292
+ try {
293
+ let currentMode = null;
294
+ const response = await fetch('/solve', {
295
+ method: 'POST',
296
+ body: formData
297
+ });
 
 
 
 
 
298
 
299
+ const reader = response.body.getReader();
300
+ const decoder = new TextDecoder();
301
+ let buffer = '';
 
 
 
 
302
 
303
+ while (true) {
304
+ const { done, value } = await reader.read();
305
+ if (done) {
306
+ stopTimer();
307
+ break;
308
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
 
310
+ buffer += decoder.decode(value, { stream: true });
311
+ let eolIndex;
312
 
313
+ while ((eolIndex = buffer.indexOf('\n\n')) >= 0) {
314
+ const line = buffer.slice(0, eolIndex).trim();
315
+ buffer = buffer.slice(eolIndex + 2);
316
 
317
+ if (line.startsWith('data:')) {
318
+ const data = JSON.parse(line.slice(5));
319
+
320
+ if (data.mode) {
321
+ currentMode = data.mode;
322
+ loader.classList.add('hidden');
323
+ solutionDiv.classList.remove('hidden');
324
+ }
325
+
326
+ if (data.content) {
327
+ const content = data.content;
328
+ if (currentMode === 'thinking') {
329
+ thoughtsContent.innerHTML += `<p>${content}</p>`;
330
+ if (window.MathJax) {
331
+ MathJax.typesetPromise([thoughtsContent]).catch((err) =>
332
+ console.log('MathJax error:', err)
333
+ );
334
  }
335
+ thoughtsContent.scrollTop = thoughtsContent.scrollHeight;
336
+ } else if (currentMode === 'answering') {
337
+ answerContent.innerHTML += content;
338
+ if (window.MathJax) {
339
+ MathJax.typesetPromise([answerContent]).catch((err) =>
340
+ console.log('MathJax error:', err)
341
+ );
 
 
 
 
 
342
  }
343
  }
344
  }
345
  }
 
 
 
 
 
 
346
  }
347
+ }
348
+ } catch (error) {
349
+ console.error('Erreur:', error);
350
+ alert('Une erreur est survenue lors du traitement de la requête.');
351
+ loader.classList.add('hidden');
352
+ stopTimer();
353
+ }
354
+ });
355
+ });
356
  </script>
357
  </body>
358
  </html>