Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +36 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import pdfplumber
|
4 |
+
|
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, ques: str):
|
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 a default question
|
17 |
+
question = ques
|
18 |
+
|
19 |
+
# Ask the question using the question-answering model
|
20 |
+
answer = qa_pipeline({"context": text, "question": question})
|
21 |
+
|
22 |
+
return answer["answer"]
|
23 |
+
|
24 |
+
except Exception as e:
|
25 |
+
return f"Error processing PDF: {str(e)}"
|
26 |
+
|
27 |
+
iface = gr.Interface(
|
28 |
+
fn=answer_question,
|
29 |
+
inputs=gr.File(label="Upload PDF"),
|
30 |
+
outputs="text",
|
31 |
+
live=True,
|
32 |
+
title="PDF Documents Question-Answering",
|
33 |
+
description="Ask a question about the contents of the uploaded PDF file.",
|
34 |
+
)
|
35 |
+
|
36 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
pdfplumber
|
3 |
+
gradio
|