Demo_public / app.py
Sbaig3229's picture
Upload 2 files
d67d1a3
raw
history blame
1.03 kB
import gradio as gr
from transformers import pipeline
import pdfplumber
# Load the pre-trained question-answering model
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
def answer_question(file, ques: str):
try:
# Read and extract text from the uploaded PDF file
with pdfplumber.open(file) as pdf:
text = ""
for page in pdf.pages:
text += page.extract_text()
# Ask a default question
question = ques
# Ask the question using the question-answering model
answer = qa_pipeline({"context": text, "question": question})
return answer["answer"]
except Exception as e:
return f"Error processing PDF: {str(e)}"
iface = gr.Interface(
fn=answer_question,
inputs=gr.File(label="Upload PDF"),
outputs="text",
live=True,
title="PDF Documents Question-Answering",
description="Ask a question about the contents of the uploaded PDF file.",
)
iface.launch()