File size: 2,314 Bytes
7cd3edc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
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()
|