File size: 1,196 Bytes
084b7de
 
1ba9a89
 
 
 
 
 
 
 
084b7de
1ba9a89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d17f001
1ba9a89
 
 
 
d17f001
 
1ba9a89
 
 
 
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
from pathlib import Path
from typing import Union
import PyPDF2
from transformers import pipeline
import gradio as gr

# Initialize question-answering pipeline
nlp = pipeline('question-answering', model='deepset/roberta-base-squad2')

# Function to extract text from PDF
def extract_text_from_pdf(pdf_file: Union[str, Path]) -> str:
    with open(pdf_file, 'rb') as pdf_file_obj:
        pdf_reader = PyPDF2.PdfReader(pdf_file_obj)
        text = ''.join(page.extract_text() for page in pdf_reader.pages)
    return text

def answer_doc_question(pdf_file, question):
    # Extract text from PDF
    context = extract_text_from_pdf(pdf_file.name)

    # Prepare question-answering input
    QA_input = {
        'question': question,
        'context': context
    }

    # Get answer
    res = nlp(QA_input, max_answer_length=500)

    return res['answer']

# Define Gradio interface
pdf_input = gr.File(type="filepath", label="Upload a PDF document and ask a question about it.")
question = gr.Textbox(label="Type a question regarding the uploaded document here.")
iface = gr.Interface(fn=answer_doc_question, inputs=[pdf_input, question], outputs="text")

# Launch the interface
iface.launch()