Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -388,7 +388,7 @@ class RAGIndex:
|
|
| 388 |
return answer
|
| 389 |
|
| 390 |
def answer(self, question: str) -> str:
|
| 391 |
-
"""Answer a question using RAG with
|
| 392 |
if not self.initialized:
|
| 393 |
return "❌ Assistant not properly initialized. Please check the logs."
|
| 394 |
|
|
@@ -427,26 +427,39 @@ class RAGIndex:
|
|
| 427 |
f"💡 Try rephrasing your question or adding more detailed documents to the knowledge base."
|
| 428 |
)
|
| 429 |
|
| 430 |
-
# Combine contexts
|
| 431 |
-
combined_context = " ".join(evidence_parts[:
|
| 432 |
|
| 433 |
-
# 3)
|
| 434 |
-
answer_prompt = f"""
|
| 435 |
|
| 436 |
Context: {combined_context}
|
| 437 |
|
| 438 |
Question: {question}
|
| 439 |
|
| 440 |
-
Answer:"""
|
| 441 |
|
| 442 |
try:
|
| 443 |
-
answer_text = self._generate_from_context(answer_prompt, max_new_tokens=
|
| 444 |
answer_text = answer_text.strip()
|
| 445 |
|
| 446 |
-
#
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
|
| 451 |
except Exception as e:
|
| 452 |
print(f"Generation error: {e}")
|
|
@@ -462,21 +475,6 @@ class RAGIndex:
|
|
| 462 |
f"**Sources:** {sources_str}"
|
| 463 |
)
|
| 464 |
|
| 465 |
-
try:
|
| 466 |
-
answer_text = self._generate_from_context(answer_prompt, max_new_tokens=128)
|
| 467 |
-
except Exception as e:
|
| 468 |
-
print(f"Generation error: {e}")
|
| 469 |
-
return (
|
| 470 |
-
"There was an error while generating the answer. "
|
| 471 |
-
"Please try again with a shorter question or different wording."
|
| 472 |
-
)
|
| 473 |
-
|
| 474 |
-
sources_str = ", ".join(sorted(used_sources)) if used_sources else "N/A"
|
| 475 |
-
|
| 476 |
-
return (
|
| 477 |
-
f"**Answer:** {answer_text}\n\n"
|
| 478 |
-
f"**Sources:** {sources_str}"
|
| 479 |
-
)
|
| 480 |
|
| 481 |
|
| 482 |
# Initialize RAG system
|
|
|
|
| 388 |
return answer
|
| 389 |
|
| 390 |
def answer(self, question: str) -> str:
|
| 391 |
+
"""Answer a question using RAG with improved generation."""
|
| 392 |
if not self.initialized:
|
| 393 |
return "❌ Assistant not properly initialized. Please check the logs."
|
| 394 |
|
|
|
|
| 427 |
f"💡 Try rephrasing your question or adding more detailed documents to the knowledge base."
|
| 428 |
)
|
| 429 |
|
| 430 |
+
# Combine contexts - use MORE context for better answers
|
| 431 |
+
combined_context = " ".join(evidence_parts[:3])[:1500] # Top 3, up to 1500 chars
|
| 432 |
|
| 433 |
+
# 3) Better prompt structure for FLAN-T5
|
| 434 |
+
answer_prompt = f"""Read the context and answer the question in 2-3 complete sentences.
|
| 435 |
|
| 436 |
Context: {combined_context}
|
| 437 |
|
| 438 |
Question: {question}
|
| 439 |
|
| 440 |
+
Answer in complete sentences:"""
|
| 441 |
|
| 442 |
try:
|
| 443 |
+
answer_text = self._generate_from_context(answer_prompt, max_new_tokens=200)
|
| 444 |
answer_text = answer_text.strip()
|
| 445 |
|
| 446 |
+
# Clean up common artifacts
|
| 447 |
+
answer_text = answer_text.replace("**", "").replace("##", "").strip()
|
| 448 |
+
|
| 449 |
+
# If answer is poor quality, try alternative approach
|
| 450 |
+
if (len(answer_text) < 20 or
|
| 451 |
+
answer_text.count(":") > 3 or
|
| 452 |
+
answer_text.startswith("Do NOT") or
|
| 453 |
+
"tone tone tone" in answer_text.lower()):
|
| 454 |
+
|
| 455 |
+
# Try extractive approach: just clean and present the best context
|
| 456 |
+
best_context = evidence_parts[0]
|
| 457 |
+
# Remove list markers and clean
|
| 458 |
+
best_context = re.sub(r'^\s*[-*]\s*', '', best_context, flags=re.MULTILINE)
|
| 459 |
+
best_context = re.sub(r'^\s*\d+\.\s*', '', best_context, flags=re.MULTILINE)
|
| 460 |
+
# Take first few sentences
|
| 461 |
+
sentences = best_context.split('.')[:3]
|
| 462 |
+
answer_text = '. '.join(s.strip() for s in sentences if s.strip()) + '.'
|
| 463 |
|
| 464 |
except Exception as e:
|
| 465 |
print(f"Generation error: {e}")
|
|
|
|
| 475 |
f"**Sources:** {sources_str}"
|
| 476 |
)
|
| 477 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
|
| 480 |
# Initialize RAG system
|