Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import time
|
3 |
|
4 |
-
def
|
5 |
print("\n=== Bot Response Function ===")
|
6 |
print(f"Received message: {message}")
|
7 |
|
@@ -67,17 +67,73 @@ def bot_response(message, history):
|
|
67 |
history.append(final_msg)
|
68 |
yield history
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
)
|
77 |
-
|
78 |
-
clear = gr.Button("Clear")
|
79 |
|
80 |
-
|
|
|
81 |
print("\n=== User Message Function ===")
|
82 |
print(f"User message: {user_message}")
|
83 |
|
@@ -86,17 +142,16 @@ with gr.Blocks() as demo:
|
|
86 |
return "", history
|
87 |
return "", history
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
[
|
92 |
-
[
|
93 |
).then(
|
94 |
-
|
95 |
-
[
|
96 |
-
|
97 |
)
|
98 |
-
|
99 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
100 |
|
101 |
-
|
102 |
-
demo.launch(debug=True)
|
|
|
1 |
import gradio as gr
|
2 |
import time
|
3 |
|
4 |
+
def bot_response_one(message, history):
|
5 |
print("\n=== Bot Response Function ===")
|
6 |
print(f"Received message: {message}")
|
7 |
|
|
|
67 |
history.append(final_msg)
|
68 |
yield history
|
69 |
|
70 |
+
def bot_response_two(message, history):
|
71 |
+
history = history or []
|
72 |
+
messages = []
|
73 |
+
|
74 |
+
# First message
|
75 |
+
messages.append({"role": "assistant", "content": "Thinking..."})
|
76 |
+
history.extend(messages)
|
77 |
+
yield history
|
78 |
+
time.sleep(1)
|
79 |
+
|
80 |
+
# Second message
|
81 |
+
messages = [{"role": "assistant", "content": "This is the part 1 of bot response"}]
|
82 |
+
history.extend(messages)
|
83 |
+
yield history
|
84 |
+
time.sleep(1)
|
85 |
+
|
86 |
+
# Third message
|
87 |
+
messages = [{"role": "assistant", "content": "And this is part 2"}]
|
88 |
+
history.extend(messages)
|
89 |
+
yield history
|
90 |
+
time.sleep(1)
|
91 |
+
|
92 |
+
# Fourth message
|
93 |
+
messages = [{"role": "assistant", "content": "LAstly, some code:\n```python\nprint('hello world')\n```"}]
|
94 |
+
history.extend(messages)
|
95 |
+
yield history
|
96 |
+
|
97 |
+
with gr.Blocks(fill_height=True) as demo:
|
98 |
+
with gr.Tab("One"):
|
99 |
+
chatbot1 = gr.Chatbot(
|
100 |
+
scale=1,
|
101 |
+
group_consecutive_messages=False,
|
102 |
+
type="messages",
|
103 |
+
bubble_full_width=False
|
104 |
+
)
|
105 |
+
msg1 = gr.Textbox()
|
106 |
+
clear1 = gr.Button("Clear")
|
107 |
+
|
108 |
+
with gr.Tab("Two"):
|
109 |
+
chatbot2 = gr.Chatbot(
|
110 |
+
scale=1,
|
111 |
+
group_consecutive_messages=False,
|
112 |
+
type="messages"
|
113 |
+
)
|
114 |
+
msg2 = gr.Textbox()
|
115 |
+
clear2 = gr.Button("Clear")
|
116 |
+
|
117 |
+
def user_two(user_message, history):
|
118 |
+
# Only add user message if it's not empty
|
119 |
+
if user_message.strip():
|
120 |
+
new_history = history + [{"role": "user", "content": user_message}]
|
121 |
+
return "", new_history
|
122 |
+
return "", history
|
123 |
+
|
124 |
+
msg2.submit(
|
125 |
+
user_two,
|
126 |
+
[msg2, chatbot2],
|
127 |
+
[msg2, chatbot2]
|
128 |
+
).then(
|
129 |
+
bot_response_two,
|
130 |
+
[msg2, chatbot2],
|
131 |
+
chatbot2
|
132 |
)
|
133 |
+
clear2.click(lambda: None, None, chatbot2, queue=False)
|
|
|
134 |
|
135 |
+
|
136 |
+
def user_one(user_message, history):
|
137 |
print("\n=== User Message Function ===")
|
138 |
print(f"User message: {user_message}")
|
139 |
|
|
|
142 |
return "", history
|
143 |
return "", history
|
144 |
|
145 |
+
msg1.submit(
|
146 |
+
user_one,
|
147 |
+
[msg1, chatbot1],
|
148 |
+
[msg1, chatbot1]
|
149 |
).then(
|
150 |
+
bot_response_one,
|
151 |
+
[msg1, chatbot1],
|
152 |
+
chatbot1
|
153 |
)
|
154 |
+
clear1.click(lambda: None, None, chatbot1, queue=False)
|
|
|
155 |
|
156 |
+
demo.queue()
|
157 |
+
demo.launch(debug=True)
|