saritha commited on
Commit
34be9dd
·
verified ·
1 Parent(s): 68f99dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -9
app.py CHANGED
@@ -32,7 +32,7 @@ async def initialize(file_path, question):
32
  pages = pdf_loader.load_and_split()
33
 
34
  # Extract content from each page and store along with page number
35
- page_contexts = [f"Page {i+1}: {page.page_content}" for i, page in enumerate(pages)]
36
  context = "\n".join(page_contexts[:30]) # Using the first 30 pages for context
37
 
38
  # Load the question-answering chain
@@ -52,17 +52,14 @@ async def initialize(file_path, question):
52
  if phrase.lower() in page.page_content.lower():
53
  page_scores[i] += 1
54
 
55
- # Determine the maximum score and get top pages
56
- max_score = max(page_scores)
57
- top_pages = [i+1 for i, score in enumerate(page_scores) if score == max_score]
58
-
59
- # Limit to the top 2 pages
60
- num_top_pages = 2
61
- top_pages = sorted(top_pages)[:num_top_pages]
62
 
63
  # Generate links for each top page
64
  file_name = os.path.basename(file_path)
65
- page_links = [f"[Page {p}](file://{os.path.abspath(file_path)}#page={p})" for p in top_pages]
 
66
  page_links_str = ', '.join(page_links)
67
 
68
  if top_pages:
@@ -91,3 +88,4 @@ async def pdf_qa(file, question):
91
 
92
  # Create Gradio Interface with share=True to enable a public link
93
  gr.Interface(fn=pdf_qa, inputs=[input_file, input_question], outputs=output_text, title="PDF Question Answering System", description="Upload a PDF file and ask questions about the content.").launch(share=True)
 
 
32
  pages = pdf_loader.load_and_split()
33
 
34
  # Extract content from each page and store along with page number
35
+ page_contexts = [page.page_content for i, page in enumerate(pages)]
36
  context = "\n".join(page_contexts[:30]) # Using the first 30 pages for context
37
 
38
  # Load the question-answering chain
 
52
  if phrase.lower() in page.page_content.lower():
53
  page_scores[i] += 1
54
 
55
+ # Determine the top pages based on highest scores
56
+ top_pages_with_scores = sorted(enumerate(page_scores), key=lambda x: x[1], reverse=True)
57
+ top_pages = [i + 1 for i, score in top_pages_with_scores if score > 0][:2] # Get top 2 pages
 
 
 
 
58
 
59
  # Generate links for each top page
60
  file_name = os.path.basename(file_path)
61
+ # Use a general link format with instructions for manual navigation if automatic links are not supported
62
+ page_links = [f"[Page {p}](file://{os.path.abspath(file_path)})" for p in top_pages]
63
  page_links_str = ', '.join(page_links)
64
 
65
  if top_pages:
 
88
 
89
  # Create Gradio Interface with share=True to enable a public link
90
  gr.Interface(fn=pdf_qa, inputs=[input_file, input_question], outputs=output_text, title="PDF Question Answering System", description="Upload a PDF file and ask questions about the content.").launch(share=True)
91
+ the content.").launch(share=True)