| <!DOCTYPE html> |
| <html lang="fr"> |
| <head> |
| <meta charset="UTF-8"> |
| |
| <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> |
| <title>Mariam AI - Analyse Littéraire</title> |
|
|
| |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/9.1.6/marked.min.js"></script> |
|
|
| |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script> |
| |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.28/jspdf.plugin.autotable.min.js"></script> |
|
|
|
|
| <style> |
| @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;700&family=Inter:wght@300;400;500;600;700&display=swap'); |
| |
| :root { |
| --primary: #4361ee; |
| --primary-dark: #3a56d4; |
| --secondary: #2b2d42; |
| --secondary-light: #8d99ae; |
| --accent: #ef476f; |
| --light: #f8f9fa; |
| --gray-100: #f1f3f5; |
| --gray-200: #e9ecef; |
| --gray-300: #dee2e6; |
| --gray-400: #ced4da; |
| --gray-700: #495057; |
| --shadow-sm: 0 1px 2px rgba(0,0,0,0.05); |
| --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06); |
| --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05); |
| --radius-sm: 0.375rem; |
| --radius-md: 0.5rem; |
| --radius-lg: 0.75rem; |
| } |
| |
| * { margin: 0; padding: 0; box-sizing: border-box; } |
| html { -webkit-overflow-scrolling: touch; } |
| body { |
| font-family: 'Inter', sans-serif; |
| background: linear-gradient(135deg, #f6f8fb 0%, #f1f3f5 100%); |
| color: var(--secondary); |
| line-height: 1.6; |
| padding-bottom: 2rem; |
| min-height: 100vh; |
| } |
| header { |
| background-color: white; |
| padding: 1.5rem 1rem; |
| text-align: center; |
| box-shadow: var(--shadow-sm); |
| position: sticky; |
| top: 0; |
| z-index: 10; |
| width: 100%; |
| } |
| .logo { |
| font-family: 'Playfair Display', serif; |
| font-size: 2rem; |
| color: var(--secondary); |
| margin-bottom: 0.25rem; |
| } |
| .tagline { |
| font-size: 0.9rem; |
| color: var(--gray-700); |
| font-weight: 500; |
| } |
| .card { |
| background: white; |
| border-radius: var(--radius-lg); |
| box-shadow: var(--shadow-md); |
| padding: 1.5rem; |
| margin: 1rem; |
| transition: transform 0.2s ease, box-shadow 0.2s ease; |
| } |
| .upload-area { |
| border: 2px dashed var(--gray-300); |
| border-radius: var(--radius-md); |
| padding: 1.5rem 1rem; |
| text-align: center; |
| cursor: pointer; |
| transition: all 0.2s ease; |
| margin: 1.5rem 0; |
| } |
| .upload-area:hover, .upload-area.active { |
| border-color: var(--primary); |
| background-color: rgba(67, 97, 238, 0.05); |
| } |
| .upload-icon { |
| width: 3.5rem; height: 3.5rem; |
| margin: 0 auto 1rem; |
| color: var(--gray-400); |
| } |
| .upload-area:hover .upload-icon, .upload-area.active .upload-icon { |
| color: var(--primary); |
| } |
| .input-group { margin-bottom: 1.5rem; } |
| .label { |
| display: block; |
| margin-bottom: 0.5rem; |
| font-weight: 600; |
| font-size: 0.9rem; |
| } |
| .input { |
| width: 100%; |
| padding: 0.75rem; |
| border: 1px solid var(--gray-300); |
| border-radius: var(--radius-md); |
| font-family: 'Inter', sans-serif; |
| font-size: 1rem; |
| transition: border-color 0.2s ease; |
| |
| -webkit-appearance: none; |
| appearance: none; |
| } |
| .input:focus { |
| outline: none; |
| border-color: var(--primary); |
| box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.1); |
| } |
| .preview-container { margin: 1.5rem 0; } |
| .preview-title { |
| font-weight: 600; |
| margin-bottom: 0.5rem; |
| font-size: 0.9rem; |
| } |
| .preview-image-container { |
| background-color: var(--gray-100); |
| border-radius: var(--radius-md); |
| overflow: hidden; |
| position: relative; |
| } |
| .preview-image { |
| max-width: 100%; |
| max-height: 15rem; |
| height: auto; |
| margin: 0 auto; |
| display: block; |
| } |
| .btn { |
| display: block; |
| width: 100%; |
| padding: 0.875rem; |
| font-weight: 600; |
| text-align: center; |
| border: none; |
| border-radius: var(--radius-md); |
| cursor: pointer; |
| transition: all 0.2s ease; |
| font-size: 1rem; |
| |
| -webkit-user-select: none; user-select: none; |
| } |
| .btn:disabled { |
| background-color: var(--gray-300) !important; |
| color: var(--gray-700) !important; |
| cursor: not-allowed; |
| opacity: 0.7; |
| transform: none !important; |
| box-shadow: none !important; |
| } |
| .btn-primary { background-color: var(--primary); color: white; } |
| .btn-primary:not(:disabled):hover, .btn-primary:not(:disabled):focus { background-color: var(--primary-dark); transform: translateY(-1px); box-shadow: var(--shadow-md); } |
| .btn-primary:not(:disabled):active { transform: translateY(0); } |
| .btn-secondary { background-color: var(--secondary-light); color: var(--secondary); margin-top: 0.75rem; } |
| .btn-secondary:not(:disabled):hover, .btn-secondary:not(:disabled):focus { background-color: var(--secondary); color: white; transform: translateY(-1px); box-shadow: var(--shadow-md); } |
| .btn-secondary:not(:disabled):active { transform: translateY(0); } |
| |
| .loading-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; z-index: 100; padding: 1rem; } |
| .loading-card { background-color: white; padding: 2rem; border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); text-align: center; max-width: 90%; } |
| .spinner { width: 3rem; height: 3rem; border: 4px solid var(--gray-200); border-radius: 50%; border-top-color: var(--primary); animation: spin 1s linear infinite; margin: 0 auto 1rem; } |
| @keyframes spin { to { transform: rotate(360deg); } } |
| |
| .result-card { padding: 0; overflow: hidden; } |
| .tabs { display: flex; border-bottom: 1px solid var(--gray-200); background-color: var(--gray-100); width: 100%; } |
| .tab { |
| flex: 1; |
| text-align: center; |
| padding: 1rem 0.5rem; |
| font-weight: 600; |
| font-size: 0.9rem; |
| color: var(--gray-700); |
| background-color: transparent; |
| border: none; |
| border-bottom: 3px solid transparent; |
| cursor: pointer; |
| transition: color 0.2s ease, border-color 0.2s ease; |
| margin-bottom: -1px; |
| |
| -webkit-user-select: none; user-select: none; |
| white-space: nowrap; |
| overflow: hidden; |
| text-overflow: ellipsis; |
| } |
| .tab:hover { color: var(--primary); } |
| .tab.active { color: var(--primary); border-bottom-color: var(--primary); } |
| .tab-content { padding: 1.5rem; } |
| |
| |
| .custom-prose { font-size: 1rem; line-height: 1.8; } |
| .custom-prose h1, .custom-prose h2, .custom-prose h3 { font-family: 'Playfair Display', serif; margin-top: 1.5em; margin-bottom: 0.75em; line-height: 1.3; color: var(--primary-dark); } |
| .custom-prose h1 { font-size: 1.75rem; } .custom-prose h2 { font-size: 1.5rem; } .custom-prose h3 { font-size: 1.25rem; } |
| .custom-prose p { margin-bottom: 1.25em; } .custom-prose ul, .custom-prose ol { margin-top: 1em; margin-bottom: 1em; padding-left: 1.5em; } .custom-prose li { margin-bottom: 0.5em; } |
| |
| |
| .markdown-table { |
| overflow-x: auto; |
| -webkit-overflow-scrolling: touch; |
| border: 1px solid var(--gray-200); |
| border-radius: var(--radius-sm); |
| margin-top: 1em; |
| margin-left: -1.5rem; |
| margin-right: -1.5rem; |
| padding-left: 0.5rem; |
| padding-right: 0.5rem; |
| } |
| |
| #pdfContentWrapper .markdown-table, @media print { overflow-x: visible; margin-left: 0; margin-right: 0; padding-left: 0; padding-right: 0;} |
| .markdown-table table { width: 100%; border-collapse: collapse; border-spacing: 0; table-layout: auto; } |
| .markdown-table th { background: var(--primary); color: white; font-weight: 600; padding: 0.75rem 1rem; text-align: left; font-size: 0.9rem; border-bottom: 2px solid var(--primary-dark); white-space: nowrap; } |
| .markdown-table td { padding: 0.75rem 1rem; border-bottom: 1px solid var(--gray-200); font-size: 0.9rem; vertical-align: top; word-break: break-word; } |
| .markdown-table tr:nth-child(even) td { background-color: var(--gray-100); } |
| .markdown-table tr:hover td { background-color: rgba(67, 97, 238, 0.1); } |
| |
| .upload-instructions { font-size: 0.85rem; color: var(--gray-700); margin-top: 0.5rem; } |
| .file-input { display: none; } |
| .slide-up { animation: slide-up 0.4s ease forwards; } |
| @keyframes slide-up { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } } |
| .streaming-cursor { display: inline-block; width: 2px; height: 1.2em; background-color: var(--primary); margin-left: 1px; animation: blink 1s infinite; vertical-align: text-bottom; opacity: 1; } |
| @keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } } |
| .final-cursor { display: none !important; } |
| .button-container { margin-top: 1.5rem; } |
| |
| |
| |
| |
| |
| @media (max-width: 600px) { |
| body { |
| padding-bottom: 1rem; |
| -webkit-text-size-adjust: 100%; |
| } |
| header { |
| padding: 1rem; |
| } |
| .logo { |
| font-size: 1.75rem; |
| } |
| .tagline { |
| font-size: 0.85rem; |
| } |
| .card { |
| margin: 0.75rem; |
| padding: 1.25rem; |
| border-radius: var(--radius-md); |
| } |
| main .card:first-of-type { |
| margin-top: 1rem; |
| } |
| .upload-area { |
| padding: 1.25rem 1rem; |
| } |
| .upload-icon { |
| width: 3rem; height: 3rem; |
| } |
| .upload-instructions { |
| font-size: 0.8rem; |
| } |
| .label { |
| font-size: 0.85rem; |
| } |
| .input { |
| padding: 0.8rem; |
| font-size: 1rem; |
| } |
| .preview-title { |
| font-size: 0.85rem; |
| } |
| .btn { |
| padding: 0.9rem; |
| font-size: 0.95rem; |
| border-radius: var(--radius-sm); |
| } |
| .btn-secondary { |
| margin-top: 1rem; |
| } |
| .tab { |
| padding: 0.9rem 0.25rem; |
| font-size: 0.8rem; |
| } |
| .tab-content { |
| padding: 1.25rem; |
| } |
| |
| .custom-prose { |
| font-size: 0.95rem; |
| line-height: 1.7; |
| } |
| .custom-prose h1 { font-size: 1.5rem; } |
| .custom-prose h2 { font-size: 1.3rem; } |
| .custom-prose h3 { font-size: 1.1rem; } |
| |
| |
| .markdown-table { |
| margin-left: -1.25rem; |
| margin-right: -1.25rem; |
| padding-left: 0.4rem; |
| padding-right: 0.4rem; |
| } |
| .markdown-table th, .markdown-table td { |
| padding: 0.6rem 0.75rem; |
| font-size: 0.85rem; |
| } |
| |
| |
| |
| .loading-card { |
| padding: 1.5rem; |
| } |
| .spinner { |
| width: 2.5rem; height: 2.5rem; |
| margin-bottom: 0.8rem; |
| } |
| .loading-card p { |
| font-size: 0.9rem; |
| } |
| } |
| |
| </style> |
| </head> |
| <body> |
| <header> |
| <h1 class="logo">Mariam AI</h1> |
| <p class="tagline">Assistant d'Analyse Littéraire</p> |
| </header> |
|
|
| <main> |
| <div class="card"> |
| <form id="uploadForm"> |
| <div class="input-group"> |
| <label for="imageInput" class="label">Votre document</label> |
| <div id="uploadArea" class="upload-area"> |
| <svg class="upload-icon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" /></svg> |
| <p>Touchez pour télécharger</p> |
| <p class="upload-instructions">PNG, JPG jusqu'à 10MB</p> |
| <input id="imageInput" type="file" class="file-input" accept="image/*" required> |
| </div> |
| </div> |
| <div class="input-group"> |
| <label for="consignesInput" class="label">Consignes (optionnel)</label> |
| <input id="consignesInput" type="text" class="input" placeholder="Entrez vos consignes ici"> |
| </div> |
| <div id="previewContainer" class="preview-container" style="display: none;"> |
| <p class="preview-title">Aperçu du document</p> |
| <div class="preview-image-container"><img id="previewImage" class="preview-image" src="#" alt="Aperçu"></div> |
| </div> |
| <div class="button-container"> |
| <button type="submit" class="btn btn-primary">Lancer l'analyse standard</button> |
| <button type="button" id="deepThinkBtn" class="btn btn-secondary">DeepThink (1/jour)</button> |
| </div> |
| </form> |
| </div> |
|
|
| <div id="result" class="card result-card" style="display: none;"> |
| <div class="tabs"> |
| <button id="dissertationTab" class="tab active">Dissertation</button> |
| <button id="tableauTab" class="tab">Tableau</button> |
| </div> |
| <div id="displayContentWrapper"> |
| <div id="dissertationContentWrapper" class="tab-content"> |
| <div id="dissertationContent" class="custom-prose"></div> |
| </div> |
| <div id="tableauContentWrapper" class="tab-content" style="display: none;"> |
| |
| <div id="tableauContent" class="markdown-table"> |
| |
| </div> |
| </div> |
| </div> |
| <div style="padding: 0 1.25rem 1.25rem;"> |
| <button id="downloadPdf" class="btn btn-primary" style="margin-top: 1rem;" disabled>Télécharger en PDF</button> |
| </div> |
| </div> |
| </main> |
|
|
| <div id="loading" class="loading-overlay" style="display: none;"> |
| <div class="loading-card"><div class="spinner"></div><p>Préparation de l'analyse...</p></div> |
| </div> |
|
|
| <script> |
| |
| if (typeof window.jspdf === 'undefined') { console.error("Erreur: jsPDF non chargée."); } |
| else { console.log("jsPDF chargée:", window.jspdf.jsPDF.version); |
| if (typeof window.jspdf.jsPDF.API?.autoTable === 'undefined') { console.error("Erreur: Plugin jsPDF-AutoTable non chargé."); } |
| else { console.log("Plugin jsPDF-AutoTable détecté."); } |
| } |
| if (typeof window.marked === 'undefined') { console.error("Erreur: Marked.js non chargée."); } |
| |
| |
| const uploadForm = document.getElementById('uploadForm'); |
| const imageInput = document.getElementById('imageInput'); |
| const consignesInput = document.getElementById('consignesInput'); |
| const uploadArea = document.getElementById('uploadArea'); |
| const previewContainer = document.getElementById('previewContainer'); |
| const previewImage = document.getElementById('previewImage'); |
| const loadingIndicator = document.getElementById('loading'); |
| const resultDiv = document.getElementById('result'); |
| const dissertationTab = document.getElementById('dissertationTab'); |
| const tableauTab = document.getElementById('tableauTab'); |
| const dissertationContentWrapper = document.getElementById('dissertationContentWrapper'); |
| const tableauContentWrapper = document.getElementById('tableauContentWrapper'); |
| const dissertationContent = document.getElementById('dissertationContent'); |
| const tableauContent = document.getElementById('tableauContent'); |
| const downloadPdfBtn = document.getElementById('downloadPdf'); |
| const deepThinkBtn = document.getElementById('deepThinkBtn'); |
| |
| |
| if (typeof window.marked !== 'undefined') { |
| marked.setOptions({ breaks: true, gfm: true, mangle: false, headerIds: false }); |
| } |
| |
| |
| let tableauMarkdownRaw = ''; |
| let dissertationMarkdownRaw = ''; |
| let currentlyStreaming = null; |
| let streamComplete = false; |
| const dissertationCursorId = 'dissertationCursor'; |
| const tableauCursorId = 'tableauCursor'; |
| const DEEPTHINK_STORAGE_KEY = 'deepThinkLastUsedDate'; |
| |
| |
| function getCursorElement(type) { return document.getElementById(type === 'dissertation' ? dissertationCursorId : tableauCursorId); } |
| function removeCursor(type) { const cursor = getCursorElement(type); if (cursor) { cursor.style.display = 'none'; cursor.classList.add('final-cursor'); } } |
| function ensureCursor(type, container) { |
| let cursor = getCursorElement(type); |
| |
| |
| const tableElement = container.querySelector('table'); |
| const targetContainer = (type === 'tableau' && tableElement) ? tableElement.parentElement : container; |
| if (!cursor) { |
| cursor = document.createElement('span'); |
| cursor.id = type === 'dissertation' ? dissertationCursorId : tableauCursorId; |
| cursor.className = 'streaming-cursor'; |
| targetContainer.appendChild(cursor); |
| } |
| cursor.style.display = 'inline-block'; |
| cursor.classList.remove('final-cursor'); |
| targetContainer.appendChild(cursor); |
| } |
| |
| |
| |
| |
| uploadArea.addEventListener('click', () => { |
| |
| if (uploadArea.dataset.touched) { |
| delete uploadArea.dataset.touched; |
| return; |
| } |
| imageInput.click(); |
| }); |
| |
| uploadArea.addEventListener('touchstart', function(e) { |
| e.preventDefault(); |
| this.classList.add('active'); |
| this.dataset.touched = true; |
| }, { passive: false }); |
| uploadArea.addEventListener('touchend', function(e) { |
| this.classList.remove('active'); |
| |
| e.preventDefault(); |
| imageInput.click(); |
| |
| setTimeout(() => { delete uploadArea.dataset.touched; }, 300); |
| }); |
| |
| |
| imageInput.addEventListener('change', function() { const file = this.files[0]; if (file) { const reader = new FileReader(); reader.onload = function(e) { previewImage.src = e.target.result; previewContainer.style.display = 'block'; previewContainer.classList.add('slide-up'); uploadArea.classList.add('active'); }; reader.readAsDataURL(file); } else { previewContainer.style.display = 'none'; uploadArea.classList.remove('active'); } }); |
| dissertationTab.addEventListener('click', () => switchTab('dissertation')); |
| tableauTab.addEventListener('click', () => switchTab('tableau')); |
| function switchTab(tabName) { const isDissertation = tabName === 'dissertation'; dissertationTab.classList.toggle('active', isDissertation); tableauTab.classList.toggle('active', !isDissertation); dissertationContentWrapper.style.display = isDissertation ? 'block' : 'none'; tableauContentWrapper.style.display = !isDissertation ? 'block' : 'none'; } |
| |
| |
| function checkDeepThinkLimit() { |
| const lastUsedDate = localStorage.getItem(DEEPTHINK_STORAGE_KEY); |
| const todayDate = new Date().toDateString(); |
| |
| if (lastUsedDate === todayDate) { |
| deepThinkBtn.disabled = true; |
| deepThinkBtn.textContent = "DeepThink (Utilisé)"; |
| console.log("DeepThink limit reached for today."); |
| } else { |
| deepThinkBtn.disabled = false; |
| deepThinkBtn.textContent = "DeepThink (1/jour)"; |
| console.log("DeepThink available."); |
| } |
| } |
| |
| function recordDeepThinkUsage() { |
| const todayDate = new Date().toDateString(); |
| localStorage.setItem(DEEPTHINK_STORAGE_KEY, todayDate); |
| deepThinkBtn.disabled = true; |
| deepThinkBtn.textContent = "DeepThink (Utilisé)"; |
| console.log("DeepThink usage recorded for today."); |
| } |
| |
| |
| function updateDisplayContent(type) { |
| if (typeof window.marked === 'undefined') return; |
| if (type === 'tableau') { |
| |
| |
| tableauContent.innerHTML = marked.parse(tableauMarkdownRaw); |
| ensureCursor('tableau', tableauContent); |
| } else if (type === 'dissertation') { |
| dissertationContent.innerHTML = marked.parse(dissertationMarkdownRaw); |
| ensureCursor('dissertation', dissertationContent); |
| } |
| } |
| function processStreamChunk(jsonData, isDeepThink) { |
| try { |
| const parsedData = JSON.parse(jsonData); |
| if (parsedData.error) { console.error("Erreur serveur:", parsedData.error); alert("Erreur serveur: " + parsedData.error); loadingIndicator.style.display = 'none'; downloadPdfBtn.disabled = true; if (currentlyStreaming) removeCursor(currentlyStreaming); streamComplete = true; return; } |
| |
| |
| if (resultDiv.style.display === 'none') { |
| resultDiv.style.display = 'block'; |
| resultDiv.classList.add('slide-up'); |
| loadingIndicator.style.display = 'none'; |
| switchTab('dissertation'); |
| |
| if (isDeepThink) { |
| recordDeepThinkUsage(); |
| } |
| } |
| |
| if (parsedData.type === 'tableau') { tableauMarkdownRaw += parsedData.chunk; } |
| else if (parsedData.type === 'dissertation') { dissertationMarkdownRaw += parsedData.chunk; } |
| else { console.warn("Type de chunk inconnu reçu:", parsedData.type); return; } |
| |
| if (currentlyStreaming !== parsedData.type) { if (currentlyStreaming) { removeCursor(currentlyStreaming); } currentlyStreaming = parsedData.type; } |
| updateDisplayContent(parsedData.type); |
| |
| } catch (e) { if (e instanceof SyntaxError) { console.warn("Chunk JSON invalide:", jsonData, e); } else { console.error("Erreur traitement chunk:", e, jsonData); } } |
| } |
| function handleStreamEnd() { console.log("Fin du stream."); streamComplete = true; if (currentlyStreaming) { removeCursor(currentlyStreaming); } loadingIndicator.style.display = 'none'; downloadPdfBtn.disabled = false; currentlyStreaming = null; } |
| |
| |
| function startAnalysis(formData) { |
| |
| tableauMarkdownRaw = ''; dissertationMarkdownRaw = ''; currentlyStreaming = null; streamComplete = false; |
| dissertationContent.innerHTML = ''; tableauContent.innerHTML = ''; |
| resultDiv.style.display = 'none'; loadingIndicator.style.display = 'flex'; downloadPdfBtn.disabled = true; switchTab('dissertation'); |
| |
| const isDeepThinkRequest = formData.get('use_deepthink') === 'true'; |
| console.log(`Lancement de l'analyse ${isDeepThinkRequest ? 'DeepThink' : 'standard'}...`); |
| |
| fetch('/analyze', { method: 'POST', body: formData }) |
| .then(response => { |
| if (!response.ok) { |
| return response.text().then(text => { |
| let errorMsg = `Erreur HTTP ${response.status}: ${response.statusText}`; |
| try { const errData = JSON.parse(text); errorMsg += `. Détail: ${errData.error || text}`; } catch (e) { errorMsg += `. Détail: ${text}`; } |
| throw new Error(errorMsg); |
| }); |
| } |
| if (!response.body) { throw new Error("Réponse serveur sans corps."); } |
| const reader = response.body.getReader(); |
| const decoder = new TextDecoder(); |
| function read() { |
| reader.read().then(({done, value}) => { |
| if (done) { handleStreamEnd(); return; } |
| const text = decoder.decode(value, {stream: true}); |
| const lines = text.split('\n').filter(line => line.trim() !== ''); |
| |
| lines.forEach(line => { processStreamChunk(line, isDeepThinkRequest); }); |
| read(); |
| }).catch(error => { console.error('Erreur lecture stream:', error); alert('Erreur réception analyse: ' + error.message); loadingIndicator.style.display = 'none'; downloadPdfBtn.disabled = true; if (currentlyStreaming) removeCursor(currentlyStreaming); streamComplete = true; }); |
| } |
| read(); |
| }) |
| .catch(error => { console.error('Erreur Fetch:', error); loadingIndicator.style.display = 'none'; alert(`Erreur connexion/analyse: ${error.message}`); downloadPdfBtn.disabled = true; streamComplete = true; }); |
| } |
| |
| |
| uploadForm.addEventListener('submit', function(e) { |
| e.preventDefault(); |
| if (imageInput.files.length === 0) { alert("Veuillez sélectionner un fichier image."); return; } |
| |
| const formData = new FormData(); |
| formData.append('image', imageInput.files[0]); |
| formData.append('consignes', consignesInput.value); |
| |
| |
| startAnalysis(formData); |
| }); |
| |
| deepThinkBtn.addEventListener('click', function() { |
| if (this.disabled) { |
| alert("La fonctionnalité DeepThink est limitée à une utilisation par jour."); |
| return; |
| } |
| if (imageInput.files.length === 0) { alert("Veuillez sélectionner un fichier image avant de lancer DeepThink."); return; } |
| |
| |
| const lastUsedDate = localStorage.getItem(DEEPTHINK_STORAGE_KEY); |
| const todayDate = new Date().toDateString(); |
| if(lastUsedDate === todayDate) { |
| alert("DeepThink a déjà été utilisé aujourd'hui."); |
| checkDeepThinkLimit(); |
| return; |
| } |
| |
| const formData = new FormData(); |
| formData.append('image', imageInput.files[0]); |
| formData.append('consignes', consignesInput.value); |
| formData.append('use_deepthink', 'true'); |
| |
| startAnalysis(formData); |
| }); |
| |
| |
| |
| downloadPdfBtn.addEventListener('click', () => { |
| if (!streamComplete || (!dissertationMarkdownRaw && !tableauMarkdownRaw)) { |
| alert("Analyse non terminée ou contenu vide."); |
| return; |
| } |
| if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined' || typeof window.jspdf.jsPDF.API.autoTable === 'undefined') { |
| alert("Erreur critique : jsPDF ou le plugin AutoTable ne sont pas disponibles."); |
| return; |
| } |
| const { jsPDF } = window.jspdf; |
| |
| console.log("Préparation du PDF avec détection de blocs..."); |
| downloadPdfBtn.disabled = true; |
| downloadPdfBtn.textContent = "Génération PDF..."; |
| |
| try { |
| const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'letter' }); |
| const pageHeight = pdf.internal.pageSize.getHeight(); |
| const pageWidth = pdf.internal.pageSize.getWidth(); |
| const margin = 40; |
| const maxLineWidth = pageWidth - margin * 2; |
| const fontSize = 10; |
| const lineHeightFactor = 1.3; |
| const lineHeight = fontSize * lineHeightFactor; |
| let cursorY = margin; |
| |
| pdf.setFontSize(fontSize); |
| |
| pdf.setFont("Helvetica", "normal"); |
| |
| function addTextToPage(text) { |
| if (!text) return; |
| |
| const simplifiedText = text.trim() |
| .replace(/^#+\s+/gm, '') |
| .replace(/^[\*\-]\s+/gm, ' ') |
| .replace(/\*\*(.*?)\*\*/g, '$1') |
| .replace(/\*(.*?)\*/g, '$1') |
| .replace(/`([^`]+)`/g, '$1') |
| .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1'); |
| const lines = pdf.splitTextToSize(simplifiedText, maxLineWidth); |
| lines.forEach(line => { |
| if (cursorY + lineHeight > pageHeight - margin) { pdf.addPage(); cursorY = margin; } |
| pdf.text(line, margin, cursorY); cursorY += lineHeight; |
| }); |
| cursorY += lineHeight * 0.5; |
| } |
| function addTitleToPage(titleText, level = 1) { |
| if (!titleText) return; |
| const titleSize = level === 1 ? 16 : (level === 2 ? 14 : 12); |
| const titleLineHeight = titleSize * lineHeightFactor; |
| if (cursorY + titleLineHeight > pageHeight - margin) { pdf.addPage(); cursorY = margin; } |
| pdf.setFontSize(titleSize); pdf.setFont("Helvetica", "bold"); |
| pdf.text(titleText.trim(), margin, cursorY); |
| pdf.setFontSize(fontSize); pdf.setFont("Helvetica", "normal"); |
| cursorY += titleLineHeight + 6; |
| } |
| function parseMarkdownTable(mdTableBlock) { |
| if (!mdTableBlock || typeof mdTableBlock !== 'string') return { head: [], body: [] }; |
| const lines = mdTableBlock.trim().split('\n'); |
| const cleanCell = (cell) => cell ? cell.trim() : ''; |
| |
| let headerLineIndex = -1, separatorLineIndex = -1; |
| |
| |
| for(let i = 0; i < lines.length; i++) { |
| const trimmedLine = lines[i].trim(); |
| if (trimmedLine.startsWith('|') && trimmedLine.endsWith('|')) { |
| |
| if (trimmedLine.includes('-') && trimmedLine.replace(/\|/g, '').replace(/-/g, '').replace(/:/g, '').trim() === '') { |
| separatorLineIndex = i; |
| |
| if (i > 0 && lines[i-1].trim().startsWith('|') && lines[i-1].trim().endsWith('|')) { |
| headerLineIndex = i - 1; |
| break; |
| } |
| } else if (headerLineIndex === -1 && separatorLineIndex === -1) { |
| |
| headerLineIndex = i; |
| } |
| } |
| } |
| |
| |
| if (headerLineIndex === -1 || separatorLineIndex === -1 || separatorLineIndex !== headerLineIndex + 1) { |
| console.warn("Structure de tableau Markdown non standard ou incomplète:", mdTableBlock.substring(0, 100)); |
| |
| if (lines.length >= 2 && lines[0].includes('|') && lines[1].includes('|') && lines[1].includes('-')) { |
| headerLineIndex = 0; separatorLineIndex = 1; |
| console.log("Fallback: Utilisation des deux premières lignes comme header/separator."); |
| } else { |
| return { head: [], body: [] }; |
| } |
| } |
| |
| const headerLine = lines[headerLineIndex]; |
| |
| const head = headerLine.split('|').map(cleanCell).filter((cell, i, arr) => i > 0 && i < arr.length - 1); |
| |
| const body = lines.slice(separatorLineIndex + 1) |
| .map(line => { |
| const trimmedLine = line.trim(); |
| |
| if (!trimmedLine.startsWith('|') || !trimmedLine.endsWith('|')) return null; |
| |
| return trimmedLine.split('|').map(cleanCell).filter((cell, i, arr) => i > 0 && i < arr.length - 1); |
| }) |
| |
| .filter(row => row && row.length === head.length); |
| |
| return { head: [head], body: body }; |
| } |
| |
| |
| |
| const fullMarkdownText = dissertationMarkdownRaw + "\n\n" + tableauMarkdownRaw; |
| const lines = fullMarkdownText.split('\n'); |
| let currentBlock = ''; |
| let isInsideTable = false; |
| |
| for (let i = 0; i < lines.length; i++) { |
| const line = lines[i]; |
| const trimmedLine = line.trim(); |
| const isTableLine = trimmedLine.startsWith('|') && trimmedLine.endsWith('|'); |
| |
| const isSeparatorLine = isTableLine && trimmedLine.includes('-') && trimmedLine.replace(/\|/g, '').replace(/-/g, '').replace(/:/g, '').trim() === ''; |
| |
| if (isTableLine && !isSeparatorLine) { |
| if (!isInsideTable) { |
| |
| const nextLineIndex = i + 1; |
| const nextLine = (nextLineIndex < lines.length) ? lines[nextLineIndex].trim() : null; |
| const nextLineIsSeparator = nextLine && nextLine.startsWith('|') && nextLine.endsWith('|') && nextLine.includes('-') && nextLine.replace(/\|/g, '').replace(/-/g, '').replace(/:/g, '').trim() === ''; |
| |
| if (nextLineIsSeparator) { |
| |
| if (currentBlock.trim()) { |
| addTextToPage(currentBlock.trim()); |
| } |
| currentBlock = line + '\n'; |
| isInsideTable = true; |
| } else { |
| |
| currentBlock += line + '\n'; |
| } |
| } else { |
| |
| currentBlock += line + '\n'; |
| } |
| } else if (isSeparatorLine) { |
| if (isInsideTable) { |
| |
| currentBlock += line + '\n'; |
| } else { |
| |
| |
| currentBlock += line + '\n'; |
| } |
| } else { |
| if (isInsideTable) { |
| |
| console.log("Traitement bloc tableau détecté."); |
| const tableData = parseMarkdownTable(currentBlock.trim()); |
| if (tableData.head.length > 0 && tableData.head[0].length > 0) { |
| if (cursorY + 30 > pageHeight - margin) { pdf.addPage(); cursorY = margin; } |
| pdf.autoTable({ |
| head: tableData.head, body: tableData.body, startY: cursorY, |
| theme: 'grid', |
| styles: { fontSize: 9, cellPadding: 5, overflow: 'linebreak', font: "Helvetica" }, |
| headStyles: { fillColor: [67, 97, 238], textColor: [255, 255, 255], fontStyle: 'bold', halign: 'center' }, |
| didParseCell: function (data) { data.cell.styles.valign = 'middle'; }, |
| margin: { left: margin, right: margin } |
| }); |
| cursorY = pdf.previousAutoTable.finalY + lineHeight; |
| } else { |
| console.warn("Échec parsing bloc tableau ou tableau vide."); |
| addTextToPage("(Erreur: Bloc tableau non formaté ou vide)\n" + currentBlock.trim()); |
| } |
| currentBlock = line + '\n'; |
| isInsideTable = false; |
| } else { |
| |
| if (trimmedLine.startsWith('#')) { |
| if (currentBlock.trim()) { addTextToPage(currentBlock.trim()); } |
| const level = (trimmedLine.match(/^#+/) || [''])[0].length; |
| const titleText = trimmedLine.replace(/^#+\s*/, ''); |
| addTitleToPage(titleText, level); |
| currentBlock = ''; |
| } else { |
| |
| currentBlock += line + '\n'; |
| } |
| } |
| } |
| } |
| |
| |
| if (currentBlock.trim()) { |
| if (isInsideTable) { |
| |
| console.log("Traitement dernier bloc (tableau)."); |
| const tableData = parseMarkdownTable(currentBlock.trim()); |
| if (tableData.head.length > 0 && tableData.head[0].length > 0) { |
| if (cursorY + 30 > pageHeight - margin) { pdf.addPage(); cursorY = margin; } |
| pdf.autoTable({ |
| head: tableData.head, body: tableData.body, startY: cursorY, |
| theme: 'grid', styles: { fontSize: 9, cellPadding: 5, overflow: 'linebreak', font: "Helvetica" }, |
| headStyles: { fillColor: [67, 97, 238], textColor: [255, 255, 255], fontStyle: 'bold', halign: 'center' }, |
| didParseCell: function (data) { data.cell.styles.valign = 'middle'; }, |
| margin: { left: margin, right: margin } |
| }); |
| } else { |
| console.warn("Échec parsing dernier bloc tableau ou tableau vide."); |
| addTextToPage("(Erreur: Dernier bloc tableau non formaté ou vide)\n" + currentBlock.trim()); |
| } |
| } else { |
| |
| console.log("Ajout dernier bloc texte."); |
| addTextToPage(currentBlock.trim()); |
| } |
| } |
| |
| console.log("Contenu ajouté, sauvegarde du PDF..."); |
| pdf.save('MariamAI-Analyse-Resultat.pdf'); |
| |
| } catch (error) { |
| console.error("Erreur lors de la génération du PDF mixte:", error); |
| alert("Une erreur s'est produite lors de la création du PDF mixte."); |
| } finally { |
| downloadPdfBtn.disabled = false; |
| downloadPdfBtn.textContent = "Télécharger en PDF"; |
| } |
| }); |
| |
| |
| document.addEventListener('DOMContentLoaded', () => { |
| switchTab('dissertation'); |
| checkDeepThinkLimit(); |
| |
| |
| const interactiveElements = document.querySelectorAll('.btn, .tab'); |
| interactiveElements.forEach(el => { |
| let touchMoved = false; |
| |
| el.addEventListener('touchstart', function() { |
| touchMoved = false; |
| if (!this.disabled) { |
| |
| this.style.transition = 'transform 0.1s ease, background-color 0.1s ease'; |
| this.style.transform = 'scale(0.97)'; |
| |
| |
| } |
| }, { passive: true }); |
| |
| el.addEventListener('touchmove', function() { |
| touchMoved = true; |
| this.style.transform = 'scale(1)'; |
| |
| }, { passive: true }); |
| |
| el.addEventListener('touchend', function() { |
| |
| this.style.transform = 'scale(1)'; |
| |
| }); |
| |
| |
| el.addEventListener('mousedown', function() { if (!this.disabled) { this.style.transform = 'scale(0.97)'; } }); |
| el.addEventListener('mouseup', function() { this.style.transform = 'scale(1)'; }); |
| el.addEventListener('mouseleave', function() { this.style.transform = 'scale(1)'; }); |
| }); |
| }); |
| |
| </script> |
|
|
| </body> |
| </html> |