Spaces:
Sleeping
Sleeping
import gradio as gr | |
import time | |
def bot_response(message, history): | |
print("\n=== Bot Response Function ===") | |
print(f"Received message: {message}") | |
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 | |
with gr.Blocks() as demo: | |
chatbot = gr.Chatbot( | |
height=600, | |
group_consecutive_messages=False, | |
type="messages", | |
bubble_full_width=False | |
) | |
msg = gr.Textbox() | |
clear = gr.Button("Clear") | |
def user(user_message, history): | |
print("\n=== User Message Function ===") | |
print(f"User message: {user_message}") | |
if user_message.strip(): | |
history.append(gr.ChatMessage(role="user", content=user_message)) | |
return "", history | |
return "", history | |
msg.submit( | |
user, | |
[msg, chatbot], | |
[msg, chatbot] | |
).then( | |
bot_response, | |
[msg, chatbot], | |
chatbot | |
) | |
clear.click(lambda: None, None, chatbot, queue=False) | |
#demo.queue() | |
demo.launch(debug=True) |