Spaces:
Running
Running
File size: 11,837 Bytes
86b351a c55ccab 86b351a d8e2b36 86b351a 21fc892 0a18f7d 9730d19 86b351a ee968a7 86b351a d8e2b36 ee968a7 d8e2b36 ee968a7 d8e2b36 86b351a c55ccab 86b351a 21fc892 1af8d03 21fc892 66d8948 0a18f7d 1af8d03 0a18f7d 86b351a 21fc892 a277e33 86b351a d8e2b36 9ec6bf4 86b351a d8e2b36 4310b90 d8e2b36 9ec6bf4 d8e2b36 9ec6bf4 d9ec72e 4310b90 d8e2b36 86b351a d8e2b36 86b351a d8e2b36 9ec6bf4 86b351a 9ec6bf4 86b351a a277e33 86b351a c55ccab 9ec6bf4 c55ccab 86b351a a277e33 c55ccab 86b351a c55ccab 86b351a c55ccab 86b351a c55ccab 4310b90 45fabe9 c55ccab 86b351a c55ccab f42cab1 c55ccab 21fc892 86b351a 21fc892 86b351a d8e2b36 86b351a 9ec6bf4 86b351a 45fabe9 86b351a c55ccab 86b351a 9ec6bf4 86b351a 45fabe9 c55ccab 86b351a 21fc892 2e43905 86b351a 9ec6bf4 45fabe9 9ec6bf4 86b351a 0a18f7d 45fabe9 0a18f7d 45fabe9 86b351a 45fabe9 86b351a 45fabe9 2e43905 cf0b823 |
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 |
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,
)
from app_description import app_description
CONCURRENCY_LIMIT = 10000
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
)
num_visits = 0
async def generate_user_hash():
hash = str(uuid.uuid4())
global num_visits
num_visits += 1
logger.info(f"Generated user hash: {hash}, num_visits: {num_visits}")
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>")
ls_user_hash = gr.BrowserState("", "user_hash")
# Constructor Interface (visible by default)
with gr.Column(
visible=True, elem_id="constructor-interface", elem_classes=["constructor-page"]
) as constructor_interface:
with gr.Row():
app_description()
with gr.Row():
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:
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=[
ls_user_hash,
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,
],
)
back_btn.click(
fn=return_to_constructor,
inputs=[ls_user_hash],
outputs=[
loading_indicator,
constructor_interface,
game_interface,
error_message,
],
)
game_choices.change(
fn=update_scene,
inputs=[ls_user_hash, game_choices],
outputs=[game_text, game_image, game_choices, custom_choice],
concurrency_limit=CONCURRENCY_LIMIT,
)
custom_choice.submit(
fn=update_scene,
inputs=[ls_user_hash, custom_choice],
outputs=[game_text, game_image, game_choices, custom_choice],
)
demo.unload(cleanup_music_session)
demo.load(
fn=generate_user_hash,
inputs=[],
outputs=[ls_user_hash],
)
ls_user_hash.change(
fn=update_audio,
inputs=[ls_user_hash],
outputs=[audio_out],
)
demo.queue(default_concurrency_limit=CONCURRENCY_LIMIT)
demo.launch(ssr_mode=False)
|