saritha commited on
Commit
68f99dc
·
verified ·
1 Parent(s): 001d160

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -11,7 +11,7 @@ from langchain.chains.question_answering import load_qa_chain # Import load_qa_
11
  async def initialize(file_path, question):
12
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
13
  model = genai.GenerativeModel('gemini-pro')
14
- model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.0)
15
 
16
  # Refined prompt template to encourage precise and concise answers
17
  prompt_template = """Answer the question precisely and concisely using the provided context. Avoid any additional commentary or system messages.
@@ -56,22 +56,24 @@ async def initialize(file_path, question):
56
  max_score = max(page_scores)
57
  top_pages = [i+1 for i, score in enumerate(page_scores) if score == max_score]
58
 
59
- # Optionally, limit the number of top pages to display
60
- num_top_pages = 5 # Adjust this value based on your needs
61
- if len(top_pages) > num_top_pages:
62
- top_pages = sorted(top_pages)[:num_top_pages]
 
 
 
 
63
 
64
  if top_pages:
65
- page_numbers = ', '.join(str(p) for p in sorted(top_pages))
66
- source_str = f"Top relevant page(s): {page_numbers}"
67
  else:
68
  source_str = "Top relevant page(s): Not found in specific page"
69
 
70
  # Create a clickable link for the document
71
- file_name = os.path.basename(file_path)
72
- source_link = f"[{file_name}](file://{os.path.abspath(file_path)})"
73
 
74
- return f"Answer: {answer}\n{source_str}\n[Document: {source_link}]"
75
  else:
76
  return "Error: Unable to process the document. Please ensure the PDF file is valid."
77
 
 
11
  async def initialize(file_path, question):
12
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
13
  model = genai.GenerativeModel('gemini-pro')
14
+ model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
15
 
16
  # Refined prompt template to encourage precise and concise answers
17
  prompt_template = """Answer the question precisely and concisely using the provided context. Avoid any additional commentary or system messages.
 
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:
69
+ source_str = f"Top relevant page(s): {page_links_str}"
 
70
  else:
71
  source_str = "Top relevant page(s): Not found in specific page"
72
 
73
  # Create a clickable link for the document
74
+ source_link = f"[Document: {file_name}](file://{os.path.abspath(file_path)})"
 
75
 
76
+ return f"Answer: {answer}\n{source_str}\n{source_link}"
77
  else:
78
  return "Error: Unable to process the document. Please ensure the PDF file is valid."
79