Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -427,28 +427,47 @@ class RAGIndex:
|
|
| 427 |
|
| 428 |
combined_text = "\n\n".join(combined_context)
|
| 429 |
|
| 430 |
-
#
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
"\"I don't know based on the provided documents.\"\n\n"
|
| 448 |
-
f"
|
| 449 |
f"Question: {question}\n\n"
|
| 450 |
-
"Answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
)
|
|
|
|
|
|
|
| 452 |
|
| 453 |
|
| 454 |
try:
|
|
|
|
| 427 |
|
| 428 |
combined_text = "\n\n".join(combined_context)
|
| 429 |
|
| 430 |
+
# STEP 1 — Summarize each chunk individually
|
| 431 |
+
summaries = []
|
| 432 |
+
for ctx in combined_context:
|
| 433 |
+
prompt_summary = (
|
| 434 |
+
"Summarize the following text in one concise sentence, keeping only the core idea:\n\n"
|
| 435 |
+
f"{ctx}\n\nSummary:"
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
inputs = self.qa_tokenizer(prompt_summary, return_tensors="pt", truncation=True).to(self.qa_model.device)
|
| 439 |
+
output = self.qa_model.generate(
|
| 440 |
+
**inputs,
|
| 441 |
+
max_new_tokens=64,
|
| 442 |
+
do_sample=False
|
| 443 |
+
)
|
| 444 |
+
summary_text = self.qa_tokenizer.decode(output[0], skip_special_tokens=True).strip()
|
| 445 |
+
summaries.append(summary_text)
|
| 446 |
+
|
| 447 |
+
# STEP 2 — Combine all summaries into a clean evidence pool
|
| 448 |
+
evidence = " ".join(summaries)
|
| 449 |
+
|
| 450 |
+
# STEP 3 — Ask model to answer based on summaries only
|
| 451 |
+
prompt_answer = (
|
| 452 |
+
"You are an AI assistant that answers questions using only the summarized evidence below.\n"
|
| 453 |
+
"Write a clear and complete answer in 1–3 sentences.\n"
|
| 454 |
+
"Do NOT repeat numbers, headings, markdown, or irrelevant text.\n"
|
| 455 |
+
"Do NOT say where the information came from.\n"
|
| 456 |
+
"If the answer cannot be found in the evidence, reply:\n"
|
| 457 |
"\"I don't know based on the provided documents.\"\n\n"
|
| 458 |
+
f"Evidence:\n{evidence}\n\n"
|
| 459 |
f"Question: {question}\n\n"
|
| 460 |
+
"Answer:"
|
| 461 |
+
)
|
| 462 |
+
|
| 463 |
+
inputs = self.qa_tokenizer(prompt_answer, return_tensors="pt", truncation=True).to(self.qa_model.device)
|
| 464 |
+
output = self.qa_model.generate(
|
| 465 |
+
**inputs,
|
| 466 |
+
max_new_tokens=128,
|
| 467 |
+
do_sample=False
|
| 468 |
)
|
| 469 |
+
answer_text = self.qa_tokenizer.decode(output[0], skip_special_tokens=True).strip()
|
| 470 |
+
|
| 471 |
|
| 472 |
|
| 473 |
try:
|