Spaces:
Running
Running
File size: 10,839 Bytes
86b351a c55ccab 86b351a c55ccab 86b351a c55ccab 86b351a c55ccab 86b351a c55ccab 86b351a c55ccab 86b351a c55ccab 86b351a c55ccab 86b351a c55ccab 86b351a c55ccab 86b351a c55ccab 86b351a |
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 |
import gradio as gr
from css import custom_css, loading_css_styles
from audio.audio_generator import (
update_audio,
change_music_tone,
cleanup_music_session,
)
import logging
from agent.llm_agent import process_user_input
from images.image_generator import generate_image
import uuid
from game_state import story, state
from game_constructor import (
SETTING_SUGGESTIONS,
CHARACTER_SUGGESTIONS,
GENRE_OPTIONS,
load_setting_suggestion,
load_character_suggestion,
start_game_with_settings,
)
import asyncio
logger = logging.getLogger(__name__)
def return_to_constructor():
"""Return to the game constructor interface, ensure loading is hidden."""
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 update_scene(request: gr.Request, choice):
logger.info(f"Updating scene with choice: {choice}")
if isinstance(choice, str):
old_scene = state["scene"]
new_scene = str(uuid.uuid4())
story[new_scene] = {
**story[old_scene],
}
state["scene"] = new_scene
user_story = f"""Current scene description:
{story[old_scene]["text"]}
User's choice: {choice}
"""
response = await process_user_input(user_story)
story[new_scene]["text"] = response.game_message
story[new_scene]["choices"] = [
option.option_description for option in response.player_options
]
if response.change_scene.change_scene:
img_path, _ = await generate_image(response.change_scene.scene_description)
if img_path:
story[new_scene]["image"] = img_path
if response.change_music.change_music:
await change_music_tone(request, response.change_music.music_description)
scene = story[state["scene"]]
return (
scene["text"],
scene["image"],
gr.Radio(
choices=scene["choices"],
label="What do you choose?",
value=None,
elem_classes=["choice-buttons"],
),
)
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(
request: gr.Request,
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
)
# First, get the game interface updates
result = await start_game_with_settings(
request,
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>")
# 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():
back_btn = gr.Button("โฌ
๏ธ Back to Constructor", variant="secondary")
gr.Markdown("### Playing your custom game!")
# 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,
)
game_choices = gr.Radio(
choices=[],
label="What do you choose?",
value=None,
elem_classes=["choice-buttons"],
)
# 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=[
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,
],
)
back_btn.click(
fn=return_to_constructor,
inputs=[],
outputs=[
loading_indicator,
constructor_interface,
game_interface,
error_message,
],
)
game_choices.change(
fn=update_scene,
inputs=[game_choices],
outputs=[game_text, game_image, game_choices],
)
demo.unload(cleanup_music_session)
demo.load(
fn=update_audio,
inputs=None,
outputs=[audio_out],
)
demo.launch()
|