ask2pdf / ask2pdf.py
aitude's picture
Upload folder using huggingface_hub
7cd3edc verified
raw
history blame
2.31 kB
import pdfplumber
from transformers import pipeline
import gradio as gr
# Load the pre-trained question-answering model
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
# Function to extract text from the PDF
def extract_text_from_pdf(pdf_path):
text = ""
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
text += page.extract_text()
return text
# Function to handle question-answering
def qa_interface(pdf, question):
if not pdf:
return "Please upload a PDF file."
if not question.strip():
return "Please enter a question."
try:
context = extract_text_from_pdf(pdf.name)
if len(context.strip()) == 0:
return "The PDF appears to be empty or text could not be extracted. Please try another file."
result = qa_pipeline(question=question, context=context)
return result["answer"]
except Exception as e:
return f"An error occurred: {str(e)}"
# Gradio Interface
demo = gr.Interface(
fn=qa_interface,
inputs=[
gr.File(label="Upload PDF", file_types=[".pdf"]),
gr.Textbox(label="Enter your question", placeholder="What do you want to know about the PDF?"),
],
outputs=gr.Textbox(label="Answer"),
title="PDF Question Answering by Aitude.com",
description="""
Welcome to the PDF Question Answering demo powered by Aitude.com.
- Upload a PDF document (ensure it contains selectable text, as scanned PDFs are not supported).
- Enter a question based on the content of the uploaded PDF.
- The model will extract the relevant answer from the PDF.
This demo is ideal for querying reports, articles, and other text-based documents effortlessly.
""",
examples=[
["aitude.pdf", "What is AITUDE's core area of expertise?"],
["aitude.pdf", "How does AITUDE leverage natural language processing (NLP) models?"],
["aitude.pdf", "What is Generative AI and how is AITUDE involved in this field?"],
["aitude.pdf", "What are some potential applications of AITUDE's advancements in Generative AI?"],
["aitude.pdf", "How has Sandeep Kumar Mishra's leadership contributed to AITUDE's success?"],
],
)
if __name__ == "__main__":
demo.launch()