sofzcc commited on
Commit
bb87ddf
·
verified ·
1 Parent(s): bf0ef35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -17
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:** Try one of the buttons below.
223
  """
224
  )
225
 
226
  with gr.Row():
227
- outputs = []
228
- for label, _ in HELPFUL_SUGGESTIONS:
229
- btn = gr.Button(label)
230
- btn.click(fn=lambda L=label: quick_intent(L), outputs=None)\
231
- .then(fn=respond, inputs=[gr.State(quick_intent(label)), gr.State([])], outputs=gr.Chatbot())
232
- outputs.append(btn)
233
-
234
- chat = gr.ChatInterface(
235
- fn=respond,
236
- chatbot=gr.Chatbot(height=420, show_copy_button=True),
237
- textbox=gr.Textbox(placeholder="e.g., How do I connect WhatsApp?"),
238
- retry_btn="Retry",
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
- rebuild.click(fn=rebuild_index, outputs=status)
 
 
 
 
 
 
 
 
 
 
 
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()