File size: 1,030 Bytes
d67d1a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()