Pdf_chatbot7 / app.py
Fitsume's picture
Update app.py
274c981 verified
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()