Spaces:
Sleeping
Sleeping
Complete step 2
Browse files- npc_social_network/models/npc.py +30 -0
- npc_social_network/models/npc_manager.py +7 -0
- npc_social_network/routes/npc_route.py +15 -1
- test.ipynb +12 -0
npc_social_network/models/npc.py
CHANGED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# portfolio/npc_social_network/models/npc.py
|
2 |
+
import datetime
|
3 |
+
|
4 |
+
class NPC:
|
5 |
+
def __init__(self, name, personality):
|
6 |
+
self.name = name # npc μ΄λ¦
|
7 |
+
self.personality = personality # μ±κ²© ν€μλ (λ¬Έμμ΄ or λνΈ)
|
8 |
+
self.memory = [] # μ μ μμ λν κΈ°λ‘
|
9 |
+
self.relationship_with_user = 0.0 # μ μ μμ κ΄κ³ μ μ
|
10 |
+
self.relationship_with_npcs = {} # λ€λ₯Έ npcλ€κ³Όμ κ΄κ³ {μ΄λ¦: μ μ}
|
11 |
+
|
12 |
+
def remember_conversation(self, user_input, npc_response):
|
13 |
+
self.memory.append({
|
14 |
+
"timestamp": datetime.datetime.now().isoformat(),
|
15 |
+
"user": user_input,
|
16 |
+
"npc": npc_response
|
17 |
+
})
|
18 |
+
|
19 |
+
def update_user_relationship(self, score_delta):
|
20 |
+
self.relationship_with_user += score_delta
|
21 |
+
self.relationship_with_user = max(min(self.relationship_with_user, 1.0), -1.0) # -1.0~1.0 λ²μ
|
22 |
+
|
23 |
+
def update_npc_relationship(self, other_npc_name, score_delta):
|
24 |
+
if other_npc_name not in self.relationship_with_npcs:
|
25 |
+
self.relationship_with_npcs[other_npc_name] == 0.0
|
26 |
+
self.relationship_with_npcs[other_npc_name] += score_delta
|
27 |
+
self.relationship_with_npcs[other_npc_name] = max(min(self.relationship_with_npcs[other_npc_name], 1.0), -1.0)
|
28 |
+
|
29 |
+
def get_latest_memory(self, n=5):
|
30 |
+
return self.memory[-n:]
|
npc_social_network/models/npc_manager.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .npc import NPC
|
2 |
+
|
3 |
+
npc_list = {
|
4 |
+
"Alice": NPC("Alice", "μΉμ νκ³ λμ²μ μΈ μ±κ²©"),
|
5 |
+
"Bob": NPC("Bob", "λ
Όλ¦¬μ μ΄κ³ μ‘°μ©ν¨"),
|
6 |
+
"Charlie": NPC("Charlie", "κ°μ κΈ°λ³΅μ΄ μ¬νκ³ μλ―Όν¨")
|
7 |
+
}
|
npc_social_network/routes/npc_route.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
# portfolio/npc_social_network/routes/npc_route.py
|
2 |
from flask import Blueprint, render_template, request, jsonify
|
|
|
3 |
|
4 |
npc_bp = Blueprint(
|
5 |
"npc_social",
|
@@ -19,4 +20,17 @@ def chat():
|
|
19 |
|
20 |
# κ°λ¨ν μλ΅ μμ (GPT μ°λ μ )
|
21 |
reply = f"{npc_name}μ λλ΅: '{user_input}'μ λν΄ μκ°ν΄λ³Όκ²."
|
22 |
-
return jsonify({'response': reply})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# portfolio/npc_social_network/routes/npc_route.py
|
2 |
from flask import Blueprint, render_template, request, jsonify
|
3 |
+
from npc_social_network.models.npc_manager import npc_list
|
4 |
|
5 |
npc_bp = Blueprint(
|
6 |
"npc_social",
|
|
|
20 |
|
21 |
# κ°λ¨ν μλ΅ μμ (GPT μ°λ μ )
|
22 |
reply = f"{npc_name}μ λλ΅: '{user_input}'μ λν΄ μκ°ν΄λ³Όκ²."
|
23 |
+
return jsonify({'response': reply})
|
24 |
+
|
25 |
+
@npc_bp.route("/test_npc")
|
26 |
+
def test_npc():
|
27 |
+
npc = npc_list["Alice"]
|
28 |
+
npc.remember_conversation("μλ
!", "μλ
νμΈμ~")
|
29 |
+
npc.update_user_relationship(0.2)
|
30 |
+
npc.update_npc_relationship("Bob", -0.3)
|
31 |
+
|
32 |
+
return {
|
33 |
+
"latest_memory": npc.get_latest_memory(),
|
34 |
+
"relationship_with_user": npc.relationship_with_user,
|
35 |
+
"relationship_with_npcs": npc.relationship_with_npcs
|
36 |
+
}
|
test.ipynb
CHANGED
@@ -21,6 +21,18 @@
|
|
21 |
"# txt λΌμ΄λΈλ¬λ¦¬ μ€μΉνκΈ°\n",
|
22 |
"!pip install -r requirements.txt"
|
23 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
}
|
25 |
],
|
26 |
"metadata": {
|
|
|
21 |
"# txt λΌμ΄λΈλ¬λ¦¬ μ€μΉνκΈ°\n",
|
22 |
"!pip install -r requirements.txt"
|
23 |
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": null,
|
28 |
+
"id": "fec385de",
|
29 |
+
"metadata": {},
|
30 |
+
"outputs": [],
|
31 |
+
"source": [
|
32 |
+
"# λμ€μ ν λ΄μ©\n",
|
33 |
+
"# 1. ai λͺ¨λΈ νμ΅μ μν΄ μ΄λ€ λ΄μ©μ΄ νμνμ§\n",
|
34 |
+
"# 2. νμ΅ν λͺ¨λΈμ μ μ₯, νμ©ν μ μλμ§"
|
35 |
+
]
|
36 |
}
|
37 |
],
|
38 |
"metadata": {
|