Update space
Browse files
app.py
CHANGED
@@ -111,6 +111,25 @@ def prepare_topic_action(topic_index, chatbot, system_message):
|
|
111 |
print(f"Error in prepare_topic_action: {e}")
|
112 |
return chatbot
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
# Gradio app
|
115 |
with gr.Blocks() as demo:
|
116 |
with gr.Row():
|
@@ -153,16 +172,16 @@ with gr.Blocks() as demo:
|
|
153 |
|
154 |
# Submit button handler
|
155 |
submit.click(
|
156 |
-
fn=
|
157 |
-
inputs=[chatbot],
|
158 |
-
outputs=[chatbot]
|
159 |
).then(
|
160 |
fn=lambda: "", # Clear input textbox
|
161 |
-
outputs=[msg]
|
162 |
).then(
|
163 |
-
fn=
|
164 |
inputs=[chatbot, system_message],
|
165 |
-
outputs=[chatbot]
|
166 |
)
|
167 |
|
168 |
# Clear button handler
|
|
|
111 |
print(f"Error in prepare_topic_action: {e}")
|
112 |
return chatbot
|
113 |
|
114 |
+
def add_user_message(chatbot, message):
|
115 |
+
if message:
|
116 |
+
return chatbot + [(message, None)]
|
117 |
+
return chatbot
|
118 |
+
|
119 |
+
def generate_response(chatbot, system_message):
|
120 |
+
# Generate response for the last user message
|
121 |
+
if chatbot and chatbot[-1][1] is None:
|
122 |
+
last_user_message = chatbot[-1][0]
|
123 |
+
response_generator = respond(last_user_message, chatbot[:-1], system_message)
|
124 |
+
full_response = ""
|
125 |
+
for chunk in response_generator:
|
126 |
+
full_response = chunk
|
127 |
+
|
128 |
+
# Update the last message with the full response
|
129 |
+
chatbot[-1] = (chatbot[-1][0], full_response)
|
130 |
+
|
131 |
+
return chatbot
|
132 |
+
|
133 |
# Gradio app
|
134 |
with gr.Blocks() as demo:
|
135 |
with gr.Row():
|
|
|
172 |
|
173 |
# Submit button handler
|
174 |
submit.click(
|
175 |
+
fn=add_user_message,
|
176 |
+
inputs=[chatbot, msg],
|
177 |
+
outputs=[chatbot]
|
178 |
).then(
|
179 |
fn=lambda: "", # Clear input textbox
|
180 |
+
outputs=[msg]
|
181 |
).then(
|
182 |
+
fn=generate_response,
|
183 |
inputs=[chatbot, system_message],
|
184 |
+
outputs=[chatbot]
|
185 |
)
|
186 |
|
187 |
# Clear button handler
|