Spaces:
Sleeping
Sleeping
PiyushS2025
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
# Load the pre-trained model for Q&A
|
6 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
7 |
+
|
8 |
+
def extract_text_from_pdf(pdf):
|
9 |
+
# Extract text from the uploaded PDF
|
10 |
+
reader = PdfReader(pdf)
|
11 |
+
text = ""
|
12 |
+
for page in reader.pages:
|
13 |
+
text += page.extract_text()
|
14 |
+
return text
|
15 |
+
|
16 |
+
def generate_qa(pdf, question):
|
17 |
+
# Extract PDF content
|
18 |
+
content = extract_text_from_pdf(pdf)
|
19 |
+
|
20 |
+
# Perform question-answering
|
21 |
+
answer = qa_pipeline(question=question, context=content)
|
22 |
+
return answer["answer"]
|
23 |
+
|
24 |
+
# Create the Gradio app interface
|
25 |
+
with gr.Blocks() as app:
|
26 |
+
gr.Markdown("## PDF Q&A Application")
|
27 |
+
|
28 |
+
pdf_file = gr.File(label="Upload PDF", type="file")
|
29 |
+
question = gr.Textbox(label="Ask a question about the PDF")
|
30 |
+
answer = gr.Textbox(label="Answer")
|
31 |
+
|
32 |
+
btn = gr.Button("Submit")
|
33 |
+
btn.click(generate_qa, inputs=[pdf_file, question], outputs=answer)
|
34 |
+
|
35 |
+
app.launch()
|