ivyblossom commited on
Commit
663bca5
1 Parent(s): d6f3e2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -2
app.py CHANGED
@@ -27,6 +27,13 @@ def question_answering(question, pdf_path):
27
 
28
  return answer, pdf_text_with_pages
29
 
 
 
 
 
 
 
 
30
  def main():
31
  st.title("Question Answering on a PDF File")
32
 
@@ -49,9 +56,9 @@ def main():
49
 
50
  # Display context where the answer came from
51
  context_page_num = answer['start']
52
- context_text = pdf_text_with_pages[context_page_num][1] # Use the page number directly (no need to adjust)
53
  st.write("Context:")
54
  st.write(context_text)
55
 
56
  if __name__ == "__main__":
57
- main()
 
27
 
28
  return answer, pdf_text_with_pages
29
 
30
+ def get_context_text(pdf_text_with_pages, context_page_num, context_window=3):
31
+ context_start = max(0, context_page_num - context_window - 1)
32
+ context_end = min(len(pdf_text_with_pages), context_page_num + context_window)
33
+ context_lines = [text for _, text in pdf_text_with_pages[context_start:context_end]]
34
+ context_text = "\n".join(context_lines)
35
+ return context_text
36
+
37
  def main():
38
  st.title("Question Answering on a PDF File")
39
 
 
56
 
57
  # Display context where the answer came from
58
  context_page_num = answer['start']
59
+ context_text = get_context_text(pdf_text_with_pages, context_page_num)
60
  st.write("Context:")
61
  st.write(context_text)
62
 
63
  if __name__ == "__main__":
64
+ main()