Spaces:
Sleeping
Sleeping
File size: 12,014 Bytes
cfd75dc 1418833 cfd75dc 1418833 cfd75dc 1418833 cfd75dc 0845b6c cfd75dc 83b49d3 cfd75dc dcf824e cfd75dc dcf824e cfd75dc 0845b6c cfd75dc c451b20 cfd75dc 5287ab1 34e3bdb cfd75dc 34e3bdb 40a9697 c90268f cfd75dc 40a9697 cfd75dc 42abd03 cfd75dc c90268f cfd75dc 0845b6c cfd75dc c90268f cfd75dc 0845b6c cfd75dc 0845b6c cfd75dc 0845b6c cfd75dc 42abd03 40a9697 42abd03 cfd75dc 0845b6c cfd75dc c90268f cfd75dc 0845b6c cfd75dc 0845b6c cfd75dc 0845b6c c451b20 0845b6c c90268f 0845b6c c451b20 0845b6c cfd75dc 0845b6c cfd75dc |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
import gradio as gr
from css import custom_css, loading_css_styles
from audio.audio_generator import (
update_audio,
cleanup_music_session,
)
import logging
from agent.runner import process_step
import uuid
from game_constructor import (
SETTING_SUGGESTIONS,
CHARACTER_SUGGESTIONS,
GENRE_OPTIONS,
load_setting_suggestion,
load_character_suggestion,
start_game_with_settings,
)
CONCURRENCY_LIMIT = 100
logger = logging.getLogger(__name__)
async def return_to_constructor(user_hash: str):
"""Return to the constructor and reset user state and audio."""
from agent.redis_state import reset_user_state
await reset_user_state(user_hash)
await cleanup_music_session(user_hash)
return (
gr.update(visible=False), # loading_indicator
gr.update(visible=True), # constructor_interface
gr.update(visible=False), # game_interface
gr.update(visible=False), # error_message
)
async def generate_user_hash():
hash = str(uuid.uuid4())
logger.info(f"Generated user hash: {hash}")
return gr.update(value=hash)
async def update_scene(user_hash: str, choice):
logger.info(f"Updating scene with choice: {choice}")
if not isinstance(choice, str):
return gr.update(), gr.update(), gr.update(), gr.update()
result = await process_step(
user_hash=user_hash,
step="choose",
choice_text=choice,
)
if result.get("game_over"):
ending = result["ending"]
ending_text = (
ending.get("description") or ending.get("condition", "")
) + "\n[THE END]"
ending_image = result.get("image")
return (
gr.update(value=ending_text),
gr.update(value=ending_image),
gr.Radio(choices=[], label="", value=None, visible=False),
gr.update(value="", visible=False),
)
scene = result["scene"]
return (
scene["description"],
scene.get("image", ""),
gr.Radio(
choices=[ch["text"] for ch in scene.get("choices", [])],
label="What do you choose? (select an option or write your own)",
value=None,
elem_classes=["choice-buttons"],
),
gr.update(value=""),
)
def update_preview(setting, name, age, background, personality, genre):
"""Update the configuration preview"""
if not any([setting, name, age, background, personality]):
return "Fill in the fields to see a preview..."
preview = f"""๐ SETTING: {setting[:100]}{"..." if len(setting) > 100 else ""}
๐ค CHARACTER: {name} (Age: {age})
๐ Background: {background}
๐ญ Personality: {personality}
๐ญ GENRE: {genre}"""
return preview
async def start_game_with_music(
user_hash: str,
setting_desc: str,
char_name: str,
char_age: str,
char_background: str,
char_personality: str,
genre: str,
):
"""Start the game with custom settings and initialize music"""
yield (
gr.update(visible=True), # loading indicator
gr.update(), # constructor_interface
gr.update(), # game_interface
gr.update(), # error_message
gr.update(),
gr.update(),
gr.update(), # game components unchanged
gr.update(), # custom choice unchanged
)
# First, get the game interface updates
result = await start_game_with_settings(
user_hash,
setting_desc,
char_name,
char_age,
char_background,
char_personality,
genre,
)
yield result
with gr.Blocks(
theme="soft",
title="Game Constructor & Visual Novel",
css=custom_css + loading_css_styles,
) as demo:
# Fullscreen Loading Indicator (hidden by default)
with gr.Column(visible=False, elem_id="loading-indicator") as loading_indicator:
gr.HTML("<div class='loading-text'>๐ Starting your adventure...</div>")
local_storage = gr.BrowserState("", "user_hash")
# Constructor Interface (visible by default)
with gr.Column(
visible=True, elem_id="constructor-interface"
) as constructor_interface:
gr.Markdown("# ๐ฎ Interactive Game Constructor")
gr.Markdown(
"Create your own interactive story game by defining the setting, character, and genre!"
)
# Error message area
error_message = gr.Textbox(
label="โ ๏ธ Error",
visible=False,
interactive=False,
elem_classes=["error-message"],
)
with gr.Row():
with gr.Column(scale=2):
# Setting Description Section
with gr.Group():
gr.Markdown("## ๐ Setting Description")
setting_suggestions = gr.Dropdown(
choices=["Select a suggestion..."] + SETTING_SUGGESTIONS,
label="Quick Suggestions",
value="Select a suggestion...",
interactive=True,
)
setting_description = gr.Textbox(
label="Describe your game setting",
placeholder="Enter a detailed description of where your story takes place...",
lines=4,
max_lines=6,
)
# Character Description Section
with gr.Group():
gr.Markdown("## ๐ค Character Description")
character_suggestions = gr.Dropdown(
choices=["None"]
+ [
f"{char['name']} - {char['background'][:50]}..."
for char in CHARACTER_SUGGESTIONS
],
label="Character Templates",
value="None",
interactive=True,
)
with gr.Row():
char_name = gr.Textbox(
label="Character Name",
placeholder="Enter character name...",
)
char_age = gr.Textbox(label="Age", placeholder="25")
char_background = gr.Textbox(
label="Background/Profession",
placeholder="Describe your character's background, profession, or role...",
lines=2,
)
char_personality = gr.Textbox(
label="Personality & Traits",
placeholder="Describe personality, quirks, motivations, fears...",
lines=2,
)
# Genre Selection Section
with gr.Group():
gr.Markdown("## ๐ญ Genre & Style")
genre_selection = gr.Dropdown(
choices=GENRE_OPTIONS,
label="Choose your story genre",
value=GENRE_OPTIONS[0],
interactive=True,
)
with gr.Column(scale=1):
# Preview Section
with gr.Group():
gr.Markdown("## ๐ Configuration Preview")
preview_box = gr.Textbox(
label="Game Summary",
lines=8,
interactive=False,
placeholder="Fill in the fields to see a preview...",
)
with gr.Group():
gr.Markdown("## ๐ฎ Ready to Play?")
start_btn = gr.Button("โถ๏ธ Start Game", variant="primary", size="lg")
with gr.Column(visible=False, elem_id="game-interface") as game_interface:
gr.Markdown("# ๐ฎ Your Interactive Story")
with gr.Row():
gr.Markdown("### Playing your custom game!")
back_btn = gr.Button(
"โฌ
๏ธ Back to Constructor",
variant="secondary",
elem_id="back-btn",
)
# Audio component for background music
audio_out = gr.Audio(
autoplay=True, streaming=True, interactive=False, visible=False
)
# Background image (fullscreen)
with gr.Column(elem_classes=["image-container"]):
game_image = gr.Image(type="filepath", interactive=False, show_label=False)
# Overlay content (text and buttons)
with gr.Column(elem_classes=["overlay-content"]):
game_text = gr.Textbox(
label="",
interactive=False,
show_label=False,
elem_classes=["narrative-text"],
lines=3,
)
with gr.Column(elem_classes=["choice-area"]):
game_choices = gr.Radio(
choices=[],
label="What do you choose? (select an option or write your own)",
value=None,
elem_classes=["choice-buttons"],
)
custom_choice = gr.Textbox(
label="",
show_label=False,
placeholder="Type your option and press Enter",
lines=1,
elem_classes=["choice-input"],
)
# Event handlers for constructor interface
setting_suggestions.change(
fn=load_setting_suggestion,
inputs=[setting_suggestions],
outputs=[setting_description],
)
character_suggestions.change(
fn=load_character_suggestion,
inputs=[character_suggestions],
outputs=[char_name, char_age, char_background, char_personality],
)
# Update preview when any field changes
for component in [
setting_description,
char_name,
char_age,
char_background,
char_personality,
genre_selection,
]:
component.change(
fn=update_preview,
inputs=[
setting_description,
char_name,
char_age,
char_background,
char_personality,
genre_selection,
],
outputs=[preview_box],
)
# Interface switching handlers
start_btn.click(
fn=start_game_with_music,
inputs=[
local_storage,
setting_description,
char_name,
char_age,
char_background,
char_personality,
genre_selection,
],
outputs=[
loading_indicator,
constructor_interface,
game_interface,
error_message,
game_text,
game_image,
game_choices,
custom_choice,
],
concurrency_limit=CONCURRENCY_LIMIT,
)
back_btn.click(
fn=return_to_constructor,
inputs=[local_storage],
outputs=[
loading_indicator,
constructor_interface,
game_interface,
error_message,
],
)
game_choices.change(
fn=update_scene,
inputs=[local_storage, game_choices],
outputs=[game_text, game_image, game_choices, custom_choice],
concurrency_limit=CONCURRENCY_LIMIT,
)
custom_choice.submit(
fn=update_scene,
inputs=[local_storage, custom_choice],
outputs=[game_text, game_image, game_choices, custom_choice],
concurrency_limit=CONCURRENCY_LIMIT,
)
demo.unload(cleanup_music_session)
demo.load(
fn=generate_user_hash,
inputs=[],
outputs=[local_storage],
)
local_storage.change(
fn=update_audio,
inputs=[local_storage],
outputs=[audio_out],
concurrency_limit=CONCURRENCY_LIMIT,
)
demo.launch(ssr_mode=False)
|