saritha commited on
Commit
9c5ca00
·
verified ·
1 Parent(s): 9751da0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -1
app.py CHANGED
@@ -16,7 +16,11 @@ def initialize(pdf_file, question):
16
  # Construct potential file path based on temporary directory and filename
17
  file_path = os.path.join("/tmp", file_info.name) # Adjust temporary directory if needed
18
  if os.path.exists(file_path):
19
- # ... rest of your code for processing the PDF using the file path
 
 
 
 
20
  else:
21
  return "Error: The uploaded file could not be found."
22
  else:
@@ -25,6 +29,23 @@ def initialize(pdf_file, question):
25
  except Exception as e:
26
  return f"An error occurred: {e}" # Generic error handling
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # Create a Gradio interface
29
  interface = gr.Interface(
30
  fn=initialize,
 
16
  # Construct potential file path based on temporary directory and filename
17
  file_path = os.path.join("/tmp", file_info.name) # Adjust temporary directory if needed
18
  if os.path.exists(file_path):
19
+ # Process the PDF
20
+ pdf_loader = PyPDFLoader(file_path)
21
+ pages = pdf_loader.load_and_split()
22
+ context = "\n".join(str(page.page_content) for page in pages[:30]) # Limit to first 30 pages
23
+ # ... rest of your code for processing the PDF using context and question
24
  else:
25
  return "Error: The uploaded file could not be found."
26
  else:
 
29
  except Exception as e:
30
  return f"An error occurred: {e}" # Generic error handling
31
 
32
+ # Configure Google Generative AI (replace with your API key)
33
+ genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
34
+
35
+ # Prompt template for formatting context and question
36
+ 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"
37
+
38
+ Context:
39
+ {context}
40
+
41
+ Question:
42
+ {question}
43
+
44
+ Answer:
45
+ """
46
+
47
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
48
+
49
  # Create a Gradio interface
50
  interface = gr.Interface(
51
  fn=initialize,