bridge.models.nemotron_vl.nemotron_vl_utils#
Module Contents#
Functions#
Encode a PIL image to a base64-encoded data URL. |
|
Sample frames from a video and return base64-encoded data URLs along with metadata. |
|
Convert a path or URL to data URLs, handling videos, images, and remote files. |
|
Decode a base64-encoded image to a PIL image. |
|
Adjust each image placeholder without merging samples in the batch. |
Data#
API#
- bridge.models.nemotron_vl.nemotron_vl_utils.logger#
βgetLogger(β¦)β
- bridge.models.nemotron_vl.nemotron_vl_utils._ALLOW_PRIVATE_URL_FETCH_ENV#
None
- bridge.models.nemotron_vl.nemotron_vl_utils._is_safe_public_http_url#
None
- bridge.models.nemotron_vl.nemotron_vl_utils._safe_url_open#
None
- bridge.models.nemotron_vl.nemotron_vl_utils.encode_pil_to_jpeg_data_url(pil_image)#
Encode a PIL image to a base64-encoded data URL.
- bridge.models.nemotron_vl.nemotron_vl_utils.sample_video_frames_to_data_urls(
- video_path_local,
- fps=1,
- nframe=0,
- nframe_max=-1,
Sample frames from a video and return base64-encoded data URLs along with metadata.
- Parameters:
video_path_local β Path to the video file
fps β Target frames per second for sampling (if > 0, uses fps-based sampling)
nframe β Number of frames to sample (used if fps <= 0)
nframe_max β Maximum number of frames to sample
- Returns:
(frame_data_urls, metadata)
frame_data_urls: List of base64-encoded frame images
metadata: VideoMetadata dataclass containing info about the sampled frames:
total_num_frames: Number of sampled frames
fps: Effective frame rate of the sampled frames
duration: Duration covered by the sampled frames (in seconds)
video_backend: Backend used for video processing (βdecordβ)
- Return type:
tuple
- bridge.models.nemotron_vl.nemotron_vl_utils.maybe_path_or_url_to_data_urls(
- path_or_url,
- fps=1,
- nframe=0,
- nframe_max=-1,
Convert a path or URL to data URLs, handling videos, images, and remote files.
- Parameters:
path_or_url β Path or URL to the media file
fps β Target frames per second for video sampling (if > 0, uses fps-based sampling)
nframe β Number of frames to sample from video (used if fps <= 0)
nframe_max β Maximum number of frames to sample
- Returns:
(data_urls, metadata)
data_urls: List of base64-encoded data URLs
metadata: VideoMetadata dataclass with video metadata or None for images
- Return type:
tuple
- bridge.models.nemotron_vl.nemotron_vl_utils.pil_image_from_base64(b64_str: str) PIL.Image.Image#
Decode a base64-encoded image to a PIL image.
- bridge.models.nemotron_vl.nemotron_vl_utils.adjust_image_tokens(
- input_ids: torch.Tensor | dict[str, torch.Tensor],
- num_tiles: int | list[int] | torch.Tensor,
- img_start_token_id: int,
- img_end_token_id: int,
- *,
- padding_values: collections.abc.Mapping[str, int | float] | None = None,
Adjust each image placeholder without merging samples in the batch.
num_tilescontains one desired media-token count per image, flattened in row-major sample order. A text-only row consumes no entry, while a row with multiple images consumes one entry for each<img>...</img>region. Every tensor in an input dictionary is adjusted at the same positions and rows are right-padded back to a common length. If the dictionary containsattention_mask, trailing masked positions are discarded before the adjustment so stale processor padding cannot pin the adjusted batch width... rubric:: Example
input_ids decoded may look like this System: β¦ User:β¦ Image 1:
β¦ # adjust number of tokens to be num_tiles[0] Image 2: β¦ # adjust number of tokens to be num_tiles[1] β¦ etc - Parameters:
input_ids β A 2D token tensor, or a dictionary containing
input_idsand other sequence-aligned 2D tensors with the same shape.num_tiles β Desired media-token counts for all images in row-major order.
img_start_token_id β Token ID for
<img>.img_end_token_id β Token ID for
</img>.padding_values β Per-tensor values used when adjusted rows have different lengths. Unspecified tensors default to zero.
- Returns:
The adjusted tensor or dictionary, preserving the input batch size.
- Raises:
ValueError β If tensors are not aligned, image boundaries are malformed, or the number of tile counts does not match the number of images.