"""Data-only schema-v2 graphs for Workflow Studio's local generation function. Source Space IDs describe preset provenance, never execution destinations. The app binds ``generate_video`` to its complete local Qwen/H3/MP4 request; model stages must not become separate operators or GPU allocations. Admission describes the bundled input defaults only. The generation function must recheck the actual request. A rejected estimate stays rejected, with no replacement estimate or cap. Importing this module does not import the app, Gradio, model libraries, credentials, or network clients. """ from __future__ import annotations from preset_core import list_presets, plan_request from studio_service import WORKFLOW_RESERVATION_CAP_S, workflow_reservation_seconds _DEFAULT_PROMPT = "A small robot walks across a desk." _DEFAULT_SECONDS = 2 _DEFAULT_SEED = 42 _MODES = { "t2va": "Text to video", "i2va": "First frame to video", "fl2va": "First + last frame to video", } # Declaration order is the bound function's positional call contract. _INPUTS = ( ("preset_id", "text"), ("prompt", "text"), ("image_path", "image"), ("last_image_path", "image"), ("canvas", "text"), ("duration", "number"), ("seed", "number"), ) # Choice inputs stay unwired so the operator renders them as dropdowns. _INLINE = {"preset_id", "canvas"} # Two input columns, one local operator, and an output column fit 1300 x 650. _REFERENCE_LAYOUT = { "prompt": (20, 20, 300, 170), "image_path": (20, 210, 300, 125), "last_image_path": (20, 355, 300, 125), "duration": (350, 20, 230, 100), "seed": (350, 135, 230, 100), } def _asset_node(node_id, label, role, asset_type, layout, value=None, choices=None): x, y, width, height = layout output = {"id": "out", "label": label, "type": asset_type} if role == "reference": output["default_value"] = value if choices is not None: output["choices"] = list(choices) return { "id": node_id, "label": label, "role": role, "asset_type": asset_type, "inputs": [{"id": "in", "label": label, "type": asset_type}], "outputs": [output], "x": x, "y": y, "width": width, "height": height, "data": {"out": value} if role == "reference" else {}, } def _edge(edge_id, source, source_port, target, target_port, asset_type): return { "id": edge_id, "from_node_id": source, "from_port_id": source_port, "to_node_id": target, "to_port_id": target_port, "type": asset_type, } def _template(preset: dict, mode: str) -> dict: has_first, has_last = mode in {"i2va", "fl2va"}, mode == "fl2va" defaults = { "preset_id": preset["id"], "prompt": _DEFAULT_PROMPT, "image_path": None, "last_image_path": None, "canvas": preset["default_canvas"], "duration": _DEFAULT_SECONDS, "seed": _DEFAULT_SEED, } plan = plan_request( preset["id"], _DEFAULT_PROMPT, canvas=preset["default_canvas"], duration=_DEFAULT_SECONDS, seed=_DEFAULT_SEED, has_first=has_first, has_last=has_last, ) try: seconds = workflow_reservation_seconds(plan, has_first, has_last) except ValueError as error: admission = { "allowed": False, "reservation_seconds": None, "cap_seconds": WORKFLOW_RESERVATION_CAP_S, "reason": str(error), } admission_text = f"Default admission blocked: {error}." else: admission = { "allowed": True, "reservation_seconds": seconds, "cap_seconds": WORKFLOW_RESERVATION_CAP_S, "reason": None, } admission_text = ( f"Default admission admitted: {seconds} s reservation, " f"{WORKFLOW_RESERVATION_CAP_S} s cap." ) name = f"{preset['label']} · {_MODES[mode]}" description = ( f"{preset['description']} Preset {preset['id']}, {preset['steps']} steps, " f"adapter {preset['adapter']}; eager, compiled=false, {preset['verification']}. " f"Source Space settings are provenance only: {', '.join(preset['source_space_ids'])} " f"at {preset['artifact_revision']}. Mode {mode}; defaults {_DEFAULT_SECONDS} s, " f"{preset['default_canvas']}, seed {_DEFAULT_SEED}. " "One LOCAL generate_video call groups Qwen conditioning, H3 generation and " "MP4 output; model stages share one ZeroGPU allocation. " f"{admission_text} Conservative admission budgeting, not measured latency " "or runtime readiness; rechecked for the actual inputs at execution." ) labels = { "prompt": "Prompt", "image_path": "First frame · required", "last_image_path": "Last frame · required", "duration": "Seconds", "seed": "Seed", } choices = {"preset_id": [preset["id"]], "canvas": preset["allowed_canvases"]} references, inputs, edges, inline = [], [], [], {} for index, (argument, asset_type) in enumerate(_INPUTS): included = not ((argument == "image_path" and not has_first) or (argument == "last_image_path" and not has_last)) port_id = f"in_{index}" port = { "id": port_id, "label": argument, "type": asset_type, "default_value": defaults[argument], "required": included, } if argument in choices: port["choices"] = list(choices[argument]) inputs.append(port) if argument in _INLINE: inline[port_id] = defaults[argument] elif included: node_id = f"ref_{argument}" references.append(_asset_node( node_id, labels[argument], "reference", asset_type, _REFERENCE_LAYOUT[argument], defaults[argument], )) edges.append(_edge( f"e_{argument}", node_id, "out", "op_generate", port_id, asset_type, )) outputs, subjects = [], [] for index, (argument, asset_type, layout) in enumerate(( ("video", "video", (960, 20, 300, 280)), ("metadata", "json", (960, 330, 300, 290)), )): port_id, node_id = f"out_{index}", f"sub_{argument}" outputs.append({ "id": port_id, "label": argument, "type": asset_type, "output_index": index, }) subjects.append(_asset_node( node_id, argument.capitalize(), "subject", asset_type, layout, )) edges.append(_edge( f"e_{argument}", "op_generate", port_id, node_id, "in", asset_type, )) graph = { "schema_version": "2", "name": name, "description": description, "runtime": {"default": "client"}, "view": {"default": "canvas"}, "references": references, "operators": [{ "id": "op_generate", "label": "generate_video · LOCAL", "role": "operator", "kind": "fn", "source": "fn", "fn": "generate_video", "inputs": inputs, "outputs": outputs, "x": 620, "y": 125, "width": 300, "height": 390, "data": inline, }], "subjects": subjects, "edges": edges, } return { "id": f"{preset['id']}-{mode}", "name": name, "description": description, "graph": graph, "preset_id": preset["id"], "mode": mode, "source_space_ids": list(preset["source_space_ids"]), "admission": admission, } def list_templates() -> list[dict]: """Return fresh catalog entries, ordered by preset then stable keyframe modes. Each entry contains id, name, description, graph, preset_id, mode, source_space_ids and admission. Admission has allowed, reservation_seconds, cap_seconds and reason; rejected estimates have no fabricated seconds value. """ presets = list_presets() stable = next(preset for preset in presets if preset["id"] == "stable") return [*(_template(preset, "t2va") for preset in presets), _template(stable, "i2va"), _template(stable, "fl2va")] def get_template(template_id: str) -> dict: """Return a fresh graph, not its catalog wrapper. Unknown or non-string identifiers raise ValueError. IDs are matched exactly, without trimming whitespace, changing case, or falling back to a preset. """ if isinstance(template_id, str): for entry in list_templates(): if entry["id"] == template_id: return entry["graph"] raise ValueError("unknown workflow template id") def _port(index, label, kind, default=None, required=True, choices=None): port = {"id": f"in_{index}", "label": label, "type": kind, "default_value": default, "required": required} if choices is not None: port["choices"] = list(choices) return port def function_palette() -> list[dict]: """Local nodes for building custom workflows, in each function's argument order.""" from preset_core import CANVASES, REFERENCE_DEFAULT_CANVAS canvases = list(CANVASES) presets = [preset["id"] for preset in list_presets()] video_out = [{"id": "out_0", "label": "video", "type": "video"}, {"id": "out_1", "label": "metadata", "type": "json"}] generate_inputs = [ _port(0, "preset_id", "text", "stable", choices=presets), _port(1, "prompt", "text", _DEFAULT_PROMPT), _port(2, "image_path", "image", required=False), _port(3, "last_image_path", "image", required=False), _port(4, "canvas", "text", "960x544 · 16:9 fast", choices=canvases), _port(5, "duration", "number", _DEFAULT_SECONDS), _port(6, "seed", "number", _DEFAULT_SEED), ] reference_inputs = [ _port(0, "prompt", "text", _DEFAULT_PROMPT), _port(1, "image_1", "image"), _port(2, "image_2", "image", required=False), _port(3, "image_3", "image", required=False), _port(4, "audio", "audio", required=False), _port(5, "video", "video", required=False), _port(6, "canvas", "text", REFERENCE_DEFAULT_CANVAS, choices=canvases), _port(7, "duration", "number", _DEFAULT_SECONDS), _port(8, "seed", "number", _DEFAULT_SEED), ] return [ {"fn": "generate_video", "label": "generate_video · LOCAL", "inputs": generate_inputs, "outputs": video_out}, {"fn": "reference_to_video", "label": "reference_to_video · LOCAL", "inputs": reference_inputs, "outputs": video_out}, {"fn": "last_frame", "label": "last_frame", "inputs": [_port(0, "video", "video")], "outputs": [{"id": "out_0", "label": "image", "type": "image"}]}, {"fn": "first_frame", "label": "first_frame", "inputs": [_port(0, "video", "video")], "outputs": [{"id": "out_0", "label": "image", "type": "image"}]}, {"fn": "concat_videos", "label": "concat_videos", "inputs": [_port(0, "first", "video"), _port(1, "second", "video")], "outputs": [{"id": "out_0", "label": "video", "type": "video"}]}, ] def _operator(node_id, fn, label, layout, wired=(), chained=()): """A local function node from the palette; unwired choice ports become dropdowns. Ports fed by another node (chained) are required, so the canvas skips this node when that node fails instead of running it with the input missing. """ entry = next(item for item in function_palette() if item["fn"] == fn) x, y, width, height = layout inputs = [{**port, "required": True} if port["id"] in chained else port for port in entry["inputs"]] return { "id": node_id, "label": label, "role": "operator", "kind": "fn", "source": "fn", "fn": fn, "inputs": inputs, "outputs": entry["outputs"], "x": x, "y": y, "width": width, "height": height, "data": {port["id"]: port["default_value"] for port in entry["inputs"] if "choices" in port and port["id"] not in wired}, } def _graph(name, description, references, operators, subjects, edges): return {"schema_version": "2", "name": name, "description": description, "runtime": {"default": "client"}, "view": {"default": "canvas"}, "references": references, "operators": operators, "subjects": subjects, "edges": edges} def _reference_workflow(): name = "Reference to video · 2 images" description = ( "Ref2VA on the stable base model: the reference images are read in order, then the prompt. " "One LOCAL reference_to_video call runs Qwen conditioning, the transformer_ref generator and " "MP4 output in one ZeroGPU allocation. The GPU budget depends on the image sizes and is " "checked on Run. Add image 3, audio or a short video on the node's optional inputs.") refs = [ _asset_node("ref_prompt", "Prompt", "reference", "text", (20, 20, 300, 170), _DEFAULT_PROMPT), _asset_node("ref_image_1", "Reference image 1", "reference", "image", (20, 210, 300, 125)), _asset_node("ref_image_2", "Reference image 2", "reference", "image", (20, 355, 300, 125)), _asset_node("ref_duration", "Seconds", "reference", "number", (350, 20, 230, 100), _DEFAULT_SECONDS), _asset_node("ref_seed", "Seed", "reference", "number", (350, 135, 230, 100), _DEFAULT_SEED), ] wiring = (("ref_prompt", "in_0", "text"), ("ref_image_1", "in_1", "image"), ("ref_image_2", "in_2", "image"), ("ref_duration", "in_7", "number"), ("ref_seed", "in_8", "number")) operator = _operator("op_reference", "reference_to_video", "reference_to_video · LOCAL", (620, 125, 300, 430), {port for _, port, _ in wiring}) subjects = [_asset_node("sub_video", "Video", "subject", "video", (960, 20, 300, 280)), _asset_node("sub_metadata", "Metadata", "subject", "json", (960, 330, 300, 290))] edges = [_edge(f"e_{source}", source, "out", "op_reference", port, kind) for source, port, kind in wiring] edges += [_edge("e_video", "op_reference", "out_0", "sub_video", "in", "video"), _edge("e_metadata", "op_reference", "out_1", "sub_metadata", "in", "json")] return {"id": "ref2va-images", "name": name, "description": description, "mode": "ref2va", "functions": ["reference_to_video"], "gpu_runs": 1, "graph": _graph(name, description, refs, [operator], subjects, edges)} def _extend_workflow(): name = "Extend a clip · 2 shots" description = ( "A custom chain: shot 1 from text, its last frame starts shot 2, and both shots are joined. " "Two LOCAL generate_video runs, each its own ZeroGPU allocation, plus CPU frame and concat " "nodes. Rewire, swap presets or add nodes from Function.") refs = [ _asset_node("ref_prompt_1", "Prompt · shot 1", "reference", "text", (20, 20, 300, 170), _DEFAULT_PROMPT), _asset_node("ref_prompt_2", "Prompt · shot 2", "reference", "text", (20, 210, 300, 170), "The small robot turns and waves at the camera."), _asset_node("ref_duration", "Seconds", "reference", "number", (20, 400, 230, 100), _DEFAULT_SECONDS), _asset_node("ref_seed", "Seed", "reference", "number", (20, 515, 230, 100), _DEFAULT_SEED), ] shot = {"in_1", "in_5", "in_6"} operators = [ _operator("op_shot_1", "generate_video", "generate_video · shot 1", (360, 20, 300, 390), shot), _operator("op_last_frame", "last_frame", "last_frame", (700, 20, 240, 120)), _operator("op_shot_2", "generate_video", "generate_video · shot 2", (700, 170, 300, 390), shot | {"in_2"}, chained={"in_2"}), _operator("op_concat", "concat_videos", "concat_videos", (1040, 170, 240, 150)), ] subjects = [_asset_node("sub_video", "Final video", "subject", "video", (1320, 170, 300, 280))] edges = [ _edge("e_prompt_1", "ref_prompt_1", "out", "op_shot_1", "in_1", "text"), _edge("e_prompt_2", "ref_prompt_2", "out", "op_shot_2", "in_1", "text"), _edge("e_seconds_1", "ref_duration", "out", "op_shot_1", "in_5", "number"), _edge("e_seconds_2", "ref_duration", "out", "op_shot_2", "in_5", "number"), _edge("e_seed_1", "ref_seed", "out", "op_shot_1", "in_6", "number"), _edge("e_seed_2", "ref_seed", "out", "op_shot_2", "in_6", "number"), _edge("e_shot_1_frame", "op_shot_1", "out_0", "op_last_frame", "in_0", "video"), _edge("e_frame_shot_2", "op_last_frame", "out_0", "op_shot_2", "in_2", "image"), _edge("e_concat_first", "op_shot_1", "out_0", "op_concat", "in_0", "video"), _edge("e_concat_second", "op_shot_2", "out_0", "op_concat", "in_1", "video"), _edge("e_final", "op_concat", "out_0", "sub_video", "in", "video"), ] return {"id": "extend-clip", "name": name, "description": description, "mode": "extend", "functions": ["generate_video", "last_frame", "concat_videos"], "gpu_runs": 2, "graph": _graph(name, description, refs, operators, subjects, edges)} def list_workflow_templates() -> list[dict]: """Multi-node and Ref2VA workflows, built from the function palette's ports.""" return [_reference_workflow(), _extend_workflow()]