Spaces:
Sleeping
Sleeping
File size: 3,993 Bytes
b0205d9 25e5c12 b0205d9 61a2221 b0205d9 61a2221 b0205d9 61a2221 b0205d9 61a2221 b0205d9 25e5c12 b0205d9 25e5c12 b0205d9 25e5c12 15ffc1b 61a2221 15ffc1b b0205d9 25e5c12 15ffc1b 25e5c12 b0205d9 25e5c12 b0205d9 25e5c12 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
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)
|