Spaces:
Sleeping
Sleeping
haepa_mac commited on
Commit Β·
67cb297
1
Parent(s): 1b3095d
π§ Fix API integration issues and enhance error handling
Browse files- app.py +50 -8
- modules/persona_generator.py +26 -8
app.py
CHANGED
|
@@ -682,14 +682,28 @@ def chat_with_loaded_persona(persona, user_message, chat_history=None, api_provi
|
|
| 682 |
if chat_history is None:
|
| 683 |
chat_history = []
|
| 684 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 685 |
try:
|
| 686 |
# κΈλ‘λ² persona_generator μ¬μ© (API μ€μ μ΄ μ μ©λ μν)
|
| 687 |
generator = persona_generator
|
| 688 |
-
if generator is None:
|
| 689 |
-
generator = PersonaGenerator(api_provider=api_provider, api_key=api_key)
|
| 690 |
else:
|
| 691 |
-
# API μ€μ μ
λ°μ΄νΈ
|
| 692 |
-
generator.set_api_config(api_provider, api_key)
|
| 693 |
|
| 694 |
# Gradio messages νμμμ λν κΈ°λ‘ λ³ν
|
| 695 |
conversation_history = []
|
|
@@ -712,8 +726,20 @@ def chat_with_loaded_persona(persona, user_message, chat_history=None, api_provi
|
|
| 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):
|
|
@@ -860,11 +886,27 @@ def apply_api_configuration(api_provider, api_key):
|
|
| 860 |
return "β **API ν€λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ.**\n\nπ **API ν€ λ°κΈ λ°©λ²:**\n- **Gemini**: https://makersuite.google.com/app/apikey\n- **OpenAI**: https://platform.openai.com/api-keys"
|
| 861 |
|
| 862 |
try:
|
| 863 |
-
#
|
| 864 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
|
| 866 |
provider_name = "Google Gemini" if api_provider == "gemini" else "OpenAI"
|
| 867 |
-
return f"β
**{provider_name} API μ€μ μλ£!**\n\nπ― **μ΄μ λν κΈ°λ₯μ μ¬μ©ν μ μμ΅λλ€.**\n\nπ‘ **μ¬μ©λ²**: λννκΈ° νμμ JSON νμΌμ μ
λ‘λνμ¬ νλ₯΄μλμ λνλ₯Ό μμνμΈμ."
|
| 868 |
|
| 869 |
except Exception as e:
|
| 870 |
return f"β **API μ€μ μ€ μ€λ₯ λ°μ**\n\nπ **μ€λ₯ λ΄μ©**: {str(e)}\n\nπ‘ **νμΈμ¬ν**:\n- API ν€κ° μ¬λ°λ₯Έμ§ νμΈνμΈμ\n- μΈν°λ· μ°κ²°μ νμΈνμΈμ"
|
|
|
|
| 682 |
if chat_history is None:
|
| 683 |
chat_history = []
|
| 684 |
|
| 685 |
+
# νλ₯΄μλ 체ν¬
|
| 686 |
+
if not persona:
|
| 687 |
+
error_msg = "β λ¨Όμ νλ₯΄μλλ₯Ό λΆλ¬μμ£ΌμΈμ! λννκΈ° νμμ JSON νμΌμ μ
λ‘λνμΈμ."
|
| 688 |
+
chat_history.append({"role": "user", "content": user_message})
|
| 689 |
+
chat_history.append({"role": "assistant", "content": error_msg})
|
| 690 |
+
return chat_history, ""
|
| 691 |
+
|
| 692 |
+
# API ν€ μ²΄ν¬
|
| 693 |
+
if not api_key or not api_key.strip():
|
| 694 |
+
error_msg = "β API ν€κ° μ€μ λμ§ μμμ΅λλ€. μλ¨μ 'API μ€μ ' μΉμ
μμ λ¨Όμ API ν€λ₯Ό μ€μ ν΄μ£ΌμΈμ!"
|
| 695 |
+
chat_history.append({"role": "user", "content": user_message})
|
| 696 |
+
chat_history.append({"role": "assistant", "content": error_msg})
|
| 697 |
+
return chat_history, ""
|
| 698 |
+
|
| 699 |
try:
|
| 700 |
# κΈλ‘λ² persona_generator μ¬μ© (API μ€μ μ΄ μ μ©λ μν)
|
| 701 |
generator = persona_generator
|
| 702 |
+
if generator is None or not hasattr(generator, 'api_key') or generator.api_key != api_key.strip():
|
| 703 |
+
generator = PersonaGenerator(api_provider=api_provider, api_key=api_key.strip())
|
| 704 |
else:
|
| 705 |
+
# API μ€μ μ
λ°μ΄νΈ (ν€κ° λ€λ₯Ό μ μμΌλ―λ‘)
|
| 706 |
+
generator.set_api_config(api_provider, api_key.strip())
|
| 707 |
|
| 708 |
# Gradio messages νμμμ λν κΈ°λ‘ λ³ν
|
| 709 |
conversation_history = []
|
|
|
|
| 726 |
|
| 727 |
except Exception as e:
|
| 728 |
error_message = f"μ±ν
μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
| 729 |
+
print(f"π¨ λν μ€λ₯: {error_message}") # λλ²κΉ
μ©
|
| 730 |
+
import traceback
|
| 731 |
+
traceback.print_exc() # μμΈν μ€λ₯ μ 보
|
| 732 |
+
|
| 733 |
+
# μ¬μ©μμκ² μΉκ·Όν μ€λ₯ λ©μμ§
|
| 734 |
+
if "API" in str(e):
|
| 735 |
+
friendly_error = "API μ°κ²°μ λ¬Έμ κ° μμ΄μ. API ν€λ₯Ό λ€μ νμΈν΄λ³΄μκ² μ΄μ? π"
|
| 736 |
+
elif "μΈν°λ·" in str(e) or "network" in str(e).lower():
|
| 737 |
+
friendly_error = "μΈν°λ· μ°κ²°μ νμΈν΄λ³΄μΈμ! π"
|
| 738 |
+
else:
|
| 739 |
+
friendly_error = f"μ, λ―Έμν΄... λκ° λ¬Έμ κ° μκΈ΄ κ² κ°μ... π
\n\nπ **κΈ°μ μ μ 보**: {str(e)}"
|
| 740 |
+
|
| 741 |
chat_history.append({"role": "user", "content": user_message})
|
| 742 |
+
chat_history.append({"role": "assistant", "content": friendly_error})
|
| 743 |
return chat_history, ""
|
| 744 |
|
| 745 |
def import_persona_from_json(json_file):
|
|
|
|
| 886 |
return "β **API ν€λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ.**\n\nπ **API ν€ λ°κΈ λ°©λ²:**\n- **Gemini**: https://makersuite.google.com/app/apikey\n- **OpenAI**: https://platform.openai.com/api-keys"
|
| 887 |
|
| 888 |
try:
|
| 889 |
+
# API ν€ νμ κ²μ¦
|
| 890 |
+
api_key = api_key.strip()
|
| 891 |
+
if api_provider == "gemini" and not api_key.startswith("AI"):
|
| 892 |
+
return "β **Gemini API ν€ νμμ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€.**\n\nπ‘ **Gemini API ν€λ 'AIza...' ννλ‘ μμν΄μΌ ν©λλ€.**"
|
| 893 |
+
elif api_provider == "openai" and not api_key.startswith("sk-"):
|
| 894 |
+
return "β **OpenAI API ν€ νμμ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€.**\n\nπ‘ **OpenAI API ν€λ 'sk-...' ννλ‘ μμν΄μΌ ν©λλ€.**"
|
| 895 |
+
|
| 896 |
+
# μλ‘μ΄ PersonaGenerator μΈμ€ν΄μ€ μμ± λ° ν
μ€νΈ
|
| 897 |
+
test_generator = PersonaGenerator(api_provider=api_provider, api_key=api_key)
|
| 898 |
+
|
| 899 |
+
# κ°λ¨ν API ν
μ€νΈ
|
| 900 |
+
test_response = test_generator._generate_text_with_api("μλ
νμΈμ! κ°λ¨ν μλ΅ν΄μ£ΌμΈμ.")
|
| 901 |
+
|
| 902 |
+
if "API ν€κ° μ€μ λμ§ μμμ΅λλ€" in test_response or "API μ€λ₯" in test_response:
|
| 903 |
+
return f"β **API μ°κ²° μ€ν¨**\n\nπ **μ€λ₯ λ΄μ©**: {test_response}\n\nπ‘ **νμΈμ¬ν**:\n- API ν€κ° μ ν¨νμ§ νμΈνμΈμ\n- μΈν°λ· μ°κ²°μ νμΈνμΈμ"
|
| 904 |
+
|
| 905 |
+
# μ±κ³΅μ κΈλ‘λ² μΈμ€ν΄μ€ κ΅μ²΄
|
| 906 |
+
persona_generator = test_generator
|
| 907 |
|
| 908 |
provider_name = "Google Gemini" if api_provider == "gemini" else "OpenAI"
|
| 909 |
+
return f"β
**{provider_name} API μ€μ μλ£!**\n\nπ― **μ΄μ λν κΈ°λ₯μ μ¬μ©ν μ μμ΅λλ€.**\n\nπ‘ **μ¬μ©λ²**: λννκΈ° νμμ JSON νμΌμ μ
λ‘λνμ¬ νλ₯΄μλμ λνλ₯Ό μμνμΈμ.\n\nπ¬ **ν
μ€νΈ μλ΅**: {test_response[:100]}..."
|
| 910 |
|
| 911 |
except Exception as e:
|
| 912 |
return f"β **API μ€μ μ€ μ€λ₯ λ°μ**\n\nπ **μ€λ₯ λ΄μ©**: {str(e)}\n\nπ‘ **νμΈμ¬ν**:\n- API ν€κ° μ¬λ°λ₯Έμ§ νμΈνμΈμ\n- μΈν°λ· μ°κ²°μ νμΈνμΈμ"
|
modules/persona_generator.py
CHANGED
|
@@ -1019,7 +1019,12 @@ class PersonaGenerator:
|
|
| 1019 |
return "Gemini API ν€κ° μ€μ λμ§ μμμ΅λλ€."
|
| 1020 |
|
| 1021 |
try:
|
| 1022 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1023 |
|
| 1024 |
if image:
|
| 1025 |
response = model.generate_content([prompt, image])
|
|
@@ -2075,7 +2080,7 @@ class PersonaGenerator:
|
|
| 2075 |
)
|
| 2076 |
|
| 2077 |
# π§ 3λ¨κ³ κΈ°μ΅ μμ€ν
μμ 컨ν
μ€νΈ κ°μ Έμ€κΈ°
|
| 2078 |
-
memory_context = self.conversation_memory.
|
| 2079 |
|
| 2080 |
# 127κ° λ³μ κΈ°λ° μΈλΆ μ±κ²© νΉμ±
|
| 2081 |
detailed_personality_prompt = self._generate_detailed_personality_instructions(personality_profile)
|
|
@@ -2110,6 +2115,23 @@ class PersonaGenerator:
|
|
| 2110 |
# π 127κ° λ³μ κΈ°λ° μν©λ³ λ°μ κ°μ΄λ
|
| 2111 |
situational_guide = self._generate_situational_response_guide(personality_profile, user_message)
|
| 2112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2113 |
# μ΅μ’
ν둬ννΈ μ‘°ν© (κΈ°μ΅ μμ€ν
컨ν
μ€νΈ ν¬ν¨)
|
| 2114 |
full_prompt = f"""{base_prompt}
|
| 2115 |
|
|
@@ -2119,11 +2141,7 @@ class PersonaGenerator:
|
|
| 2119 |
|
| 2120 |
{personality_specific_prompt}
|
| 2121 |
|
| 2122 |
-
{
|
| 2123 |
-
|
| 2124 |
-
{memory_context['medium_term_insights']}
|
| 2125 |
-
|
| 2126 |
-
{memory_context['long_term_adaptations']}
|
| 2127 |
|
| 2128 |
{history_text}
|
| 2129 |
|
|
@@ -2147,7 +2165,7 @@ class PersonaGenerator:
|
|
| 2147 |
response_text = self._generate_text_with_api(full_prompt)
|
| 2148 |
|
| 2149 |
# π§ κΈ°μ΅ μμ€ν
μ μλ‘μ΄ μνΈμμ© μΆκ°
|
| 2150 |
-
self.conversation_memory.
|
| 2151 |
|
| 2152 |
return response_text
|
| 2153 |
|
|
|
|
| 1019 |
return "Gemini API ν€κ° μ€μ λμ§ μμμ΅λλ€."
|
| 1020 |
|
| 1021 |
try:
|
| 1022 |
+
# Gemini 2.0 Flash λͺ¨λΈ μ¬μ© (μ΅μ λ²μ )
|
| 1023 |
+
try:
|
| 1024 |
+
model = genai.GenerativeModel('gemini-2.0-flash-exp')
|
| 1025 |
+
except:
|
| 1026 |
+
# fallback to stable version
|
| 1027 |
+
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 1028 |
|
| 1029 |
if image:
|
| 1030 |
response = model.generate_content([prompt, image])
|
|
|
|
| 2080 |
)
|
| 2081 |
|
| 2082 |
# π§ 3λ¨κ³ κΈ°μ΅ μμ€ν
μμ 컨ν
μ€νΈ κ°μ Έμ€κΈ°
|
| 2083 |
+
memory_context = self.conversation_memory.get_relevant_context(user_message, session_id)
|
| 2084 |
|
| 2085 |
# 127κ° λ³μ κΈ°λ° μΈλΆ μ±κ²© νΉμ±
|
| 2086 |
detailed_personality_prompt = self._generate_detailed_personality_instructions(personality_profile)
|
|
|
|
| 2115 |
# π 127κ° λ³μ κΈ°λ° μν©λ³ λ°μ κ°μ΄λ
|
| 2116 |
situational_guide = self._generate_situational_response_guide(personality_profile, user_message)
|
| 2117 |
|
| 2118 |
+
# κΈ°μ΅ μ»¨ν
μ€νΈ ν¬λ§·ν
|
| 2119 |
+
memory_insights = ""
|
| 2120 |
+
if memory_context:
|
| 2121 |
+
if memory_context.get("recent_conversations"):
|
| 2122 |
+
memory_insights += "\n## π§ μ΅κ·Ό λν κΈ°μ΅:\n"
|
| 2123 |
+
for conv in memory_context["recent_conversations"][-2:]:
|
| 2124 |
+
memory_insights += f"- {conv.get('user_message', '')[:30]}...\n"
|
| 2125 |
+
|
| 2126 |
+
if memory_context.get("user_profile"):
|
| 2127 |
+
profile = memory_context["user_profile"]
|
| 2128 |
+
relationship_level = profile.get("relationship_level", "μλ‘μ΄_λ§λ¨")
|
| 2129 |
+
memory_insights += f"\n## π₯ κ΄κ³ μμ€: {relationship_level}\n"
|
| 2130 |
+
|
| 2131 |
+
if profile.get("message_count", 0) > 3:
|
| 2132 |
+
memory_insights += f"- λν νμ: {profile['message_count']}ν\n"
|
| 2133 |
+
memory_insights += f"- μν΅ μ€νμΌ: {profile.get('communication_style', '보ν΅')}\n"
|
| 2134 |
+
|
| 2135 |
# μ΅μ’
ν둬ννΈ μ‘°ν© (κΈ°μ΅ μμ€ν
컨ν
μ€νΈ ν¬ν¨)
|
| 2136 |
full_prompt = f"""{base_prompt}
|
| 2137 |
|
|
|
|
| 2141 |
|
| 2142 |
{personality_specific_prompt}
|
| 2143 |
|
| 2144 |
+
{memory_insights}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2145 |
|
| 2146 |
{history_text}
|
| 2147 |
|
|
|
|
| 2165 |
response_text = self._generate_text_with_api(full_prompt)
|
| 2166 |
|
| 2167 |
# π§ κΈ°μ΅ μμ€ν
μ μλ‘μ΄ μνΈμμ© μΆκ°
|
| 2168 |
+
self.conversation_memory.add_conversation(user_message, response_text, session_id)
|
| 2169 |
|
| 2170 |
return response_text
|
| 2171 |
|