Spaces:
Sleeping
Sleeping
Add Markdown compability
Browse files- static/script.js +41 -13
- templates/index.html +2 -1
static/script.js
CHANGED
|
@@ -89,7 +89,7 @@ class DocumentSearchChatBot {
|
|
| 89 |
const data = await response.json();
|
| 90 |
this.searchResults = data.results || [];
|
| 91 |
this.displayResults();
|
| 92 |
-
|
| 93 |
} catch (error) {
|
| 94 |
console.error('Error on search:', error);
|
| 95 |
this.showError(`Error on search. Please try again. : ${error}`);
|
|
@@ -101,7 +101,7 @@ class DocumentSearchChatBot {
|
|
| 101 |
displayResults() {
|
| 102 |
const resultsContainer = document.getElementById('results-container');
|
| 103 |
const resultsSection = document.getElementById('results-section');
|
| 104 |
-
|
| 105 |
if (this.searchResults.length === 0) {
|
| 106 |
resultsContainer.innerHTML = '<p class="no-results">No results has been found.</p>';
|
| 107 |
resultsSection.classList.remove('hidden');
|
|
@@ -109,7 +109,7 @@ class DocumentSearchChatBot {
|
|
| 109 |
}
|
| 110 |
|
| 111 |
resultsContainer.innerHTML = '';
|
| 112 |
-
|
| 113 |
this.searchResults.forEach((result, index) => {
|
| 114 |
const card = this.createResultCard(result, index);
|
| 115 |
resultsContainer.appendChild(card);
|
|
@@ -159,7 +159,7 @@ class DocumentSearchChatBot {
|
|
| 159 |
toggleDocumentSelection(index) {
|
| 160 |
const card = document.querySelector(`[data-index="${index}"]`);
|
| 161 |
const checkbox = card.querySelector('.select-checkbox');
|
| 162 |
-
|
| 163 |
if (checkbox.checked) {
|
| 164 |
this.selectedDocuments.add(index);
|
| 165 |
card.classList.add('selected');
|
|
@@ -167,7 +167,7 @@ class DocumentSearchChatBot {
|
|
| 167 |
this.selectedDocuments.delete(index);
|
| 168 |
card.classList.remove('selected');
|
| 169 |
}
|
| 170 |
-
|
| 171 |
this.updateChatButtonState();
|
| 172 |
}
|
| 173 |
|
|
@@ -208,14 +208,17 @@ class DocumentSearchChatBot {
|
|
| 208 |
}
|
| 209 |
|
| 210 |
startChat() {
|
| 211 |
-
|
| 212 |
if (this.selectedDocuments.size === 0) return;
|
| 213 |
|
| 214 |
const selectedDocs = Array.from(this.selectedDocuments).map(index => {
|
| 215 |
const doc = this.searchResults[index];
|
| 216 |
return `(${doc.id} ${doc.title} ${doc.section} ${doc.content || ""})`
|
| 217 |
}).join("\n");
|
| 218 |
-
this.chatHistory = [{
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
document.getElementById('search-section').classList.add('hidden');
|
| 221 |
document.getElementById('results-section').classList.add('hidden');
|
|
@@ -229,7 +232,7 @@ class DocumentSearchChatBot {
|
|
| 229 |
document.getElementById('chat-section').classList.add('hidden');
|
| 230 |
document.getElementById('search-section').classList.remove('hidden');
|
| 231 |
document.getElementById('results-section').classList.remove('hidden');
|
| 232 |
-
|
| 233 |
// Vider les messages du chat
|
| 234 |
document.getElementById('chat-messages').innerHTML = '';
|
| 235 |
}
|
|
@@ -245,7 +248,10 @@ class DocumentSearchChatBot {
|
|
| 245 |
this.addChatMessage('user', message);
|
| 246 |
input.value = '';
|
| 247 |
|
| 248 |
-
this.chatHistory.push({
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
// Désactiver le formulaire pendant l'envoi
|
| 251 |
const form = document.getElementById('chat-form');
|
|
@@ -265,14 +271,20 @@ class DocumentSearchChatBot {
|
|
| 265 |
});
|
| 266 |
|
| 267 |
if (!response.ok) {
|
| 268 |
-
this.chatHistory.push({
|
|
|
|
|
|
|
|
|
|
| 269 |
throw new Error(`HTTP error! status: ${response.status}`);
|
| 270 |
}
|
| 271 |
|
| 272 |
const data = await response.json();
|
| 273 |
this.addChatMessage('bot', data.response);
|
| 274 |
-
this.chatHistory.push({
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
| 276 |
} catch (error) {
|
| 277 |
console.error('Error on sending message:', error);
|
| 278 |
this.addChatMessage('bot', `Sorry, an error has occurred. Please try again. : ${error}`);
|
|
@@ -285,7 +297,23 @@ class DocumentSearchChatBot {
|
|
| 285 |
const messagesContainer = document.getElementById('chat-messages');
|
| 286 |
const messageDiv = document.createElement('div');
|
| 287 |
messageDiv.className = `message ${sender}`;
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
messagesContainer.appendChild(messageDiv);
|
| 290 |
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 291 |
}
|
|
|
|
| 89 |
const data = await response.json();
|
| 90 |
this.searchResults = data.results || [];
|
| 91 |
this.displayResults();
|
| 92 |
+
|
| 93 |
} catch (error) {
|
| 94 |
console.error('Error on search:', error);
|
| 95 |
this.showError(`Error on search. Please try again. : ${error}`);
|
|
|
|
| 101 |
displayResults() {
|
| 102 |
const resultsContainer = document.getElementById('results-container');
|
| 103 |
const resultsSection = document.getElementById('results-section');
|
| 104 |
+
|
| 105 |
if (this.searchResults.length === 0) {
|
| 106 |
resultsContainer.innerHTML = '<p class="no-results">No results has been found.</p>';
|
| 107 |
resultsSection.classList.remove('hidden');
|
|
|
|
| 109 |
}
|
| 110 |
|
| 111 |
resultsContainer.innerHTML = '';
|
| 112 |
+
|
| 113 |
this.searchResults.forEach((result, index) => {
|
| 114 |
const card = this.createResultCard(result, index);
|
| 115 |
resultsContainer.appendChild(card);
|
|
|
|
| 159 |
toggleDocumentSelection(index) {
|
| 160 |
const card = document.querySelector(`[data-index="${index}"]`);
|
| 161 |
const checkbox = card.querySelector('.select-checkbox');
|
| 162 |
+
|
| 163 |
if (checkbox.checked) {
|
| 164 |
this.selectedDocuments.add(index);
|
| 165 |
card.classList.add('selected');
|
|
|
|
| 167 |
this.selectedDocuments.delete(index);
|
| 168 |
card.classList.remove('selected');
|
| 169 |
}
|
| 170 |
+
|
| 171 |
this.updateChatButtonState();
|
| 172 |
}
|
| 173 |
|
|
|
|
| 208 |
}
|
| 209 |
|
| 210 |
startChat() {
|
| 211 |
+
|
| 212 |
if (this.selectedDocuments.size === 0) return;
|
| 213 |
|
| 214 |
const selectedDocs = Array.from(this.selectedDocuments).map(index => {
|
| 215 |
const doc = this.searchResults[index];
|
| 216 |
return `(${doc.id} ${doc.title} ${doc.section} ${doc.content || ""})`
|
| 217 |
}).join("\n");
|
| 218 |
+
this.chatHistory = [{
|
| 219 |
+
"role": "system",
|
| 220 |
+
"content": `You are a helpful AI assistant. You will answer any questions related to the following specifications: ${selectedDocs}`
|
| 221 |
+
}];
|
| 222 |
|
| 223 |
document.getElementById('search-section').classList.add('hidden');
|
| 224 |
document.getElementById('results-section').classList.add('hidden');
|
|
|
|
| 232 |
document.getElementById('chat-section').classList.add('hidden');
|
| 233 |
document.getElementById('search-section').classList.remove('hidden');
|
| 234 |
document.getElementById('results-section').classList.remove('hidden');
|
| 235 |
+
|
| 236 |
// Vider les messages du chat
|
| 237 |
document.getElementById('chat-messages').innerHTML = '';
|
| 238 |
}
|
|
|
|
| 248 |
this.addChatMessage('user', message);
|
| 249 |
input.value = '';
|
| 250 |
|
| 251 |
+
this.chatHistory.push({
|
| 252 |
+
"role": "user",
|
| 253 |
+
"content": message
|
| 254 |
+
});
|
| 255 |
|
| 256 |
// Désactiver le formulaire pendant l'envoi
|
| 257 |
const form = document.getElementById('chat-form');
|
|
|
|
| 271 |
});
|
| 272 |
|
| 273 |
if (!response.ok) {
|
| 274 |
+
this.chatHistory.push({
|
| 275 |
+
"role": "assistant",
|
| 276 |
+
"content": `HTTP error! status: ${response.status}`
|
| 277 |
+
})
|
| 278 |
throw new Error(`HTTP error! status: ${response.status}`);
|
| 279 |
}
|
| 280 |
|
| 281 |
const data = await response.json();
|
| 282 |
this.addChatMessage('bot', data.response);
|
| 283 |
+
this.chatHistory.push({
|
| 284 |
+
"role": "assistant",
|
| 285 |
+
"content": data.response
|
| 286 |
+
})
|
| 287 |
+
|
| 288 |
} catch (error) {
|
| 289 |
console.error('Error on sending message:', error);
|
| 290 |
this.addChatMessage('bot', `Sorry, an error has occurred. Please try again. : ${error}`);
|
|
|
|
| 297 |
const messagesContainer = document.getElementById('chat-messages');
|
| 298 |
const messageDiv = document.createElement('div');
|
| 299 |
messageDiv.className = `message ${sender}`;
|
| 300 |
+
|
| 301 |
+
try {
|
| 302 |
+
// Vérifier si marked est disponible
|
| 303 |
+
if (typeof marked !== 'undefined' && marked.parse) {
|
| 304 |
+
messageDiv.innerHTML = marked.parse(message);
|
| 305 |
+
} else if (typeof marked !== 'undefined' && marked.marked) {
|
| 306 |
+
// Pour les versions plus anciennes
|
| 307 |
+
messageDiv.innerHTML = marked.marked(message);
|
| 308 |
+
} else {
|
| 309 |
+
console.error('Marked library not loaded properly');
|
| 310 |
+
messageDiv.textContent = message; // Fallback
|
| 311 |
+
}
|
| 312 |
+
} catch (error) {
|
| 313 |
+
console.error('Markdown parsing error:', error);
|
| 314 |
+
messageDiv.textContent = message; // Fallback en cas d'erreur
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
messagesContainer.appendChild(messageDiv);
|
| 318 |
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
| 319 |
}
|
templates/index.html
CHANGED
|
@@ -9,6 +9,7 @@
|
|
| 9 |
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
|
| 10 |
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
| 11 |
<link rel="stylesheet" href="/static/styles.css">
|
|
|
|
| 12 |
</head>
|
| 13 |
<body>
|
| 14 |
<div class="container">
|
|
@@ -114,6 +115,6 @@
|
|
| 114 |
<p>Searching ...</p>
|
| 115 |
</div>
|
| 116 |
|
| 117 |
-
<script src="/static/script.js"></script>
|
| 118 |
</body>
|
| 119 |
</html>
|
|
|
|
| 9 |
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
|
| 10 |
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
| 11 |
<link rel="stylesheet" href="/static/styles.css">
|
| 12 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/15.0.12/marked.min.js"></script>
|
| 13 |
</head>
|
| 14 |
<body>
|
| 15 |
<div class="container">
|
|
|
|
| 115 |
<p>Searching ...</p>
|
| 116 |
</div>
|
| 117 |
|
| 118 |
+
<script type="module" src="/static/script.js"></script>
|
| 119 |
</body>
|
| 120 |
</html>
|