Spaces:
Sleeping
Sleeping
Update app.py
Browse filesstate = gr.State({"chunks": [], "index": None, 'pdf_count':0})
with gr.Row():
with gr.Column(scale=1):
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
pdf_status = gr.Textbox(label="Status", interactive=False)
with gr.Column(scale=2):
with gr.row()
question = gr.Textbox(label="Your question", lines=2)
clear_button = gr.Button("Clear Question")
answer = gr.Textbox(label="Answer", lines=8)
ask_button = gr.Button("Ask")
app.py
CHANGED
|
@@ -147,25 +147,26 @@ def generate_answer(question, context):
|
|
| 147 |
# ============================================================
|
| 148 |
|
| 149 |
def load_pdf(file, state):
|
| 150 |
-
state["chunks"] = None
|
| 151 |
-
state["index"] = None
|
| 152 |
-
|
| 153 |
if file is None:
|
| 154 |
return "No file uploaded.", state
|
| 155 |
|
| 156 |
try:
|
|
|
|
| 157 |
text = read_pdf(file)
|
| 158 |
-
|
| 159 |
|
| 160 |
-
if not
|
| 161 |
return "Could not extract text from PDF.", state
|
| 162 |
|
| 163 |
-
|
|
|
|
| 164 |
|
| 165 |
-
|
|
|
|
| 166 |
state["index"] = index
|
| 167 |
|
| 168 |
-
|
|
|
|
| 169 |
|
| 170 |
except Exception as e:
|
| 171 |
return f"Error processing PDF: {e}", state
|
|
@@ -199,7 +200,7 @@ def answer_question(question, state):
|
|
| 199 |
with gr.Blocks(title="PDF Q&A Chatbot") as demo:
|
| 200 |
gr.Markdown("## 📄 PDF Q&A Chatbot\nUpload a PDF and ask questions about its content.")
|
| 201 |
|
| 202 |
-
state = gr.State({"chunks":
|
| 203 |
|
| 204 |
with gr.Row():
|
| 205 |
with gr.Column(scale=1):
|
|
@@ -207,12 +208,17 @@ with gr.Blocks(title="PDF Q&A Chatbot") as demo:
|
|
| 207 |
pdf_status = gr.Textbox(label="Status", interactive=False)
|
| 208 |
|
| 209 |
with gr.Column(scale=2):
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
| 211 |
answer = gr.Textbox(label="Answer", lines=8)
|
| 212 |
ask_button = gr.Button("Ask")
|
|
|
|
| 213 |
|
| 214 |
pdf_input.upload(load_pdf, inputs=[pdf_input, state], outputs=[pdf_status, state])
|
| 215 |
ask_button.click(answer_question, inputs=[question, state], outputs=answer)
|
|
|
|
| 216 |
|
| 217 |
|
| 218 |
|
|
|
|
| 147 |
# ============================================================
|
| 148 |
|
| 149 |
def load_pdf(file, state):
|
|
|
|
|
|
|
|
|
|
| 150 |
if file is None:
|
| 151 |
return "No file uploaded.", state
|
| 152 |
|
| 153 |
try:
|
| 154 |
+
# Read and chunk the new PDF
|
| 155 |
text = read_pdf(file)
|
| 156 |
+
new_chunks = chunk_text(text)
|
| 157 |
|
| 158 |
+
if not new_chunks:
|
| 159 |
return "Could not extract text from PDF.", state
|
| 160 |
|
| 161 |
+
# Append to existing chunks
|
| 162 |
+
state["chunks"].extend(new_chunks)
|
| 163 |
|
| 164 |
+
# Rebuild vectorstore with ALL chunks
|
| 165 |
+
index, _ = build_vectorstore(state["chunks"])
|
| 166 |
state["index"] = index
|
| 167 |
|
| 168 |
+
state["pdf_count"] += 1
|
| 169 |
+
return f"PDF #{state['pdf_count']} added. Total chunks: {len(state['chunks'])}", state
|
| 170 |
|
| 171 |
except Exception as e:
|
| 172 |
return f"Error processing PDF: {e}", state
|
|
|
|
| 200 |
with gr.Blocks(title="PDF Q&A Chatbot") as demo:
|
| 201 |
gr.Markdown("## 📄 PDF Q&A Chatbot\nUpload a PDF and ask questions about its content.")
|
| 202 |
|
| 203 |
+
state = gr.State({"chunks": [], "index": None, 'pdf_count':0})
|
| 204 |
|
| 205 |
with gr.Row():
|
| 206 |
with gr.Column(scale=1):
|
|
|
|
| 208 |
pdf_status = gr.Textbox(label="Status", interactive=False)
|
| 209 |
|
| 210 |
with gr.Column(scale=2):
|
| 211 |
+
with gr.row()
|
| 212 |
+
question = gr.Textbox(label="Your question", lines=2)
|
| 213 |
+
clear_button = gr.Button("Clear Question")
|
| 214 |
+
|
| 215 |
answer = gr.Textbox(label="Answer", lines=8)
|
| 216 |
ask_button = gr.Button("Ask")
|
| 217 |
+
|
| 218 |
|
| 219 |
pdf_input.upload(load_pdf, inputs=[pdf_input, state], outputs=[pdf_status, state])
|
| 220 |
ask_button.click(answer_question, inputs=[question, state], outputs=answer)
|
| 221 |
+
clear_button.click(lambda: "", outputs=question)
|
| 222 |
|
| 223 |
|
| 224 |
|