jdesiree commited on
Commit
6980be0
·
verified ·
1 Parent(s): 9957683

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
app.py CHANGED
@@ -1323,7 +1323,7 @@ def remove_loading_animations(chat_history):
1323
  def add_user_message(message, chat_history, conversation_state):
1324
  """
1325
  Add user message with proper state management.
1326
- ✅ FIXED: Uses Gradio state instead of overwriting with global manager.
1327
  """
1328
  callback_start = log_step("add_user_message")
1329
 
@@ -1331,23 +1331,23 @@ def add_user_message(message, chat_history, conversation_state):
1331
  log_step("add_user_message", callback_start)
1332
  return "", chat_history, conversation_state
1333
 
1334
- # Add to both states
1335
- conversation_state.append({"role": "user", "content": message})
1336
- chat_history.append({"role": "user", "content": message})
1337
 
1338
  # Update global state for persistence
1339
- global_state_manager.update_conversation_state(chat_history, conversation_state)
1340
 
1341
  log_step("add_user_message", callback_start)
1342
 
1343
- # Return updated states to Gradio
1344
- return "", chat_history, conversation_state
1345
 
1346
 
1347
  def add_loading_animation(chat_history, conversation_state):
1348
  """
1349
  Add loading animation with proper state management.
1350
- ✅ FIXED: Uses Gradio state instead of overwriting.
1351
  """
1352
  callback_start = log_step("add_loading_animation")
1353
 
@@ -1356,24 +1356,24 @@ def add_loading_animation(chat_history, conversation_state):
1356
  return chat_history, conversation_state
1357
 
1358
  # Remove any existing loading animations
1359
- chat_history = remove_loading_animations(chat_history)
1360
 
1361
- # Add loading animation
1362
  gif_data = get_loading_animation_base64()
1363
  if gif_data:
1364
  loading_html = f'<div class="loading-animation" style="display: flex; align-items: center; justify-content: center; padding: 0.5px;"><img src="{gif_data}" alt="Thinking..." style="height: 64px; width: auto; max-width: 80px;" /></div>'
1365
  else:
1366
  loading_html = '<div class="loading-animation" style="display: flex; align-items: center; justify-content: center; padding: 0.5px;"><div style="width: 64px; height: 64px;"></div></div>'
1367
 
1368
- chat_history.append({"role": "assistant", "content": loading_html})
1369
 
1370
  # Update global state for persistence
1371
- global_state_manager.update_conversation_state(chat_history, conversation_state)
1372
 
1373
  log_step("add_loading_animation", callback_start)
1374
 
1375
- # Return updated states to Gradio
1376
- return chat_history, conversation_state
1377
 
1378
 
1379
  def generate_response(chat_history, conversation_state):
@@ -1420,24 +1420,28 @@ def generate_response(chat_history, conversation_state):
1420
  chat_history = remove_loading_animations(chat_history)
1421
  first_chunk = False
1422
 
1423
- # Update chat display
1424
  if chat_history and chat_history[-1]["role"] == "assistant":
1425
- chat_history[-1]["content"] = chunk
 
1426
  else:
1427
- chat_history.append({"role": "assistant", "content": chunk})
 
 
 
1428
 
1429
  # Yield to update UI during streaming
1430
  yield chat_history, conversation_state
1431
 
1432
- # Add final response to conversation state
1433
  final_response = chunk if 'chunk' in locals() else raw_response
1434
- conversation_state.append({"role": "assistant", "content": final_response})
1435
 
1436
  # Update global state with final conversation
1437
- global_state_manager.update_conversation_state(chat_history, conversation_state)
1438
 
1439
  # Final yield with complete states
1440
- yield chat_history, conversation_state
1441
 
1442
  except Exception as e:
1443
  logger.error(f"Response generation error: {e}")
@@ -1446,13 +1450,12 @@ def generate_response(chat_history, conversation_state):
1446
 
1447
  error_msg = f"I encountered an error: {str(e)}"
1448
 
1449
- # Clean up and show error
1450
- chat_history = remove_loading_animations(chat_history)
1451
- chat_history.append({"role": "assistant", "content": error_msg})
1452
- conversation_state.append({"role": "assistant", "content": error_msg})
1453
 
1454
- global_state_manager.update_conversation_state(chat_history, conversation_state)
1455
- yield chat_history, conversation_state
1456
 
1457
  log_step("generate_response", callback_start)
1458
 
 
1323
  def add_user_message(message, chat_history, conversation_state):
1324
  """
1325
  Add user message with proper state management.
1326
+ ✅ FIXED: Creates new lists to avoid reference issues with Gradio state.
1327
  """
1328
  callback_start = log_step("add_user_message")
1329
 
 
1331
  log_step("add_user_message", callback_start)
1332
  return "", chat_history, conversation_state
1333
 
1334
+ # Create new lists with the user message appended
1335
+ new_conversation_state = conversation_state + [{"role": "user", "content": message}]
1336
+ new_chat_history = chat_history + [{"role": "user", "content": message}]
1337
 
1338
  # Update global state for persistence
1339
+ global_state_manager.update_conversation_state(new_chat_history, new_conversation_state)
1340
 
1341
  log_step("add_user_message", callback_start)
1342
 
1343
+ # Return NEW states to Gradio
1344
+ return "", new_chat_history, new_conversation_state
1345
 
1346
 
1347
  def add_loading_animation(chat_history, conversation_state):
1348
  """
1349
  Add loading animation with proper state management.
1350
+ ✅ FIXED: Creates new lists to avoid reference issues.
1351
  """
1352
  callback_start = log_step("add_loading_animation")
1353
 
 
1356
  return chat_history, conversation_state
1357
 
1358
  # Remove any existing loading animations
1359
+ new_chat_history = remove_loading_animations(chat_history)
1360
 
1361
+ # Add loading animation to NEW list
1362
  gif_data = get_loading_animation_base64()
1363
  if gif_data:
1364
  loading_html = f'<div class="loading-animation" style="display: flex; align-items: center; justify-content: center; padding: 0.5px;"><img src="{gif_data}" alt="Thinking..." style="height: 64px; width: auto; max-width: 80px;" /></div>'
1365
  else:
1366
  loading_html = '<div class="loading-animation" style="display: flex; align-items: center; justify-content: center; padding: 0.5px;"><div style="width: 64px; height: 64px;"></div></div>'
1367
 
1368
+ new_chat_history = new_chat_history + [{"role": "assistant", "content": loading_html}]
1369
 
1370
  # Update global state for persistence
1371
+ global_state_manager.update_conversation_state(new_chat_history, conversation_state)
1372
 
1373
  log_step("add_loading_animation", callback_start)
1374
 
1375
+ # Return NEW states to Gradio
1376
+ return new_chat_history, conversation_state
1377
 
1378
 
1379
  def generate_response(chat_history, conversation_state):
 
1420
  chat_history = remove_loading_animations(chat_history)
1421
  first_chunk = False
1422
 
1423
+ # Update chat display - create new list for Gradio to detect change
1424
  if chat_history and chat_history[-1]["role"] == "assistant":
1425
+ # Update existing assistant message
1426
+ new_chat_history = chat_history[:-1] + [{"role": "assistant", "content": chunk}]
1427
  else:
1428
+ # Add new assistant message
1429
+ new_chat_history = chat_history + [{"role": "assistant", "content": chunk}]
1430
+
1431
+ chat_history = new_chat_history
1432
 
1433
  # Yield to update UI during streaming
1434
  yield chat_history, conversation_state
1435
 
1436
+ # Add final response to conversation state (create new list)
1437
  final_response = chunk if 'chunk' in locals() else raw_response
1438
+ new_conversation_state = conversation_state + [{"role": "assistant", "content": final_response}]
1439
 
1440
  # Update global state with final conversation
1441
+ global_state_manager.update_conversation_state(chat_history, new_conversation_state)
1442
 
1443
  # Final yield with complete states
1444
+ yield chat_history, new_conversation_state
1445
 
1446
  except Exception as e:
1447
  logger.error(f"Response generation error: {e}")
 
1450
 
1451
  error_msg = f"I encountered an error: {str(e)}"
1452
 
1453
+ # Clean up and show error (create new lists)
1454
+ new_chat_history = remove_loading_animations(chat_history) + [{"role": "assistant", "content": error_msg}]
1455
+ new_conversation_state = conversation_state + [{"role": "assistant", "content": error_msg}]
 
1456
 
1457
+ global_state_manager.update_conversation_state(new_chat_history, new_conversation_state)
1458
+ yield new_chat_history, new_conversation_state
1459
 
1460
  log_step("generate_response", callback_start)
1461