| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from dataclasses import dataclass |
| 5 | from pathlib import Path |
| 6 | from typing import Literal |
| 7 | |
| 8 | import torch |
| 9 | from PIL import Image |
| 10 | |
| 11 | from .base import get_vllm_public_assets |
| 12 | |
| 13 | VLM_IMAGES_DIR = "vision_model_images" |
| 14 | |
| 15 | ImageAssetName = Literal[ |
| 16 | "stop_sign", |
| 17 | "cherry_blossom", |
| 18 | "hato", |
| 19 | "2560px-Gfp-wisconsin-madison-the-nature-boardwalk", |
| 20 | "Grayscale_8bits_palette_sample_image", |
| 21 | "1280px-Venn_diagram_rgb", |
| 22 | "RGBA_comp", |
| 23 | "237-400x300", |
| 24 | "231-200x300", |
| 25 | "27-500x500", |
| 26 | "17-150x600", |
| 27 | "handelsblatt-preview", |
| 28 | "paper-11", |
| 29 | ] |
| 30 | |
| 31 | |
| 32 | @dataclass(frozen=True) |
| 33 | class ImageAsset: |
| 34 | name: ImageAssetName |
| 35 | |
| 36 | def get_path(self, ext: str) -> Path: |
| 37 | """Return s3 path for given image.""" |
| 38 | return get_vllm_public_assets( |
| 39 | filename=f"{self.name}.{ext}", s3_prefix=VLM_IMAGES_DIR |
| 40 | ) |
| 41 | |
| 42 | @property |
| 43 | def pil_image(self) -> Image.Image: |
| 44 | return self.pil_image_ext(ext="jpg") |
| 45 | |
| 46 | def pil_image_ext(self, ext: str) -> Image.Image: |
| 47 | image_path = self.get_path(ext=ext) |
| 48 | return Image.open(image_path) |
| 49 | |
| 50 | @property |
| 51 | def image_embeds(self) -> torch.Tensor: |
| 52 | """Image embeddings, only used for testing purposes with llava 1.5.""" |
| 53 | image_path = self.get_path("pt") |
| 54 | return torch.load(image_path, map_location="cpu", weights_only=True) |
| 55 | |
| 56 | def read_bytes(self, ext: str) -> bytes: |
| 57 | p = Path(self.get_path(ext)) |
| 58 | return p.read_bytes() |