saritha commited on
Commit
82c6cf9
·
verified ·
1 Parent(s): 27f2b4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -32
app.py CHANGED
@@ -1,41 +1,55 @@
1
  import os
2
- from langchain import PromptTemplate
3
  from langchain.chains.question_answering import load_qa_chain
4
- from langchain.document_loaders import PyPDFLoader
5
- from langchain_google_genai import ChatGoogleGenerativeAI
6
  import google.generativeai as genai
7
- import gradio as gr
8
 
9
  # Function for initialization
10
  def initialize(pdf_file, question):
11
- with open("/tmp/uploaded_file.pdf", "wb") as f:
12
- f.write(pdf_file.read())
13
- file_path = "/tmp/uploaded_file.pdf"
14
- genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
15
- model = genai.GenerativeModel('gemini-pro')
16
- model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
17
- prompt_template = """Answer the question as precise as possible using the provided context. If the answer is
18
- not contained in the context, say "answer not available in context" \n\n
19
- Context: \n {context}?\n
20
- Question: \n {question} \n
21
- Answer:
22
- """
23
- prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
24
- if os.path.exists(file_path):
25
- pdf_loader = PyPDFLoader(file_path)
26
- pages = pdf_loader.load_and_split()
27
- context = "\n".join(str(page.page_content) for page in pages[:30])
28
- stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
29
- stuff_answer = stuff_chain({"input_documents": pages, "question": question, "context": context}, return_only_outputs=True)
30
- return stuff_answer['output_text']
31
- else:
32
- return "Error: Unable to process the document. Please ensure the PDF file is valid."
33
-
34
- # Create a Gradio interface for file upload and question input
35
- iface = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  fn=initialize,
37
  inputs=[
38
- gr.File(label="Upload PDF", type="file"),
39
  gr.Textbox(label="Question")
40
  ],
41
  outputs="text",
@@ -44,8 +58,7 @@ iface = gr.Interface(
44
  )
45
 
46
  # Launch the interface
47
- iface.launch()
48
-
49
 
50
 
51
 
 
1
  import os
2
+ from langchain_core.prompts import PromptTemplate
3
  from langchain.chains.question_answering import load_qa_chain
4
+ from langchain_community.document_loaders import PyPDFLoader
 
5
  import google.generativeai as genai
6
+ from gradio import Interface
7
 
8
  # Function for initialization
9
  def initialize(pdf_file, question):
10
+ try:
11
+ # Save the uploaded PDF content temporarily
12
+ with open("/tmp/uploaded_file.pdf", "wb") as f:
13
+ f.write(pdf_file.read())
14
+ file_path = "/tmp/uploaded_file.pdf"
15
+
16
+ # Configure Google Generative AI
17
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
18
+ model = genai.GenerativeModel('gemini-pro')
19
+ model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
20
+
21
+ # Prompt template for formatting context and question
22
+ prompt_template = """Answer the question as precise as possible using the provided context. If the answer is not contained in the context, say "answer not available in context"
23
+
24
+ Context:
25
+ {context}
26
+
27
+ Question:
28
+ {question}
29
+
30
+ Answer:
31
+ """
32
+
33
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
34
+
35
+ # Process the PDF if it exists
36
+ if os.path.exists(file_path):
37
+ pdf_loader = PyPDFLoader(file_path)
38
+ pages = pdf_loader.load_and_split()
39
+ context = "\n".join(str(page.page_content) for page in pages[:30]) # Limit to first 30 pages
40
+ stuff_chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
41
+ stuff_answer = stuff_chain({"input_documents": pages, "question": question, "context": context}, return_only_outputs=True)
42
+ return stuff_answer['output_text']
43
+ else:
44
+ return "Error: Unable to process the document. Please ensure the PDF file is valid."
45
+ except Exception as e:
46
+ return f"An error occurred: {e}" # Generic error handling
47
+
48
+ # Create a Gradio interface
49
+ interface = Interface(
50
  fn=initialize,
51
  inputs=[
52
+ gr.File(label="Upload PDF"), # No need for 'type' argument
53
  gr.Textbox(label="Question")
54
  ],
55
  outputs="text",
 
58
  )
59
 
60
  # Launch the interface
61
+ interface.launch()
 
62
 
63
 
64