AndrewRqy commited on
Commit ·
744bc9b
1
Parent(s): 8d2a52b
Add visual-mode dropdown on step 0 — live toggle between lean and full
Browse filesFiles changed (2)
- app.py+66-27
- oracles/state.py+7
75 unmodified lines7677787980818283848586878889909192939495443 unmodified lines53954054154254354454544 unmodified lines590591592593594595596129 unmodified lines7267277287297307317328 unmodified lines74174274374474574674794 unmodified lines84284384484584684784818 unmodified lines8678688698708718728732599 unmodified lines3473347434753476347734783479515 unmodified lines39953996399739983999400040014002400340 unmodified lines40444045404640474048404940503 unmodified lines405440554056405740584059406040614062406340644065406640671 unmodified line4069407040714072407340744075599 unmodified lines467546764677467846794680208 unmodified lines4889489048914892489348944895489675 unmodified lines_GRIMOIRE_TEXTURE_PNG = _HERE / "assets" / "sprites" / "grimoire_page_texture.png"def _full_visual_mode() -> bool:"""Visual-mode toggle. Read from $ORACLES_VISUAL_MODE on every call soit stays accurate across reloads.- ``lean`` (default, HF Space): skip heavy decorative PNGs — parallaxbanner, grimoire parchment, the four phase backdrops, the scenelandscapes, the wizard-desk backdrop, the open-book illustration,and the pipeline-demo card backdrop. Total saving: ~2-3 MB percold load on HF Space's slow egress bandwidth.- ``full``: enable everything. Use locally where bandwidth is fast.To run locally in full mode: ``ORACLES_VISUAL_MODE=full ./run.sh``or pass ``--full`` to ``run.sh`` (which sets the env var)."""return os.environ.get("ORACLES_VISUAL_MODE", "lean").strip().lower() == "full"443 unmodified lines# Lean mode (default for HF Space): skip the open-book illustration —# the PNG was ~200-300 KB. The heading + numbered list carry the# meaning. Full mode restores the illustration above the list.if _full_visual_mode():book_uri = _sprite_data_uri_app("open_book_spread")alt_text = _t("alt_sealed_grimoire", lang)book_img = (44 unmodified lines# Lean mode (default for HF Space): skip the demo card painted# backdrop (~200-400 KB). Panels still have CSS borders + shading.# Full mode restores the painted background.if _full_visual_mode():bg = _sprite_data_uri_app("demo_card_backdrop")bg_style = f"background-image:url('{bg}');" if bg else ""else:129 unmodified linesgetattr(state, "theme", "fantasy") if state else "fantasy") or "fantasy"lang_for_cache = _tlang(state)cache_key = (theme_key_for_sprites, lang_for_cache)if cache_key in _INSCRIBE_DECORATION_CACHE:return _INSCRIBE_DECORATION_CACHE[cache_key]from oracles.themes import get_theme as _get_theme8 unmodified lines# Lean mode (default for HF Space): skip the wizard-desk backdrop —# the PNG is ~200-400 KB. Full mode restores it as a fade-behind# layer for the mentor/apprentice figures.desk_uri = _sprite_data_uri_app("wizard_desk_scene") if _full_visual_mode() else ""if not wizard_uri or not hero_uri:fallback = ("<div class='oracle-inscribe-figs' style='text-align:center;"94 unmodified linestheme_key = getattr(state, "theme", "fantasy") if state else "fantasy"lang = _tlang(state)mode = getattr(state, "mode", "inscribe") if state else "inscribe"cache_key = (theme_key, lang, mode)if cache_key in _BANNER_HTML_CACHE:return _BANNER_HTML_CACHE[cache_key]from oracles.themes import get_theme as _get_theme18 unmodified lines# Lean mode (default for HF Space): use a CSS gradient instead of the# ~455 KB parallax PNG. Full mode loads the PNG via Gradio's file# route so the browser caches it after the first request.if _full_visual_mode() and _PARALLAX_PNG.exists():src = f"{_FILE_URL_PREFIX}{_url_quote(_PARALLAX_PNG.as_posix(), safe='/')}"result = (f"<div class='oracle-banner{extra_cls}'>"2599 unmodified lines# — each scene_<theme>_landscape PNG is ~200-500 KB and slow over HF's# egress. The hero + obstacle sprites still load and the panel uses a# CSS gradient sky. Full mode restores the landscape.env_uri = _theme_pick(env_name, is_env=True) if _full_visual_mode() else ""hero_uri = _theme_pick("hero")ob_uri = _theme_pick(ob_name) if ob_name else ""515 unmodified lineslanguage_label: str,theme_label: str = "",narration_label: str = "",) -> GameState:"""Mutate state from the three spread-0 dropdown values. Shared bythe live-preview .change() handler and the click-to-advance button."""from oracles.themes import THEMES, DEFAULT_THEMEfrom oracles.state import NARRATION_LENGTHS as _NL40 unmodified linesdef handle_lang_theme_preview(state: GameState, language_label: str,theme_label: str = "",narration_label: str = ""):"""Live-preview handler bound to the dropdowns' ``.change()`` event.Re-renders every theme/lang-driven slot (banner title, palette,3 unmodified linesor language."""if state.mode != "inscribe" or state.inscribe_step != 0:return _ui_bundle(state)_apply_spread0_choices(state, language_label, theme_label, narration_label)state.grimoire_error = ""return _ui_bundle(state)def handle_grimoire_lang(state: GameState, language_label: str,theme_label: str = "",narration_label: str = ""):"""Step 0 → 1: apply the language/theme/narration picks and advance.The actual mutation lives in `_apply_spread0_choices` so the live-1 unmodified linethe step."""if state.mode != "inscribe" or state.inscribe_step != 0:return _ui_bundle(state)_apply_spread0_choices(state, language_label, theme_label, narration_label)state.grimoire_error = ""state.inscribe_step = 1return _ui_bundle(state)599 unmodified lines"different choice."),)grimoire_lang_btn = gr.Button("Inscribe my choice →", variant="primary", size="lg")208 unmodified lines# in the new theme/language WITHOUT advancing the step. The# actual step advance still requires clicking the button._spread0_inputs = [state, language_dropdown, theme_dropdown,narration_length_dropdown]for _dd in (language_dropdown, theme_dropdown, narration_length_dropdown):_dd.change(handle_lang_theme_preview,inputs=_spread0_inputs,75 unmodified lines767778798081828384858687888990919293949596979899443 unmodified lines54354454554654754854944 unmodified lines594595596597598599600129 unmodified lines7307317327337347357368 unmodified lines74574674774874975075194 unmodified lines84684784884985085185285318 unmodified lines8728738748758768778782599 unmodified lines3478347934803481348234833484515 unmodified lines400040014002400340044005400640074008400940104011401240134014401540 unmodified lines405640574058405940604061406240633 unmodified lines40674068406940704071407240734074407540764077407840794080408140821 unmodified line40844085408640874088408940904091599 unmodified lines4691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718208 unmodified lines49274928492949304931493249334934493575 unmodified lines_GRIMOIRE_TEXTURE_PNG = _HERE / "assets" / "sprites" / "grimoire_page_texture.png"def _full_visual_mode(state: Optional["GameState"] = None) -> bool:"""Visual-mode toggle. Resolution order:1. ``state.visual_mode`` if set to "full" or "lean" (per-session override).2. ``$ORACLES_VISUAL_MODE`` env var (process-wide default).3. ``lean``.- ``lean``: skip heavy decorative PNGs — parallax banner, grimoireparchment, the four phase backdrops, the scene landscapes, thewizard-desk backdrop, the open-book illustration, and thepipeline-demo card backdrop. ~2-3 MB saved per cold load on HFSpace's slow egress bandwidth.- ``full``: enable everything. Use locally where bandwidth is fast,or flip the dropdown on inscribe step 0."""if state is not None:vm = (getattr(state, "visual_mode", "") or "").strip().lower()if vm in ("full", "lean"):return vm == "full"return os.environ.get("ORACLES_VISUAL_MODE", "lean").strip().lower() == "full"443 unmodified lines# Lean mode (default for HF Space): skip the open-book illustration —# the PNG was ~200-300 KB. The heading + numbered list carry the# meaning. Full mode restores the illustration above the list.if _full_visual_mode(state):book_uri = _sprite_data_uri_app("open_book_spread")alt_text = _t("alt_sealed_grimoire", lang)book_img = (44 unmodified lines# Lean mode (default for HF Space): skip the demo card painted# backdrop (~200-400 KB). Panels still have CSS borders + shading.# Full mode restores the painted background.if _full_visual_mode(state):bg = _sprite_data_uri_app("demo_card_backdrop")bg_style = f"background-image:url('{bg}');" if bg else ""else:129 unmodified linesgetattr(state, "theme", "fantasy") if state else "fantasy") or "fantasy"lang_for_cache = _tlang(state)cache_key = (theme_key_for_sprites, lang_for_cache, _full_visual_mode(state))if cache_key in _INSCRIBE_DECORATION_CACHE:return _INSCRIBE_DECORATION_CACHE[cache_key]from oracles.themes import get_theme as _get_theme8 unmodified lines# Lean mode (default for HF Space): skip the wizard-desk backdrop —# the PNG is ~200-400 KB. Full mode restores it as a fade-behind# layer for the mentor/apprentice figures.desk_uri = _sprite_data_uri_app("wizard_desk_scene") if _full_visual_mode(state) else ""if not wizard_uri or not hero_uri:fallback = ("<div class='oracle-inscribe-figs' style='text-align:center;"94 unmodified linestheme_key = getattr(state, "theme", "fantasy") if state else "fantasy"lang = _tlang(state)mode = getattr(state, "mode", "inscribe") if state else "inscribe"vm_full = _full_visual_mode(state)cache_key = (theme_key, lang, mode, vm_full)if cache_key in _BANNER_HTML_CACHE:return _BANNER_HTML_CACHE[cache_key]from oracles.themes import get_theme as _get_theme18 unmodified lines# Lean mode (default for HF Space): use a CSS gradient instead of the# ~455 KB parallax PNG. Full mode loads the PNG via Gradio's file# route so the browser caches it after the first request.if _full_visual_mode(state) and _PARALLAX_PNG.exists():src = f"{_FILE_URL_PREFIX}{_url_quote(_PARALLAX_PNG.as_posix(), safe='/')}"result = (f"<div class='oracle-banner{extra_cls}'>"2599 unmodified lines# — each scene_<theme>_landscape PNG is ~200-500 KB and slow over HF's# egress. The hero + obstacle sprites still load and the panel uses a# CSS gradient sky. Full mode restores the landscape.env_uri = _theme_pick(env_name, is_env=True) if _full_visual_mode(state) else ""hero_uri = _theme_pick("hero")ob_uri = _theme_pick(ob_name) if ob_name else ""515 unmodified lineslanguage_label: str,theme_label: str = "",narration_label: str = "",visual_mode_label: str = "",) -> GameState:"""Mutate state from the four spread-0 dropdown values. Shared bythe live-preview .change() handler and the click-to-advance button."""if visual_mode_label:# Map the human label back to the internal key.if "Full" in visual_mode_label:state.visual_mode = "full"elif "Lean" in visual_mode_label:state.visual_mode = "lean"from oracles.themes import THEMES, DEFAULT_THEMEfrom oracles.state import NARRATION_LENGTHS as _NL40 unmodified linesdef handle_lang_theme_preview(state: GameState, language_label: str,theme_label: str = "",narration_label: str = "",visual_mode_label: str = ""):"""Live-preview handler bound to the dropdowns' ``.change()`` event.Re-renders every theme/lang-driven slot (banner title, palette,3 unmodified linesor language."""if state.mode != "inscribe" or state.inscribe_step != 0:return _ui_bundle(state)_apply_spread0_choices(state, language_label, theme_label,narration_label, visual_mode_label)state.grimoire_error = ""return _ui_bundle(state)def handle_grimoire_lang(state: GameState, language_label: str,theme_label: str = "",narration_label: str = "",visual_mode_label: str = ""):"""Step 0 → 1: apply the language/theme/narration picks and advance.The actual mutation lives in `_apply_spread0_choices` so the live-1 unmodified linethe step."""if state.mode != "inscribe" or state.inscribe_step != 0:return _ui_bundle(state)_apply_spread0_choices(state, language_label, theme_label,narration_label, visual_mode_label)state.grimoire_error = ""state.inscribe_step = 1return _ui_bundle(state)599 unmodified lines"different choice."),)# Visual-mode selector — flips the full-fat decorative# PNGs (parallax banner, scene landscapes, etc.) on# and off live. Default reads from the env var the# process launched with (HF Space → lean, ./run.sh# --full → full). The player can override either way._vm_initial = ("Full visuals (all backdrops)"if _full_visual_mode() else"Lean (fast on slow networks)")visual_mode_dropdown = gr.Dropdown(label="Visuals",choices=["Lean (fast on slow networks)","Full visuals (all backdrops)",],value=_vm_initial,info=("Lean skips the heavy decorative PNGs for ""fast loading. Full adds the parallax banner, ""scene landscapes, parchment, etc. The choice ""applies instantly."),)grimoire_lang_btn = gr.Button("Inscribe my choice →", variant="primary", size="lg")208 unmodified lines# in the new theme/language WITHOUT advancing the step. The# actual step advance still requires clicking the button._spread0_inputs = [state, language_dropdown, theme_dropdown,narration_length_dropdown, visual_mode_dropdown]for _dd in (language_dropdown, theme_dropdown, narration_length_dropdown,visual_mode_dropdown):_dd.change(handle_lang_theme_preview,inputs=_spread0_inputs,
99 unmodified lines10010110210310410599 unmodified linesepilogue: str = ""lang: str = "en" # language code, see LANG_LABELStheme: str = "fantasy" # theme key, see oracles.themes.THEMES# Precomputed resolution cache: (oracle_index, obstacle_index) -> Resolution.# Populated by a background thread kicked off in send_off so that clicking99 unmodified lines10010110210310410510610710810911011111299 unmodified linesepilogue: str = ""lang: str = "en" # language code, see LANG_LABELStheme: str = "fantasy" # theme key, see oracles.themes.THEMES# Per-session visual mode override. "lean" skips heavy decorative PNGs# (parallax banner, parchment, phase backdrops, scene landscapes, etc.)# for fast loading. "full" enables them. Empty string ⇒ fall back to# the ORACLES_VISUAL_MODE env var. The session picks the initial value# at fresh_state() time and the player can flip it from the inscribe# step-0 dropdown next to the language picker.visual_mode: str = ""# Precomputed resolution cache: (oracle_index, obstacle_index) -> Resolution.# Populated by a background thread kicked off in send_off so that clicking
app.py+66-27
oracles/state.py+7