Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ from langchain_google_genai import ChatGoogleGenerativeAI
|
|
8 |
import google.generativeai as genai
|
9 |
from langchain.chains.question_answering import load_qa_chain # Import load_qa_chain
|
10 |
|
11 |
-
|
12 |
async def initialize(file_path, question):
|
13 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
model = genai.GenerativeModel('gemini-pro')
|
@@ -31,7 +30,8 @@ async def initialize(file_path, question):
|
|
31 |
if os.path.exists(file_path):
|
32 |
pdf_loader = PyPDFLoader(file_path)
|
33 |
pages = pdf_loader.load_and_split()
|
34 |
-
|
|
|
35 |
stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
36 |
|
37 |
# Use ainvoke to get the result
|
@@ -40,14 +40,14 @@ async def initialize(file_path, question):
|
|
40 |
# Access the correct key for the answer
|
41 |
answer = stuff_answer.get('output_text', '').strip()
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
for i, page in enumerate(pages):
|
46 |
-
if
|
47 |
-
|
48 |
|
49 |
-
if
|
50 |
-
source_str = f" (Source: {', '.join(
|
51 |
else:
|
52 |
source_str = " (Source: Not found in specific page)"
|
53 |
|
|
|
8 |
import google.generativeai as genai
|
9 |
from langchain.chains.question_answering import load_qa_chain # Import load_qa_chain
|
10 |
|
|
|
11 |
async def initialize(file_path, question):
|
12 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
13 |
model = genai.GenerativeModel('gemini-pro')
|
|
|
30 |
if os.path.exists(file_path):
|
31 |
pdf_loader = PyPDFLoader(file_path)
|
32 |
pages = pdf_loader.load_and_split()
|
33 |
+
page_contexts = [f"Page {i+1}: {page.page_content}" for i, page in enumerate(pages[:30])]
|
34 |
+
context = "\n".join(page_contexts)
|
35 |
stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
36 |
|
37 |
# Use ainvoke to get the result
|
|
|
40 |
# Access the correct key for the answer
|
41 |
answer = stuff_answer.get('output_text', '').strip()
|
42 |
|
43 |
+
# Find the most relevant pages by searching for content overlap with the answer
|
44 |
+
relevant_pages = []
|
45 |
for i, page in enumerate(pages):
|
46 |
+
if any(phrase in page.page_content for phrase in answer.split()):
|
47 |
+
relevant_pages.append(f"Page {i+1}")
|
48 |
|
49 |
+
if relevant_pages:
|
50 |
+
source_str = f" (Source: {', '.join(relevant_pages)})"
|
51 |
else:
|
52 |
source_str = " (Source: Not found in specific page)"
|
53 |
|