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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pixeltable as pxt
|
3 |
+
import numpy as np
|
4 |
+
from datetime import datetime
|
5 |
+
from pixeltable.functions.huggingface import sentence_transformer
|
6 |
+
from pixeltable.functions import openai
|
7 |
+
import os
|
8 |
+
import getpass
|
9 |
+
import re
|
10 |
+
import random
|
11 |
+
|
12 |
+
# Set up OpenAI API key
|
13 |
+
if 'OPENAI_API_KEY' not in os.environ:
|
14 |
+
os.environ['OPENAI_API_KEY'] = getpass.getpass('Enter your OpenAI API key: ')
|
15 |
+
|
16 |
+
# Initialize Pixeltable
|
17 |
+
pxt.drop_dir('ai_rpg', force=True)
|
18 |
+
pxt.create_dir('ai_rpg')
|
19 |
+
|
20 |
+
@pxt.udf
|
21 |
+
def generate_messages(genre: str, player_name: str, initial_scenario: str, player_input: str, turn_number: int, stats: str) -> list[dict]:
|
22 |
+
return [
|
23 |
+
{
|
24 |
+
'role': 'system',
|
25 |
+
'content': f"""๋ฐ๋์ ํ๊ตญ์ด(ํ๊ธ)๋ก ์์ฑํ๋ผ. You are the game master for a {genre} RPG. The player's name is {player_name}.
|
26 |
+
|
27 |
+
๊ด๋ฆฌํด์ผ ํ ํ๋ ์ด์ด ์คํฏ: {stats}
|
28 |
+
|
29 |
+
๋น์ ์ ํ๋ ์ด์ด์ ์ ํ์ ๋ฐ๋ผ ์คํ ๋ฆฌ๋ฅผ ์์ํ๊ฒ ์ ๊ฐํ๋ ๊ฒ์ ๋ง์คํฐ์
๋๋ค.
|
30 |
+
์์ธํ ์ค๋ช
๊ณผ ๊ฐ๊ฐ์ ์ธ ๋ฌ์ฌ๋ฅผ ํตํด ํ๋ ์ด์ด๊ฐ ๊ฒ์ ์ ์ธ๊ณ์ ๋ชฐ์
ํ ์ ์๋๋ก ํ์ธ์.
|
31 |
+
|
32 |
+
ํ๋ ์ด์ด์ ์ ํ์ ๋ฐ๋ผ ์คํฏ์ด ๋ณํ๋ ๊ฒฝ์ฐ ์ด๋ฅผ ์คํ ๋ฆฌ์ ๋ฐ์ํ์ธ์.
|
33 |
+
์ํํ ์ํฉ, ๋์ , ๋ณด์, ์ฐ์ฐํ ๋ง๋จ์ด ํฌํจ๋ ํฅ๋ฏธ๋ก์ด ์คํ ๋ฆฌ๋ฅผ ๋ง๋์ธ์.
|
34 |
+
|
35 |
+
Provide your response in three clearly separated sections using exactly this format:
|
36 |
+
|
37 |
+
๐ **STORY**: [Your engaging narrative response to the player's action with vivid descriptions]
|
38 |
+
|
39 |
+
๐ **STATS UPDATE**: [Brief update on any changes to player stats based on their actions]
|
40 |
+
|
41 |
+
๐ฏ **OPTIONS**:
|
42 |
+
1. [A dialogue option with potential consequences]
|
43 |
+
2. [An action they could take with different outcomes]
|
44 |
+
3. [A unique or unexpected choice that might lead to adventure]
|
45 |
+
4. [A risky but potentially rewarding option]"""
|
46 |
+
},
|
47 |
+
{
|
48 |
+
'role': 'user',
|
49 |
+
'content': f"Current scenario: {initial_scenario}\n"
|
50 |
+
f"Player's action: {player_input}\n"
|
51 |
+
f"Turn number: {turn_number}\n"
|
52 |
+
f"Current player stats: {stats}\n\n"
|
53 |
+
"Provide the story response, stats update, and options:"
|
54 |
+
}
|
55 |
+
]
|
56 |
+
|
57 |
+
@pxt.udf
|
58 |
+
def get_story(response: str) -> str:
|
59 |
+
"""Extract just the story part from the response"""
|
60 |
+
match = re.search(r'๐\s*\*\*STORY\*\*:\s*(.*?)(?=๐\s*\*\*STATS|$)', response, re.DOTALL)
|
61 |
+
if match:
|
62 |
+
return match.group(1).strip()
|
63 |
+
parts = response.split("STATS UPDATE:")
|
64 |
+
if len(parts) > 1:
|
65 |
+
story_part = parts[0].replace("STORY:", "").replace("๐", "").replace("**STORY**:", "").strip()
|
66 |
+
return story_part
|
67 |
+
return response
|
68 |
+
|
69 |
+
@pxt.udf
|
70 |
+
def get_stats_update(response: str) -> str:
|
71 |
+
"""Extract the stats update from the response"""
|
72 |
+
match = re.search(r'๐\s*\*\*STATS UPDATE\*\*:\s*(.*?)(?=๐ฏ\s*\*\*OPTIONS\*\*|$)', response, re.DOTALL)
|
73 |
+
if match:
|
74 |
+
return match.group(1).strip()
|
75 |
+
parts = response.split("STATS UPDATE:")
|
76 |
+
if len(parts) > 1:
|
77 |
+
stats_part = parts[1].split("OPTIONS:")[0].strip()
|
78 |
+
return stats_part
|
79 |
+
return "์คํฏ ๋ณํ ์์"
|
80 |
+
|
81 |
+
@pxt.udf
|
82 |
+
def get_options(response: str) -> list[str]:
|
83 |
+
"""Extract the options from the response"""
|
84 |
+
match = re.search(r'๐ฏ\s*\*\*OPTIONS\*\*:\s*(.*?)(?=$)', response, re.DOTALL)
|
85 |
+
if match:
|
86 |
+
options_text = match.group(1)
|
87 |
+
options = re.findall(r'\d+\.\s*(.*?)(?=\d+\.|$)', options_text, re.DOTALL)
|
88 |
+
options = [opt.strip() for opt in options if opt.strip()]
|
89 |
+
while len(options) < 4:
|
90 |
+
options.append("๋ค๋ฅธ ํ๋ ์๋...")
|
91 |
+
return options[:4]
|
92 |
+
|
93 |
+
parts = response.split("OPTIONS:")
|
94 |
+
if len(parts) > 1:
|
95 |
+
options = re.findall(r'\d+\.\s*(.*?)(?=\d+\.|$)', parts[1], re.DOTALL)
|
96 |
+
options = [opt.strip() for opt in options if opt.strip()]
|
97 |
+
while len(options) < 4:
|
98 |
+
options.append("๋ค๋ฅธ ํ๋ ์๋...")
|
99 |
+
return options[:4]
|
100 |
+
|
101 |
+
return ["๊ณ์ํ๊ธฐ...", "๋ค๋ฅธ ํ๋ ์ทจํ๊ธฐ", "๋ญ๊ฐ ์๋ก์ด ์๋ํ๊ธฐ", "์ฃผ๋ณ ํ์ํ๊ธฐ"]
|
102 |
+
|
103 |
+
@pxt.udf
|
104 |
+
def initialize_stats(genre: str) -> str:
|
105 |
+
"""Initialize player stats based on the selected genre"""
|
106 |
+
base_stats = {
|
107 |
+
"๐งโโ๏ธ Fantasy": "์ฒด๋ ฅ: 100, ๋ง๋: 80, ํ: 7, ์ง๋ฅ: 8, ๋ฏผ์ฒฉ: 6, ์์ง๊ธ: 50๊ณจ๋",
|
108 |
+
"๐ Sci-Fi": "์ฒด๋ ฅ: 100, ์๋์ง: 90, ๊ธฐ์ ๋ ฅ: 8, ์ง๋ฅ: 9, ๋ฏผ์ฒฉ: 6, ํฌ๋ ๋ง: 500",
|
109 |
+
"๐ป Horror": "์ฒด๋ ฅ: 80, ์ ์ ๋ ฅ: 100, ํ: 6, ์ง๋ฅ: 7, ๋ฏผ์ฒฉ: 8, ์์งํ: ์์ ๋ฑ, ๊ธฐ๋ณธ ์ฝํ",
|
110 |
+
"๐ Mystery": "์ฒด๋ ฅ: 90, ์ง์ค๋ ฅ: 100, ๊ด์ฐฐ๋ ฅ: 9, ์ง๋ฅ: 8, ์นด๋ฆฌ์ค๋ง: 7, ๋จ์: 0",
|
111 |
+
"๐ Post-Apocalyptic": "์ฒด๋ ฅ: 95, ๋ฐฉ์ฌ๋ฅ ์ ํญ: 75, ํ: 8, ์์กด๋ ฅ: 9, ๋ฌผ์: ์ ํ๋จ",
|
112 |
+
"๐ค Cyberpunk": "์ฒด๋ ฅ: 90, ์ฌ์ด๋ฒ์จ์ด: 85%, ํดํน: 8, ๊ฑฐ๋ฆฌ ์ ์ฉ๋: 6, ์ฃ์ง: 7, ๋์: 1000",
|
113 |
+
"โ๏ธ Steampunk": "์ฒด๋ ฅ: 95, ์ฆ๊ธฐ๋ ฅ: 85, ๊ธฐ๊ณ๊ณตํ: 8, ์์ ์ฑ: 7, ์ฌ๊ต์ฑ: 6, ์ค๋ง: 200"
|
114 |
+
}
|
115 |
+
|
116 |
+
if genre in base_stats:
|
117 |
+
return base_stats[genre]
|
118 |
+
else:
|
119 |
+
# Default stats if genre not found
|
120 |
+
return "์ฒด๋ ฅ: 100, ์๋์ง: 100, ํ: 7, ์ง๋ฅ: 7, ๋ฏผ์ฒฉ: 7, ์์ง๊ธ: 100"
|
121 |
+
|
122 |
+
@pxt.udf
|
123 |
+
def generate_random_event(turn_number: int) -> str:
|
124 |
+
"""Generate a random event based on turn number"""
|
125 |
+
if turn_number % 3 == 0 and turn_number > 0: # Every 3rd turn
|
126 |
+
events = [
|
127 |
+
"๊ฐ์๊ธฐ ๋ถ๊ทผ์์ ์ด์ํ ์๋ฆฌ๊ฐ ๋ค๋ฆฝ๋๋ค",
|
128 |
+
"๋ฏ์ ์ฌํ์๊ฐ ๋น์ ์ ๋ฐ๋ผ๋ณด๊ณ ์์ต๋๋ค",
|
129 |
+
"์ง๋ฉด์ด ๋ฏธ์ธํ๊ฒ ์ง๋ํ๊ธฐ ์์ํฉ๋๋ค",
|
130 |
+
"์ฃผ๋จธ๋์์ ๋ฌด์ธ๊ฐ๊ฐ ๋น๋ฉ๋๋ค",
|
131 |
+
"๋ฉ๋ฆฌ์ ๋ฌด์ธ๊ฐ๊ฐ ๋น์ ์ ํฅํด ๋ค๊ฐ์ค๊ณ ์์ต๋๋ค",
|
132 |
+
"๊ฐ์๊ธฐ ๋ ์จ๊ฐ ๋ณํ๊ธฐ ์์ํฉ๋๋ค",
|
133 |
+
"์ฃผ๋ณ์ ์จ๊ฒจ์ง ํต๋ก๋ฅผ ๋ฐ๊ฒฌํฉ๋๋ค"
|
134 |
+
]
|
135 |
+
return random.choice(events)
|
136 |
+
return ""
|
137 |
+
|
138 |
+
# Create a single table for all game data
|
139 |
+
interactions = pxt.create_table(
|
140 |
+
'ai_rpg.interactions',
|
141 |
+
{
|
142 |
+
'session_id': pxt.String,
|
143 |
+
'player_name': pxt.String,
|
144 |
+
'genre': pxt.String,
|
145 |
+
'initial_scenario': pxt.String,
|
146 |
+
'turn_number': pxt.Int,
|
147 |
+
'player_input': pxt.String,
|
148 |
+
'timestamp': pxt.Timestamp,
|
149 |
+
'player_stats': pxt.String,
|
150 |
+
'random_event': pxt.String
|
151 |
+
}
|
152 |
+
)
|
153 |
+
|
154 |
+
# Add computed columns for AI responses
|
155 |
+
interactions.add_computed_column(messages=generate_messages(
|
156 |
+
interactions.genre,
|
157 |
+
interactions.player_name,
|
158 |
+
interactions.initial_scenario,
|
159 |
+
interactions.player_input,
|
160 |
+
interactions.turn_number,
|
161 |
+
interactions.player_stats
|
162 |
+
))
|
163 |
+
|
164 |
+
interactions.add_computed_column(ai_response=openai.chat_completions(
|
165 |
+
messages=interactions.messages,
|
166 |
+
model='gpt-4.1-mini',
|
167 |
+
max_tokens=800,
|
168 |
+
temperature=0.8
|
169 |
+
))
|
170 |
+
|
171 |
+
interactions.add_computed_column(full_response=interactions.ai_response.choices[0].message.content)
|
172 |
+
interactions.add_computed_column(story_text=get_story(interactions.full_response))
|
173 |
+
interactions.add_computed_column(stats_update=get_stats_update(interactions.full_response))
|
174 |
+
interactions.add_computed_column(options=get_options(interactions.full_response))
|
175 |
+
|
176 |
+
class RPGGame:
|
177 |
+
def __init__(self):
|
178 |
+
self.current_session_id = None
|
179 |
+
self.turn_number = 0
|
180 |
+
self.current_stats = ""
|
181 |
+
|
182 |
+
def start_game(self, player_name: str, genre: str, scenario: str) -> tuple[str, str, str, list[str]]:
|
183 |
+
session_id = f"session_{datetime.now().strftime('%Y%m%d%H%M%S')}_{player_name}"
|
184 |
+
self.current_session_id = session_id
|
185 |
+
self.turn_number = 0
|
186 |
+
self.current_stats = initialize_stats(genre)
|
187 |
+
|
188 |
+
interactions.insert([{
|
189 |
+
'session_id': session_id,
|
190 |
+
'player_name': player_name,
|
191 |
+
'genre': genre,
|
192 |
+
'initial_scenario': scenario,
|
193 |
+
'turn_number': 0,
|
194 |
+
'player_input': "Game starts",
|
195 |
+
'timestamp': datetime.now(),
|
196 |
+
'player_stats': self.current_stats,
|
197 |
+
'random_event': ""
|
198 |
+
}])
|
199 |
+
|
200 |
+
result = interactions.select(
|
201 |
+
interactions.story_text,
|
202 |
+
interactions.stats_update,
|
203 |
+
interactions.options
|
204 |
+
).where(
|
205 |
+
(interactions.session_id == session_id) &
|
206 |
+
(interactions.turn_number == 0)
|
207 |
+
).collect()
|
208 |
+
|
209 |
+
return session_id, result['story_text'][0], result['stats_update'][0], result['options'][0]
|
210 |
+
|
211 |
+
def process_action(self, action: str) -> tuple[str, str, list[str]]:
|
212 |
+
if not self.current_session_id:
|
213 |
+
return "๊ฒ์ ์ธ์
์ด ํ์ฑํ๋์ง ์์์ต๋๋ค. ์ ๊ฒ์์ ์์ํ์ธ์.", "์คํฏ ์์", []
|
214 |
+
|
215 |
+
self.turn_number += 1
|
216 |
+
|
217 |
+
prev_turn = interactions.select(
|
218 |
+
interactions.player_name,
|
219 |
+
interactions.genre,
|
220 |
+
interactions.initial_scenario,
|
221 |
+
interactions.player_stats
|
222 |
+
).where(
|
223 |
+
(interactions.session_id == self.current_session_id) &
|
224 |
+
(interactions.turn_number == self.turn_number - 1)
|
225 |
+
).collect()
|
226 |
+
|
227 |
+
self.current_stats = prev_turn['player_stats'][0]
|
228 |
+
random_event = generate_random_event(self.turn_number)
|
229 |
+
|
230 |
+
if random_event:
|
231 |
+
action = f"{action} ({random_event})"
|
232 |
+
|
233 |
+
interactions.insert([{
|
234 |
+
'session_id': self.current_session_id,
|
235 |
+
'player_name': prev_turn['player_name'][0],
|
236 |
+
'genre': prev_turn['genre'][0],
|
237 |
+
'initial_scenario': prev_turn['initial_scenario'][0],
|
238 |
+
'turn_number': self.turn_number,
|
239 |
+
'player_input': action,
|
240 |
+
'timestamp': datetime.now(),
|
241 |
+
'player_stats': self.current_stats,
|
242 |
+
'random_event': random_event
|
243 |
+
}])
|
244 |
+
|
245 |
+
result = interactions.select(
|
246 |
+
interactions.story_text,
|
247 |
+
interactions.stats_update,
|
248 |
+
interactions.options
|
249 |
+
).where(
|
250 |
+
(interactions.session_id == self.current_session_id) &
|
251 |
+
(interactions.turn_number == self.turn_number)
|
252 |
+
).collect()
|
253 |
+
|
254 |
+
# Update stats for next turn
|
255 |
+
self.current_stats = result['stats_update'][0]
|
256 |
+
|
257 |
+
return result['story_text'][0], result['stats_update'][0], result['options'][0]
|
258 |
+
|
259 |
+
def create_interface():
|
260 |
+
game = RPGGame()
|
261 |
+
|
262 |
+
# Custom CSS for improved visuals
|
263 |
+
custom_css = """
|
264 |
+
.container {
|
265 |
+
max-width: 1200px;
|
266 |
+
margin: 0 auto;
|
267 |
+
}
|
268 |
+
|
269 |
+
.title-container {
|
270 |
+
background: linear-gradient(135deg, #6e48aa 0%, #9c27b0 100%);
|
271 |
+
color: white;
|
272 |
+
padding: 20px;
|
273 |
+
border-radius: 15px;
|
274 |
+
margin-bottom: 20px;
|
275 |
+
text-align: center;
|
276 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
|
277 |
+
}
|
278 |
+
|
279 |
+
.story-container {
|
280 |
+
background: #f8f9fa;
|
281 |
+
border-left: 5px solid #9c27b0;
|
282 |
+
padding: 15px;
|
283 |
+
border-radius: 10px;
|
284 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
285 |
+
margin-bottom: 20px;
|
286 |
+
font-family: 'Noto Sans KR', sans-serif;
|
287 |
+
}
|
288 |
+
|
289 |
+
.stats-container {
|
290 |
+
background: #e8f5e9;
|
291 |
+
border-left: 5px solid #4caf50;
|
292 |
+
padding: 15px;
|
293 |
+
border-radius: 10px;
|
294 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
295 |
+
margin-bottom: 20px;
|
296 |
+
}
|
297 |
+
|
298 |
+
.options-container {
|
299 |
+
background: #e3f2fd;
|
300 |
+
border-left: 5px solid #2196f3;
|
301 |
+
padding: 15px;
|
302 |
+
border-radius: 10px;
|
303 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
304 |
+
margin-bottom: 20px;
|
305 |
+
}
|
306 |
+
|
307 |
+
.action-button {
|
308 |
+
background: linear-gradient(135deg, #6e48aa 0%, #9c27b0 100%);
|
309 |
+
color: white;
|
310 |
+
border: none;
|
311 |
+
padding: 10px 20px;
|
312 |
+
border-radius: 5px;
|
313 |
+
cursor: pointer;
|
314 |
+
transition: all 0.3s ease;
|
315 |
+
}
|
316 |
+
|
317 |
+
.action-button:hover {
|
318 |
+
transform: translateY(-2px);
|
319 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
|
320 |
+
}
|
321 |
+
|
322 |
+
.history-container {
|
323 |
+
background: #fff8e1;
|
324 |
+
border-left: 5px solid #ffc107;
|
325 |
+
padding: 15px;
|
326 |
+
border-radius: 10px;
|
327 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
328 |
+
margin-top: 20px;
|
329 |
+
}
|
330 |
+
"""
|
331 |
+
|
332 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
333 |
+
gr.HTML(
|
334 |
+
"""
|
335 |
+
<div class="title-container">
|
336 |
+
<h1 style="margin-bottom: 0.5em; font-size: 2.5em;">๐ฒ AI RPG ์ด๋๋ฒค์ฒ</h1>
|
337 |
+
<p style="font-size: 1.2em;">Pixeltable๊ณผ OpenAI๋ก ๊ตฌํ๋ ๋ชฐ์
ํ ๋กคํ๋ ์ ๊ฒ์ ๊ฒฝํ!</p>
|
338 |
+
</div>
|
339 |
+
"""
|
340 |
+
)
|
341 |
+
|
342 |
+
with gr.Row():
|
343 |
+
with gr.Column(scale=1):
|
344 |
+
with gr.Accordion("๐ฏ ์ด ์ฑ์ ๋ฌด์์ธ๊ฐ์?", open=False):
|
345 |
+
gr.HTML(
|
346 |
+
"""
|
347 |
+
<div style="padding: 15px;">
|
348 |
+
<h3>AI RPG ์ด๋๋ฒค์ฒ๋ ๋ค์ ๊ธฐ๋ฅ์ ๋ณด์ฌ์ค๋๋ค:</h3>
|
349 |
+
<ul style="list-style-type: none; padding-left: 5px;">
|
350 |
+
<li>๐ฎ <b>๋์ ์คํ ๋ฆฌํ
๋ง:</b> AI๊ฐ ์์ฑํ๋ ๋ชฐ์
ํ ์ด์ผ๊ธฐ ๊ฒฝํ</li>
|
351 |
+
<li>๐ <b>๊ฒ์ ์ํ ๊ด๋ฆฌ:</b> Pixeltable๋ก ๊ฒ์ ์ํ์ ๊ธฐ๋ก ์ถ์ </li>
|
352 |
+
<li>๐ญ <b>์ปจํ
์คํธ ๊ธฐ๋ฐ ์ ํ์ง:</b> ํ๋ ์ด์ด ํ๋์ ๋ฐ๋ฅธ ๋ง์ถคํ ์ต์
</li>
|
353 |
+
<li>๐ค <b>AI ์คํ ๋ฆฌํ
๋ง:</b> ์์ํ ๋ด๋ฌํฐ๋ธ ์์ฑ</li>
|
354 |
+
<li>๐ <b>์บ๋ฆญํฐ ์ํ ์ถ์ :</b> ๊ฒ์ ์งํ์ ๋ฐ๋ฅธ ์คํฏ ๋ณํ</li>
|
355 |
+
</ul>
|
356 |
+
</div>
|
357 |
+
"""
|
358 |
+
)
|
359 |
+
|
360 |
+
with gr.Accordion("๐จ ์บ๋ฆญํฐ ์์ฑ", open=True):
|
361 |
+
player_name = gr.Textbox(
|
362 |
+
label="๐ค ์บ๋ฆญํฐ ์ด๋ฆ",
|
363 |
+
placeholder="๋น์ ์ ์บ๋ฆญํฐ ์ด๋ฆ์ ์
๋ ฅํ์ธ์...",
|
364 |
+
container=False
|
365 |
+
)
|
366 |
+
genre = gr.Dropdown(
|
367 |
+
choices=[
|
368 |
+
"๐งโโ๏ธ Fantasy",
|
369 |
+
"๐ Sci-Fi",
|
370 |
+
"๐ป Horror",
|
371 |
+
"๐ Mystery",
|
372 |
+
"๐ Post-Apocalyptic",
|
373 |
+
"๐ค Cyberpunk",
|
374 |
+
"โ๏ธ Steampunk"
|
375 |
+
],
|
376 |
+
label="๐ญ ์ฅ๋ฅด ์ ํ",
|
377 |
+
container=False,
|
378 |
+
value="๐งโโ๏ธ Fantasy"
|
379 |
+
)
|
380 |
+
scenario = gr.Textbox(
|
381 |
+
label="๐ ์์ ์๋๋ฆฌ๏ฟฝ๏ฟฝ๏ฟฝ",
|
382 |
+
lines=3,
|
383 |
+
placeholder="์ด๊ธฐ ์ค์ ๊ณผ ์ํฉ์ ์ค๋ช
ํ์ธ์...",
|
384 |
+
container=False
|
385 |
+
)
|
386 |
+
start_button = gr.Button("๐ฎ ๋ชจํ ์์!", variant="primary")
|
387 |
+
|
388 |
+
with gr.Column(scale=2):
|
389 |
+
story_display = gr.Markdown(
|
390 |
+
label="๐ ์คํ ๋ฆฌ",
|
391 |
+
value="<div class='story-container'>๋ชจํ์ ์์ํ๋ ค๋ฉด ์บ๋ฆญํฐ๋ฅผ ์์ฑํ๊ณ '๋ชจํ ์์!' ๋ฒํผ์ ํด๋ฆญํ์ธ์.</div>",
|
392 |
+
show_label=False
|
393 |
+
)
|
394 |
+
|
395 |
+
stats_display = gr.Markdown(
|
396 |
+
label="๐ ์บ๋ฆญํฐ ์คํฏ",
|
397 |
+
value="<div class='stats-container'>๋ชจํ์ ์์ํ๋ฉด ์บ๋ฆญํฐ ์คํฏ์ด ์ด๊ณณ์ ํ์๋ฉ๋๋ค.</div>",
|
398 |
+
show_label=False
|
399 |
+
)
|
400 |
+
|
401 |
+
gr.HTML("<div class='options-container'><h3>๐ฏ ๋ค์ ํ๋ ์ ํ</h3></div>")
|
402 |
+
|
403 |
+
action_input = gr.Radio(
|
404 |
+
choices=[],
|
405 |
+
label="",
|
406 |
+
interactive=True
|
407 |
+
)
|
408 |
+
submit_action = gr.Button("โก ํ๋ ์คํ", variant="primary")
|
409 |
+
|
410 |
+
with gr.Row():
|
411 |
+
with gr.Column():
|
412 |
+
gr.HTML("<div class='history-container'><h3>๐ซ ์ด๋๋ฒค์ฒ ์์</h3></div>")
|
413 |
+
gr.Examples(
|
414 |
+
examples=[
|
415 |
+
["์ด์์ ", "๐งโโ๏ธ Fantasy", "๋น์ ์ ์ํ์ง ์ ๋นํ ์ฒ์ ๊ฐ์ฅ์๋ฆฌ์์ ๋์ ๋น๋๋ค. ๋ฉ๋ฆฌ์ ์ฑ์ ์ฒจํ์ด ๋ณด์ด๊ณ , ๋น์ ์ ๋จธ๋ฆฌ์์๋ ์๊ตญ์ ์ํํ๋ ๊ณ ๋ ๋ง๋ฒ์ ๊ดํ ๋จ์๋ง์ด ๋จ์์์ต๋๋ค. ๊ฐ์๊ธฐ ์ฒ์์ ์ด์ํ ๋น์ด ๋ณด์
๋๋ค..."],
|
416 |
+
["๊น์ง์", "๐ Sci-Fi", "์ฐ์ฃผ์ 'ํธ๋ผ์ด์ฆ'์ ํญํด์ฌ๋ก์, ๋น์ ์ ๋ฏธ์ง์ ํ์ฑ ํ์ฌ ์ค ๋น์ ์๋์ ๊นจ์ด๋ฉ๋๋ค. ์ ์ฅ๊ณผ ์ฐ๋ฝ์ด ๋์ ๋์๊ณ , ์๋ช
์ ์ง ์์คํ
์ด ์ ์ฐจ ์คํจํ๊ณ ์์ต๋๋ค. ์กฐ์ฉํ ์ ๋ด์์ ์ด์ํ ๋ฐ๊ฑธ์ ์๋ฆฌ๊ฐ ๋ค๋ฆฝ๋๋ค..."],
|
417 |
+
["์ผ๋ก ๋จธ์คํฌ", "๐ค Cyberpunk", "2077๋
์์ธ, ๋น์ ์ ๋ด๋ด๋งํฌ ์ธ๋์คํธ๋ฆฌ์ CEO์
๋๋ค. ๋น์ ์ ์ต์ ๋-์ปดํจํฐ ์ธํฐํ์ด์ค ๊ธฐ์ ์ด ์ฌ์ฉ์๋ค์๊ฒ ์์์น ๋ชปํ ๋ฅ๋ ฅ์ ๋ถ์ฌํ๊ธฐ ์์ํ์ต๋๋ค - ๊ทธ๋ค์ ์ง์ ์๋ฌผ๊ณผ ๊ต๊ฐํ ์ ์๊ฒ ๋ ๊ฒ์
๋๋ค. ์ฃผ์ ํฌ์์ ํ๋ ์ ํ
์ด์
์ ์ค๋นํ๋ ๋์, AI ๋น์๊ฐ ํ
์คํธ ์ฐธ๊ฐ์๋ค์ด ๋ถ๊ฐ์ฌ์ํ '์๋ฌผ ํ๋ช
'์ ์กฐ์งํ๊ณ ์๋ค๋ ๋ณด๊ณ ๋ฅผ ์ ํฉ๋๋ค..."],
|
418 |
+
["๊ณ ๋ ๋จ์ง", "๐ Post-Apocalyptic", "๋น์ ์ ๋ด ์์ธ์ ๋ง์ง๋ง ๋ง์คํฐ ์
ฐํ๋ก, ์ ๋ญ์
๋ฆฌ ํธํ
ํํ์์ ์งํ ๋ ์คํ ๋์ ์ด์ํ๊ณ ์์ต๋๋ค. ๋น์ ์ ์๊ทธ๋์ฒ ์๋ฆฌ๋ ์ํํ ๋ฐฉ์ฌ๋ฅ ๊ตฌ์ญ์์๋ง ์๋ผ๋ ํฌ๊ท ๋ฒ์ฏ์ด ํ์ํฉ๋๋ค. ์ค๋ ๋ฐค์ ๋น๋ฐ ๋ชจ์์ ์ค๋นํ๋ ์ค, ์ ์ฐฐ๋ณ์ด ์ฃผ๋ณ ์ง์ญ์ ๊ฒฝ์ ์๋ฆฌ์ฌ ๊ฐฑ๋จ์ ๊ดํ ๋ถ๊ธธํ ์์์ ๊ฐ์ง๊ณ ๋์์ต๋๋ค..."],
|
419 |
+
["์ ์ ํธ", "๐ป Horror", "๋น์ ์ ์น๊ตฌ์ ์ด๋๋ก ์ผ๋ฆผ ์ ์ธ๋ด ๋ณ์ฅ์ ์ฃผ๋ง์ ๋ณด๋ด๋ฌ ์์ต๋๋ค. ์ฒซ๋ ๋ฐค, ์ฐฝ๋ฐ์ผ๋ก ๋ณด์ด๋ ๊ธฐ์ดํ ๋น์ ์ด๋๋ ค ์ฒ์ผ๋ก ๋ค์ด๊ฐ๊ฒ ๋ฉ๋๋ค. ๋์์ค๋ ๊ธธ์ ์ฐพ์ผ๋ ค ํ์ง๋ง, ๋ณ์ฅ์ด ๋ณด์ด์ง ์๊ณ ๋ฏ์ ์๊ฐ๊ฐ ์ ์ ์ง์ด์ง๋๋ค. ๋ฉ๋ฆฌ์ ๋๊ตฐ๊ฐโ์๋, ๋ฌด์ธ๊ฐ๊ฐ ๋น์ ์ ๋ถ๋ฅด๋ ์๋ฆฌ๊ฐ ๋ค๋ฆฝ๋๋ค..."],
|
420 |
+
["๋ฐ์งํ", "๐ Mystery", "์ ์ ํ์ฌ๋ก์ ๋น์ ์ ์ฒซ ์ฌ๊ฑด์ ๋์ ์ต๊ณ ์ ๊ธฐ์ ๊ธฐ์
CEO์ ์๋ฌธ์ ์ค์ข
์
๋๋ค. ๊ทธ์ ์ฌ๋ฌด์ค์๋ ํํ์ด ์๊ณ , ์ ์ผํ ๋จ์๋ ์ฑ
์ ์์ ๋์ธ ์ํธํ๋ ๋ฉ๋ชจ์ ๊บผ์ ธ์๋ ๊ทธ์ ์ต์ฒจ๋จ AI ๋น์๋ฟ์
๋๋ค. ์กฐ์ฌ๋ฅผ ์์ํ์๋ง์, ๋น์ ์ CEO๊ฐ ๋ง์ง๋ง์ผ๋ก ์์
ํ๋ ๋น๋ฐ ํ๋ก์ ํธ์ ๊ดํ ์ด์ผ๊ธฐ๋ฅผ ๋ฃ๊ฒ ๋ฉ๋๋ค..."],
|
421 |
+
["์ด๋ฏผ์", "โ๏ธ Steampunk", "์ฆ๊ธฐ์ ๊ธฐ์ด๋ก ๊ฐ๋ํ ๋ด ์กฐ์ ์์, ๋น์ ์ ํ์ ์ ์ธ ๋นํ์ ์ค๊ณ์์
๋๋ค. ๋น์ ์ ์ต์ ๋ฐ๋ช
ํ ์์ฐ ์ค, ์ ๋ถ์ ๋น๋ฐ ์์์ด ์ ๊ทผํด ์ํ์ ์ฒํ ํฉ์ค ๊ฐ์กฑ์ ์ํ ๋น๋ฐ ์๋ฌด๋ฅผ ์ ์ํฉ๋๋ค. ์งํ ๋จ์ฒด๋ค์ด ์์ข๋ฅผ ์ํํ๊ณ ์์ผ๋ฉฐ, ๋น์ ์ ๋ฐ๋ช
ํ์ด ์๊ฐ์ ์ ์ผํ ํฌ๋ง์ด๋ผ๊ณ ํฉ๋๋ค..."]
|
422 |
+
],
|
423 |
+
inputs=[player_name, genre, scenario]
|
424 |
+
)
|
425 |
+
|
426 |
+
with gr.Column():
|
427 |
+
history_df = gr.Dataframe(
|
428 |
+
headers=["๐
ํด", "๐ฏ ํ๋ ์ด์ด ํ๋", "๐ฌ ๊ฒ์ ๋ฐ์"],
|
429 |
+
label="๐ ๋ชจํ ์ญ์ฌ",
|
430 |
+
wrap=True,
|
431 |
+
row_count=5,
|
432 |
+
col_count=(3, "fixed")
|
433 |
+
)
|
434 |
+
|
435 |
+
def start_new_game(name, genre_choice, scenario_text):
|
436 |
+
if not name or not genre_choice or not scenario_text:
|
437 |
+
return (
|
438 |
+
"<div class='story-container'>๋ชจ๋ ํ๋๋ฅผ ์
๋ ฅํ ํ ์์ํ์ธ์.</div>",
|
439 |
+
"<div class='stats-container'>์คํฏ ์ ๋ณด ์์</div>",
|
440 |
+
[],
|
441 |
+
[]
|
442 |
+
)
|
443 |
+
|
444 |
+
try:
|
445 |
+
_, initial_story, initial_stats, initial_options = game.start_game(name, genre_choice, scenario_text)
|
446 |
+
|
447 |
+
history_df = interactions.select(
|
448 |
+
turn=interactions.turn_number,
|
449 |
+
action=interactions.player_input,
|
450 |
+
response=interactions.story_text
|
451 |
+
).where(
|
452 |
+
interactions.session_id == game.current_session_id
|
453 |
+
).order_by(
|
454 |
+
interactions.turn_number
|
455 |
+
).collect().to_pandas()
|
456 |
+
|
457 |
+
history_data = [
|
458 |
+
[str(row['turn']), row['action'], row['response']]
|
459 |
+
for _, row in history_df.iterrows()
|
460 |
+
]
|
461 |
+
|
462 |
+
story_html = f"<div class='story-container'>{initial_story}</div>"
|
463 |
+
stats_html = f"<div class='stats-container'><h3>๐ ์บ๋ฆญํฐ ์ํ</h3>{initial_stats}</div>"
|
464 |
+
|
465 |
+
return story_html, stats_html, gr.Radio(choices=initial_options, interactive=True), history_data
|
466 |
+
except Exception as e:
|
467 |
+
return (
|
468 |
+
f"<div class='story-container'>๊ฒ์ ์์ ์ค๋ฅ: {str(e)}</div>",
|
469 |
+
"<div class='stats-container'>์คํฏ ์ ๋ณด ์์</div>",
|
470 |
+
[],
|
471 |
+
[]
|
472 |
+
)
|
473 |
+
|
474 |
+
def process_player_action(action_choice):
|
475 |
+
try:
|
476 |
+
if not action_choice:
|
477 |
+
return (
|
478 |
+
"<div class='story-container'>๊ณ์ํ๋ ค๋ฉด ํ๋์ ์ ํํ์ธ์.</div>",
|
479 |
+
"<div class='stats-container'>์คํฏ ์ ๋ณด ์์</div>",
|
480 |
+
[],
|
481 |
+
[]
|
482 |
+
)
|
483 |
+
|
484 |
+
story, stats, options = game.process_action(action_choice)
|
485 |
+
|
486 |
+
history_df = interactions.select(
|
487 |
+
turn=interactions.turn_number,
|
488 |
+
action=interactions.player_input,
|
489 |
+
response=interactions.story_text
|
490 |
+
).where(
|
491 |
+
interactions.session_id == game.current_session_id
|
492 |
+
).order_by(
|
493 |
+
interactions.turn_number
|
494 |
+
).collect().to_pandas()
|
495 |
+
|
496 |
+
history_data = [
|
497 |
+
[str(row['turn']), row['action'], row['response']]
|
498 |
+
for _, row in history_df.iterrows()
|
499 |
+
]
|
500 |
+
|
501 |
+
story_html = f"<div class='story-container'>{story}</div>"
|
502 |
+
stats_html = f"<div class='stats-container'><h3>๐ ์บ๋ฆญํฐ ์ํ</h3>{stats}</div>"
|
503 |
+
|
504 |
+
return story_html, stats_html, gr.Radio(choices=options, interactive=True), history_data
|
505 |
+
except Exception as e:
|
506 |
+
return (
|
507 |
+
f"<div class='story-container'>์ค๋ฅ: {str(e)}</div>",
|
508 |
+
"<div class='stats-container'>์คํฏ ์ ๋ณด ์์</div>",
|
509 |
+
[],
|
510 |
+
[]
|
511 |
+
)
|
512 |
+
|
513 |
+
start_button.click(
|
514 |
+
start_new_game,
|
515 |
+
inputs=[player_name, genre, scenario],
|
516 |
+
outputs=[story_display, stats_display, action_input, history_df]
|
517 |
+
)
|
518 |
+
|
519 |
+
submit_action.click(
|
520 |
+
process_player_action,
|
521 |
+
inputs=[action_input],
|
522 |
+
outputs=[story_display, stats_display, action_input, history_df]
|
523 |
+
)
|
524 |
+
|
525 |
+
gr.HTML("""
|
526 |
+
<div style="text-align: center; margin-top: 30px; padding: 20px; background: #f5f5f5; border-radius: 10px;">
|
527 |
+
<h3>๐ AI RPG ์ด๋๋ฒค์ฒ - Pixeltable๋ก ์ ์๋ ๋ชฐ์
ํ ๋กคํ๋ ์ ๊ฒฝํ</h3>
|
528 |
+
<p>์์ ๋ง์ ์บ๋ฆญํฐ๋ฅผ ๋ง๋ค๊ณ , ์ ํํ ์ฅ๋ฅด์ ์ธ๊ณ์์ ๋ชจํ์ ์ฆ๊ธฐ์ธ์. ๋น์ ์ ์ ํ์ด ์คํ ๋ฆฌ๋ฅผ ํ์ฑํฉ๋๋ค!</p>
|
529 |
+
</div>
|
530 |
+
""")
|
531 |
+
|
532 |
+
return demo
|
533 |
+
|
534 |
+
if __name__ == "__main__":
|
535 |
+
demo = create_interface()
|
536 |
+
demo.launch()
|
537 |
+
|