viggle-mine-workflow / workflow_ui.py
ZIP Space Deployer
Deploy ZIP — 2026-10-14 15:17 UTC
18bfeb9
Raw History Blame Contribute Delete
2.98 kB
# SPDX-License-Identifier: Apache-2.0
"""UI construction can be exercised without downloading the Viggle model."""
from pathlib import Path
import gradio as gr
from gradio.workflow_api import _active_blocks
from gradio.routes import ORJSONResponse
from workflow_nodes import Nodes
from picker_http import install_picker
import uuid
from fastapi.responses import RedirectResponse
def create_workflow(core, *, register_gpu=False):
if gr.__version__ != "6.30.0":
raise RuntimeError(f"This reviewed Workflow requires Gradio 6.30.0, got {gr.__version__}.")
demo = gr.Workflow(graph=str(Path(__file__).with_name("workflow.json")), bind=Nodes(core).bindings())
if register_gpu:
# A genuine decorated function for Spaces startup detection, not a
# fabricated no-op GPU allocation. Register before creating the app.
# Gradio's own helper avoids Blocks.__exit__: reentering the Workflow
# duplicates callable-value load events and recreates the component app.
with _active_blocks(demo):
gr.api(core.render_all_segments, api_name="v22_render_all_segments",
api_visibility="private", concurrency_limit=1, concurrency_id="viggle-v22")
# launch(_app=...) calls create_app itself. Supply a custom-route-only App,
# otherwise every Gradio route and OAuth/session middleware is added twice.
app = type(demo.app)(default_response_class=ORJSONResponse)
@app.middleware("http")
async def workspace_cookie(request, call_next):
# One immutable take counter per browser workspace, including when
# changed upstream data is traversed by a Recovery/API run.
ident = request.cookies.get("vm_workspace", "")
from workflow_artifacts import ID
new = not ID.fullmatch(ident)
if new:
ident = uuid.uuid4().hex
# Make it visible to this request too, before the response cookie.
request.state.vm_workspace = ident
# The generated single-submit App cannot pause for the human picker.
# Use Gradio's supported view parameter, not a custom canvas injection.
if request.url.path == "/" and "ui" not in request.query_params:
# HF terminates TLS before forwarding to the app: request.url can
# therefore say http even while the browser is on HTTPS. An
# absolute redirect would downgrade the embedded page to HTTP.
# Keep the redirect relative to the browser's current origin.
query = request.url.include_query_params(ui="canvas").query
response = RedirectResponse(f"{request.url.path}?{query}", status_code=307)
response.headers["Cache-Control"] = "no-store"
else:
response = await call_next(request)
if new:
response.set_cookie("vm_workspace", ident, httponly=True, samesite="lax", max_age=86400*30)
return response
install_picker(app, core)
return demo, app