Spaces:
Sleeping
Sleeping
haepa_mac commited on
Commit Β·
1b3095d
1
Parent(s): b721a79
π§ Fix critical runtime errors - datetime, chatbot format, download functions
Browse files
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.
|
| 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,
|
| 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(
|
|
|
|
| 712 |
|
| 713 |
return chat_history, ""
|
| 714 |
|
| 715 |
except Exception as e:
|
| 716 |
error_message = f"μ±ν
μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 717 |
-
chat_history.append(
|
|
|
|
| 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.
|
| 901 |
filename = f"conversation_history_{timestamp}.json"
|
| 902 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 1391 |
).then(
|
| 1392 |
-
lambda x: gr.update(visible=True) if x
|
| 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 |
)
|