Spaces:
Running
Running
File size: 4,290 Bytes
4345e70 0482473 4345e70 96692cf 0482473 96692cf 4345e70 7b26e52 4345e70 0482473 7b26e52 0482473 7b26e52 0482473 7b26e52 0482473 7b26e52 1177fdc 7b26e52 6fe0d0c 0482473 7b26e52 0482473 4345e70 0482473 4345e70 0482473 4345e70 0482473 96692cf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# κ°μ μν, κ°μ μ
λ°μ΄νΈ, decay μ²λ¦¬
# portfolio/npc_social_network/npc/npc_emotion.py
from .emotion_config import EMOTION_LIST, EMOTION_DECAY_RATE, EMOTION_CATEGORY_MAP
class EmotionManager:
def __init__(self, decay_rate, personality=None, initial_baseline=None):
# λ΄λΆ: float κ°μ μμΉ κ΄λ¦¬μ© λ²νΌ
self._emotion_buffer = {emo: 0.0 for emo in EMOTION_LIST}
# baseline λ²νΌ μΆκ°
self._baseline_buffer = initial_baseline or {emo: 0.0 for emo in EMOTION_LIST}
# κ°μ λ³ ν볡 μλ (ms λ¨μκ° μλ λ¨μλΉ κ°μ μλ)
self.decay_rate = decay_rate
self.personality = personality or {}
def update_emotion(self, emotion: str, strength: float = 1.0, context: str = "general"):
"""
νΉμ κ°μ μ κ°μ μνμ λ°μ (μΉ΄ν
κ³ λ¦¬ κΈ°λ°)
"""
if emotion not in self._emotion_buffer:
print(f"[κ²½κ³ ] '{emotion}'μ(λ) μ μλ κ°μ μ΄ μλλλ€.")
return
# κ°μ± κ³μ λ°μ
multiplier = self.personality.get("sensitive", 1.0)
category = EMOTION_CATEGORY_MAP.get(emotion, None)
if category == "core":
multiplier *= self.personality.get("affect_bias", 1.0)
elif category == "social":
# μ¬νμ κ°μ μ social contextμμλ§ λ°μ
if context != "social":
return # 무μ
multiplier *= self.personality.get("social_bias", 1.0)
elif category == "cognitive":
multiplier *= self.personality.get("cognitive_bias", 1.0)
elif category == "complex":
multiplier *= self.personality.get("complex_bias", 1.0)
# κ°μ λ°μ
self._emotion_buffer[emotion] += strength * multiplier
# clip μ μ© (μ΅λ 100)
self._emotion_buffer[emotion] = min(max(0.0, self._emotion_buffer[emotion]), 100.0)
def decay_emotions(self):
"""
μκ°μ΄ μ§λ¨μ λ°λΌ λͺ¨λ κ°μ μ΄ μμν κ°μ
"""
for emotion in EMOTION_LIST:
rate = self.decay_rate.get(emotion, 0.5)
modifier = 1.0 - self.personality.get("stoic", 0.0)
self._emotion_buffer[emotion] = max(0.0, self._emotion_buffer[emotion] - rate * modifier)
def get_state(self):
"""
κ°μ μν λ°ν (μΆλ ₯μ©, μ μλ‘ λ³ν)
"""
return {emo: int(self._emotion_buffer[emo]) for emo in EMOTION_LIST}
def get_dominant_emotion(self, layer: str = None):
"""
νμ¬ κ°μ₯ κ°ν κ°μ λ°ν (μ 체 κ°μ μ€ μ΅λκ° 1κ°)
"""
if layer:
# νΉμ layer λμ
layer_emotions = [emo for emo, cat in EMOTION_CATEGORY_MAP.items() if cat == layer]
if not layer_emotions:
return None
dominant_emo, score = max(
((emo, self._emotion_buffer[emo]) for emo in layer_emotions),
key=lambda x: x[1],
default=(None, 0.0)
)
if score == 0.0:
return None
return dominant_emo
else:
# μ 체 λμ
dominant_emo, score = max(self._emotion_buffer.items(), key=lambda x:x[1])
if score == 0.0:
return None
return dominant_emo
def get_buffer(self):
"""
emotion_buffer 볡μ¬λ³Έ λ°ν
"""
return self._emotion_buffer.copy()
def get_current_emotions_state(self):
"""
νμ¬ κ°μ μν λ°ν (dict)
"""
return self._emotion_buffer
def get_top_emotions(self, top_n=5):
"""
νμ¬ κ°μ λ²νΌμμ μμ Nκ° κ°μ λ°ν
:return: List of (emotion_name, value) tuples
"""
# μ λ ¬ ν μμ Nκ° λ°ν
sorted_emotions = sorted(self._emotion_buffer.items(), key=lambda x: x[1], reverse=True)
top_emotions = sorted_emotions[:top_n]
return top_emotions
def set_baseline(self, baseline_buffer):
"""
baseline bufferλ₯Ό μΈλΆμμ μ€μ (npc_base.py μ΄κΈ°ν μ μ¬μ©)
"""
self._baseline_buffer = baseline_buffer.copy()
|