test / app.py
PiyushS2025's picture
Update app.py
c873e3c verified
raw
history blame contribute delete
870 Bytes
import gradio as gr
from PyPDF2 import PdfReader
from transformers import pipeline
# Load a pre-trained Question-Answering model
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
# Function to process the PDF and answer questions
def answer_question(file, question):
# Read PDF content
pdf_reader = PdfReader(file.name)
pdf_text = ""
for page in pdf_reader.pages:
pdf_text += page.extract_text()
# Use the QA pipeline to answer the question
result = qa_pipeline(question=question, context=pdf_text)
return result["answer"]
# Gradio Interface
demo = gr.Interface(
fn=answer_question,
inputs=[
gr.File(label="Upload PDF", file_types=[".pdf"]),
gr.Textbox(label="Enter your question"),
],
outputs="text",
title="Ask Questions from PDF",
)
demo.launch()