Adibvafa commited on
Commit
605cfb5
·
1 Parent(s): 465688a

Fix bugs of interface

Browse files
Files changed (1) hide show
  1. interface.py +12 -19
interface.py CHANGED
@@ -191,7 +191,6 @@ class ChatInterface:
191
  tool_name = pending_call["name"]
192
  tool_args = pending_call["args"]
193
 
194
- # First, display the tool usage card
195
  try:
196
  # Handle case where tool returns tuple (output, metadata)
197
  content = msg.content
@@ -201,9 +200,11 @@ class ChatInterface:
201
  tool_output_str = json.dumps(tool_output_json, indent=2)
202
  except (json.JSONDecodeError, TypeError):
203
  tool_output_str = str(msg.content)
204
-
205
  tool_args_str = json.dumps(tool_args, indent=2)
 
206
  description = f"**Input:**\n```json\n{tool_args_str}\n```\n\n**Output:**\n```json\n{tool_output_str}\n```"
 
207
  metadata = {
208
  "title": f"⚒️ Tool: {tool_name}",
209
  "description": description,
@@ -216,8 +217,8 @@ class ChatInterface:
216
  metadata=metadata,
217
  )
218
  )
 
219
 
220
- # Special handling for image_visualizer
221
  if tool_name == "image_visualizer":
222
  try:
223
  # Handle case where tool returns tuple (output, metadata)
@@ -233,12 +234,9 @@ class ChatInterface:
233
  content={"path": self.display_file_path},
234
  )
235
  )
236
- except Exception:
 
237
  pass
238
-
239
- # Yield a single update for this tool event
240
- yield chat_history, self.display_file_path, ""
241
-
242
 
243
  except Exception as e:
244
  chat_history.append(
@@ -275,7 +273,7 @@ def create_demo(agent, tools_dict):
275
  with gr.Column(scale=5):
276
  chatbot = gr.Chatbot(
277
  [],
278
- height=1200,
279
  container=True,
280
  show_label=True,
281
  elem_classes="chat-box",
@@ -308,18 +306,14 @@ def create_demo(agent, tools_dict):
308
  file_types=["file"],
309
  )
310
  with gr.Row():
311
- clear_btn = gr.Button("Clear Chat")
312
- new_thread_btn = gr.Button("New Thread")
313
 
314
  # Event handlers
315
- def clear_chat():
316
  interface.original_file_path = None
317
  interface.display_file_path = None
318
- return [], None
319
-
320
- def new_thread():
321
  interface.current_thread_id = str(time.time())
322
- return [], interface.display_file_path
323
 
324
  def handle_file_upload(file):
325
  return interface.handle_upload(file.name)
@@ -338,7 +332,6 @@ def create_demo(agent, tools_dict):
338
 
339
  dicom_upload.upload(handle_file_upload, inputs=dicom_upload, outputs=image_display)
340
 
341
- clear_btn.click(clear_chat, outputs=[chatbot, image_display])
342
- new_thread_btn.click(new_thread, outputs=[chatbot, image_display])
343
 
344
- return demo
 
191
  tool_name = pending_call["name"]
192
  tool_args = pending_call["args"]
193
 
 
194
  try:
195
  # Handle case where tool returns tuple (output, metadata)
196
  content = msg.content
 
200
  tool_output_str = json.dumps(tool_output_json, indent=2)
201
  except (json.JSONDecodeError, TypeError):
202
  tool_output_str = str(msg.content)
203
+
204
  tool_args_str = json.dumps(tool_args, indent=2)
205
+
206
  description = f"**Input:**\n```json\n{tool_args_str}\n```\n\n**Output:**\n```json\n{tool_output_str}\n```"
207
+
208
  metadata = {
209
  "title": f"⚒️ Tool: {tool_name}",
210
  "description": description,
 
217
  metadata=metadata,
218
  )
219
  )
220
+ yield chat_history, self.display_file_path, ""
221
 
 
222
  if tool_name == "image_visualizer":
223
  try:
224
  # Handle case where tool returns tuple (output, metadata)
 
234
  content={"path": self.display_file_path},
235
  )
236
  )
237
+ yield chat_history, self.display_file_path, ""
238
+ except (json.JSONDecodeError, TypeError):
239
  pass
 
 
 
 
240
 
241
  except Exception as e:
242
  chat_history.append(
 
273
  with gr.Column(scale=5):
274
  chatbot = gr.Chatbot(
275
  [],
276
+ height=1000,
277
  container=True,
278
  show_label=True,
279
  elem_classes="chat-box",
 
306
  file_types=["file"],
307
  )
308
  with gr.Row():
309
+ new_chat_btn = gr.Button("New Chat")
 
310
 
311
  # Event handlers
312
+ def new_chat():
313
  interface.original_file_path = None
314
  interface.display_file_path = None
 
 
 
315
  interface.current_thread_id = str(time.time())
316
+ return [], None
317
 
318
  def handle_file_upload(file):
319
  return interface.handle_upload(file.name)
 
332
 
333
  dicom_upload.upload(handle_file_upload, inputs=dicom_upload, outputs=image_display)
334
 
335
+ new_chat_btn.click(new_chat, outputs=[chatbot, image_display])
 
336
 
337
+ return demo