sofzcc commited on
Commit
2d28f5c
·
verified ·
1 Parent(s): df86717

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -19
app.py CHANGED
@@ -427,28 +427,47 @@ class RAGIndex:
427
 
428
  combined_text = "\n\n".join(combined_context)
429
 
430
- # Clean markdown / numbering / duplicates
431
- combined_text = clean_context_text(combined_text)
432
-
433
- # Limit context length to keep it manageable
434
- max_context_chars = 4000
435
- if len(combined_text) > max_context_chars:
436
- combined_text = combined_text[:max_context_chars]
437
-
438
- # Prompt for the generative model
439
- prompt = (
440
- "You are an AI assistant that answers questions using only the provided context.\n"
441
- "Your task is to synthesize a clear, natural explanation in your own words.\n"
442
- "- Do NOT copy headings or section numbers from the context.\n"
443
- "- Do NOT include markdown like '#', '##', '---', or bullet/list markers.\n"
444
- "- Do NOT mention file names, sources, or internal labels in your answer.\n"
445
- "- Do NOT just repeat full sentences from the context; always paraphrase.\n"
446
- "- If the answer cannot be found in the context, reply exactly with: "
 
 
 
 
 
 
 
 
 
 
447
  "\"I don't know based on the provided documents.\"\n\n"
448
- f"Context:\n{combined_text}\n\n"
449
  f"Question: {question}\n\n"
450
- "Answer in 1–3 concise sentences of plain text:"
 
 
 
 
 
 
 
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: