Spaces:
Sleeping
Sleeping
import gradio as gr | |
import time | |
def bot_response(message, history): | |
print("\n=== Bot Response Function ===") | |
print(f"Received message: {message}") | |
print(f"Initial history: {history}") | |
history = history or [] | |
messages = [] | |
# First message | |
print("\nAdding first message pair...") | |
messages.append({"role": "assistant", "content": "Thinking..."}) | |
history.extend(messages) | |
print(f"History after first message: {history}") | |
yield history | |
time.sleep(1) | |
# Second message | |
print("\nAdding second message...") | |
messages = [{"role": "assistant", "content": "This is the part 1 of bot response"}] | |
history.extend(messages) | |
print(f"History after second message: {history}") | |
yield history | |
time.sleep(1) | |
# Third message | |
print("\nAdding third message...") | |
messages = [{"role": "assistant", "content": "And this is part 2"}] | |
history.extend(messages) | |
print(f"History after third message: {history}") | |
yield history | |
time.sleep(1) | |
# Fourth message | |
print("\nAdding fourth message...") | |
messages = [{"role": "assistant", "content": "LAstly, some code:\n```python\nprint('hello world')\n```"}] | |
history.extend(messages) | |
print(f"History after fourth message: {history}") | |
yield history | |
with gr.Blocks() as demo: | |
chatbot = gr.Chatbot( | |
height=600, | |
group_consecutive_messages=False, | |
type="messages" | |
) | |
msg = gr.Textbox() | |
clear = gr.Button("Clear") | |
def user(user_message, history): | |
print("\n=== User Message Function ===") | |
print(f"User message: {user_message}") | |
print(f"Current history: {history}") | |
# Only add user message if it's not empty | |
if user_message.strip(): | |
new_history = history + [{"role": "user", "content": user_message}] | |
print(f"New history after adding user message: {new_history}") | |
return "", new_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) |