Spaces:
Running
Update app.py
Browse fileswith gr.Blocks(
title="PDF Q&A Chatbot",
css=".clear-btn button { height: 100%; }"
) as demo:
gr.Markdown("## ๐ PDF Q&A Chatbot\nUpload a PDF and ask questions about its content.")
state = gr.State({"chunks": [], "index": None, 'pdf_count':0})
with gr.Row():
with gr.Column(scale=1):
pdf_input = gr.File(label="Upload PDF(s)", file_types=[".pdf"], multiple=True)
pdf_status = gr.Textbox(label="Status", interactive=False)
with gr.Column(scale=2):
with gr.Row():
question = gr.Textbox(label="Your question", lines=2, scale =3)
clear_button = gr.Button("Clear", scale=1, min_width=80, elem_classes="clear-btn")
answer = gr.Textbox(label="Answer", lines=8)
ask_button = gr.Button("Ask")
pdf_input.upload(load_pdf, inputs=[pdf_input, state], outputs=[pdf_status, state])
ask_button.click(answer_question, inputs=[question, state], outputs=answer)
clear_button.click(lambda: "", outputs=question)
|
@@ -146,32 +146,35 @@ def generate_answer(question, context):
|
|
| 146 |
# GRADIO CALLBACKS
|
| 147 |
# ============================================================
|
| 148 |
|
| 149 |
-
def load_pdf(
|
| 150 |
-
if
|
| 151 |
return "No file uploaded.", state
|
| 152 |
|
| 153 |
try:
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
|
| 164 |
# Rebuild vectorstore with ALL chunks
|
| 165 |
index, _ = build_vectorstore(state["chunks"])
|
| 166 |
state["index"] = index
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
| 170 |
|
| 171 |
except Exception as e:
|
| 172 |
return f"Error processing PDF: {e}", state
|
| 173 |
|
| 174 |
|
|
|
|
| 175 |
def answer_question(question, state):
|
| 176 |
|
| 177 |
print("HF_TOKEN loaded:", HF_TOKEN is not None)
|
|
@@ -197,20 +200,24 @@ def answer_question(question, state):
|
|
| 197 |
# GRADIO UI
|
| 198 |
# ============================================================
|
| 199 |
|
| 200 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
| 207 |
-
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 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
|
| 214 |
|
| 215 |
answer = gr.Textbox(label="Answer", lines=8)
|
| 216 |
ask_button = gr.Button("Ask")
|
|
@@ -222,5 +229,6 @@ with gr.Blocks(title="PDF Q&A Chatbot") as demo:
|
|
| 222 |
|
| 223 |
|
| 224 |
|
|
|
|
| 225 |
if __name__ == "__main__":
|
| 226 |
demo.launch()
|
|
|
|
| 146 |
# GRADIO CALLBACKS
|
| 147 |
# ============================================================
|
| 148 |
|
| 149 |
+
def load_pdf(files, state):
|
| 150 |
+
if not files:
|
| 151 |
return "No file uploaded.", state
|
| 152 |
|
| 153 |
try:
|
| 154 |
+
for file in files:
|
| 155 |
+
text = read_pdf(file)
|
| 156 |
+
new_chunks = chunk_text(text)
|
| 157 |
|
| 158 |
+
if not new_chunks:
|
| 159 |
+
continue
|
| 160 |
|
| 161 |
+
state["chunks"].extend(new_chunks)
|
| 162 |
+
state["pdf_count"] += 1
|
| 163 |
|
| 164 |
# Rebuild vectorstore with ALL chunks
|
| 165 |
index, _ = build_vectorstore(state["chunks"])
|
| 166 |
state["index"] = index
|
| 167 |
|
| 168 |
+
return (
|
| 169 |
+
f"{state['pdf_count']} PDF(s) loaded. Total chunks: {len(state['chunks'])}",
|
| 170 |
+
state,
|
| 171 |
+
)
|
| 172 |
|
| 173 |
except Exception as e:
|
| 174 |
return f"Error processing PDF: {e}", state
|
| 175 |
|
| 176 |
|
| 177 |
+
|
| 178 |
def answer_question(question, state):
|
| 179 |
|
| 180 |
print("HF_TOKEN loaded:", HF_TOKEN is not None)
|
|
|
|
| 200 |
# GRADIO UI
|
| 201 |
# ============================================================
|
| 202 |
|
| 203 |
+
with gr.Blocks(
|
| 204 |
+
title="PDF Q&A Chatbot",
|
| 205 |
+
css=".clear-btn button { height: 100%; }"
|
| 206 |
+
) as demo:
|
| 207 |
+
|
| 208 |
gr.Markdown("## ๐ PDF Q&A Chatbot\nUpload a PDF and ask questions about its content.")
|
| 209 |
|
| 210 |
state = gr.State({"chunks": [], "index": None, 'pdf_count':0})
|
| 211 |
|
| 212 |
with gr.Row():
|
| 213 |
with gr.Column(scale=1):
|
| 214 |
+
pdf_input = gr.File(label="Upload PDF(s)", file_types=[".pdf"], multiple=True)
|
| 215 |
pdf_status = gr.Textbox(label="Status", interactive=False)
|
| 216 |
|
| 217 |
with gr.Column(scale=2):
|
| 218 |
with gr.Row():
|
| 219 |
+
question = gr.Textbox(label="Your question", lines=2, scale =3)
|
| 220 |
+
clear_button = gr.Button("Clear", scale=1, min_width=80, elem_classes="clear-btn")
|
| 221 |
|
| 222 |
answer = gr.Textbox(label="Answer", lines=8)
|
| 223 |
ask_button = gr.Button("Ask")
|
|
|
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
+
|
| 233 |
if __name__ == "__main__":
|
| 234 |
demo.launch()
|