Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ async def initialize(file_path, question):
|
|
13 |
model = genai.GenerativeModel('gemini-pro')
|
14 |
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
15 |
|
16 |
-
#
|
17 |
prompt_template = """Answer the question precisely and concisely using the provided context. Avoid any additional commentary or system messages.
|
18 |
If the answer is not contained in the context, respond with "answer not available in context".
|
19 |
|
@@ -42,16 +42,23 @@ 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
|
46 |
relevant_pages = []
|
47 |
for i, page in enumerate(pages):
|
48 |
-
if
|
49 |
relevant_pages.append(f"Page {i+1}")
|
50 |
|
51 |
if relevant_pages:
|
52 |
source_str = f" (Source: {', '.join(relevant_pages)})"
|
53 |
else:
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# Create a clickable link for the document
|
57 |
file_name = os.path.basename(file_path)
|
|
|
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.
|
18 |
If the answer is not contained in the context, respond with "answer not available in context".
|
19 |
|
|
|
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)
|