ErenalpCet commited on
Commit
67e28db
·
verified ·
1 Parent(s): 8cff666

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -292,10 +292,11 @@ class PersonaChat:
292
  yield status, self.enhanced_profile, self.enhanced_profile, []
293
  self.system_prompt = generate_system_prompt_with_llm(name, profile_for_prompt, context)
294
  # Clean tokenizer artifacts from system prompt
295
- self.system_prompt = re.sub(r'<\|im_end\|>|<\|im_start\|>|<think>.*?</think>', '', self.system_prompt).strip()
296
  self.messages = [{"role": "system", "content": self.system_prompt}]
297
- print(f"set_persona: Final yield with messages: {self.messages}")
298
- yield f"Persona set to '{name}'. Ready to chat!", self.system_prompt, self.enhanced_profile, self.messages
 
299
  except Exception as e:
300
  error_msg = f"An unexpected error occurred during persona setup: {str(e)}"
301
  print(f"set_persona: Yielding exception: {error_msg}")
@@ -382,25 +383,25 @@ def create_interface():
382
  initial_profile = "Enhanced profile will appear here..."
383
  initial_history = []
384
  print(f"set_persona_flow: Starting with initial history: {initial_history}")
385
- # Log initial chatbot state
386
  print(f"set_persona_flow: Initial chatbot state: {chatbot.value if hasattr(chatbot, 'value') else 'Unknown'}")
387
  yield initial_status, initial_prompt, initial_profile, initial_character_display, initial_history
388
  final_status, final_prompt, final_profile, final_character_display, final_history = "Error", "", "", f"### Error creating {name}", []
389
  try:
390
  for status_update, prompt_update, profile_update, history_update in persona_chat.set_persona(name, context):
391
- # Filter history to ensure valid messages
392
  filtered_history = [
393
  msg for msg in (history_update or [])
394
  if isinstance(msg, dict) and "content" in msg and isinstance(msg["content"], str) and msg["content"]
395
  ]
396
- # Check chatbot state for stale messages
397
  current_chatbot_state = chatbot.value if hasattr(chatbot, 'value') else []
398
  print(f"set_persona_flow: Current chatbot state: {current_chatbot_state}")
399
- # Combine filtered history with cleaned chatbot state
400
  cleaned_chatbot_state = [
401
  msg for msg in current_chatbot_state
402
  if isinstance(msg, dict) and "content" in msg and isinstance(msg["content"], str) and msg["content"]
403
  ]
 
404
  combined_history = cleaned_chatbot_state + filtered_history
405
  print(f"set_persona_flow: Processing yield - status: {status_update}, filtered_history: {filtered_history}, combined_history: {combined_history}")
406
  final_status = status_update
@@ -428,11 +429,9 @@ def create_interface():
428
  history.append({"role": "assistant", "content": "Error: Please create a valid persona first."})
429
  return "", history
430
  history.append({"role": "user", "content": message})
431
- history.append({"role": "assistant", "content": None})
432
- yield "", history
433
  response_text = persona_chat.chat(message)
434
- history[-1]["content"] = response_text
435
- yield "", history
436
  set_persona_button.click(
437
  set_persona_flow,
438
  inputs=[name_input, context_input],
 
292
  yield status, self.enhanced_profile, self.enhanced_profile, []
293
  self.system_prompt = generate_system_prompt_with_llm(name, profile_for_prompt, context)
294
  # Clean tokenizer artifacts from system prompt
295
+ self.system_prompt = re.sub(r'<\|im_end\|>|<\|im_start\|>|<think>.*?</think>|^assistant\s*', '', self.system_prompt).strip()
296
  self.messages = [{"role": "system", "content": self.system_prompt}]
297
+ print(f"set_persona: Final yield with messages (not sent to Chatbot): {self.messages}")
298
+ # Yield empty history for Chatbot to avoid system message issues
299
+ yield f"Persona set to '{name}'. Ready to chat!", self.system_prompt, self.enhanced_profile, []
300
  except Exception as e:
301
  error_msg = f"An unexpected error occurred during persona setup: {str(e)}"
302
  print(f"set_persona: Yielding exception: {error_msg}")
 
383
  initial_profile = "Enhanced profile will appear here..."
384
  initial_history = []
385
  print(f"set_persona_flow: Starting with initial history: {initial_history}")
 
386
  print(f"set_persona_flow: Initial chatbot state: {chatbot.value if hasattr(chatbot, 'value') else 'Unknown'}")
387
  yield initial_status, initial_prompt, initial_profile, initial_character_display, initial_history
388
  final_status, final_prompt, final_profile, final_character_display, final_history = "Error", "", "", f"### Error creating {name}", []
389
  try:
390
  for status_update, prompt_update, profile_update, history_update in persona_chat.set_persona(name, context):
391
+ # Strict validation of history
392
  filtered_history = [
393
  msg for msg in (history_update or [])
394
  if isinstance(msg, dict) and "content" in msg and isinstance(msg["content"], str) and msg["content"]
395
  ]
396
+ # Log chatbot state
397
  current_chatbot_state = chatbot.value if hasattr(chatbot, 'value') else []
398
  print(f"set_persona_flow: Current chatbot state: {current_chatbot_state}")
399
+ # Clean and validate chatbot state
400
  cleaned_chatbot_state = [
401
  msg for msg in current_chatbot_state
402
  if isinstance(msg, dict) and "content" in msg and isinstance(msg["content"], str) and msg["content"]
403
  ]
404
+ # Combine histories
405
  combined_history = cleaned_chatbot_state + filtered_history
406
  print(f"set_persona_flow: Processing yield - status: {status_update}, filtered_history: {filtered_history}, combined_history: {combined_history}")
407
  final_status = status_update
 
429
  history.append({"role": "assistant", "content": "Error: Please create a valid persona first."})
430
  return "", history
431
  history.append({"role": "user", "content": message})
 
 
432
  response_text = persona_chat.chat(message)
433
+ history.append({"role": "assistant", "content": response_text})
434
+ return "", history
435
  set_persona_button.click(
436
  set_persona_flow,
437
  inputs=[name_input, context_input],