Update templates/index.html
Browse files- templates/index.html +164 -141
templates/index.html
CHANGED
|
@@ -1,151 +1,174 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
::-webkit-scrollbar-track {
|
| 15 |
-
background: #f1f1f1;
|
| 16 |
-
border-radius: 10px;
|
| 17 |
-
}
|
| 18 |
-
::-webkit-scrollbar-thumb {
|
| 19 |
-
background: #888;
|
| 20 |
-
border-radius: 10px;
|
| 21 |
-
}
|
| 22 |
-
::-webkit-scrollbar-thumb:hover {
|
| 23 |
-
background: #555;
|
| 24 |
-
}
|
| 25 |
-
/* Empêche le FOUC (Flash of Unstyled Content) */
|
| 26 |
-
[x-cloak] { display: none !important; }
|
| 27 |
-
</style>
|
| 28 |
-
<!-- Alpine.js pour une interactivité simple (optionnel mais utile) -->
|
| 29 |
-
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
|
| 30 |
-
</head>
|
| 31 |
-
<body class="bg-gray-100 flex items-center justify-center min-h-screen" x-data="{ processing: {{ 'true' if processing_message else 'false' }} }">
|
| 32 |
-
|
| 33 |
-
<div class="chat-container bg-white rounded-lg shadow-xl w-full max-w-3xl h-[90vh] flex flex-col" x-cloak>
|
| 34 |
-
<!-- En-tête -->
|
| 35 |
-
<div class="chat-header bg-gradient-to-r from-emerald-500 to-teal-600 text-white p-4 rounded-t-lg flex justify-between items-center">
|
| 36 |
-
<h1 class="text-xl font-semibold">Mariam AI!</h1>
|
| 37 |
-
<!-- Bouton pour effacer (optionnel mais utile) -->
|
| 38 |
-
<form action="{{ url_for('clear_chat') }}" method="post" onsubmit="return confirm('Voulez-vous vraiment effacer la conversation ?');">
|
| 39 |
-
<button type="submit" title="Effacer la conversation" class="text-white hover:text-red-300 transition duration-150">
|
| 40 |
-
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
|
| 41 |
-
<path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12.576 0a48.108 48.108 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
|
| 42 |
-
</svg>
|
| 43 |
-
</button>
|
| 44 |
-
</form>
|
| 45 |
-
</div>
|
| 46 |
-
|
| 47 |
-
<!-- Zone des messages -->
|
| 48 |
-
<div class="chat-messages flex-grow overflow-y-auto p-4 md:p-6 space-y-4" id="chat-messages">
|
| 49 |
-
{% for message in chat_history %}
|
| 50 |
-
<div class="message flex {% if message.role == 'user' %}justify-end{% else %}justify-start{% endif %}">
|
| 51 |
-
<div class="max-w-xs md:max-w-md lg:max-w-lg px-4 py-2 rounded-lg shadow {% if message.role == 'user' %}bg-emerald-500 text-white rounded-br-none{% else %}bg-gray-200 text-gray-800 rounded-bl-none{% endif %}">
|
| 52 |
-
<p class="text-sm break-words whitespace-pre-wrap">{{ message.text }}</p>
|
| 53 |
-
</div>
|
| 54 |
-
</div>
|
| 55 |
-
{% endfor %}
|
| 56 |
-
|
| 57 |
-
<!-- Indicateur de chargement -->
|
| 58 |
-
<div x-show="processing" class="flex justify-center items-center py-4">
|
| 59 |
-
<div class="animate-spin rounded-full h-6 w-6 border-b-2 border-emerald-500"></div>
|
| 60 |
-
<span class="ml-2 text-gray-500 italic">
|
| 61 |
-
{% if session.get('processing_web_search') %}
|
| 62 |
-
Recherche web et génération en cours...
|
| 63 |
-
{% else %}
|
| 64 |
-
Génération de la réponse...
|
| 65 |
-
{% endif %}
|
| 66 |
-
</span>
|
| 67 |
-
</div>
|
| 68 |
-
</div>
|
| 69 |
-
|
| 70 |
-
<!-- Affichage des erreurs -->
|
| 71 |
-
{% if error %}
|
| 72 |
-
<div class="error-message bg-red-100 border border-red-400 text-red-700 px-4 py-2 rounded-md mx-4 mb-2 text-sm">
|
| 73 |
-
<strong>Erreur :</strong> {{ error }}
|
| 74 |
-
</div>
|
| 75 |
-
{% endif %}
|
| 76 |
-
|
| 77 |
-
<!-- Zone de saisie et options -->
|
| 78 |
-
<div class="chat-input-area border-t border-gray-200 p-4 bg-gray-50 rounded-b-lg">
|
| 79 |
-
<form method="POST" action="{{ url_for('chat') }}" enctype="multipart/form-data" id="chat-form" @submit="processing = true">
|
| 80 |
-
<!-- Options : Recherche Web & Upload -->
|
| 81 |
-
<div class="options flex flex-col sm:flex-row justify-between items-center mb-3 gap-3 text-sm text-gray-600">
|
| 82 |
-
<label for="web_search_toggle" class="flex items-center cursor-pointer">
|
| 83 |
-
<input type="checkbox" id="web_search_toggle" name="web_search" value="true" {% if web_search_active %}checked{% endif %} class="mr-2 h-4 w-4 rounded border-gray-300 text-emerald-600 focus:ring-emerald-500">
|
| 84 |
-
Activer la recherche web
|
| 85 |
-
</label>
|
| 86 |
-
|
| 87 |
-
<label for="file_upload" class="flex items-center cursor-pointer bg-white border border-gray-300 rounded-md px-3 py-1 hover:bg-gray-50 transition duration-150">
|
| 88 |
-
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 mr-2 text-gray-500">
|
| 89 |
-
<path stroke-linecap="round" stroke-linejoin="round" d="m18.375 12.739-7.693 7.693a4.5 4.5 0 0 1-6.364-6.364l10.94-10.94A3 3 0 1 1 19.5 7.372L8.552 18.32m.009-.01-.01.01m5.699-9.941-7.81 7.81a1.5 1.5 0 0 0 2.122 2.122l7.81-7.81" />
|
| 90 |
-
</svg>
|
| 91 |
-
<span id="file-label-text">Joindre un fichier</span> (.jpg, .png, .pdf, .txt)
|
| 92 |
-
<input type="file" id="file_upload" name="file" class="hidden" accept=".jpg,.jpeg,.png,.pdf,.txt" onchange="updateFileLabel(this)">
|
| 93 |
-
</label>
|
| 94 |
-
</div>
|
| 95 |
-
|
| 96 |
-
<!-- Champ de saisie et bouton envoyer -->
|
| 97 |
-
<div class="flex items-center gap-2">
|
| 98 |
-
<input type="text" name="prompt" id="prompt-input" placeholder="Posez votre question à Mariam..." required autofocus
|
| 99 |
-
class="flex-grow p-3 border border-gray-300 rounded-full focus:outline-none focus:ring-2 focus:ring-emerald-500 focus:border-transparent transition duration-150"
|
| 100 |
-
:disabled="processing"> <!-- Désactivé pendant le traitement -->
|
| 101 |
-
|
| 102 |
-
<button type="submit" id="send-button"
|
| 103 |
-
class="bg-emerald-500 hover:bg-emerald-600 text-white rounded-full p-3 focus:outline-none focus:ring-2 focus:ring-emerald-500 focus:ring-offset-1 transition duration-150 disabled:opacity-50 disabled:cursor-not-allowed"
|
| 104 |
-
:disabled="processing"> <!-- Désactivé pendant le traitement -->
|
| 105 |
-
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
|
| 106 |
-
<path stroke-linecap="round" stroke-linejoin="round" d="M6 12 3.269 3.125A59.769 59.769 0 0 1 21.485 12 59.768 59.768 0 0 1 3.27 20.875L5.999 12Zm0 0h7.5" />
|
| 107 |
-
</svg>
|
| 108 |
-
</button>
|
| 109 |
-
</div>
|
| 110 |
-
</form>
|
| 111 |
-
</div>
|
| 112 |
-
</div>
|
| 113 |
-
|
| 114 |
-
<script>
|
| 115 |
-
// Fonction pour faire défiler vers le bas
|
| 116 |
function scrollToBottom() {
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
}
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
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
|
|
|
| 1 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 2 |
+
const chatForm = document.getElementById('chat-form');
|
| 3 |
+
const promptInput = document.getElementById('prompt');
|
| 4 |
+
const chatMessages = document.getElementById('chat-messages');
|
| 5 |
+
const loadingIndicator = document.getElementById('loading-indicator');
|
| 6 |
+
const errorMessageDiv = document.getElementById('error-message');
|
| 7 |
+
const webSearchToggle = document.getElementById('web_search_toggle');
|
| 8 |
+
const fileUpload = document.getElementById('file_upload');
|
| 9 |
+
const fileNameSpan = document.getElementById('file-name');
|
| 10 |
+
const sendButton = document.getElementById('send-button');
|
| 11 |
+
|
| 12 |
+
// --- Fonctions Utilitaires ---
|
| 13 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
function scrollToBottom() {
|
| 15 |
+
// Ajoute un léger délai pour laisser le temps au DOM de se mettre à jour
|
| 16 |
+
setTimeout(() => {
|
| 17 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
| 18 |
+
}, 50);
|
| 19 |
}
|
| 20 |
|
| 21 |
+
function showLoading(show) {
|
| 22 |
+
loadingIndicator.style.display = show ? 'block' : 'none';
|
| 23 |
+
sendButton.disabled = show;
|
| 24 |
+
promptInput.disabled = show;
|
| 25 |
+
// Optionnel: changer l'apparence du bouton pendant le chargement
|
| 26 |
+
sendButton.classList.toggle('opacity-50', show);
|
| 27 |
+
sendButton.classList.toggle('cursor-not-allowed', show);
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
+
function displayError(message) {
|
| 31 |
+
errorMessageDiv.textContent = message;
|
| 32 |
+
errorMessageDiv.style.display = 'block';
|
| 33 |
+
// Cache le message après quelques secondes
|
| 34 |
+
setTimeout(() => {
|
| 35 |
+
errorMessageDiv.style.display = 'none';
|
| 36 |
+
}, 5000);
|
| 37 |
+
}
|
| 38 |
|
| 39 |
+
function addMessageToChat(role, text) {
|
| 40 |
+
// Assainir le texte avant de l'insérer (très basique, pour éviter XSS simple)
|
| 41 |
+
// Pour du Markdown, une bibliothèque comme 'marked' ou 'showdown' serait nécessaire côté client
|
| 42 |
+
// ou s'assurer que le backend renvoie du HTML sûr.
|
| 43 |
+
// Ici, on suppose que le backend renvoie du texte simple ou du HTML déjà aseptisé avec | safe
|
| 44 |
+
const messageDiv = document.createElement('div');
|
| 45 |
+
messageDiv.classList.add('flex', role === 'user' ? 'justify-end' : 'justify-start');
|
| 46 |
|
| 47 |
+
const bubbleDiv = document.createElement('div');
|
| 48 |
+
bubbleDiv.classList.add('p-3', 'rounded-lg', 'max-w-xs', 'md:max-w-md', 'shadow');
|
| 49 |
+
if (role === 'user') {
|
| 50 |
+
bubbleDiv.classList.add('bg-blue-500', 'text-white', 'rounded-br-none');
|
| 51 |
+
} else {
|
| 52 |
+
// Pour l'assistant, créer une div interne pour le formatage prose si nécessaire
|
| 53 |
+
bubbleDiv.classList.add('bg-gray-200', 'text-gray-800', 'rounded-bl-none');
|
| 54 |
+
const proseDiv = document.createElement('div');
|
| 55 |
+
proseDiv.classList.add('prose', 'prose-sm', 'max-w-none');
|
| 56 |
+
// IMPORTANT: Si le backend renvoie du HTML, il DOIT être sûr.
|
| 57 |
+
// Sinon, utiliser textContent pour éviter l'injection de script.
|
| 58 |
+
proseDiv.innerHTML = text; // Suppose que le HTML reçu est sûr (ex: via Markdown rendu côté serveur)
|
| 59 |
+
// Si juste du texte : proseDiv.textContent = text;
|
| 60 |
+
bubbleDiv.appendChild(proseDiv);
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// Si c'est un message utilisateur simple (pas de HTML)
|
| 64 |
+
if(role === 'user'){
|
| 65 |
+
const paragraph = document.createElement('p');
|
| 66 |
+
paragraph.classList.add('text-sm');
|
| 67 |
+
paragraph.textContent = text; // Utiliser textContent pour la sécurité
|
| 68 |
+
bubbleDiv.appendChild(paragraph);
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
messageDiv.appendChild(bubbleDiv);
|
| 73 |
+
chatMessages.appendChild(messageDiv);
|
| 74 |
+
|
| 75 |
+
// Insérer l'indicateur *après* le dernier message pour qu'il soit en bas
|
| 76 |
+
chatMessages.appendChild(loadingIndicator);
|
| 77 |
+
|
| 78 |
+
scrollToBottom();
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
// --- Gestionnaires d'événements ---
|
| 82 |
+
|
| 83 |
+
// Afficher le nom du fichier sélectionné
|
| 84 |
+
fileUpload.addEventListener('change', () => {
|
| 85 |
+
if (fileUpload.files.length > 0) {
|
| 86 |
+
fileNameSpan.textContent = fileUpload.files[0].name;
|
| 87 |
+
fileNameSpan.title = fileUpload.files[0].name; // Pour le nom complet au survol
|
| 88 |
+
} else {
|
| 89 |
+
fileNameSpan.textContent = '';
|
| 90 |
+
fileNameSpan.title = '';
|
| 91 |
+
}
|
| 92 |
});
|
|
|
|
| 93 |
|
| 94 |
+
// Soumission du formulaire via Fetch API
|
| 95 |
+
chatForm.addEventListener('submit', async (e) => {
|
| 96 |
+
e.preventDefault(); // Empêche le rechargement de la page
|
| 97 |
+
|
| 98 |
+
const prompt = promptInput.value.trim();
|
| 99 |
+
const file = fileUpload.files[0];
|
| 100 |
+
const useWebSearch = webSearchToggle.checked;
|
| 101 |
+
|
| 102 |
+
if (!prompt && !file) {
|
| 103 |
+
displayError("Veuillez entrer un message ou sélectionner un fichier.");
|
| 104 |
+
return;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
// Cacher les erreurs précédentes
|
| 108 |
+
errorMessageDiv.style.display = 'none';
|
| 109 |
+
|
| 110 |
+
// Afficher le message utilisateur immédiatement
|
| 111 |
+
let userMessageText = prompt;
|
| 112 |
+
if (file) {
|
| 113 |
+
userMessageText = `[Fichier: ${file.name}]\n\n${prompt}`;
|
| 114 |
+
}
|
| 115 |
+
addMessageToChat('user', userMessageText);
|
| 116 |
+
|
| 117 |
+
// Préparer les données du formulaire pour l'envoi (y compris le fichier)
|
| 118 |
+
const formData = new FormData();
|
| 119 |
+
formData.append('prompt', prompt);
|
| 120 |
+
formData.append('web_search', useWebSearch);
|
| 121 |
+
if (file) {
|
| 122 |
+
formData.append('file', file);
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// Afficher le chargement et désactiver les entrées
|
| 126 |
+
showLoading(true);
|
| 127 |
+
promptInput.value = ''; // Vider l'input après l'envoi
|
| 128 |
+
fileUpload.value = ''; // Réinitialiser l'input fichier
|
| 129 |
+
fileNameSpan.textContent = ''; // Vider le nom du fichier affiché
|
| 130 |
+
|
| 131 |
+
try {
|
| 132 |
+
const response = await fetch("{{ url_for('chat_api') }}", { // Appel vers la nouvelle route API
|
| 133 |
+
method: 'POST',
|
| 134 |
+
body: formData, // Pas besoin de 'Content-Type', le navigateur le définit pour FormData
|
| 135 |
+
});
|
| 136 |
+
|
| 137 |
+
if (!response.ok) {
|
| 138 |
+
// Essayer de lire l'erreur JSON si possible
|
| 139 |
+
let errorMsg = `Erreur HTTP: ${response.status}`;
|
| 140 |
+
try {
|
| 141 |
+
const errorData = await response.json();
|
| 142 |
+
errorMsg = errorData.error || errorMsg;
|
| 143 |
+
} catch (jsonError) {
|
| 144 |
+
// Ignorer si la réponse n'est pas du JSON
|
| 145 |
+
}
|
| 146 |
+
throw new Error(errorMsg);
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
const data = await response.json();
|
| 150 |
+
|
| 151 |
+
if (data.success && data.message) {
|
| 152 |
+
// IMPORTANT: S'assurer que data.message est du HTML sûr ou l'assainir ici
|
| 153 |
+
addMessageToChat('assistant', data.message);
|
| 154 |
+
} else if (data.error) {
|
| 155 |
+
displayError(data.error);
|
| 156 |
+
// Optionnel: supprimer le message utilisateur si l'IA n'a pas pu répondre
|
| 157 |
+
// (peut être déroutant pour l'utilisateur)
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
} catch (error) {
|
| 161 |
+
console.error("Erreur lors de l'envoi du message:", error);
|
| 162 |
+
displayError(error.message || "Une erreur inconnue est survenue.");
|
| 163 |
+
// Optionnel: supprimer le message utilisateur en cas d'erreur réseau grave
|
| 164 |
+
} finally {
|
| 165 |
+
// Cacher le chargement et réactiver les entrées
|
| 166 |
+
showLoading(false);
|
| 167 |
+
promptInput.focus(); // Remettre le focus sur l'input
|
| 168 |
+
}
|
| 169 |
+
});
|
| 170 |
|
| 171 |
+
// Scroll initial vers le bas au chargement de la page
|
| 172 |
+
scrollToBottom();
|
| 173 |
+
promptInput.focus(); // Focus sur l'input au chargement
|
| 174 |
+
});
|