File size: 861 Bytes
31b5b58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d5af9e
 
 
 
31b5b58
 
 
 
 
 
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
import gradio as gr
import fitz  # PyMuPDF
from transformers import pipeline

# Load the Hugging Face question-answering pipeline
qa_pipeline = pipeline("question-answering")

def answer_question(pdf_file, question):
    # Read the PDF file
    doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
    text = ""
    for page in doc:
        text += page.get_text()

    # Use the QA model to answer the question
    answer = qa_pipeline(question=question, context=text)
    return answer['answer']

# Define the Gradio interface
interface = gr.Interface(
    fn=answer_question,
    inputs=[
        gr.File(type="pdf"), 
        gr.Textbox(lines=2, placeholder="Type your question here...")
    ],
    outputs="text",
    title="PDF Question Answering",
    description="Upload a PDF document and ask a question based on its content."
)

interface.launch()