tayyab-077 commited on
Commit
6a98756
Β·
1 Parent(s): 2d04800

updatation

Browse files
Files changed (1) hide show
  1. app.py +16 -40
app.py CHANGED
@@ -143,15 +143,14 @@ def export_chat_files(history):
143
  # ----------------------
144
  # Core chat function
145
  # ----------------------
146
- def generate_reply(user_msg: str, history: Optional[List[List[str]]]):
147
  if history is None:
148
  history = []
149
 
150
- user_msg = user_msg.strip()
151
- if not user_msg:
152
  return history
153
 
154
- # ----- INTENT HANDLING -----
155
  intent = None
156
  low = user_msg.lower()
157
  for key in INTENT_TEMPLATES:
@@ -160,23 +159,20 @@ def generate_reply(user_msg: str, history: Optional[List[List[str]]]):
160
  user_msg = user_msg[len(key):].strip()
161
  break
162
 
163
- system_prefix = INTENT_TEMPLATES.get(intent, None)
164
  if system_prefix:
165
  prompt = f"{system_prefix}\nUser: {user_msg}"
166
  else:
167
  prompt = f"User: {user_msg}"
168
 
 
169
  bot_reply = bot.ask(prompt)
170
  ts = now_ts()
171
- bot_reply_final = f"{bot_reply}\n\nπŸ•’ {ts}"
172
 
173
- # ------------------------------
174
- # FIX: Store in GRADIO-CHATBOT FORMAT
175
- # ["user message", "assistant reply"]
176
- # ------------------------------
177
- history.append([user_msg, bot_reply_final])
178
 
179
- # ---- Save to memory (optional) ----
180
  try:
181
  memory.add(user_msg, bot_reply)
182
  except:
@@ -310,37 +306,28 @@ PAGE_JS = """
310
  # ----------------------
311
  # UI
312
  # ----------------------
313
-
314
  with gr.Blocks(title="Tayyab β€” Chatbot") as demo:
315
 
316
  gr.HTML(f"""
317
- <style>
318
- {CUSTOM_CSS}
319
- </style>
320
-
321
- <script>
322
- {PAGE_JS}
323
- </script>
324
  """)
325
 
326
- # your UI components here...
327
-
328
-
329
  with gr.Row():
330
  with gr.Column(scale=1, min_width=220):
331
  gr.Markdown("### ⚑ Tools & Export")
332
 
333
  new_chat_btn = gr.Button("βž• New Chat", elem_classes="tool-btn")
334
  export_btn = gr.Button("πŸ“₯ Export TXT/PDF", elem_classes="tool-btn")
335
-
336
- # Export output panel
337
  with gr.Accordion("πŸ“„ Exported Files", open=False):
338
  file_txt = gr.File(label="Download TXT", visible=False)
339
  file_pdf = gr.File(label="Download PDF", visible=False)
340
 
341
  with gr.Column(scale=3, elem_id="main_card"):
342
  gr.Markdown("<h3>Smart Learning Assistant - Tayyab</h3>")
343
- chatbot = gr.Chatbot(height=480, elem_id="chatbot_box", avatar_images=(None, None), type="value")
 
344
 
345
  with gr.Row():
346
  msg = gr.Textbox(
@@ -353,34 +340,23 @@ with gr.Blocks(title="Tayyab β€” Chatbot") as demo:
353
  mic_btn = gr.Button("🎀 Voice input", elem_classes="icon-btn")
354
  mic_btn.click(None, None, None, js='startVoiceRecognition("message-box")')
355
 
356
-
357
- # Backend connections
358
  send_btn.click(generate_reply, [msg, chatbot], [chatbot])
359
  msg.submit(generate_reply, [msg, chatbot], [chatbot])
360
-
361
  def new_chat():
362
  memory.clear()
363
  return []
364
 
365
  new_chat_btn.click(new_chat, outputs=[chatbot])
366
 
367
-
368
  def export_handler(history):
369
- if history is None:
370
- history = []
371
-
372
  files = export_chat_files(history)
373
-
374
  return (
375
  gr.update(value=files["txt"], visible=True),
376
- gr.update(value=files["pdf"], visible=(files["pdf"] is not None))
377
  )
378
 
379
- export_btn.click(
380
- export_handler,
381
- inputs=[chatbot],
382
- outputs=[file_txt, file_pdf]
383
- )
384
 
385
  if __name__ == "__main__":
386
  demo.launch()
 
143
  # ----------------------
144
  # Core chat function
145
  # ----------------------
146
+ def generate_reply(user_msg: str, history):
147
  if history is None:
148
  history = []
149
 
150
+ if not user_msg.strip():
 
151
  return history
152
 
153
+ # Prepare prompt
154
  intent = None
155
  low = user_msg.lower()
156
  for key in INTENT_TEMPLATES:
 
159
  user_msg = user_msg[len(key):].strip()
160
  break
161
 
162
+ system_prefix = INTENT_TEMPLATES.get(intent, "")
163
  if system_prefix:
164
  prompt = f"{system_prefix}\nUser: {user_msg}"
165
  else:
166
  prompt = f"User: {user_msg}"
167
 
168
+ # Generate bot reply
169
  bot_reply = bot.ask(prompt)
170
  ts = now_ts()
171
+ bot_reply_ts = f"{bot_reply}\n\nπŸ•’ {ts}"
172
 
173
+ # ---- FIX: Append pair (user, bot) ----
174
+ history.append((user_msg, bot_reply_ts))
 
 
 
175
 
 
176
  try:
177
  memory.add(user_msg, bot_reply)
178
  except:
 
306
  # ----------------------
307
  # UI
308
  # ----------------------
 
309
  with gr.Blocks(title="Tayyab β€” Chatbot") as demo:
310
 
311
  gr.HTML(f"""
312
+ <style>{CUSTOM_CSS}</style>
313
+ <script>{PAGE_JS}</script>
 
 
 
 
 
314
  """)
315
 
 
 
 
316
  with gr.Row():
317
  with gr.Column(scale=1, min_width=220):
318
  gr.Markdown("### ⚑ Tools & Export")
319
 
320
  new_chat_btn = gr.Button("βž• New Chat", elem_classes="tool-btn")
321
  export_btn = gr.Button("πŸ“₯ Export TXT/PDF", elem_classes="tool-btn")
322
+
 
323
  with gr.Accordion("πŸ“„ Exported Files", open=False):
324
  file_txt = gr.File(label="Download TXT", visible=False)
325
  file_pdf = gr.File(label="Download PDF", visible=False)
326
 
327
  with gr.Column(scale=3, elem_id="main_card"):
328
  gr.Markdown("<h3>Smart Learning Assistant - Tayyab</h3>")
329
+
330
+ chatbot = gr.Chatbot(height=480, elem_id="chatbot_box") # FIXED
331
 
332
  with gr.Row():
333
  msg = gr.Textbox(
 
340
  mic_btn = gr.Button("🎀 Voice input", elem_classes="icon-btn")
341
  mic_btn.click(None, None, None, js='startVoiceRecognition("message-box")')
342
 
 
 
343
  send_btn.click(generate_reply, [msg, chatbot], [chatbot])
344
  msg.submit(generate_reply, [msg, chatbot], [chatbot])
345
+
346
  def new_chat():
347
  memory.clear()
348
  return []
349
 
350
  new_chat_btn.click(new_chat, outputs=[chatbot])
351
 
 
352
  def export_handler(history):
 
 
 
353
  files = export_chat_files(history)
 
354
  return (
355
  gr.update(value=files["txt"], visible=True),
356
+ gr.update(value=files["pdf"], visible=files["pdf"] is not None)
357
  )
358
 
359
+ export_btn.click(export_handler, inputs=[chatbot], outputs=[file_txt, file_pdf])
 
 
 
 
360
 
361
  if __name__ == "__main__":
362
  demo.launch()