areefmohammad commited on
Commit
665299e
Β·
verified Β·
1 Parent(s): 48d7180

Upload app.py

Browse files

Resolving More Errors

Files changed (1) hide show
  1. app.py +20 -3
app.py CHANGED
@@ -400,7 +400,7 @@ Rules:
400
  # CHATBOT RESPONSE (RAG + tool-calling loop)
401
  # ══════════════════════════════════════════════════════════════════════════════
402
 
403
- def respond_chatbot(user_message: str, history: list[dict]) -> str:
404
  """
405
  Gradio chat handler. For each user turn:
406
 
@@ -446,9 +446,25 @@ def respond_chatbot(user_message: str, history: list[dict]) -> str:
446
  print(sep)
447
 
448
  # ── 4. Assemble message list ───────────────────────────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449
  messages: list[dict] = (
450
  [{"role": "system", "content": augmented_system}]
451
- + history
452
  + [{"role": "user", "content": user_message}]
453
  )
454
 
@@ -514,7 +530,8 @@ demo = gr.ChatInterface(
514
  "Chat with an AI representation of Mohammad Areef Penukonda. "
515
  "Ask about his background, experience, or education."
516
  ),
517
- examples = ["What's your background?", "Professional Experience"],
 
518
  theme=gr.themes.Soft(),
519
  )
520
 
 
400
  # CHATBOT RESPONSE (RAG + tool-calling loop)
401
  # ══════════════════════════════════════════════════════════════════════════════
402
 
403
+ def respond_chatbot(user_message: str, history: list) -> str:
404
  """
405
  Gradio chat handler. For each user turn:
406
 
 
446
  print(sep)
447
 
448
  # ── 4. Assemble message list ───────────────────────────────────────────
449
+ # Normalise history: Gradio 3.x passes list[[user,assistant]] pairs;
450
+ # Gradio 4.x passes list[{"role":..,"content":..}] dicts.
451
+ # We convert both to the OpenAI dict format.
452
+ normalised_history: list[dict] = []
453
+ for entry in history:
454
+ if isinstance(entry, dict):
455
+ # Gradio 4.x format β€” already correct
456
+ normalised_history.append(entry)
457
+ elif isinstance(entry, (list, tuple)) and len(entry) == 2:
458
+ # Gradio 3.x format β€” [user_msg, assistant_msg]
459
+ user_turn, assistant_turn = entry
460
+ if user_turn:
461
+ normalised_history.append({"role": "user", "content": user_turn})
462
+ if assistant_turn:
463
+ normalised_history.append({"role": "assistant", "content": assistant_turn})
464
+
465
  messages: list[dict] = (
466
  [{"role": "system", "content": augmented_system}]
467
+ + normalised_history
468
  + [{"role": "user", "content": user_message}]
469
  )
470
 
 
530
  "Chat with an AI representation of Mohammad Areef Penukonda. "
531
  "Ask about his background, experience, or education."
532
  ),
533
+ # NOTE: type="messages" removed β€” only supported in Gradio 4.x+
534
+ examples=["What's your background?", "Professional Experience"],
535
  theme=gr.themes.Soft(),
536
  )
537