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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mariam AI - SVT</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/4.0.2/marked.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');
        
        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #f6f8fc 0%, #e9f0f7 100%);
        }

        .animate-fade-in {
            animation: fadeIn 0.5s ease-in;
        }

        .animate-slide-up {
            animation: slideUp 0.5s ease-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        @keyframes slideUp {
            from { transform: translateY(20px); opacity: 0; }
            to { transform: translateY(0); opacity: 1; }
        }

        .glass-effect {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        .hover-scale {
            transition: transform 0.2s;
        }

        .hover-scale:hover {
            transform: scale(1.02);
        }

        .image-preview {
            transition: all 0.3s ease;
        }

        .image-preview:hover .image-overlay {
            opacity: 1;
        }

        .image-overlay {
            opacity: 0;
            transition: opacity 0.3s ease;
            background: rgba(0, 0, 0, 0.5);
        }

        .preview-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            gap: 1rem;
        }

        /* Responsive adjustments */
        @media (max-width: 640px) {
            .preview-container {
                grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
            }
            .image-preview img {
                height: 32 object-cover;
            }
            #historyContainer .grid {
                grid-template-columns: repeat(1, minmax(0, 1fr));
            }
        }
        
    </style>
</head>
<body class="min-h-screen flex items-center justify-center p-4 md:p-8">
    <div class="container mx-auto glass-effect rounded-2xl shadow-xl max-w-4xl animate-fade-in p-6 md:p-8">
        <header class="flex flex-col md:flex-row justify-between items-center mb-8">
            <div class="text-center md:text-left mb-4 md:mb-0">
                <h1 class="text-4xl font-bold text-blue-900">Mariam AI</h1>
                <p class="text-gray-600 mt-2">Assistant SVT Intelligent</p>
            </div>
            <button onclick="showInfo()" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-3 rounded-xl hover-scale transition-all duration-300 flex items-center gap-2">
                <i class="fas fa-info-circle"></i>
                <span>Guide d'utilisation</span>
            </button>
        </header>

        <div class="space-y-8 animate-slide-up">
            <div class="relative">
                <label for="svtOption" class="block mb-3 text-lg font-medium text-gray-700">Type d'exercice :</label>
                <select id="svtOption" class="w-full p-4 border border-gray-200 rounded-xl bg-white shadow-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-300">
                    <option value="Restitution organisée des connaissances">Restitution Organisée des Connaissances</option>
                    <option value="Exploitation du document">Exploitation du Document</option>
                    <option value="Synthèse">Synthèse</option>
                </select>
            </div>

            <div class="relative">
                <label class="block mb-3 text-lg font-medium text-gray-700">Images du sujet :</label>
                <div class="border-2 border-dashed border-gray-300 rounded-xl p-8 text-center hover:border-blue-500 transition-all duration-300">
                    <input type="file" id="imageUpload" class="hidden" multiple accept="image/*" onchange="handleImageUpload(event)">
                    <label for="imageUpload" class="cursor-pointer">
                        <i class="fas fa-cloud-upload-alt text-4xl text-blue-500 mb-4"></i>
                        <p class="text-gray-600">Glissez vos images ici ou cliquez pour sélectionner</p>
                        <p class="text-sm text-gray-500 mt-2">Format acceptés : JPG, PNG, GIF</p>
                    </label>
                </div>

                <!-- Conteneur des prévisualisations -->
                <div id="previewContainer" class="preview-container mt-4">
                    <!-- Les prévisualisations seront ajoutées ici -->
                </div>
            </div>

            <button onclick="submitQuestion()" class="w-full bg-gradient-to-r from-blue-600 to-blue-800 text-white py-4 rounded-xl hover-scale transition-all duration-300 font-semibold text-lg flex items-center justify-center gap-2">
                <i class="fas fa-paper-plane"></i>
                Analyser
            </button>

            <div id="loader" class="hidden">
                <div class="flex flex-col items-center space-y-4 p-8">
                    <div class="animate-spin rounded-full h-12 w-12 border-4 border-blue-500 border-t-transparent"></div>
                    <p class="text-gray-600 text-lg">Analyse en cours...</p>
                </div>
            </div>

            <div id="response" class="mt-6 p-6 bg-white rounded-xl shadow-sm prose max-w-none"></div>

            <div id="copyResponseContainer" class="hidden mb-8">
                <button onclick="copyResponse()" class="w-full bg-gray-800 hover:bg-gray-900 text-white px-6 py-3 rounded-xl hover-scale transition-all duration-300 flex items-center justify-center gap-2">
                    <i class="fas fa-copy"></i>
                    Copier la réponse
                </button>
            </div>
        </div>
        
        <!-- Historique des réponses déplacé ici -->
        <div class="space-y-8 animate-slide-up">
            <h2 class="text-2xl font-bold text-blue-900 mb-4">Historique des Réponses</h2>
            <button onclick="clearLocalStorage()" class="bg-red-500 hover:bg-red-700 text-white px-4 py-2 rounded-lg">
                Effacer l'historique
            </button>
            <div id="historyContainer">
                <!-- L'historique des réponses sera affiché ici -->
            </div>
        </div>
    </div>

    

    <script>
        let uploadedFiles = [];

        function handleImageUpload(event) {
            const files = event.target.files;
            const previewContainer = document.getElementById('previewContainer');

            for (let i = 0; i < files.length; i++) {
                const file = files[i];
                uploadedFiles.push(file);

                const reader = new FileReader();
                reader.onload = function(e) {
                    const imageId = `img-${Date.now()}-${i}`;
                    const previewDiv = document.createElement('div');
                    previewDiv.className = 'image-preview relative rounded-lg overflow-hidden';
                    previewDiv.id = imageId;
                    previewDiv.innerHTML = `
                        <img src="${e.target.result}" alt="${file.name}" class="w-full h-40 object-cover">
                        <div class="image-overlay absolute inset-0 flex items-center justify-center">
                            <button onclick="removeImage('${imageId}')" class="bg-red-500 hover:bg-red-600 text-white p-2 rounded-full transition-all duration-300">
                                <i class="fas fa-trash"></i>
                            </button>
                            <button onclick="previewImage('${e.target.result}')" class="bg-blue-500 hover:bg-blue-600 text-white p-2 rounded-full ml-2 transition-all duration-300">
                                <i class="fas fa-eye"></i>
                            </button>
                        </div>
                        <div class="bg-black bg-opacity-50 text-white text-xs p-1 absolute bottom-0 left-0 right-0">
                            ${file.name.substring(0, 15)}${file.name.length > 15 ? '...' : ''}
                        </div>
                    `;
                    previewContainer.appendChild(previewDiv);
                };
                reader.readAsDataURL(file);
            }
        }

        function removeImage(imageId) {
            const imageIndex = uploadedFiles.findIndex(file => {
                const fileId = `img-${file.lastModified}-${uploadedFiles.indexOf(file)}`;
                return fileId === imageId;
            });

            if (imageIndex !== -1) {
                uploadedFiles.splice(imageIndex, 1);
            }

            const element = document.getElementById(imageId);
            if (element) {
                element.remove();
            }
        }

        function previewImage(src) {
            Swal.fire({
                imageUrl: src,
                imageAlt: 'Prévisualisation',
                width: '80%',
                showConfirmButton: false,
                showCloseButton: true,
                customClass: {
                    image: 'max-h-[80vh] object-contain'
                }
            });
        }

        function showInfo() {
            Swal.fire({
                title: 'Guide d\'utilisation',
                html: `
                    <div class="text-left space-y-4">
                        <div class="flex items-start gap-3">
                            <i class="fas fa-check-circle text-green-500 mt-1"></i>
                            <p>Sélectionnez le type d'exercice correspondant à votre sujet.</p>
                        </div>
                        <div class="flex items-start gap-3">
                            <i class="fas fa-image text-blue-500 mt-1"></i>
                            <p>Assurez-vous que vos images sont nettes et bien cadrées.</p>
                        </div>
                        <div class="flex items-start gap-3">
                            <i class="fas fa-crop-alt text-purple-500 mt-1"></i>
                            <p>Rognez vos images pour ne garder que l'essentiel du sujet.</p>
                        </div>
                    </div>
                `,
                icon: 'info',
                confirmButtonText: 'Compris',
                confirmButtonColor: '#2563eb',
                customClass: {
                    container: 'font-sans'
                }
            });
        }

        function copyResponse() {
            const responseDiv = document.getElementById('response');
            const range = document.createRange();
            range.selectNode(responseDiv);
            window.getSelection().removeAllRanges();
            window.getSelection().addRange(range);
            document.execCommand('copy');
            window.getSelection().removeAllRanges();
            
            Swal.fire({
                icon: 'success',
                title: 'Copié !',
                text: 'La réponse a été copiée dans le presse-papiers.',
                showConfirmButton: false,
                timer: 1500,
                customClass: {
                    popup: 'animate-fade-in'
                }
            });
        }

        async function submitQuestion() {
            if (uploadedFiles.length === 0) {
                Swal.fire({
                    icon: 'error',
                    title: 'Images manquantes',
                    text: 'Veuillez sélectionner au moins une image du sujet.',
                    confirmButtonColor: '#2563eb'
                });
                return;
            }

            const option = document.getElementById('svtOption').value;
            const loader = document.getElementById('loader');
            const responseDiv = document.getElementById('response');
            const copyResponseContainer = document.getElementById('copyResponseContainer');

            loader.classList.remove('hidden');
            responseDiv.innerHTML = '';
            copyResponseContainer.classList.add('hidden');

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

            for (let i = 0; i < uploadedFiles.length; i++) {
                formData.append('images', uploadedFiles[i]);
            }

            const response = await fetch('/svt_submit', {
                method: 'POST',
                body: formData
            });

            const data = await response.json();
            loader.classList.add('hidden');

            if (data.error) {
                responseDiv.innerHTML = `
                    <div class="bg-red-50 border-l-4 border-red-500 p-4 rounded">
                        <p class="text-red-700">Erreur : ${data.error}</p>
                    </div>
                `;
            } else {
                const htmlContent = marked.parse(data.response);
                responseDiv.innerHTML = htmlContent;
                responseDiv.classList.add('animate-fade-in');
                copyResponseContainer.classList.remove('hidden');

                // Convertir les images en base64 avant de les sauvegarder
                const imagesData = await Promise.all(uploadedFiles.map(file => {
                    return new Promise((resolve) => {
                        const reader = new FileReader();
                        reader.onload = (e) => resolve(e.target.result);
                        reader.readAsDataURL(file);
                    });
                }));

                saveResponseToLocalStorage(option, imagesData, data.response);
                displayHistory();
            }
        }

        // Fonctions pour la gestion du localStorage
        function saveResponseToLocalStorage(option, images, response) {
            const timestamp = new Date().toISOString();
            const data = { option, images, response, timestamp };
            localStorage.setItem('svt_response_' + timestamp, JSON.stringify(data));
        }

        function loadResponsesFromLocalStorage() {
            const responses = [];
            for (let i = 0; i < localStorage.length; i++) {
                const key = localStorage.key(i);
                if (key.startsWith('svt_response_')) {
                    const data = JSON.parse(localStorage.getItem(key));
                    responses.push(data);
                }
            }
            return responses.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
        }

        function clearLocalStorage() {
            const keysToRemove = [];
            for (let i = 0; i < localStorage.length; i++) {
                const key = localStorage.key(i);
                if (key.startsWith('svt_response_')) {
                    keysToRemove.push(key);
                }
            }
            for (const key of keysToRemove) {
                localStorage.removeItem(key);
            }
            displayHistory();
        }

        function displayHistory() {
            const responses = loadResponsesFromLocalStorage();
            const historyContainer = document.getElementById('historyContainer');
            historyContainer.innerHTML = '';

            if (responses.length === 0) {
                historyContainer.innerHTML = '<p class="text-gray-500">Aucun historique disponible.</p>';
                return;
            }

            const responseList = document.createElement('ul');
            responseList.className = 'grid gap-4 md:grid-cols-2'; 

            responses.forEach(response => {
                const listItem = document.createElement('li');
                listItem.className = 'bg-white p-4 rounded-lg shadow-md hover:shadow-lg transition duration-300';

                const title = document.createElement('h4');
                title.className = 'text-lg font-semibold text-blue-800 mb-2';
                title.textContent = `${response.option} - ${new Date(response.timestamp).toLocaleString()}`;
                listItem.appendChild(title);

                const previewContainer = document.createElement('div');
                previewContainer.className = 'flex gap-2 mb-2';
                response.images.forEach(imageData => {
                    const img = document.createElement('img');
                    img.src = imageData;
                    img.className = 'h-12 w-12 object-cover rounded-md cursor-pointer'; 
                    img.onclick = () => previewImage(imageData);
                    previewContainer.appendChild(img);
                });
                listItem.appendChild(previewContainer);

                const responsePreview = document.createElement('p');
                responsePreview.className = 'text-gray-600 text-sm';
                responsePreview.textContent = response.response.substring(0, 200) + (response.response.length > 200 ? '...' : '');
                listItem.appendChild(responsePreview);

                const viewButton = document.createElement('button');
                viewButton.className = 'bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg mt-2 text-sm';
                viewButton.textContent = 'Voir';
                viewButton.onclick = () => {
                    document.getElementById('response').innerHTML = marked.parse(response.response);
                    document.getElementById('copyResponseContainer').classList.remove('hidden');
                    window.scrollTo({ top: document.getElementById('response').offsetTop, behavior: 'smooth' });
                };
                listItem.appendChild(viewButton);

                responseList.appendChild(listItem);
            });

            historyContainer.appendChild(responseList);
        }

        displayHistory();
    </script>
</body>
</html>