Commit
·
6610ef3
1
Parent(s):
10178ba
Fix gemini response in roundtable
Browse files
app.py
CHANGED
@@ -279,7 +279,11 @@ def send_message(channel: str, username: str, message: str):
|
|
279 |
state["showBubbles"].append("Gemini")
|
280 |
if len(state["showBubbles"]) > 4:
|
281 |
state["showBubbles"] = state["showBubbles"][-4:]
|
282 |
-
|
|
|
|
|
|
|
|
|
283 |
else:
|
284 |
state["currentSpeaker"] = None
|
285 |
else:
|
@@ -296,9 +300,9 @@ def send_message(channel: str, username: str, message: str):
|
|
296 |
return final_history, final_roundtable_json, final_chatbot_formatted, final_roundtable_json, ""
|
297 |
|
298 |
def get_summary_or_opinion(channel: str, prompt_template: str) -> Tuple[List[Dict], str]:
|
299 |
-
"""Handles summary/opinion
|
300 |
|
301 |
-
with history_lock:
|
302 |
history_copy = chat_histories.get(channel, []).copy()
|
303 |
|
304 |
history_for_llm = [{"role": "system", "content": prompt_template}] + consolidate_history_for_gemini(history_copy)
|
@@ -309,23 +313,47 @@ def get_summary_or_opinion(channel: str, prompt_template: str) -> Tuple[List[Dic
|
|
309 |
content = response_text if response_text and "Error:" not in response_text else "Could not generate the response."
|
310 |
|
311 |
system_msg = {"role": role, "content": content}
|
|
|
312 |
with history_lock:
|
313 |
chat_histories[channel].append(system_msg)
|
314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
return chat_histories.get(channel, []), roundtable_json
|
316 |
|
317 |
def format_history_for_chatbot_display(history: List[Dict]) -> List[Dict]:
|
318 |
"""Applies HTML formatting for gr.Chatbot display using the 'messages' format."""
|
319 |
formatted_history = []
|
320 |
-
for msg in history:
|
321 |
-
new_msg = msg.copy()
|
322 |
role, content, username = (
|
323 |
new_msg.get("role"),
|
324 |
new_msg.get("content", ""),
|
325 |
new_msg.get("username"),
|
326 |
)
|
327 |
-
|
328 |
-
|
|
|
|
|
|
|
|
|
329 |
display_content = ""
|
330 |
|
331 |
if role == "user" and username:
|
@@ -350,7 +378,7 @@ def format_history_for_chatbot_display(history: List[Dict]) -> List[Dict]:
|
|
350 |
f"<div style='background-color:#f8f9fa; border-left: 5px solid #ccc; padding: 10px; margin: 10px 0; border-radius: 5px;'>"
|
351 |
f"<b>{title}:</b><br>{response_content}</div>"
|
352 |
)
|
353 |
-
else:
|
354 |
display_content = content
|
355 |
|
356 |
if display_content:
|
@@ -459,7 +487,7 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="Multi-Agent Chat") as demo:
|
|
459 |
exit_button = gr.Button("🚪 Exit Chat")
|
460 |
|
461 |
with gr.Column(scale=3):
|
462 |
-
view_switch = gr.Radio(["
|
463 |
|
464 |
roundtable_display = consilium_roundtable(
|
465 |
label="🎭 Live Discussion Roundtable",
|
@@ -514,12 +542,14 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="Multi-Agent Chat") as demo:
|
|
514 |
fn=send_message,
|
515 |
inputs=[channel_display, username_display, msg_input],
|
516 |
outputs=[unformatted_history_state, roundtable_state_json, chatbot_display, roundtable_display, msg_input],
|
|
|
517 |
api_name="send_message"
|
518 |
)
|
519 |
msg_input.submit(
|
520 |
fn=send_message,
|
521 |
inputs=[channel_display, username_display, msg_input],
|
522 |
outputs=[unformatted_history_state, roundtable_state_json, chatbot_display, roundtable_display, msg_input],
|
|
|
523 |
api_name="send_message_submit"
|
524 |
)
|
525 |
|
@@ -537,6 +567,7 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="Multi-Agent Chat") as demo:
|
|
537 |
).then(
|
538 |
fn=format_all_views_from_state,
|
539 |
inputs=[unformatted_history_state, roundtable_state_json],
|
|
|
540 |
outputs=[chatbot_display, roundtable_display]
|
541 |
)
|
542 |
|
@@ -549,6 +580,7 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="Multi-Agent Chat") as demo:
|
|
549 |
).then(
|
550 |
fn=format_all_views_from_state,
|
551 |
inputs=[unformatted_history_state, roundtable_state_json],
|
|
|
552 |
outputs=[chatbot_display, roundtable_display]
|
553 |
)
|
554 |
|
|
|
279 |
state["showBubbles"].append("Gemini")
|
280 |
if len(state["showBubbles"]) > 4:
|
281 |
state["showBubbles"] = state["showBubbles"][-4:]
|
282 |
+
|
283 |
+
state["currentSpeaker"] = None
|
284 |
+
|
285 |
+
if "thinking" in state and "Gemini" in state["thinking"]:
|
286 |
+
state["thinking"].remove("Gemini")
|
287 |
else:
|
288 |
state["currentSpeaker"] = None
|
289 |
else:
|
|
|
300 |
return final_history, final_roundtable_json, final_chatbot_formatted, final_roundtable_json, ""
|
301 |
|
302 |
def get_summary_or_opinion(channel: str, prompt_template: str) -> Tuple[List[Dict], str]:
|
303 |
+
"""Handles summary/opinion and updates BOTH chat histories."""
|
304 |
|
305 |
+
with history_lock:
|
306 |
history_copy = chat_histories.get(channel, []).copy()
|
307 |
|
308 |
history_for_llm = [{"role": "system", "content": prompt_template}] + consolidate_history_for_gemini(history_copy)
|
|
|
313 |
content = response_text if response_text and "Error:" not in response_text else "Could not generate the response."
|
314 |
|
315 |
system_msg = {"role": role, "content": content}
|
316 |
+
|
317 |
with history_lock:
|
318 |
chat_histories[channel].append(system_msg)
|
319 |
+
|
320 |
+
state = roundtable_states[channel]
|
321 |
+
title = "Conversation Summary" if is_summary else "Gemini's Opinion"
|
322 |
+
|
323 |
+
roundtable_text = f"**{title}**:\n\n{clean_html_for_llm(content)}"
|
324 |
+
roundtable_msg = {"speaker": "Gemini", "text": roundtable_text}
|
325 |
+
|
326 |
+
state["messages"].append(roundtable_msg)
|
327 |
+
|
328 |
+
if "Gemini" not in state["showBubbles"]:
|
329 |
+
state["showBubbles"].append("Gemini")
|
330 |
+
if len(state["showBubbles"]) > 4:
|
331 |
+
state["showBubbles"] = state["showBubbles"][-4:]
|
332 |
+
|
333 |
+
state["currentSpeaker"] = None
|
334 |
+
if "thinking" in state and "Gemini" in state["thinking"]:
|
335 |
+
state["thinking"].remove("Gemini")
|
336 |
+
|
337 |
+
roundtable_json = json.dumps(state)
|
338 |
+
|
339 |
return chat_histories.get(channel, []), roundtable_json
|
340 |
|
341 |
def format_history_for_chatbot_display(history: List[Dict]) -> List[Dict]:
|
342 |
"""Applies HTML formatting for gr.Chatbot display using the 'messages' format."""
|
343 |
formatted_history = []
|
344 |
+
for msg in history:
|
345 |
+
new_msg = msg.copy()
|
346 |
role, content, username = (
|
347 |
new_msg.get("role"),
|
348 |
new_msg.get("content", ""),
|
349 |
new_msg.get("username"),
|
350 |
)
|
351 |
+
|
352 |
+
if (role == "assistant" or role.startswith("system_")) and role != "system_join_leave":
|
353 |
+
display_role = "assistant"
|
354 |
+
else:
|
355 |
+
display_role = "user"
|
356 |
+
|
357 |
display_content = ""
|
358 |
|
359 |
if role == "user" and username:
|
|
|
378 |
f"<div style='background-color:#f8f9fa; border-left: 5px solid #ccc; padding: 10px; margin: 10px 0; border-radius: 5px;'>"
|
379 |
f"<b>{title}:</b><br>{response_content}</div>"
|
380 |
)
|
381 |
+
else:
|
382 |
display_content = content
|
383 |
|
384 |
if display_content:
|
|
|
487 |
exit_button = gr.Button("🚪 Exit Chat")
|
488 |
|
489 |
with gr.Column(scale=3):
|
490 |
+
view_switch = gr.Radio(["Roundtable", "Chat"], label="Chat View", value="Roundtable")
|
491 |
|
492 |
roundtable_display = consilium_roundtable(
|
493 |
label="🎭 Live Discussion Roundtable",
|
|
|
542 |
fn=send_message,
|
543 |
inputs=[channel_display, username_display, msg_input],
|
544 |
outputs=[unformatted_history_state, roundtable_state_json, chatbot_display, roundtable_display, msg_input],
|
545 |
+
show_progress=False,
|
546 |
api_name="send_message"
|
547 |
)
|
548 |
msg_input.submit(
|
549 |
fn=send_message,
|
550 |
inputs=[channel_display, username_display, msg_input],
|
551 |
outputs=[unformatted_history_state, roundtable_state_json, chatbot_display, roundtable_display, msg_input],
|
552 |
+
show_progress=False,
|
553 |
api_name="send_message_submit"
|
554 |
)
|
555 |
|
|
|
567 |
).then(
|
568 |
fn=format_all_views_from_state,
|
569 |
inputs=[unformatted_history_state, roundtable_state_json],
|
570 |
+
show_progress=False,
|
571 |
outputs=[chatbot_display, roundtable_display]
|
572 |
)
|
573 |
|
|
|
580 |
).then(
|
581 |
fn=format_all_views_from_state,
|
582 |
inputs=[unformatted_history_state, roundtable_state_json],
|
583 |
+
show_progress=False,
|
584 |
outputs=[chatbot_display, roundtable_display]
|
585 |
)
|
586 |
|