Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,55 @@
|
|
1 |
import os
|
2 |
-
from
|
3 |
from langchain.chains.question_answering import load_qa_chain
|
4 |
-
from
|
5 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
6 |
import google.generativeai as genai
|
7 |
-
|
8 |
|
9 |
# Function for initialization
|
10 |
def initialize(pdf_file, question):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
fn=initialize,
|
37 |
inputs=[
|
38 |
-
gr.File(label="Upload PDF", type
|
39 |
gr.Textbox(label="Question")
|
40 |
],
|
41 |
outputs="text",
|
@@ -44,8 +58,7 @@ iface = gr.Interface(
|
|
44 |
)
|
45 |
|
46 |
# Launch the interface
|
47 |
-
|
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 |
|