mackenzietechdocs commited on
Commit
cf69bc6
·
1 Parent(s): 4e815bd

fixed roles issue in chat

Browse files
Files changed (1) hide show
  1. app.py +24 -5
app.py CHANGED
@@ -98,8 +98,24 @@ def generate_reply(chat_history, user_message, max_new_tokens=512, temperature=0
98
  return reply
99
 
100
  def gradio_chat(user_message, history, max_new_tokens, temperature, top_p):
101
- # Gradio 6.0.2 Chatbot returns list of (user, assistant) tuples
102
- history_pairs = history or []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  reply = generate_reply(
105
  history_pairs,
@@ -109,8 +125,10 @@ def gradio_chat(user_message, history, max_new_tokens, temperature, top_p):
109
  top_p=float(top_p),
110
  )
111
 
112
- # Return as list of (user, assistant) tuples
113
- new_history = history_pairs + [(user_message, reply)]
 
 
114
 
115
  return "", new_history
116
 
@@ -481,7 +499,8 @@ with gr.Blocks() as demo:
481
  height=550,
482
  show_label=False,
483
  avatar_images=(None, "ml-chat.png"),
484
- elem_classes="chatbot"
 
485
  )
486
 
487
  # Settings sidebar
 
98
  return reply
99
 
100
  def gradio_chat(user_message, history, max_new_tokens, temperature, top_p):
101
+ # Chatbot (type="messages") returns list of dicts with role/content
102
+ history_messages = history or []
103
+
104
+ # Convert role/content messages into (user, assistant) pairs for the model
105
+ history_pairs = []
106
+ pending_user = None
107
+ for msg in history_messages:
108
+ if isinstance(msg, (list, tuple)) and len(msg) == 2:
109
+ history_pairs.append((msg[0], msg[1]))
110
+ pending_user = None
111
+ continue
112
+ role = msg.get("role")
113
+ content = msg.get("content", "")
114
+ if role == "user":
115
+ pending_user = content
116
+ elif role == "assistant":
117
+ history_pairs.append((pending_user or "", content))
118
+ pending_user = None
119
 
120
  reply = generate_reply(
121
  history_pairs,
 
125
  top_p=float(top_p),
126
  )
127
 
128
+ new_history = history_messages + [
129
+ {"role": "user", "content": user_message},
130
+ {"role": "assistant", "content": reply},
131
+ ]
132
 
133
  return "", new_history
134
 
 
499
  height=550,
500
  show_label=False,
501
  avatar_images=(None, "ml-chat.png"),
502
+ elem_classes="chatbot",
503
+ type="messages",
504
  )
505
 
506
  # Settings sidebar