Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -388,93 +388,77 @@ class RAGIndex:
|
|
| 388 |
return answer
|
| 389 |
|
| 390 |
def answer(self, question: str) -> str:
|
| 391 |
-
"""Answer a question using RAG -
|
| 392 |
if not self.initialized:
|
| 393 |
return "β Assistant not properly initialized. Please check the logs."
|
| 394 |
-
|
| 395 |
if not question or not question.strip():
|
| 396 |
return "Please ask a question."
|
| 397 |
-
|
| 398 |
if self.index is None or not self.chunks:
|
| 399 |
return (
|
| 400 |
f"π Knowledge base is empty.\n\n"
|
| 401 |
f"Please add documents to: `{KB_DIR}`\n"
|
| 402 |
f"Supported formats: .txt, .md, .pdf, .docx"
|
| 403 |
)
|
| 404 |
-
|
| 405 |
-
# Retrieve relevant contexts
|
| 406 |
contexts = self.retrieve(question, top_k=3)
|
| 407 |
-
|
| 408 |
if not contexts:
|
| 409 |
return (
|
| 410 |
f"{NO_ANSWER_MSG}\n\n"
|
| 411 |
f"π‘ Try rephrasing your question or check if relevant documents exist in the knowledge base."
|
| 412 |
)
|
| 413 |
-
|
| 414 |
used_sources = set()
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
# Find the best matching context
|
| 419 |
for ctx, source, score in contexts:
|
| 420 |
used_sources.add(source)
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
if not
|
| 426 |
-
return
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
generated = self._generate_from_context(prompt, max_new_tokens=150)
|
| 463 |
-
generated = generated.strip()
|
| 464 |
-
|
| 465 |
-
# Only use generated answer if it looks good
|
| 466 |
-
if (len(generated) > 30 and
|
| 467 |
-
not generated.startswith(("Do NOT", "You are", "##", "**")) and
|
| 468 |
-
generated.count(':') < 3):
|
| 469 |
-
answer_text = generated
|
| 470 |
-
# Otherwise, keep the cleaned extractive answer
|
| 471 |
-
|
| 472 |
-
except Exception as e:
|
| 473 |
-
print(f"Generation error (using extractive fallback): {e}")
|
| 474 |
-
# Keep the cleaned extractive answer
|
| 475 |
-
|
| 476 |
sources_str = ", ".join(sorted(used_sources)) if used_sources else "N/A"
|
| 477 |
-
|
| 478 |
return (
|
| 479 |
f"**Answer:** {answer_text}\n\n"
|
| 480 |
f"**Sources:** {sources_str}"
|
|
|
|
| 388 |
return answer
|
| 389 |
|
| 390 |
def answer(self, question: str) -> str:
|
| 391 |
+
"""Answer a question using RAG with a generative seq2seq model (Flan-T5, BART, etc.)."""
|
| 392 |
if not self.initialized:
|
| 393 |
return "β Assistant not properly initialized. Please check the logs."
|
| 394 |
+
|
| 395 |
if not question or not question.strip():
|
| 396 |
return "Please ask a question."
|
| 397 |
+
|
| 398 |
if self.index is None or not self.chunks:
|
| 399 |
return (
|
| 400 |
f"π Knowledge base is empty.\n\n"
|
| 401 |
f"Please add documents to: `{KB_DIR}`\n"
|
| 402 |
f"Supported formats: .txt, .md, .pdf, .docx"
|
| 403 |
)
|
| 404 |
+
|
| 405 |
+
# 1) Retrieve relevant contexts
|
| 406 |
contexts = self.retrieve(question, top_k=3)
|
| 407 |
+
|
| 408 |
if not contexts:
|
| 409 |
return (
|
| 410 |
f"{NO_ANSWER_MSG}\n\n"
|
| 411 |
f"π‘ Try rephrasing your question or check if relevant documents exist in the knowledge base."
|
| 412 |
)
|
| 413 |
+
|
| 414 |
used_sources = set()
|
| 415 |
+
context_texts = []
|
| 416 |
+
|
|
|
|
|
|
|
| 417 |
for ctx, source, score in contexts:
|
| 418 |
used_sources.add(source)
|
| 419 |
+
cleaned_ctx = clean_context_text(ctx)
|
| 420 |
+
if cleaned_ctx:
|
| 421 |
+
context_texts.append(cleaned_ctx)
|
| 422 |
+
|
| 423 |
+
if not context_texts:
|
| 424 |
+
return (
|
| 425 |
+
f"{NO_ANSWER_MSG}\n\n"
|
| 426 |
+
f"π‘ Try adding more detailed documents to the knowledge base."
|
| 427 |
+
)
|
| 428 |
+
|
| 429 |
+
# 2) Combine contexts into a single evidence block
|
| 430 |
+
combined_context = "\n\n".join(context_texts)
|
| 431 |
+
|
| 432 |
+
# Keep context at a reasonable size for the model
|
| 433 |
+
max_context_chars = 3000
|
| 434 |
+
if len(combined_context) > max_context_chars:
|
| 435 |
+
combined_context = combined_context[:max_context_chars]
|
| 436 |
+
|
| 437 |
+
# 3) Build a prompt that works for both BART (summarization-style)
|
| 438 |
+
# and instruction-tuned models like Flan-T5.
|
| 439 |
+
prompt = (
|
| 440 |
+
"You are an assistant that answers questions about a knowledge base.\n"
|
| 441 |
+
"Using only the information in the passages below, answer the question in 2β4 sentences.\n"
|
| 442 |
+
"Explain in clear, natural language. Do NOT copy section numbers, markdown headings, or bullet symbols.\n\n"
|
| 443 |
+
f"Passages:\n{combined_context}\n\n"
|
| 444 |
+
f"Question: {question}\n\n"
|
| 445 |
+
"Answer:"
|
| 446 |
+
)
|
| 447 |
+
|
| 448 |
+
try:
|
| 449 |
+
answer_text = self._generate_from_context(prompt, max_new_tokens=180).strip()
|
| 450 |
+
except Exception as e:
|
| 451 |
+
print(f"Generation error: {e}")
|
| 452 |
+
return (
|
| 453 |
+
"There was an error while generating the answer. "
|
| 454 |
+
"Please try again with a shorter question or different wording."
|
| 455 |
+
)
|
| 456 |
+
|
| 457 |
+
if not answer_text:
|
| 458 |
+
answer_text = NO_ANSWER_MSG
|
| 459 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
sources_str = ", ".join(sorted(used_sources)) if used_sources else "N/A"
|
| 461 |
+
|
| 462 |
return (
|
| 463 |
f"**Answer:** {answer_text}\n\n"
|
| 464 |
f"**Sources:** {sources_str}"
|