Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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 |
+
messages.append({"role": "user", "content": message})
|
10 |
+
messages.append({"role": "assistant", "content": "I'm thinking about your message..."})
|
11 |
+
history.extend(messages)
|
12 |
+
yield history
|
13 |
+
time.sleep(1)
|
14 |
+
|
15 |
+
# Second message
|
16 |
+
messages = [{"role": "assistant", "content": "Here's part 1 of my response"}]
|
17 |
+
history.extend(messages)
|
18 |
+
yield history
|
19 |
+
time.sleep(1)
|
20 |
+
|
21 |
+
# Third message
|
22 |
+
messages = [{"role": "assistant", "content": "And here's part 2"}]
|
23 |
+
history.extend(messages)
|
24 |
+
yield history
|
25 |
+
time.sleep(1)
|
26 |
+
|
27 |
+
# Fourth message
|
28 |
+
messages = [{"role": "assistant", "content": "Here's some code:\n```python\nprint('hello world')\n```"}]
|
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 |
+
)
|
37 |
+
msg = gr.Textbox()
|
38 |
+
clear = gr.Button("Clear")
|
39 |
+
|
40 |
+
def user(user_message, history):
|
41 |
+
return "", history + [{"role": "user", "content": user_message}]
|
42 |
+
|
43 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot]).then(
|
44 |
+
bot_response, [msg, chatbot], chatbot
|
45 |
+
)
|
46 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
47 |
+
|
48 |
+
demo.queue()
|
49 |
+
demo.launch(debug=True)
|