File size: 13,505 Bytes
af5fbb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecc651f
af5fbb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
340
341
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mariam M-0 | Solution Mathématique</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">

    <!-- Optimized MathJax Configuration -->
    <script>
        window.MathJax = {
            tex: {
                inlineMath: [['$', '$']],
                displayMath: [['$$', '$$']],
                processEscapes: true,
                packages: ['base', 'ams']
            },
            options: {
                enableMenu: false,
                messageStyle: 'none'
            },
            startup: {
                pageReady: () => {
                    console.log('MathJax is fully loaded and ready.');
                    window.mathJaxReady = true; // Signal that MathJax is ready
                    // On pourrait appeler une fonction ici pour traiter du contenu initial si nécessaire
                }
            }
        };
    </script>
    <script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" id="MathJax-script" async></script>
    <script src="https://cdn.jsdelivr.net/npm/marked/lib/marked.umd.min.js"></script>

    <style>
        @import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;700&display=swap');

        body {
            font-family: 'Space Grotesk', sans-serif;
        }

        .uploadArea {
            background: #f3f4f6;
            border: 2px dashed #d1d5db;
            transition: border-color 0.2s ease;
        }

        .uploadArea:hover {
            border-color: #3b82f6;
        }

        .blue-button {
            background: #3b82f6;
            transition: background-color 0.2s ease;
        }

        .blue-button:hover {
            background: #2563eb;
        }

        .loader {
            width: 48px;
            height: 48px;
            border: 3px solid #3b82f6;
            border-bottom-color: transparent;
            border-radius: 50%;
            display: inline-block;
            animation: rotation 1s linear infinite;
        }

        @keyframes rotation {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .thought-box {
            transition: max-height 0.3s ease-out;
            max-height: 0;
            overflow: hidden;
        }

        .thought-box.open {
            max-height: 500px;
        }

        #thoughtsContent, #answerContent {
            max-height: 500px;
            overflow-y: auto;
            scroll-behavior: smooth;
            white-space: pre-wrap; /* Gérer les espaces et les sauts de ligne */
        }

        .preview-image {
            max-width: 300px;
            max-height: 300px;
            object-fit: contain;
        }

        .timestamp {
            color: #3b82f6;
            font-size: 0.9em;
            margin-left: 8px;
        }
    </style>

</head>
<body class="p-4">
    <div class="max-w-4xl mx-auto">
        <div class="p-6">
            <div class="text-center mb-8">
                <h1 class="text-4xl font-bold text-blue-600">Mariam M-0</h1>
                <p class="text-gray-600">Solution Mathématique/Physique/Chimie Intelligente</p>
            </div>

            <form id="problemForm" class="space-y-6">
                <div class="uploadArea p-8 text-center relative">
                    <input type="file" id="imageInput" accept="image/*" class="absolute inset-0 w-full h-full opacity-0 cursor-pointer">
                    <div class="space-y-3">
                        <div class="w-16 h-16 mx-auto border-2 border-blue-400 rounded-full flex items-center justify-center">
                            <svg class="w-8 h-8 text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
                            </svg>
                        </div>
                        <p class="text-gray-700 font-medium">Déposez votre image ici</p>
                        <p class="text-gray-500 text-sm">ou cliquez pour sélectionner</p>
                    </div>
                </div>

                <div id="imagePreview" class="hidden text-center">
                    <img id="previewImage" class="preview-image mx-auto" alt="Prévisualisation">
                </div>

                <button type="submit" class="blue-button w-full py-3 text-white font-medium rounded-lg">
                    Résoudre le problème
                </button>
            </form>

            <div id="loader" class="hidden mt-8 text-center">
                <span class="loader"></span>
                <p class="mt-4 text-gray-600">Analyse en cours...</p>
            </div>

            <div id="solution" class="hidden mt-8 space-y-6">
                <div class="border-t pt-4">
                    <button id="thoughtsToggle" class="w-full flex justify-between items-center p-2">
                        <span class="font-medium text-gray-700">Processus de Réflexion</span>
                        <span id="timestamp" class="timestamp"></span>
                    </button>
                    <div id="thoughtsBox" class="thought-box">
                        <div id="thoughtsContent" class="p-4 text-gray-600"></div>
                    </div>
                </div>

                <div class="border-t pt-6">
                    <h3 class="text-xl font-bold text-gray-800 mb-4">Solution</h3>
                    <div id="answerContent" class="text-gray-700"></div>
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const form = document.getElementById('problemForm');
            const imageInput = document.getElementById('imageInput');
            const loader = document.getElementById('loader');
            const solution = document.getElementById('solution');
            const thoughtsContent = document.getElementById('thoughtsContent');
            const answerContent = document.getElementById('answerContent');
            const thoughtsToggle = document.getElementById('thoughtsToggle');
            const thoughtsBox = document.getElementById('thoughtsBox');
            const imagePreview = document.getElementById('imagePreview');
            const previewImage = document.getElementById('previewImage');
            const timestamp = document.getElementById('timestamp');

            let startTime = null;
            let timerInterval = null;
            let thoughtsBuffer = '';
            let answerBuffer = '';

            const updateTimestamp = () => {
                if (startTime) {
                    timestamp.textContent = `${Math.floor((Date.now() - startTime) / 1000)}s`;
                }
            };

            const startTimer = () => {
                startTime = Date.now();
                timerInterval = setInterval(updateTimestamp, 1000);
                updateTimestamp();
            };

            const stopTimer = () => {
                clearInterval(timerInterval);
                startTime = null;
                timestamp.textContent = '';
            };

            const handleFileSelect = file => {
                if (!file) return;
                const reader = new FileReader();
                reader.onload = e => {
                    previewImage.src = e.target.result;
                    imagePreview.classList.remove('hidden');
                };
                reader.readAsDataURL(file);
            };

            thoughtsToggle.addEventListener('click', () => {
                thoughtsBox.classList.toggle('open');
            });

            imageInput.addEventListener('change', e => handleFileSelect(e.target.files[0]));

            const dropZone = document.querySelector('.uploadArea');
            dropZone.addEventListener('dragover', e => {
                e.preventDefault();
                dropZone.classList.add('border-blue-400');
            });

            dropZone.addEventListener('dragleave', e => {
                e.preventDefault();
                dropZone.classList.remove('border-blue-400');
            });

            dropZone.addEventListener('drop', e => {
                e.preventDefault();
                dropZone.classList.remove('border-blue-400');
                handleFileSelect(e.dataTransfer.files[0]);
            });

            const typesetAnswerIfReady = async () => {
                if (window.mathJaxReady) {
                    MathJax.startup.document.elements = [document.getElementById('answerContent')];
                    await MathJax.typesetPromise();
                    answerContent.scrollTop = answerContent.scrollHeight;
                } else {
                    setTimeout(typesetAnswerIfReady, 200);
                }
            };

            // Configuration de marked pour gérer les sauts de ligne
            marked.setOptions({
              breaks: true, // Interpréter les sauts de ligne simples comme des <br>
            });

            form.addEventListener('submit', async e => {
                e.preventDefault();
                const file = imageInput.files[0];
                if (!file) {
                    alert('Veuillez sélectionner une image.');
                    return;
                }

                startTimer();
                loader.classList.remove('hidden');
                solution.classList.add('hidden');
                thoughtsContent.innerHTML = '';
                answerContent.innerHTML = '';
                thoughtsBuffer = '';
                answerBuffer = '';
                thoughtsBox.classList.add('open');

                const formData = new FormData();
                formData.append('image', file);

                try {
                    let currentMode = null;
                    const response = await fetch('/solve', {
                        method: 'POST',
                        body: formData
                    });

                    const reader = response.body.getReader();
                    const decoder = new TextDecoder();
                    let buffer = '';

                    const processChunk = async (chunk) => {
                        buffer += decoder.decode(chunk, { stream: true });
                        const lines = buffer.split('\n\n');
                        buffer = lines.pop();

                        for (const line of lines) {
                            if (!line.startsWith('data:')) continue;
                            const data = JSON.parse(line.slice(5));

                            if (data.mode) {
                                currentMode = data.mode;
                                loader.classList.add('hidden');
                                solution.classList.remove('hidden');
                            }

                            if (data.content) {
                                if (currentMode === 'thinking') {
                                    thoughtsBuffer += data.content;
                                } else if (currentMode === 'answering') {
                                    answerBuffer += data.content;
                                }
                            }
                        }

                        // Mettre à jour le contenu visible à chaque chunk reçu
                        thoughtsContent.innerHTML = marked.parse(thoughtsBuffer);
                        answerContent.innerHTML = marked.parse(answerBuffer);
                        await typesetAnswerIfReady();
                    };

                    while (true) {
                        const { done, value } = await reader.read();
                        if (done) {
                            // Traiter le contenu restant dans le buffer à la fin du stream
                            if (buffer) {
                                const data = JSON.parse(buffer.slice(5));
                                if (data.content) {
                                    if (currentMode === 'thinking') {
                                        thoughtsBuffer += data.content;
                                    } else if (currentMode === 'answering') {
                                        answerBuffer += data.content;
                                    }
                                }
                            }

                            // Mettre à jour le contenu visible une dernière fois à la fin du stream
                            thoughtsContent.innerHTML = marked.parse(thoughtsBuffer);
                            answerContent.innerHTML = marked.parse(answerBuffer);
                            await typesetAnswerIfReady();
                            break;
                        }
                        await processChunk(value);
                    }
                    stopTimer();
                } catch (error) {
                    console.error('Erreur:', error);
                    alert('Une erreur est survenue.');
                    loader.classList.add('hidden');
                    stopTimer();
                }
            });
        });
    </script>
</body>
</html>