saritha commited on
Commit
9105bd6
·
verified ·
1 Parent(s): 3a47322

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -8,27 +8,38 @@ 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')
15
  model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
16
- prompt_template = """Understand the question answer to the question as precise as possible using the provided context.
17
- If the answer is not contained in the context, say "answer not available in context" \n\n
18
- Context: \n {context}?\n
19
- Question: \n {question} \n
 
 
 
 
 
 
 
20
  Answer:
21
  """
22
  prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
 
23
  if os.path.exists(file_path):
24
  pdf_loader = PyPDFLoader(file_path)
25
  pages = pdf_loader.load_and_split()
26
  context = "\n".join(f"Page {i+1}: {page.page_content}" for i, page in enumerate(pages[:30]))
27
  stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
28
 
29
- # Use ainvoke instead of arun
30
  stuff_answer = await stuff_chain.ainvoke({"input_documents": pages, "question": question, "context": context})
31
 
 
 
 
 
32
  # Extract the page number where the context was found
33
  sources = []
34
  for i, page in enumerate(pages):
@@ -43,11 +54,10 @@ async def initialize(file_path, question):
43
  # Add the clickable link to the source
44
  file_name = os.path.basename(file_path)
45
  source_link = f"[{file_name}](file://{os.path.abspath(file_path)})"
46
- return f"{stuff_answer} {source_str} - [Document: {source_link}]"
47
  else:
48
  return "Error: Unable to process the document. Please ensure the PDF file is valid."
49
 
50
-
51
  # Define Gradio Interface
52
  input_file = gr.File(label="Upload PDF File")
53
  input_question = gr.Textbox(label="Ask about the document")
 
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')
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
+
20
+ Context:
21
+ {context}
22
+
23
+ Question:
24
+ {question}
25
+
26
  Answer:
27
  """
28
  prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
29
+
30
  if os.path.exists(file_path):
31
  pdf_loader = PyPDFLoader(file_path)
32
  pages = pdf_loader.load_and_split()
33
  context = "\n".join(f"Page {i+1}: {page.page_content}" for i, page in enumerate(pages[:30]))
34
  stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
35
 
36
+ # Use ainvoke to get the result
37
  stuff_answer = await stuff_chain.ainvoke({"input_documents": pages, "question": question, "context": context})
38
 
39
+ # Post-process the answer to remove any unnecessary text or system messages
40
+ answer = stuff_answer.strip()
41
+ answer = answer.split("Answer:")[1] if "Answer:" in answer else answer
42
+
43
  # Extract the page number where the context was found
44
  sources = []
45
  for i, page in enumerate(pages):
 
54
  # Add the clickable link to the source
55
  file_name = os.path.basename(file_path)
56
  source_link = f"[{file_name}](file://{os.path.abspath(file_path)})"
57
+ return f"{answer} {source_str} - [Document: {source_link}]"
58
  else:
59
  return "Error: Unable to process the document. Please ensure the PDF file is valid."
60
 
 
61
  # Define Gradio Interface
62
  input_file = gr.File(label="Upload PDF File")
63
  input_question = gr.Textbox(label="Ask about the document")