Spaces:
Sleeping
Sleeping
Prathamesh Sable
commited on
Commit
·
64cadc5
1
Parent(s):
0c9476c
fixed issue with source
Browse files- templates/index.html +132 -67
- utils.py +1 -1
templates/index.html
CHANGED
|
@@ -51,7 +51,7 @@
|
|
| 51 |
Question Files
|
| 52 |
</h2>
|
| 53 |
<div class="flex items-center space-x-4">
|
| 54 |
-
<button class="bg-gray-200 dark:bg-gray-700 p-2 rounded-full" id="theme-toggle">
|
| 55 |
<i class="fas fa-moon dark:hidden">
|
| 56 |
</i>
|
| 57 |
<i class="fas fa-sun hidden dark:block">
|
|
@@ -158,6 +158,26 @@
|
|
| 158 |
</div>
|
| 159 |
</div>
|
| 160 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
<script src="https://code.jquery.com/jquery-3.7.1.min.js"
|
| 162 |
integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
|
| 163 |
<script>
|
|
@@ -175,6 +195,10 @@
|
|
| 175 |
const fileInput = document.getElementById('file-upload'); // input element
|
| 176 |
const fileList = document.getElementById('file-list'); // div to display files uploaded
|
| 177 |
const chat = document.getElementById('chat-messages');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
var file_list = [];
|
| 180 |
var file_count = 0;
|
|
@@ -196,79 +220,28 @@
|
|
| 196 |
}
|
| 197 |
});
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
<p>
|
| 204 |
-
${msg}
|
| 205 |
-
</p>
|
| 206 |
-
</div>`;
|
| 207 |
-
chat.appendChild(item);
|
| 208 |
-
}
|
| 209 |
-
|
| 210 |
-
function add_ai_response(resp) {
|
| 211 |
-
sources = ""
|
| 212 |
-
if (resp.is_relevant) {
|
| 213 |
-
sources += "Sources: "
|
| 214 |
-
resp.sources.forEach(function (source) {
|
| 215 |
-
sources += '<a class="text-blue-500" href="#" style="background: #00000085;border-radius: 4px;padding: 1px 3px;margin: 2px;">'
|
| 216 |
-
sources += source.metadata.source.split("\\")[1]
|
| 217 |
-
if (source.metadata.page != undefined) {
|
| 218 |
-
sources += ", Page:" + source.metadata.page
|
| 219 |
-
}
|
| 220 |
-
sources += '</a>,'
|
| 221 |
-
});
|
| 222 |
-
|
| 223 |
-
} else {
|
| 224 |
-
sources = "The Documents provided are not sufficient to answer the query provided, So response might not be accurate."
|
| 225 |
-
}
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
</p>
|
| 233 |
-
<p class="text-sm text-gray-500 dark:text-gray-400 mt-2">
|
| 234 |
-
${sources}
|
| 235 |
-
</p>
|
| 236 |
-
</div>`;
|
| 237 |
-
chat.appendChild(item);
|
| 238 |
}
|
| 239 |
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
const messageInput = document.getElementById('message-input');
|
| 245 |
-
const message = messageInput.value.trim();
|
| 246 |
-
|
| 247 |
-
messageInput.value = "";
|
| 248 |
-
|
| 249 |
-
add_user_message(message)
|
| 250 |
-
|
| 251 |
-
if (message) {
|
| 252 |
-
$.ajax({
|
| 253 |
-
url: "/ask_query",
|
| 254 |
-
method: "POST",
|
| 255 |
-
data: {
|
| 256 |
-
query: message
|
| 257 |
-
},
|
| 258 |
-
success: function (response) {
|
| 259 |
-
resp = JSON.stringify(response)
|
| 260 |
-
resp = JSON.parse(resp)
|
| 261 |
-
console.log(resp);
|
| 262 |
-
add_ai_response(resp);
|
| 263 |
-
},
|
| 264 |
-
error: function (xhr, status, error) {
|
| 265 |
-
console.log(error);
|
| 266 |
-
}
|
| 267 |
-
})
|
| 268 |
}
|
| 269 |
});
|
| 270 |
|
| 271 |
-
|
| 272 |
document.addEventListener('DOMContentLoaded', () => {
|
| 273 |
fileInput.addEventListener("change", () => {
|
| 274 |
const files = fileInput.files;
|
|
@@ -356,6 +329,98 @@
|
|
| 356 |
manageEmptyFileList();
|
| 357 |
}
|
| 358 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
});
|
| 360 |
|
| 361 |
function manageEmptyFileList() {
|
|
|
|
| 51 |
Question Files
|
| 52 |
</h2>
|
| 53 |
<div class="flex items-center space-x-4">
|
| 54 |
+
<button class="bg-gray-200 dark:bg-gray-700 p-2 rounded-full hidden" id="theme-toggle">
|
| 55 |
<i class="fas fa-moon dark:hidden">
|
| 56 |
</i>
|
| 57 |
<i class="fas fa-sun hidden dark:block">
|
|
|
|
| 158 |
</div>
|
| 159 |
</div>
|
| 160 |
</div>
|
| 161 |
+
<div id="infoPopup" class="fixed inset-0 flex items-center justify-center hidden bg-black bg-opacity-50">
|
| 162 |
+
<div class="bg-dark rounded-lg p-6 w-96 relative" style="background-color: #0f141b;">
|
| 163 |
+
<button id="closePopup" class="absolute top-2 right-2 text-gray-600 hover:text-gray-800">
|
| 164 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24"
|
| 165 |
+
stroke="currentColor">
|
| 166 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
|
| 167 |
+
</svg>
|
| 168 |
+
</button>
|
| 169 |
+
<h2 class="text-lg font-semibold mb-4">Source Information</h2>
|
| 170 |
+
<div id="popupContent" class="overflow-y-auto">
|
| 171 |
+
<p class="mb-2"><strong>Source:</strong> <span id="pop_up_source_name">Demonstrating Retrieval Augmented
|
| 172 |
+
Generation for enhanced
|
| 173 |
+
information retrieval.</span></p>
|
| 174 |
+
<p><strong>Content:</strong> <span id="pop_up_content">This project explores the implementation of RAG
|
| 175 |
+
to improve search accuracy
|
| 176 |
+
by combining pre-trained language models with external knowledge sources. It focuses on [mention
|
| 177 |
+
specific aspects of your project, e.g., efficiency, specific data sources, etc.].</span></p>
|
| 178 |
+
</div>
|
| 179 |
+
</div>
|
| 180 |
+
</div>
|
| 181 |
<script src="https://code.jquery.com/jquery-3.7.1.min.js"
|
| 182 |
integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
|
| 183 |
<script>
|
|
|
|
| 195 |
const fileInput = document.getElementById('file-upload'); // input element
|
| 196 |
const fileList = document.getElementById('file-list'); // div to display files uploaded
|
| 197 |
const chat = document.getElementById('chat-messages');
|
| 198 |
+
const infoPopup = document.getElementById('infoPopup');
|
| 199 |
+
const closePopup = document.getElementById('closePopup');
|
| 200 |
+
const popUpContent = document.getElementById('pop_up_content');
|
| 201 |
+
const popUpSourceName = document.getElementById('pop_up_source_name');
|
| 202 |
|
| 203 |
var file_list = [];
|
| 204 |
var file_count = 0;
|
|
|
|
| 220 |
}
|
| 221 |
});
|
| 222 |
|
| 223 |
+
// infoLink.addEventListener('click', (event) => {
|
| 224 |
+
// event.preventDefault(); // Prevent default anchor behavior
|
| 225 |
+
// infoPopup.classList.remove('hidden');
|
| 226 |
+
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
+
function showPopUp(source, content) {
|
| 229 |
+
console.log("Showing pop up", source, content)
|
| 230 |
+
popUpSourceName.innerText = source;
|
| 231 |
+
popUpContent.innerText = content;
|
| 232 |
+
infoPopup.classList.remove('hidden');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
}
|
| 234 |
|
| 235 |
+
closePopup.addEventListener('click', () => {
|
| 236 |
+
infoPopup.classList.add('hidden');
|
| 237 |
+
});
|
| 238 |
|
| 239 |
+
infoPopup.addEventListener('click', (event) => {
|
| 240 |
+
if (event.target === infoPopup) {
|
| 241 |
+
infoPopup.classList.add('hidden');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
}
|
| 243 |
});
|
| 244 |
|
|
|
|
| 245 |
document.addEventListener('DOMContentLoaded', () => {
|
| 246 |
fileInput.addEventListener("change", () => {
|
| 247 |
const files = fileInput.files;
|
|
|
|
| 329 |
manageEmptyFileList();
|
| 330 |
}
|
| 331 |
|
| 332 |
+
function add_user_message(msg) {
|
| 333 |
+
var item = document.createElement('div');
|
| 334 |
+
item.className = "flex items-start space-x-2 flex-row-reverse";
|
| 335 |
+
item.innerHTML = `<div class="bg-blue-500 text-white p-3 rounded-lg">
|
| 336 |
+
<p>
|
| 337 |
+
${msg}
|
| 338 |
+
</p>
|
| 339 |
+
</div>`;
|
| 340 |
+
chat.appendChild(item);
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
function add_ai_response(resp) {
|
| 344 |
+
var item = document.createElement('div');
|
| 345 |
+
item.className = "flex items-start space-x-2";
|
| 346 |
+
|
| 347 |
+
var responseDiv = document.createElement('div');
|
| 348 |
+
responseDiv.className = "bg-gray-200 dark:bg-gray-700 p-3 rounded-lg";
|
| 349 |
+
|
| 350 |
+
var answerPara = document.createElement('p');
|
| 351 |
+
answerPara.textContent = resp.answer;
|
| 352 |
+
responseDiv.appendChild(answerPara);
|
| 353 |
+
|
| 354 |
+
var sourcesPara = document.createElement('p');
|
| 355 |
+
sourcesPara.className = "text-sm text-gray-500 dark:text-gray-400 mt-2";
|
| 356 |
+
|
| 357 |
+
if (resp.is_relevant) {
|
| 358 |
+
sourcesPara.textContent = "Sources: ";
|
| 359 |
+
resp.sources.forEach(function (source) {
|
| 360 |
+
let sourceLink = document.createElement('button');
|
| 361 |
+
sourceLink.className = "text-blue-500 cursor-pointer hover:underline";
|
| 362 |
+
sourceLink.style.background = "#00000085";
|
| 363 |
+
sourceLink.style.borderRadius = "4px";
|
| 364 |
+
sourceLink.style.padding = "1px 3px";
|
| 365 |
+
sourceLink.style.margin = "2px";
|
| 366 |
+
sourceLink.textContent = source.metadata.file_name.substr(0, 20) + "...";
|
| 367 |
+
if (source.metadata.page !== undefined) {
|
| 368 |
+
sourceLink.textContent += ", Page:" + source.metadata.page;
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
// Store data in data attributes
|
| 372 |
+
sourceLink.dataset.sourceInfo = `${source.metadata.file_name.substr(0, 20)}... ${(source.metadata.page !== undefined) ? `(Page: ${source.metadata.page})` : ``}`;
|
| 373 |
+
sourceLink.dataset.pageContent = source.page_content.substr(0, 300) + "...";
|
| 374 |
+
|
| 375 |
+
console.log("Made source");
|
| 376 |
+
|
| 377 |
+
sourceLink.addEventListener('click', function () {
|
| 378 |
+
console.log("Clicked source");
|
| 379 |
+
showPopUp(this.dataset.sourceInfo, this.dataset.pageContent);
|
| 380 |
+
});
|
| 381 |
+
|
| 382 |
+
sourcesPara.appendChild(sourceLink);
|
| 383 |
+
sourcesPara.append(", "); // Add comma and space after each link
|
| 384 |
+
});
|
| 385 |
+
} else {
|
| 386 |
+
sourcesPara.textContent = "The Documents provided are not sufficient to answer the query provided, So response might not be accurate.";
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
responseDiv.appendChild(sourcesPara);
|
| 390 |
+
item.appendChild(responseDiv);
|
| 391 |
+
chat.appendChild(item);
|
| 392 |
+
}
|
| 393 |
+
// message send
|
| 394 |
+
document.getElementById('message-form').addEventListener('submit', function (e) {
|
| 395 |
+
e.preventDefault();
|
| 396 |
+
const messageInput = document.getElementById('message-input');
|
| 397 |
+
const message = messageInput.value.trim();
|
| 398 |
+
|
| 399 |
+
messageInput.value = "";
|
| 400 |
+
|
| 401 |
+
add_user_message(message)
|
| 402 |
+
|
| 403 |
+
if (message) {
|
| 404 |
+
$.ajax({
|
| 405 |
+
url: "/ask_query",
|
| 406 |
+
method: "POST",
|
| 407 |
+
data: {
|
| 408 |
+
query: message
|
| 409 |
+
},
|
| 410 |
+
success: function (response) {
|
| 411 |
+
resp = JSON.stringify(response)
|
| 412 |
+
resp = JSON.parse(resp)
|
| 413 |
+
console.log(resp);
|
| 414 |
+
add_ai_response(resp);
|
| 415 |
+
},
|
| 416 |
+
error: function (xhr, status, error) {
|
| 417 |
+
console.log(error);
|
| 418 |
+
}
|
| 419 |
+
})
|
| 420 |
+
}
|
| 421 |
+
});
|
| 422 |
+
|
| 423 |
+
|
| 424 |
});
|
| 425 |
|
| 426 |
function manageEmptyFileList() {
|
utils.py
CHANGED
|
@@ -77,7 +77,7 @@ def generate_query_response(query, db, llm_model, PROMPT_TEMPLATE):
|
|
| 77 |
top_related = db.similarity_search_with_relevance_scores(query, k=4)
|
| 78 |
|
| 79 |
# Check relevance of top result
|
| 80 |
-
is_relevant = top_related[0][1] >= 0.
|
| 81 |
|
| 82 |
# Build context from relevant chunks
|
| 83 |
context = "\n".join([chunk[0].page_content for chunk in top_related])
|
|
|
|
| 77 |
top_related = db.similarity_search_with_relevance_scores(query, k=4)
|
| 78 |
|
| 79 |
# Check relevance of top result
|
| 80 |
+
is_relevant = top_related[0][1] >= 0.4 if top_related else False
|
| 81 |
|
| 82 |
# Build context from relevant chunks
|
| 83 |
context = "\n".join([chunk[0].page_content for chunk in top_related])
|