saritha commited on
Commit
f8c8ec1
·
verified ·
1 Parent(s): 9e1463b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -42,23 +42,19 @@ async def initialize(file_path, question):
42
  stuff_answer = await stuff_chain.ainvoke({"input_documents": pages, "question": question, "context": context})
43
  answer = stuff_answer.get('output_text', '').strip()
44
 
45
- # Identify the pages that contain the answer by searching for key phrases
46
- relevant_pages = []
 
 
47
  for i, page in enumerate(pages):
48
- if any(phrase.lower() in page.page_content.lower() for phrase in answer.split()):
49
- relevant_pages.append(f"Page {i+1}")
 
50
 
51
  if relevant_pages:
52
- source_str = f" (Source: {', '.join(relevant_pages)})"
53
  else:
54
- # Additional attempt: Find any overlapping content to improve detection
55
- for i, page in enumerate(pages):
56
- if any(phrase in page.page_content for phrase in answer.split()[:10]): # Checking with a subset of the answer
57
- relevant_pages.append(f"Page {i+1}")
58
- if relevant_pages:
59
- source_str = f" (Source: {', '.join(relevant_pages)})"
60
- else:
61
- source_str = " (Source: Not found in specific page)"
62
 
63
  # Create a clickable link for the document
64
  file_name = os.path.basename(file_path)
@@ -73,6 +69,9 @@ input_question = gr.Textbox(label="Ask about the document")
73
  output_text = gr.Textbox(label="Answer - GeminiPro")
74
 
75
  async def pdf_qa(file, question):
 
 
 
76
  answer = await initialize(file.name, question)
77
  return answer
78
 
 
42
  stuff_answer = await stuff_chain.ainvoke({"input_documents": pages, "question": question, "context": context})
43
  answer = stuff_answer.get('output_text', '').strip()
44
 
45
+ # Identify key sentences or phrases
46
+ key_phrases = answer.split(". ") # Split answer into sentences for more precise matching
47
+
48
+ relevant_pages = set()
49
  for i, page in enumerate(pages):
50
+ for phrase in key_phrases:
51
+ if phrase.lower() in page.page_content.lower():
52
+ relevant_pages.add(i+1) # Add page number if phrase is found
53
 
54
  if relevant_pages:
55
+ source_str = f" (Source: {', '.join(f'Page {p}' for p in sorted(relevant_pages))})"
56
  else:
57
+ source_str = " (Source: Not found in specific page)"
 
 
 
 
 
 
 
58
 
59
  # Create a clickable link for the document
60
  file_name = os.path.basename(file_path)
 
69
  output_text = gr.Textbox(label="Answer - GeminiPro")
70
 
71
  async def pdf_qa(file, question):
72
+ if file is None:
73
+ return "Error: No file uploaded. Please upload a PDF document."
74
+
75
  answer = await initialize(file.name, question)
76
  return answer
77