Spaces:
Sleeping
Sleeping
import gradio as gr | |
import time | |
def bot_response_one(message, history): | |
history = history or [] | |
# Regular message without metadata | |
msg = gr.ChatMessage( | |
role="assistant", | |
content="Let me help you analyze that..." | |
) | |
history.append(msg) | |
yield history | |
time.sleep(1) | |
# Tool execution with metadata | |
parent_id = "tool_call_1" | |
tool_msg = gr.ChatMessage( | |
role="assistant", | |
content="```python\nprint('Running analysis...')\n```", | |
metadata={ | |
"title": "π οΈ Using Python Interpreter", | |
"id": parent_id, | |
"status": "pending" | |
} | |
) | |
history.append(tool_msg) | |
yield history | |
time.sleep(1) | |
# Regular message showing progress | |
progress_msg = gr.ChatMessage( | |
role="assistant", | |
content="Processing your request..." | |
) | |
history.append(progress_msg) | |
yield history | |
time.sleep(1) | |
# Execution result with metadata | |
execution_msg = gr.ChatMessage( | |
role="assistant", | |
content="Output: Analysis complete", | |
metadata={ | |
"title": "π Execution Result", | |
"parent_id": parent_id, | |
"status": "pending" | |
} | |
) | |
history.append(execution_msg) | |
yield history | |
time.sleep(1) | |
# Update tool messages to done | |
tool_msg.metadata["status"] = "done" | |
execution_msg.metadata["status"] = "done" | |
yield history | |
# Final message without metadata | |
final_msg = gr.ChatMessage( | |
role="assistant", | |
content="Based on the analysis, I can confirm the process completed successfully." | |
) | |
history.append(final_msg) | |
yield history | |
def bot_response_two(message, history): | |
history = history or [] | |
messages = [] | |
# First message | |
messages.append({"role": "assistant", "content": "Thinking..."}) | |
history.extend(messages) | |
yield history | |
time.sleep(1) | |
# Second message | |
messages = [{"role": "assistant", "content": "This is the part 1 of bot response"}] | |
history.extend(messages) | |
yield history | |
time.sleep(1) | |
# Third message | |
messages = [{"role": "assistant", "content": "And this is part 2"}] | |
history.extend(messages) | |
yield history | |
time.sleep(1) | |
# Fourth message | |
messages = [{"role": "assistant", "content": "LAstly, some code:\n```python\nprint('hello world')\n```"}] | |
history.extend(messages) | |
yield history | |
with gr.Blocks(fill_height=True) as demo: | |
with gr.Tab("One"): | |
chatbot1 = gr.Chatbot( | |
scale=1, | |
group_consecutive_messages=False, | |
type="messages", | |
bubble_full_width=False | |
) | |
msg1 = gr.Textbox() | |
clear1 = gr.Button("Clear") | |
with gr.Tab("Two"): | |
chatbot2 = gr.Chatbot( | |
scale=1, | |
group_consecutive_messages=False, | |
type="messages" | |
) | |
msg2 = gr.Textbox() | |
clear2 = gr.Button("Clear") | |
def user_two(user_message, history): | |
# Only add user message if it's not empty | |
if user_message.strip(): | |
new_history = history + [{"role": "user", "content": user_message}] | |
return "", new_history | |
return "", history | |
msg2.submit( | |
user_two, | |
[msg2, chatbot2], | |
[msg2, chatbot2] | |
).then( | |
bot_response_two, | |
[msg2, chatbot2], | |
chatbot2 | |
) | |
clear2.click(lambda: None, None, chatbot2, queue=False) | |
def user_one(user_message, history): | |
if user_message.strip(): | |
history.append(gr.ChatMessage(role="user", content=user_message)) | |
return "", history | |
return "", history | |
msg1.submit( | |
user_one, | |
[msg1, chatbot1], | |
[msg1, chatbot1] | |
).then( | |
bot_response_one, | |
[msg1, chatbot1], | |
chatbot1 | |
) | |
clear1.click(lambda: None, None, chatbot1, queue=False) | |
demo.queue() | |
demo.launch(debug=True) | |