alarv commited on
Commit
fd4294b
·
1 Parent(s): d0c71ea

fix: add thinking when agent is not responding

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +28 -4
Gradio_UI.py CHANGED
@@ -192,12 +192,36 @@ class GradioUI:
192
  def interact_with_agent(self, prompt, messages):
193
  import gradio as gr
194
 
 
195
  messages.append(gr.ChatMessage(role="user", content=prompt))
 
 
 
196
  yield messages
197
- for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=True):
198
- messages.append(msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  yield messages
200
- yield messages
201
 
202
  def upload_file(
203
  self,
@@ -337,7 +361,7 @@ class GradioUI:
337
  <p class=\"support\"><strong>Supported by:</strong> This work is supported by the <a href=\"https://pink-project.eu/\" target=\"_blank\">PINK Project</a> - a Horizon Europe initiative for Safe-and-Sustainable-by-Design computational approaches for chemicals and materials.</p>
338
  </div>
339
  """)
340
-
341
  stored_messages = gr.State([])
342
  file_uploads_log = gr.State([])
343
  chatbot = gr.Chatbot(
 
192
  def interact_with_agent(self, prompt, messages):
193
  import gradio as gr
194
 
195
+ # Add user message
196
  messages.append(gr.ChatMessage(role="user", content=prompt))
197
+ # Add a temporary assistant placeholder to indicate the model is working
198
+ thinking_msg = gr.ChatMessage(role="assistant", content="⏳ Thinking…")
199
+ messages.append(thinking_msg)
200
  yield messages
201
+
202
+ placeholder_active = True
203
+ try:
204
+ for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=True):
205
+ if placeholder_active:
206
+ # Replace the placeholder with the first real assistant event
207
+ messages[-1] = msg
208
+ placeholder_active = False
209
+ else:
210
+ messages.append(msg)
211
+ yield messages
212
+ except Exception as e:
213
+ # Replace the placeholder with an error message if something goes wrong
214
+ error_msg = gr.ChatMessage(role="assistant", content=f"💥 Error: {e}")
215
+ if placeholder_active:
216
+ messages[-1] = error_msg
217
+ else:
218
+ messages.append(error_msg)
219
+ yield messages
220
+ finally:
221
+ # Ensure no dangling placeholder remains
222
+ if placeholder_active and messages and messages[-1] is thinking_msg:
223
+ messages.pop()
224
  yield messages
 
225
 
226
  def upload_file(
227
  self,
 
361
  <p class=\"support\"><strong>Supported by:</strong> This work is supported by the <a href=\"https://pink-project.eu/\" target=\"_blank\">PINK Project</a> - a Horizon Europe initiative for Safe-and-Sustainable-by-Design computational approaches for chemicals and materials.</p>
362
  </div>
363
  """)
364
+
365
  stored_messages = gr.State([])
366
  file_uploads_log = gr.State([])
367
  chatbot = gr.Chatbot(