Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,47 +5,33 @@ import pdfplumber
|
|
5 |
# Load the pre-trained question-answering model
|
6 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
# Read and extract text from the uploaded PDF file
|
15 |
-
with pdfplumber.open(file) as pdf:
|
16 |
-
self.text = ""
|
17 |
-
for page in pdf.pages:
|
18 |
-
self.text += page.extract_text()
|
19 |
|
20 |
-
return "PDF loaded successfully."
|
21 |
-
|
22 |
-
except Exception as e:
|
23 |
-
# Handle exceptions, e.g., invalid PDF or other errors
|
24 |
-
return f"Error processing PDF: {str(e)}"
|
25 |
-
|
26 |
-
def answer_question(self, question):
|
27 |
-
if not self.text:
|
28 |
-
return "No PDF loaded. Upload a PDF first."
|
29 |
-
|
30 |
-
# Ask the question using the question-answering model
|
31 |
-
answer = qa_pipeline({"context": self.text, "question": question})
|
32 |
return answer["answer"]
|
33 |
|
34 |
-
|
|
|
|
|
35 |
|
36 |
iface = gr.Interface(
|
37 |
-
|
38 |
-
"Load PDF": pdf_qa.load_pdf,
|
39 |
-
"Answer Question": pdf_qa.answer_question,
|
40 |
-
},
|
41 |
inputs=[
|
42 |
-
gr.File(label="Upload PDF"
|
43 |
-
gr.Textbox(label="Enter Question", type="text"),
|
44 |
],
|
45 |
outputs="text",
|
46 |
live=True,
|
47 |
title="PDF Question-Answering",
|
48 |
-
description="Upload a PDF
|
49 |
)
|
50 |
|
51 |
iface.launch()
|
|
|
5 |
# Load the pre-trained question-answering model
|
6 |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
7 |
|
8 |
+
def answer_question(file, question):
|
9 |
+
try:
|
10 |
+
# Read and extract text from the uploaded PDF file
|
11 |
+
with pdfplumber.open(file) as pdf:
|
12 |
+
text = ""
|
13 |
+
for page in pdf.pages:
|
14 |
+
text += page.extract_text()
|
15 |
|
16 |
+
# Ask the user's question using the question-answering model
|
17 |
+
answer = qa_pipeline({"context": text, "question": question})
|
|
|
|
|
|
|
|
|
|
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
return answer["answer"]
|
20 |
|
21 |
+
except Exception as e:
|
22 |
+
# Handle exceptions, e.g., invalid PDF or other errors
|
23 |
+
return f"Error processing PDF: {str(e)}"
|
24 |
|
25 |
iface = gr.Interface(
|
26 |
+
fn=answer_question,
|
|
|
|
|
|
|
27 |
inputs=[
|
28 |
+
gr.File(label="Upload PDF"),
|
29 |
+
gr.Textbox(label="Enter Question", type="text", default="What is the main topic of the document?"),
|
30 |
],
|
31 |
outputs="text",
|
32 |
live=True,
|
33 |
title="PDF Question-Answering",
|
34 |
+
description="Upload a PDF and ask questions about its contents.",
|
35 |
)
|
36 |
|
37 |
iface.launch()
|