tonyassi commited on
Commit
f766e3b
·
verified ·
1 Parent(s): 2c4f273

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -5
app.py CHANGED
@@ -65,6 +65,39 @@ def _system_prompt_for(name: str) -> str:
65
  )
66
 
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  @spaces.GPU()
69
  def get_response(conversation):
70
  """
@@ -112,7 +145,7 @@ def get_response(conversation):
112
  def stream_chat(history, character_a, character_b):
113
  """
114
  history: list of messages (messages format):
115
- [{"role": "user" | "assistant", "content": "..."}, ...]
116
 
117
  In the UI:
118
  - user messages = Character B
@@ -122,8 +155,8 @@ def stream_chat(history, character_a, character_b):
122
  1. B says something new (as 'user')
123
  2. A replies (as 'assistant')
124
  """
125
- if history is None:
126
- history = []
127
 
128
  # ---------- B speaks (user side) ----------
129
  if len(history) == 0:
@@ -133,7 +166,7 @@ def stream_chat(history, character_a, character_b):
133
  "Introduce yourself and start the conversation."
134
  )
135
  else:
136
- # Find last assistant message (A) to respond to
137
  last_msg = history[-1]
138
  last_text = last_msg["content"]
139
  b_user_prompt = (
@@ -233,7 +266,7 @@ with gr.Blocks() as demo:
233
  gr.Markdown(" ")
234
  image_b = gr.Image(show_label=False, interactive=False)
235
 
236
- # IMPORTANT: no 'type=' argument here; your Gradio build doesn't support it
237
  chat = gr.Chatbot(show_label=False)
238
  submit_button = gr.Button("Start Conversation")
239
 
 
65
  )
66
 
67
 
68
+ def normalize_history(history):
69
+ """
70
+ Gradio may send messages where `content` is a list of rich parts:
71
+ {"role": "assistant",
72
+ "content": [{"type": "text", "text": "hello"}]}
73
+
74
+ We convert everything into:
75
+ {"role": ..., "content": "plain string"}
76
+ """
77
+ if history is None:
78
+ return []
79
+
80
+ norm = []
81
+ for msg in history:
82
+ role = msg.get("role", "user")
83
+ content = msg.get("content", "")
84
+
85
+ if isinstance(content, list):
86
+ # e.g. [{"type":"text","text":"..."}, ...]
87
+ parts = []
88
+ for part in content:
89
+ if isinstance(part, dict) and part.get("type") == "text":
90
+ parts.append(part.get("text", ""))
91
+ else:
92
+ parts.append(str(part))
93
+ content = "\n".join(parts)
94
+ else:
95
+ content = str(content)
96
+
97
+ norm.append({"role": role, "content": content})
98
+ return norm
99
+
100
+
101
  @spaces.GPU()
102
  def get_response(conversation):
103
  """
 
145
  def stream_chat(history, character_a, character_b):
146
  """
147
  history: list of messages (messages format):
148
+ [{"role": "user" | "assistant", "content": ...}, ...]
149
 
150
  In the UI:
151
  - user messages = Character B
 
155
  1. B says something new (as 'user')
156
  2. A replies (as 'assistant')
157
  """
158
+ # 🔑 Normalize history coming from Gradio into plain strings
159
+ history = normalize_history(history)
160
 
161
  # ---------- B speaks (user side) ----------
162
  if len(history) == 0:
 
166
  "Introduce yourself and start the conversation."
167
  )
168
  else:
169
+ # Last assistant message (A) to respond to
170
  last_msg = history[-1]
171
  last_text = last_msg["content"]
172
  b_user_prompt = (
 
266
  gr.Markdown(" ")
267
  image_b = gr.Image(show_label=False, interactive=False)
268
 
269
+ # No 'type' kwarg your Gradio build doesn't support it, but it *does* use messages format
270
  chat = gr.Chatbot(show_label=False)
271
  submit_button = gr.Button("Start Conversation")
272