Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,7 +20,15 @@ def initialize(pdf_file, question):
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
else:
|
25 |
return "Error: The uploaded file could not be found."
|
26 |
else:
|
@@ -29,23 +37,6 @@ def initialize(pdf_file, question):
|
|
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,
|
@@ -64,3 +55,4 @@ interface.launch()
|
|
64 |
|
65 |
|
66 |
|
|
|
|
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 |
+
|
24 |
+
# Configure Google Generative AI (replace with your API key)
|
25 |
+
model = genai.GenerativeModel('gemini-pro')
|
26 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3)
|
27 |
+
|
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 |
+
|
31 |
+
return stuff_answer['output_text']
|
32 |
else:
|
33 |
return "Error: The uploaded file could not be found."
|
34 |
else:
|
|
|
37 |
except Exception as e:
|
38 |
return f"An error occurred: {e}" # Generic error handling
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# Create a Gradio interface
|
41 |
interface = gr.Interface(
|
42 |
fn=initialize,
|
|
|
55 |
|
56 |
|
57 |
|
58 |
+
|