Spaces:
Running on Zero
Running on Zero
feat(app): add auto-canvas selection and update conditioner space
Browse files- Update `CONDITIONER_SPACE` default to `mpasila/qwen3vl-conditioner`.
- Add 3:2 and 2:3 aspect ratio options to `CANVASES`.
- Implement `auto_canvas` logic to automatically select the closest canvas aspect ratio based on uploaded image or video dimensions.
- Bind `auto_canvas` to image and video component change events in the Gradio UI.
19 unmodified lines2021222324252627 unmodified lines545556575859438 unmodified lines498499500501502503443 unmodified lines94794894995095195219 unmodified linesimport gradio as grMODEL_REPO = os.environ.get("H3_MODEL_REPO", "MiniMaxAI/MiniMax-H3")CONDITIONER_SPACE = os.environ.get("H3_CONDITIONER", "multimodalart/qwen3vl-conditioner")# `lazy` moves all 72.16 GiB onto the card on the first GPU call and leaves it there; `offload` hands placement to# `ComponentsManager.enable_auto_cpu_offload`. Startup placement is not an option here — see `load_models`.PLACEMENT = os.environ.get("H3_PLACEMENT", "lazy").lower()27 unmodified lines"1024x768 · 4:3 full": (768, 1024),"576x768 · 3:4 fast": (768, 576),"768x1024 · 3:4 full": (1024, 768),# 21:9"1152x512 · 21:9 fast": (512, 1152),"1536x672 · 21:9 full": (672, 1536),438 unmodified linesreturn video, audiodef collect(image_paths, audio_path, video_path) -> list[tuple[str, str]]:"""The `(kind, path)` references of a request, **in the order the model reads them**.443 unmodified linesduration_controls, [audio, video, match], [match, duration], show_progress="hidden", api_name=False)# `reference, strength, reference, strength, ...`, which is how `generate` unpacks them.lora_inputs = [field for pair in zip(lora_references, lora_scales) for field in pair]lora_upload.upload(_fill_lora_slots, [lora_upload, *lora_references], lora_references, api_name=False)19 unmodified lines2021222324252627 unmodified lines5455565758596061626364438 unmodified lines503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545443 unmodified lines98999099199299399499599699799899919 unmodified linesimport gradio as grMODEL_REPO = os.environ.get("H3_MODEL_REPO", "MiniMaxAI/MiniMax-H3")CONDITIONER_SPACE = os.environ.get("H3_CONDITIONER", "mpasila/qwen3vl-conditioner")# `lazy` moves all 72.16 GiB onto the card on the first GPU call and leaves it there; `offload` hands placement to# `ComponentsManager.enable_auto_cpu_offload`. Startup placement is not an option here — see `load_models`.PLACEMENT = os.environ.get("H3_PLACEMENT", "lazy").lower()27 unmodified lines"1024x768 · 4:3 full": (768, 1024),"576x768 · 3:4 fast": (768, 576),"768x1024 · 3:4 full": (1024, 768),# 3:2 / 2:3"864x576 · 3:2 fast": (576, 864),"1152x768 · 3:2 full": (768, 1152),"576x864 · 2:3 fast": (864, 576),"768x1152 · 2:3 full": (1152, 768),# 21:9"1152x512 · 21:9 fast": (512, 1152),"1536x672 · 21:9 full": (672, 1536),438 unmodified linesreturn video, audiodef _media_dimensions(path: str) -> tuple[int, int]:"""`(width, height)` of an image or a video file, from its first stream."""from PIL import Imagetry:with Image.open(path) as image:return image.sizeexcept Exception:passimport avwith av.open(path) as container:stream = container.streams.video[0]return stream.width, stream.heightdef closest_canvas(path: str | None) -> str | None:"""The canvas label whose aspect ratio is closest to a media file's, or `None` when the file ismissing or unreadable."""if not path:return Nonetry:width, height = _media_dimensions(path)except Exception:return Noneif not width or not height:return Nonetarget = width / heightreturn min(CANVASES, key=lambda label: abs(CANVASES[label][1] / CANVASES[label][0] - target))def auto_canvas(path):"""Set the canvas to the closest aspect ratio of an uploaded image or video."""label = closest_canvas(path)return gr.update(value=label) if label else gr.update()def collect(image_paths, audio_path, video_path) -> list[tuple[str, str]]:"""The `(kind, path)` references of a request, **in the order the model reads them**.443 unmodified linesduration_controls, [audio, video, match], [match, duration], show_progress="hidden", api_name=False)# Auto-select the canvas whose aspect ratio is closest to an uploaded image or video.for image in images:image.change(auto_canvas, image, canvas, show_progress="hidden", api_name=False)video.change(auto_canvas, video, canvas, show_progress="hidden", api_name=False)# `reference, strength, reference, strength, ...`, which is how `generate` unpacks them.lora_inputs = [field for pair in zip(lora_references, lora_scales) for field in pair]lora_upload.upload(_fill_lora_slots, [lora_upload, *lora_references], lora_references, api_name=False)
app.py+48-1