Spaces:
Running
Running
Create index.html
Browse files- templates/index.html +128 -0
templates/index.html
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
|
2 |
+
dropZone.addEventListener(eventName, preventDefaults, false);
|
3 |
+
});
|
4 |
+
|
5 |
+
function preventDefaults(e) {
|
6 |
+
e.preventDefault();
|
7 |
+
e.stopPropagation();
|
8 |
+
}
|
9 |
+
|
10 |
+
['dragenter', 'dragover'].forEach(eventName => {
|
11 |
+
dropZone.addEventListener(eventName, highlight, false);
|
12 |
+
});
|
13 |
+
|
14 |
+
['dragleave', 'drop'].forEach(eventName => {
|
15 |
+
dropZone.addEventListener(eventName, unhighlight, false);
|
16 |
+
});
|
17 |
+
|
18 |
+
function highlight(e) {
|
19 |
+
dropZone.classList.add('border-blue-400');
|
20 |
+
}
|
21 |
+
|
22 |
+
function unhighlight(e) {
|
23 |
+
dropZone.classList.remove('border-blue-400');
|
24 |
+
}
|
25 |
+
|
26 |
+
dropZone.addEventListener('drop', handleDrop, false);
|
27 |
+
|
28 |
+
function handleDrop(e) {
|
29 |
+
const dt = e.dataTransfer;
|
30 |
+
const files = dt.files;
|
31 |
+
|
32 |
+
if (files.length) {
|
33 |
+
imageInput.files = files;
|
34 |
+
const file = files[0];
|
35 |
+
const reader = new FileReader();
|
36 |
+
reader.onload = function(e) {
|
37 |
+
previewImage.src = e.target.result;
|
38 |
+
imagePreview.classList.remove('hidden');
|
39 |
+
}
|
40 |
+
reader.readAsDataURL(file);
|
41 |
+
}
|
42 |
+
}
|
43 |
+
|
44 |
+
form.addEventListener('submit', async (event) => {
|
45 |
+
event.preventDefault();
|
46 |
+
const file = imageInput.files[0];
|
47 |
+
if (!file) {
|
48 |
+
alert('Veuillez sélectionner une image.');
|
49 |
+
return;
|
50 |
+
}
|
51 |
+
|
52 |
+
startTimer();
|
53 |
+
|
54 |
+
loader.classList.remove('hidden');
|
55 |
+
solutionDiv.classList.add('hidden');
|
56 |
+
thoughtsContent.innerHTML = '';
|
57 |
+
answerContent.innerHTML = '';
|
58 |
+
thoughtsBox.classList.add('open');
|
59 |
+
|
60 |
+
const formData = new FormData();
|
61 |
+
formData.append('image', file);
|
62 |
+
|
63 |
+
try {
|
64 |
+
let currentMode = null;
|
65 |
+
const response = await fetch('/solve', {
|
66 |
+
method: 'POST',
|
67 |
+
body: formData
|
68 |
+
});
|
69 |
+
|
70 |
+
const reader = response.body.getReader();
|
71 |
+
const decoder = new TextDecoder();
|
72 |
+
let buffer = '';
|
73 |
+
|
74 |
+
while (true) {
|
75 |
+
const { done, value } = await reader.read();
|
76 |
+
if (done) {
|
77 |
+
stopTimer();
|
78 |
+
break;
|
79 |
+
}
|
80 |
+
|
81 |
+
buffer += decoder.decode(value, { stream: true });
|
82 |
+
let eolIndex;
|
83 |
+
|
84 |
+
while ((eolIndex = buffer.indexOf('\n\n')) >= 0) {
|
85 |
+
const line = buffer.slice(0, eolIndex).trim();
|
86 |
+
buffer = buffer.slice(eolIndex + 2);
|
87 |
+
|
88 |
+
if (line.startsWith('data:')) {
|
89 |
+
const data = JSON.parse(line.slice(5));
|
90 |
+
|
91 |
+
if (data.mode) {
|
92 |
+
currentMode = data.mode;
|
93 |
+
loader.classList.add('hidden');
|
94 |
+
solutionDiv.classList.remove('hidden');
|
95 |
+
}
|
96 |
+
|
97 |
+
if (data.content) {
|
98 |
+
const content = data.content;
|
99 |
+
if (currentMode === 'thinking') {
|
100 |
+
thoughtsContent.innerHTML += `<p>${marked.parse(content)}</p>`;
|
101 |
+
thoughtsContent.scrollTop = thoughtsContent.scrollHeight;
|
102 |
+
} else if (currentMode === 'answering') {
|
103 |
+
// Applique le rendu Markdown
|
104 |
+
answerContent.innerHTML += marked.parse(content);
|
105 |
+
// Applique le rendu MathJax
|
106 |
+
if (window.MathJax) {
|
107 |
+
MathJax.typesetPromise([answerContent]).then(() => {
|
108 |
+
// Le rendu MathJax est terminé, faites défiler vers le bas si nécessaire
|
109 |
+
answerContent.scrollTop = answerContent.scrollHeight;
|
110 |
+
}).catch((err) => console.log('MathJax error:', err));
|
111 |
+
}
|
112 |
+
}
|
113 |
+
}
|
114 |
+
}
|
115 |
+
}
|
116 |
+
}
|
117 |
+
|
118 |
+
} catch (error) {
|
119 |
+
console.error('Erreur:', error);
|
120 |
+
alert('Une erreur est survenue lors du traitement de la requête.');
|
121 |
+
loader.classList.add('hidden');
|
122 |
+
stopTimer();
|
123 |
+
}
|
124 |
+
});
|
125 |
+
});
|
126 |
+
</script>
|
127 |
+
</body>
|
128 |
+
</html>
|