Spaces:
Running
Running
Step 20 진행중 - Error 발생
Browse files
npc_social_network/npc/npc_manager.py
CHANGED
@@ -6,13 +6,16 @@ if TYPE_CHECKING:
|
|
6 |
from .npc_base import NPC
|
7 |
|
8 |
class NPCManager:
|
|
|
9 |
def __init__(self):
|
10 |
-
self.npcs = []
|
11 |
-
self.npc_dict = {}
|
12 |
|
13 |
def add_npc(self, npc: NPC):
|
14 |
-
|
15 |
-
|
|
|
|
|
16 |
|
17 |
def get_npc_by_name(self, name: str):
|
18 |
"""
|
|
|
6 |
from .npc_base import NPC
|
7 |
|
8 |
class NPCManager:
|
9 |
+
"""모든 NPC를 관리하는 중앙 관리자"""
|
10 |
def __init__(self):
|
11 |
+
self.npcs: list['NPC'] = []
|
12 |
+
self.npc_dict: dict[str, 'NPC'] = {}
|
13 |
|
14 |
def add_npc(self, npc: NPC):
|
15 |
+
"""NPC를 매니저에 추가합니다."""
|
16 |
+
if npc.name not in self.npc_dict:
|
17 |
+
self.npcs.append(npc)
|
18 |
+
self.npc_dict[npc.name] = npc
|
19 |
|
20 |
def get_npc_by_name(self, name: str):
|
21 |
"""
|
npc_social_network/routes/npc_route.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
# portfolio/npc_social_network/routes/npc_route.py
|
2 |
from flask import Blueprint, render_template, request, jsonify
|
3 |
from npc_social_network.models.gemini_setup import load_gemini
|
4 |
-
from npc_social_network.
|
5 |
-
from
|
6 |
from npc_social_network.npc.npc_memory_embedder import embed_npc_memories, search_similar_memories
|
7 |
import threading
|
8 |
import time
|
@@ -17,22 +17,16 @@ npc_bp = Blueprint(
|
|
17 |
# LLM Model
|
18 |
llm_model = load_gemini()
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
npc_manager
|
26 |
-
npc_manager
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
from npc_social_network.npc.npc_memory import Memory
|
31 |
-
|
32 |
-
ella = npc_manager.get_npc_by_name("엘라")
|
33 |
-
ella.memory_store.add_memory(Memory(content="어제 앨리스와 심하게 다퉜다", importance=9))
|
34 |
-
ella.memory_store.add_memory(Memory(content="밥이 선물해준 검이 마음에 들었다", importance=6))
|
35 |
-
ella.memory_store.add_memory(Memory(content="오늘은 찰리를 도와 농사를 지었다", importance=5))
|
36 |
|
37 |
# --------------------
|
38 |
# 라우트 정의
|
@@ -40,38 +34,34 @@ ella.memory_store.add_memory(Memory(content="오늘은 찰리를 도와 농사
|
|
40 |
|
41 |
@npc_bp.route("/", methods=['GET'])
|
42 |
def home():
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
# 감정 요약 제공
|
48 |
-
try:
|
49 |
-
emotion_summary = npc.summarize_emotional_state()
|
50 |
-
except AttributeError:
|
51 |
-
emotion_summary = "No emotion summary available."
|
52 |
-
|
53 |
-
return render_template("chat.html", npc_name=npc.name, emotion_summary=emotion_summary)
|
54 |
|
55 |
# 채팅 처리 API
|
56 |
@npc_bp.route("/chat", methods=['POST'])
|
57 |
def chat():
|
|
|
|
|
58 |
user_input = request.json.get("message")
|
59 |
npc_name = request.json.get("npc")
|
60 |
|
61 |
npc = npc_manager.get_npc_by_name(npc_name)
|
62 |
-
|
63 |
if npc is None:
|
64 |
-
return jsonify({"error": "NPC not found"}), 404
|
65 |
|
|
|
66 |
# generate_dialogue를 통해 응답 생성 및 내부 상태 업데이트
|
67 |
-
npc_reply = npc.generate_dialogue(user_input,
|
68 |
|
69 |
-
#
|
70 |
-
|
71 |
|
72 |
return jsonify({
|
73 |
"npc_reply": npc_reply,
|
74 |
-
"dominant_emotion":
|
75 |
"memory_summary": npc.summarize_emotional_state(),
|
76 |
"emotion_state": npc.get_composite_emotion_state(),
|
77 |
"relationship_with_player": npc.get_relationship_description("플레이어")
|
|
|
1 |
# portfolio/npc_social_network/routes/npc_route.py
|
2 |
from flask import Blueprint, render_template, request, jsonify
|
3 |
from npc_social_network.models.gemini_setup import load_gemini
|
4 |
+
from npc_social_network.maps.manager.simulation_manager import load_simulation, save_simulation
|
5 |
+
from ..npc.npc_manager import NPCManager
|
6 |
from npc_social_network.npc.npc_memory_embedder import embed_npc_memories, search_similar_memories
|
7 |
import threading
|
8 |
import time
|
|
|
17 |
# LLM Model
|
18 |
llm_model = load_gemini()
|
19 |
|
20 |
+
def get_npc_manager() -> NPCManager:
|
21 |
+
"""
|
22 |
+
저장된 시뮬레이션 상태 파일을 불러와 NPC 매니저를 가져옵니다.
|
23 |
+
웹은 이 함수를 통해서만 NPC 데이터에 접근합니다.
|
24 |
+
"""
|
25 |
+
npc_manager = load_simulation()
|
26 |
+
if npc_manager is None:
|
27 |
+
# 만약 저장 파일이 없다면, 웹 UI가 비어있지 않도록 임시 매니저를 생성.
|
28 |
+
npc_manager = NPCManager()
|
29 |
+
return npc_manager
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# --------------------
|
32 |
# 라우트 정의
|
|
|
34 |
|
35 |
@npc_bp.route("/", methods=['GET'])
|
36 |
def home():
|
37 |
+
# 웹 페이지가 처음 로드될 때, 저장된 NPC 목록을 전달
|
38 |
+
npc_manager = get_npc_manager()
|
39 |
+
npc_names = list(npc_manager.npc_dict.keys())
|
40 |
+
return render_template("chat.html", npc_names=npc_names)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# 채팅 처리 API
|
43 |
@npc_bp.route("/chat", methods=['POST'])
|
44 |
def chat():
|
45 |
+
"""웹 채팅 상호작용 후, 변경된 상태 파일에 저장"""
|
46 |
+
npc_manager = get_npc_manager()
|
47 |
user_input = request.json.get("message")
|
48 |
npc_name = request.json.get("npc")
|
49 |
|
50 |
npc = npc_manager.get_npc_by_name(npc_name)
|
51 |
+
|
52 |
if npc is None:
|
53 |
+
return jsonify({"error": "NPC not found. Pygame 시뮬레이션을 먼저 실행하여 NPC를 생성해주세요."}), 404
|
54 |
|
55 |
+
# generate_dialogue가 time_context를 요구하므로 임시 값을 전달
|
56 |
# generate_dialogue를 통해 응답 생성 및 내부 상태 업데이트
|
57 |
+
npc_reply = npc.generate_dialogue(user_input, time_context="Web Interaction")
|
58 |
|
59 |
+
# 상호작용 후 변경된 상태를 저장
|
60 |
+
save_simulation(npc_manager)
|
61 |
|
62 |
return jsonify({
|
63 |
"npc_reply": npc_reply,
|
64 |
+
"dominant_emotion": npc.emotion.get_dominant_emotion(),
|
65 |
"memory_summary": npc.summarize_emotional_state(),
|
66 |
"emotion_state": npc.get_composite_emotion_state(),
|
67 |
"relationship_with_player": npc.get_relationship_description("플레이어")
|
npc_social_network/templates/chat.html
CHANGED
@@ -6,18 +6,19 @@
|
|
6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
7 |
<title>NPC Chat</title>
|
8 |
|
9 |
-
<link rel="stylesheet" herf="{{ url_for('static', filename='style.css') }}">
|
10 |
</head>
|
11 |
<body>
|
12 |
<h2>NPC 소셜 네트워크</h2>
|
13 |
|
14 |
<div class="meta-info">
|
15 |
-
<
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
21 |
</select>
|
22 |
<span id="relationStatus" style="margin-left:20px";>관계 점수: 로딩 중...</span>
|
23 |
</div>
|
|
|
6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
7 |
<title>NPC Chat</title>
|
8 |
|
9 |
+
<link rel="stylesheet" herf="{{ url_for('npc_social.static', filename='css/style.css') }}">
|
10 |
</head>
|
11 |
<body>
|
12 |
<h2>NPC 소셜 네트워크</h2>
|
13 |
|
14 |
<div class="meta-info">
|
15 |
+
<select id="npc" onchange="loadNPCInfo()">
|
16 |
+
<!-- [MOD] 서버에서 전달받은 NPC 이름으로 동적 생성 -->
|
17 |
+
{% for name in npc_names %}
|
18 |
+
<option value="{{ name }}">{{ name }}</option>
|
19 |
+
{% else %}
|
20 |
+
<option>NPC 없음</option>
|
21 |
+
{% endfor %}
|
22 |
</select>
|
23 |
<span id="relationStatus" style="margin-left:20px";>관계 점수: 로딩 중...</span>
|
24 |
</div>
|