Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -212,6 +212,26 @@ def rebuild_index():
|
|
| 212 |
return gr.update(value="✅ Index rebuilt from KB.")
|
| 213 |
|
| 214 |
# ----------- Gradio UI -----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
with gr.Blocks(title="Self-Service KB Assistant", fill_height=True) as demo:
|
| 216 |
gr.Markdown(
|
| 217 |
"""
|
|
@@ -219,32 +239,40 @@ with gr.Blocks(title="Self-Service KB Assistant", fill_height=True) as demo:
|
|
| 219 |
Ask about setup, automations, billing, or troubleshooting.
|
| 220 |
The assistant answers **only from the Knowledge Base** and cites the articles it used.
|
| 221 |
|
| 222 |
-
**Quick actions:**
|
| 223 |
"""
|
| 224 |
)
|
| 225 |
|
| 226 |
with gr.Row():
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
undo_btn="Undo",
|
| 240 |
-
clear_btn="Clear",
|
| 241 |
-
)
|
| 242 |
|
| 243 |
with gr.Accordion("Admin", open=False):
|
| 244 |
gr.Markdown("Rebuild the search index after changing files in `/kb`.")
|
| 245 |
rebuild = gr.Button("Rebuild Index")
|
| 246 |
status = gr.Markdown("")
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
if __name__ == "__main__":
|
| 250 |
demo.launch()
|
|
|
|
| 212 |
return gr.update(value="✅ Index rebuilt from KB.")
|
| 213 |
|
| 214 |
# ----------- Gradio UI -----------
|
| 215 |
+
def process_message(user_input, history):
|
| 216 |
+
"""
|
| 217 |
+
history is a list of dicts: [{"role":"user","content":...}, {"role":"assistant","content":...}, ...]
|
| 218 |
+
We return updated history and a cleared textbox.
|
| 219 |
+
"""
|
| 220 |
+
user_input = (user_input or "").strip()
|
| 221 |
+
if not user_input:
|
| 222 |
+
return history, gr.update(value="")
|
| 223 |
+
reply = respond(user_input, history or [])
|
| 224 |
+
new_history = (history or []) + [
|
| 225 |
+
{"role": "user", "content": user_input},
|
| 226 |
+
{"role": "assistant", "content": reply}
|
| 227 |
+
]
|
| 228 |
+
return new_history, gr.update(value="")
|
| 229 |
+
|
| 230 |
+
def process_quick(label, history):
|
| 231 |
+
# turn a quick button into a user message
|
| 232 |
+
q = quick_intent(label)
|
| 233 |
+
return process_message(q, history)
|
| 234 |
+
|
| 235 |
with gr.Blocks(title="Self-Service KB Assistant", fill_height=True) as demo:
|
| 236 |
gr.Markdown(
|
| 237 |
"""
|
|
|
|
| 239 |
Ask about setup, automations, billing, or troubleshooting.
|
| 240 |
The assistant answers **only from the Knowledge Base** and cites the articles it used.
|
| 241 |
|
| 242 |
+
**Quick actions:** Use a button below to try a common task.
|
| 243 |
"""
|
| 244 |
)
|
| 245 |
|
| 246 |
with gr.Row():
|
| 247 |
+
chat = gr.Chatbot(height=420, show_copy_button=True, type="messages") # ✅ modern format
|
| 248 |
+
with gr.Row():
|
| 249 |
+
txt = gr.Textbox(placeholder="e.g., How do I connect WhatsApp?", scale=10)
|
| 250 |
+
send = gr.Button("Send", variant="primary")
|
| 251 |
+
|
| 252 |
+
with gr.Row():
|
| 253 |
+
# Quick action buttons
|
| 254 |
+
btn_whatsapp = gr.Button("Connect WhatsApp")
|
| 255 |
+
btn_reset = gr.Button("Reset Password")
|
| 256 |
+
btn_first = gr.Button("First Automation")
|
| 257 |
+
btn_billing = gr.Button("Billing & Invoices")
|
| 258 |
+
btn_ig = gr.Button("Fix Instagram Connect")
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
with gr.Accordion("Admin", open=False):
|
| 261 |
gr.Markdown("Rebuild the search index after changing files in `/kb`.")
|
| 262 |
rebuild = gr.Button("Rebuild Index")
|
| 263 |
status = gr.Markdown("")
|
| 264 |
+
|
| 265 |
+
# Wire up events
|
| 266 |
+
send.click(process_message, inputs=[txt, chat], outputs=[chat, txt])
|
| 267 |
+
txt.submit(process_message, inputs=[txt, chat], outputs=[chat, txt]) # Enter key
|
| 268 |
+
|
| 269 |
+
btn_whatsapp.click(process_quick, inputs=[gr.State("Connect WhatsApp"), chat], outputs=[chat, txt])
|
| 270 |
+
btn_reset.click(process_quick, inputs=[gr.State("Reset Password"), chat], outputs=[chat, txt])
|
| 271 |
+
btn_first.click(process_quick, inputs=[gr.State("First Automation"), chat], outputs=[chat, txt])
|
| 272 |
+
btn_billing.click(process_quick, inputs=[gr.State("Billing & Invoices"), chat], outputs=[chat, txt])
|
| 273 |
+
btn_ig.click(process_quick, inputs=[gr.State("Fix Instagram Connect"), chat], outputs=[chat, txt])
|
| 274 |
+
|
| 275 |
+
rebuild.click(lambda: (kb.build(KB_DIR), "✅ Index rebuilt from KB.")[1], outputs=status)
|
| 276 |
|
| 277 |
if __name__ == "__main__":
|
| 278 |
demo.launch()
|