from __future__ import annotations from typing import Any import torch def flatten_media_inputs(value: Any) -> list[Any]: """Flatten nested input-list values while preserving media objects.""" if value is None: return [] if isinstance(value, list): flattened: list[Any] = [] for item in value: flattened.extend(flatten_media_inputs(item)) return flattened return [value] def expand_image_inputs(value: Any) -> list[torch.Tensor]: """Expand nested IMAGE lists and batches into single-image tensors.""" images: list[torch.Tensor] = [] for batch in flatten_media_inputs(value): if not isinstance(batch, torch.Tensor): raise TypeError("images must contain only torch.Tensor values") if batch.ndim != 4 or batch.shape[0] < 1: raise ValueError( "Each image input must have shape [B, H, W, C] with B >= 1" ) images.extend(batch.split(1, dim=0)) return images