File size: 1,555 Bytes
e6e5f94
0273b5c
 
 
 
274c981
0273b5c
 
 
 
 
4566b95
 
 
 
 
 
 
0273b5c
4566b95
 
 
 
 
274c981
4566b95
 
 
274c981
4566b95
 
 
274c981
4566b95
0273b5c
4566b95
e6e5f94
274c981
 
0273b5c
274c981
4566b95
274c981
4566b95
 
 
274c981
4566b95
274c981
 
 
4566b95
 
e6e5f94
0273b5c
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
import gradio as gr
from PyPDF2 import PdfReader

# Function to extract text from PDF
def extract_text(pdf_file):
    reader = PdfReader(pdf_file)  # filepath is directly passed now
    text = ""
    for page in reader.pages:
        text += page.extract_text() or ""
    return text

# Store extracted text
pdf_text = ""

def upload_pdf(file):
    global pdf_text
    pdf_text = extract_text(file)
    return "βœ… PDF uploaded. You can now ask questions."

# Very simple QA function (keyword-based)
def ask_question(question):
    global pdf_text
    if not pdf_text:
        return "❌ Please upload a PDF first."

    # Simple search: find if keywords exist in PDF text
    question_words = question.lower().split()
    sentences = pdf_text.split("\n")

    for sentence in sentences:
        if all(word in sentence.lower() for word in question_words):
            return sentence

    return "πŸ€” Sorry, I couldn't find an exact match in the PDF."

# Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("# πŸ“„ PDF Chatbot")

    with gr.Row():
        pdf_upload = gr.File(label="Upload a PDF", type="filepath")
        upload_btn = gr.Button("Process PDF")

    chatbox = gr.Chatbot()
    question_input = gr.Textbox(label="Ask me anything about the PDF")
    ask_btn = gr.Button("Ask")

    upload_btn.click(upload_pdf, inputs=pdf_upload, outputs=chatbox)
    ask_btn.click(
        lambda q: ("You:", q), inputs=question_input, outputs=chatbox
    ).then(
        ask_question, inputs=question_input, outputs=chatbox
    )

demo.launch()