File size: 14,083 Bytes
79914b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35fdca9
 
 
 
 
 
 
79914b8
 
 
 
 
 
 
 
 
 
35fdca9
 
 
 
 
79914b8
 
 
 
 
 
 
 
 
 
 
43f84cb
79914b8
 
 
 
 
 
35fdca9
 
 
79914b8
 
 
 
 
 
43f84cb
 
79914b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43f84cb
79914b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35fdca9
79914b8
 
 
35fdca9
 
 
 
 
79914b8
 
35fdca9
 
 
 
 
79914b8
 
 
 
 
35fdca9
 
 
79914b8
 
35fdca9
 
 
79914b8
 
35fdca9
 
 
 
 
 
 
 
 
 
 
79914b8
 
35fdca9
 
 
 
79914b8
35fdca9
 
 
 
79914b8
 
 
 
 
 
 
35fdca9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79914b8
35fdca9
 
79914b8
35fdca9
79914b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2854e12
 
79914b8
 
 
 
 
 
 
 
 
43f84cb
79914b8
 
 
 
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
342
343
344
345
346
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Résolveur d'Images</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.7/katex.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.7/contrib/auto-render.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.7/katex.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .container {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        .upload-section {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
            border: 2px dashed #ccc;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s;
        }
        .upload-section:hover {
            border-color: #888;
        }
        #file-input {
            display: none;
        }
        .preview-container {
            width: 100%;
            text-align: center;
            margin-top: 10px;
        }
        #image-preview {
            max-width: 100%;
            max-height: 300px;
            display: none;
        }
        #solving-container {
            display: none;
            background-color: #f5f5f5;
            padding: 20px;
            border-radius: 8px;
        }
        .response-container {
            margin-top: 20px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            background-color: #fff;
            display: none;
        }
        .thinking {
            color: #777;
            font-style: italic;
        }
        .button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 10px 2px;
            cursor: pointer;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        .button:hover {
            background-color: #45a049;
        }
        .button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        .copy-button {
            background-color: #2196F3;
        }
        .copy-button:hover {
            background-color: #0b7dda;
        }
        .telegram-notice {
            background-color: #e3f2fd;
            border-left: 4px solid #2196F3;
            padding: 10px;
            margin: 15px 0;
            font-size: 14px;
        }
        .loading {
            text-align: center;
            font-style: italic;
            margin: 10px 0;
        }
        .status {
            text-align: center;
            margin-bottom: 10px;
            font-weight: bold;
        }
        .status small {
            font-weight: normal;
            color: #666;
            font-size: 0.85em;
        }
    </style>
</head>
<body>
    <h1>Résolveur d'Images</h1>
    
    <div class="container">
        <div id="upload-section" class="upload-section">
            <p>Cliquez ou glissez-déposez une image ici</p>
            <input type="file" id="file-input" accept="image/*">
            <div class="preview-container">
                <img id="image-preview" src="#" alt="Aperçu de l'image">
            </div>
        </div>
        
        <button id="solve-button" class="button" disabled>Résoudre</button>
        
        <div id="solving-container">
            <div class="status" id="status">En attente de résolution...</div>
            <div class="telegram-notice">
                La réponse complète sera également envoyée sous forme de fichier texte sur Telegram.
            </div>
            <div class="loading" id="loading-text">Traitement en cours...</div>
            <div class="response-container" id="response-container">
                <div id="response"></div>
                <button id="copy-button" class="button copy-button">Copier la réponse</button>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const uploadSection = document.getElementById('upload-section');
            const fileInput = document.getElementById('file-input');
            const imagePreview = document.getElementById('image-preview');
            const solveButton = document.getElementById('solve-button');
            const solvingContainer = document.getElementById('solving-container');
            const responseContainer = document.getElementById('response-container');
            const response = document.getElementById('response');
            const copyButton = document.getElementById('copy-button');
            const statusElement = document.getElementById('status');
            const loadingText = document.getElementById('loading-text');
            
            let selectedFile = null;
            
            // Événements pour l'upload d'image
            uploadSection.addEventListener('click', () => fileInput.click());
            
            uploadSection.addEventListener('dragover', (e) => {
                e.preventDefault();
                uploadSection.style.borderColor = '#2196F3';
            });
            
            uploadSection.addEventListener('dragleave', () => {
                uploadSection.style.borderColor = '#ccc';
            });
            
            uploadSection.addEventListener('drop', (e) => {
                e.preventDefault();
                uploadSection.style.borderColor = '#ccc';
                
                if (e.dataTransfer.files.length) {
                    handleFileSelection(e.dataTransfer.files[0]);
                }
            });
            
            fileInput.addEventListener('change', (e) => {
                if (e.target.files.length) {
                    handleFileSelection(e.target.files[0]);
                }
            });
            
            function handleFileSelection(file) {
                if (!file.type.startsWith('image/')) {
                    alert('Veuillez sélectionner une image valide');
                    return;
                }
                
                selectedFile = file;
                solveButton.disabled = false;
                
                const reader = new FileReader();
                reader.onload = (e) => {
                    imagePreview.src = e.target.result;
                    imagePreview.style.display = 'block';
                };
                reader.readAsDataURL(file);
            }
            
            // Événement pour résoudre l'image
            solveButton.addEventListener('click', () => {
                if (!selectedFile) return;
                
                solveButton.disabled = true;
                solvingContainer.style.display = 'block';
                responseContainer.style.display = 'none';
                statusElement.textContent = 'En attente de résolution...';
                loadingText.style.display = 'block';
                response.innerHTML = '';
                
                const formData = new FormData();
                formData.append('image', selectedFile);
                
                // Soumettre l'image pour traitement en arrière-plan
                fetch('/solve', {
                    method: 'POST',
                    body: formData
                })
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        throw new Error(data.error);
                    }
                    
                    const taskId = data.task_id;
                    statusElement.textContent = 'Traitement en arrière-plan (ID: ' + taskId + ')';
                    
                    // Création d'une connexion SSE pour suivre le progrès
                    const eventSource = new EventSource('/stream/' + taskId);
                    let fullResponse = '';
                    
                    eventSource.onmessage = function(event) {
                        const data = JSON.parse(event.data);
                        
                        if (data.error) {
                            statusElement.textContent = 'Erreur:';
                            response.innerHTML = data.error;
                            responseContainer.style.display = 'block';
                            loadingText.style.display = 'none';
                            eventSource.close();
                            solveButton.disabled = false;
                            return;
                        }
                        
                        if (data.status === 'pending') {
                            statusElement.textContent = 'En attente de traitement...';
                        } else if (data.status === 'processing') {
                            statusElement.textContent = 'Gemini traite votre image...';
                            statusElement.innerHTML += '<br><small>La réponse sera également envoyée sur Telegram</small>';
                        } else if (data.status === 'completed') {
                            statusElement.textContent = 'Traitement terminé!';
                            responseContainer.style.display = 'block';
                            loadingText.style.display = 'none';
                            
                            fullResponse = data.response;
                            response.innerHTML = fullResponse;
                            renderMathInElement(response);
                            
                            eventSource.close();
                            solveButton.disabled = false;
                        } else if (data.status === 'error') {
                            statusElement.textContent = 'Erreur:';
                            response.innerHTML = data.error || 'Une erreur inattendue est survenue';
                            responseContainer.style.display = 'block';
                            loadingText.style.display = 'none';
                            
                            eventSource.close();
                            solveButton.disabled = false;
                        }
                    };
                    
                    eventSource.onerror = function() {
                        eventSource.close();
                        // Essayer de récupérer le statut via une requête GET normale
                        fetch('/task/' + taskId)
                            .then(response => response.json())
                            .then(taskData => {
                                if (taskData.status === 'completed') {
                                    statusElement.textContent = 'Traitement terminé!';
                                    responseContainer.style.display = 'block';
                                    loadingText.style.display = 'none';
                                    
                                    response.innerHTML = taskData.response;
                                    renderMathInElement(response);
                                } else if (taskData.status === 'error') {
                                    throw new Error(taskData.error || 'Une erreur inattendue est survenue');
                                }
                            })
                            .catch(error => {
                                statusElement.textContent = 'Erreur de connexion:';
                                response.innerHTML = 'La connexion a été perdue, mais le traitement continue en arrière-plan. La réponse sera envoyée sur Telegram.';
                                responseContainer.style.display = 'block';
                                loadingText.style.display = 'none';
                            })
                            .finally(() => {
                                solveButton.disabled = false;
                            });
                    };
                })
                .catch(error => {
                    statusElement.textContent = 'Erreur:';
                    response.innerHTML = error.message || 'Une erreur est survenue lors de la communication avec le serveur.';
                    responseContainer.style.display = 'block';
                    loadingText.style.display = 'none';
                    solveButton.disabled = false;
                });
            });
            
            // Événement pour copier la réponse
            copyButton.addEventListener('click', () => {
                const range = document.createRange();
                range.selectNode(response);
                window.getSelection().removeAllRanges();
                window.getSelection().addRange(range);
                document.execCommand('copy');
                window.getSelection().removeAllRanges();
                
                copyButton.textContent = 'Copié!';
                setTimeout(() => {
                    copyButton.textContent = 'Copier la réponse';
                }, 2000);
            });
        });
        
        // Rendu des formules LaTeX
        document.addEventListener('DOMContentLoaded', function() {
            renderMathInElement(document.body, {
                delimiters: [
                    {left: '$$', right: '$$', display: true},
                    {left: '$', right: '$', display: false},
                    {left: '\\(', right: '\\)', display: false},
                    {left: '\\[', right: '\\]', display: true}
                ]
            });
        });
    </script>
</body>
</html>