Cosmos3 video to video generation (#4266)

Signed-off-by: Maciej Bala <mbala@nvidia.com>
This commit is contained in:
MaciejBalaNV authored and GitHub committed 2026-06-09 03:04:05 +08:00
1 parent 9c9d4ed4a8
commit 2f25195750
14 files changed
+1037 -94

No files matched your search

+147 -6
View File
@@ -8,8 +8,9 @@ from __future__ import annotations
import base64
import binascii
from collections import deque
from io import BytesIO
from typing import Any
from typing import Any, Literal
import httpx
import numpy as np
@@ -19,8 +20,11 @@ from PIL import Image, UnidentifiedImageError
from vllm_omni.entrypoints.openai.errors import InvalidInputReferenceError
from vllm_omni.entrypoints.openai.protocol.videos import (
FileImageReference,
FileVideoReference,
ImageReference,
UrlImageReference,
UrlVideoReference,
VideoReference,
)
@@ -31,6 +35,70 @@ def _decode_image_bytes(image_bytes: bytes, *, source: str) -> Image.Image:
raise InvalidInputReferenceError(f"Invalid {source}: provided content is not a valid image.") from exc
def _decode_video_bytes(
video_bytes: bytes,
*,
source: str,
max_frames: int | None = None,
keep: Literal["first", "last"] = "first",
) -> list[Image.Image]:
try:
import av
except ImportError as exc: # pragma: no cover - av is a serving dependency via media_utils
raise InvalidInputReferenceError(f"Invalid {source}: video decoding requires PyAV.") from exc
if keep not in {"first", "last"}:
raise InvalidInputReferenceError(f"Invalid {source}: video frame selection must be 'first' or 'last'.")
if max_frames is not None and max_frames <= 0:
raise InvalidInputReferenceError(f"Invalid {source}: max video frames must be positive.")
frames: list[Image.Image] = []
tail_frames: deque[Image.Image] | None = (
deque(maxlen=max_frames) if keep == "last" and max_frames is not None else None
)
try:
with av.open(BytesIO(video_bytes)) as container:
for frame in container.decode(video=0):
image = frame.to_image().convert("RGB")
if tail_frames is not None:
tail_frames.append(image)
else:
frames.append(image)
if keep == "first" and max_frames is not None and len(frames) >= max_frames:
break
except Exception as exc:
raise InvalidInputReferenceError(f"Invalid {source}: provided content is not a valid video.") from exc
if tail_frames is not None:
frames = list(tail_frames)
if not frames:
raise InvalidInputReferenceError(f"Invalid {source}: provided content is not a valid video.")
return frames
def _decode_media_bytes(
media_bytes: bytes,
*,
source: str,
max_video_frames: int | None = None,
video_keep: Literal["first", "last"] = "first",
) -> Image.Image | list[Image.Image]:
try:
return _decode_image_bytes(media_bytes, source=source)
except InvalidInputReferenceError:
try:
return _decode_video_bytes(
media_bytes,
source=source,
max_frames=max_video_frames,
keep=video_keep,
)
except InvalidInputReferenceError as video_exc:
raise InvalidInputReferenceError(
f"Invalid {source}: provided content is not a valid image or video."
) from video_exc
def _decode_base64_image(input_reference: str, *, source: str) -> Image.Image:
if input_reference:
if input_reference.startswith("data:image"):
@@ -64,23 +132,96 @@ async def decode_image_url(image_url: str) -> Image.Image:
raise InvalidInputReferenceError("Invalid image_reference.image_url: must be an http(s) URL or data URL.")
def _decode_base64_video(
video_reference: str,
*,
source: str,
max_frames: int | None = None,
keep: Literal["first", "last"] = "first",
) -> list[Image.Image]:
if video_reference:
if video_reference.startswith("data:video"):
_, b64_data = video_reference.split(",", 1)
else:
b64_data = video_reference
try:
video_bytes = base64.b64decode(b64_data)
except (binascii.Error, ValueError) as exc: # pragma: no cover - malformed base64
raise InvalidInputReferenceError(f"Invalid {source}: video data is not valid base64.") from exc
return _decode_video_bytes(video_bytes, source=source, max_frames=max_frames, keep=keep)
raise InvalidInputReferenceError(f"Invalid {source}: video data is empty.")
async def decode_video_url(
video_url: str,
*,
max_frames: int | None = None,
keep: Literal["first", "last"] = "first",
) -> list[Image.Image]:
if video_url.startswith("data:video"):
return _decode_base64_video(
video_url,
source="video_reference.video_url",
max_frames=max_frames,
keep=keep,
)
if video_url.startswith(("http://", "https://")):
async with httpx.AsyncClient(timeout=60) as client:
try:
response = await client.get(video_url)
response.raise_for_status()
except httpx.HTTPError as exc:
raise InvalidInputReferenceError(
"Invalid video_reference.video_url: failed to download video."
) from exc
return _decode_video_bytes(
response.content,
source="video_reference.video_url",
max_frames=max_frames,
keep=keep,
)
raise InvalidInputReferenceError("Invalid video_reference.video_url: must be an http(s) URL or data URL.")
async def decode_input_reference(
image_reference: ImageReference | None,
video_reference: VideoReference | None,
input_reference_bytes: bytes | None,
) -> Image.Image | None:
"""Decode image input from multipart bytes, base64/data URL, or image_reference."""
*,
max_video_frames: int | None = None,
video_keep: Literal["first", "last"] = "first",
) -> Image.Image | list[Image.Image] | None:
"""Decode media input from multipart bytes, data URLs, or typed references."""
if input_reference_bytes is not None and image_reference is not None:
raise InvalidInputReferenceError("Provide either input_reference or image_reference, not both.")
provided = sum(item is not None for item in (input_reference_bytes, image_reference, video_reference))
if provided > 1:
raise InvalidInputReferenceError("Provide only one of input_reference, image_reference, or video_reference.")
if isinstance(input_reference_bytes, bytes):
return _decode_image_bytes(input_reference_bytes, source="input_reference")
return _decode_media_bytes(
input_reference_bytes,
source="input_reference",
max_video_frames=max_video_frames,
video_keep=video_keep,
)
if isinstance(image_reference, UrlImageReference):
return await decode_image_url(image_reference.image_url)
elif isinstance(image_reference, FileImageReference):
raise InvalidInputReferenceError("Invalid image_reference: file_id is not supported yet.")
if isinstance(video_reference, UrlVideoReference):
return await decode_video_url(
video_reference.video_url,
max_frames=max_video_frames,
keep=video_keep,
)
elif isinstance(video_reference, FileVideoReference):
raise InvalidInputReferenceError("Invalid video_reference: file_id is not supported yet.")
return None