Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,35 +2,47 @@ import gradio as gr
|
|
2 |
import time
|
3 |
|
4 |
def bot_response(message, history):
|
|
|
|
|
|
|
|
|
5 |
history = history or []
|
6 |
messages = []
|
7 |
|
8 |
# First message
|
9 |
-
|
10 |
-
messages.append({"role": "assistant", "content": "
|
11 |
history.extend(messages)
|
|
|
12 |
yield history
|
13 |
time.sleep(1)
|
14 |
|
15 |
# Second message
|
16 |
-
|
|
|
17 |
history.extend(messages)
|
|
|
18 |
yield history
|
19 |
time.sleep(1)
|
20 |
|
21 |
# Third message
|
22 |
-
|
|
|
23 |
history.extend(messages)
|
|
|
24 |
yield history
|
25 |
time.sleep(1)
|
26 |
|
27 |
# Fourth message
|
28 |
-
|
|
|
29 |
history.extend(messages)
|
|
|
30 |
yield history
|
31 |
|
32 |
with gr.Blocks() as demo:
|
33 |
chatbot = gr.Chatbot(
|
|
|
34 |
group_consecutive_messages=False,
|
35 |
type="messages"
|
36 |
)
|
@@ -38,12 +50,28 @@ with gr.Blocks() as demo:
|
|
38 |
clear = gr.Button("Clear")
|
39 |
|
40 |
def user(user_message, history):
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
msg.submit(
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
)
|
|
|
46 |
clear.click(lambda: None, None, chatbot, queue=False)
|
47 |
|
48 |
-
demo.queue()
|
49 |
demo.launch(debug=True)
|
|
|
2 |
import time
|
3 |
|
4 |
def bot_response(message, history):
|
5 |
+
print("\n=== Bot Response Function ===")
|
6 |
+
print(f"Received message: {message}")
|
7 |
+
print(f"Initial history: {history}")
|
8 |
+
|
9 |
history = history or []
|
10 |
messages = []
|
11 |
|
12 |
# First message
|
13 |
+
print("\nAdding first message pair...")
|
14 |
+
messages.append({"role": "assistant", "content": "Thinking..."})
|
15 |
history.extend(messages)
|
16 |
+
print(f"History after first message: {history}")
|
17 |
yield history
|
18 |
time.sleep(1)
|
19 |
|
20 |
# Second message
|
21 |
+
print("\nAdding second message...")
|
22 |
+
messages = [{"role": "assistant", "content": "This is the part 1 of bot response"}]
|
23 |
history.extend(messages)
|
24 |
+
print(f"History after second message: {history}")
|
25 |
yield history
|
26 |
time.sleep(1)
|
27 |
|
28 |
# Third message
|
29 |
+
print("\nAdding third message...")
|
30 |
+
messages = [{"role": "assistant", "content": "And this is part 2"}]
|
31 |
history.extend(messages)
|
32 |
+
print(f"History after third message: {history}")
|
33 |
yield history
|
34 |
time.sleep(1)
|
35 |
|
36 |
# Fourth message
|
37 |
+
print("\nAdding fourth message...")
|
38 |
+
messages = [{"role": "assistant", "content": "LAstly, some code:\n```python\nprint('hello world')\n```"}]
|
39 |
history.extend(messages)
|
40 |
+
print(f"History after fourth message: {history}")
|
41 |
yield history
|
42 |
|
43 |
with gr.Blocks() as demo:
|
44 |
chatbot = gr.Chatbot(
|
45 |
+
height=600,
|
46 |
group_consecutive_messages=False,
|
47 |
type="messages"
|
48 |
)
|
|
|
50 |
clear = gr.Button("Clear")
|
51 |
|
52 |
def user(user_message, history):
|
53 |
+
print("\n=== User Message Function ===")
|
54 |
+
print(f"User message: {user_message}")
|
55 |
+
print(f"Current history: {history}")
|
56 |
+
|
57 |
+
# Only add user message if it's not empty
|
58 |
+
if user_message.strip():
|
59 |
+
new_history = history + [{"role": "user", "content": user_message}]
|
60 |
+
print(f"New history after adding user message: {new_history}")
|
61 |
+
return "", new_history
|
62 |
+
return "", history
|
63 |
|
64 |
+
msg.submit(
|
65 |
+
user,
|
66 |
+
[msg, chatbot],
|
67 |
+
[msg, chatbot]
|
68 |
+
).then(
|
69 |
+
bot_response,
|
70 |
+
[msg, chatbot],
|
71 |
+
chatbot
|
72 |
)
|
73 |
+
|
74 |
clear.click(lambda: None, None, chatbot, queue=False)
|
75 |
|
76 |
+
#demo.queue()
|
77 |
demo.launch(debug=True)
|