haepa_mac commited on
Commit
1b3095d
Β·
1 Parent(s): b721a79

πŸ”§ Fix critical runtime errors - datetime, chatbot format, download functions

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -652,7 +652,7 @@ def export_persona_to_json(persona):
652
 
653
  # 파일λͺ… 생성
654
  persona_name = persona_clean.get("기본정보", {}).get("이름", "persona")
655
- timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
656
  filename = f"{persona_name}_{timestamp}.json"
657
 
658
  # μž„μ‹œ 파일 μ €μž₯
@@ -691,14 +691,11 @@ def chat_with_loaded_persona(persona, user_message, chat_history=None, api_provi
691
  # API μ„€μ • μ—…λ°μ΄νŠΈ
692
  generator.set_api_config(api_provider, api_key)
693
 
694
- # λŒ€ν™” 기둝 λ³€ν™˜
695
  conversation_history = []
696
  for message in chat_history:
697
- if isinstance(message, tuple):
698
  conversation_history.append(message)
699
- else:
700
- conversation_history.append({"role": "user", "content": message[0]})
701
- conversation_history.append({"role": "assistant", "content": message[1]})
702
 
703
  # 🧠 μ„Έμ…˜ ID 생성 (페λ₯΄μ†Œλ‚˜ 이름 기반)
704
  persona_name = persona.get("기본정보", {}).get("이름", "μ•Œ 수 μ—†λŠ” 페λ₯΄μ†Œλ‚˜")
@@ -707,14 +704,16 @@ def chat_with_loaded_persona(persona, user_message, chat_history=None, api_provi
707
  # 페λ₯΄μ†Œλ‚˜μ™€ μ±„νŒ… (3단계 κΈ°μ–΅ μ‹œμŠ€ν…œ ν™œμš©)
708
  response = generator.chat_with_persona(persona, user_message, conversation_history, session_id)
709
 
710
- # μ±„νŒ… 기둝 μ—…λ°μ΄νŠΈ
711
- chat_history.append((user_message, response))
 
712
 
713
  return chat_history, ""
714
 
715
  except Exception as e:
716
  error_message = f"μ±„νŒ… 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
717
- chat_history.append((user_message, "μ•—, λ―Έμ•ˆν•΄... λ­”κ°€ λ¬Έμ œκ°€ 생긴 것 κ°™μ•„... πŸ˜…"))
 
718
  return chat_history, ""
719
 
720
  def import_persona_from_json(json_file):
@@ -897,9 +896,17 @@ def export_conversation_history():
897
  global persona_generator
898
  if persona_generator and hasattr(persona_generator, 'conversation_memory'):
899
  json_data = persona_generator.conversation_memory.export_to_json()
900
- timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
901
  filename = f"conversation_history_{timestamp}.json"
902
- return json_data, filename
 
 
 
 
 
 
 
 
903
  else:
904
  return None, "conversation_empty.json"
905
 
@@ -1387,9 +1394,9 @@ def create_main_interface():
1387
  # 이벀트 μ—°κ²°
1388
  conversation_export_btn.click(
1389
  export_conversation_history,
1390
- outputs=[conversation_download_file, conversation_download_file]
1391
  ).then(
1392
- lambda x: gr.update(visible=True) if x[0] else gr.update(visible=False),
1393
  inputs=[conversation_download_file],
1394
  outputs=[conversation_download_file]
1395
  )
 
652
 
653
  # 파일λͺ… 생성
654
  persona_name = persona_clean.get("기본정보", {}).get("이름", "persona")
655
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
656
  filename = f"{persona_name}_{timestamp}.json"
657
 
658
  # μž„μ‹œ 파일 μ €μž₯
 
691
  # API μ„€μ • μ—…λ°μ΄νŠΈ
692
  generator.set_api_config(api_provider, api_key)
693
 
694
+ # Gradio messages ν˜•μ‹μ—μ„œ λŒ€ν™” 기둝 λ³€ν™˜
695
  conversation_history = []
696
  for message in chat_history:
697
+ if isinstance(message, dict) and 'role' in message and 'content' in message:
698
  conversation_history.append(message)
 
 
 
699
 
700
  # 🧠 μ„Έμ…˜ ID 생성 (페λ₯΄μ†Œλ‚˜ 이름 기반)
701
  persona_name = persona.get("기본정보", {}).get("이름", "μ•Œ 수 μ—†λŠ” 페λ₯΄μ†Œλ‚˜")
 
704
  # 페λ₯΄μ†Œλ‚˜μ™€ μ±„νŒ… (3단계 κΈ°μ–΅ μ‹œμŠ€ν…œ ν™œμš©)
705
  response = generator.chat_with_persona(persona, user_message, conversation_history, session_id)
706
 
707
+ # Gradio messages ν˜•μ‹μœΌλ‘œ μ±„νŒ… 기둝 μ—…λ°μ΄νŠΈ
708
+ chat_history.append({"role": "user", "content": user_message})
709
+ chat_history.append({"role": "assistant", "content": response})
710
 
711
  return chat_history, ""
712
 
713
  except Exception as e:
714
  error_message = f"μ±„νŒ… 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
715
+ chat_history.append({"role": "user", "content": user_message})
716
+ chat_history.append({"role": "assistant", "content": "μ•—, λ―Έμ•ˆν•΄... λ­”κ°€ λ¬Έμ œκ°€ 생긴 것 κ°™μ•„... πŸ˜…"})
717
  return chat_history, ""
718
 
719
  def import_persona_from_json(json_file):
 
896
  global persona_generator
897
  if persona_generator and hasattr(persona_generator, 'conversation_memory'):
898
  json_data = persona_generator.conversation_memory.export_to_json()
899
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
900
  filename = f"conversation_history_{timestamp}.json"
901
+
902
+ # μž„μ‹œ 파일 μ €μž₯
903
+ temp_dir = "/tmp" if os.path.exists("/tmp") else "."
904
+ filepath = os.path.join(temp_dir, filename)
905
+
906
+ with open(filepath, 'w', encoding='utf-8') as f:
907
+ f.write(json_data)
908
+
909
+ return filepath, filename
910
  else:
911
  return None, "conversation_empty.json"
912
 
 
1394
  # 이벀트 μ—°κ²°
1395
  conversation_export_btn.click(
1396
  export_conversation_history,
1397
+ outputs=[conversation_download_file]
1398
  ).then(
1399
+ lambda x: gr.update(visible=True) if x else gr.update(visible=False),
1400
  inputs=[conversation_download_file],
1401
  outputs=[conversation_download_file]
1402
  )