Skip to content

modosaic.text.generators.impl.internvl_3_5_2b

modosaic.text.generators.impl.internvl_3_5_2b

InternVL352B

InternVL352B()

Bases: TextGenerator

InternVL 3.5 2B image-caption generator.

Load the pinned InternVL 3.5 pipeline.

Source code in modosaic/text/generators/impl/internvl_3_5_2b.py
21
22
23
24
25
26
27
28
29
def __init__(self) -> None:
    """Load the pinned InternVL 3.5 pipeline."""
    super().__init__()
    self.model_specs = HFModelSpec(
        model_id="OpenGVLab/InternVL3_5-2B-HF",
        revision="3f301ffcf3dcbb47893afae6650ea3e78d96fb6d",
        dtype=self.dtype,
    )
    self.pipeline = self._load_pipeline()

generate

generate(record, prompt=CAPTIONING_PROMPT)

Generate a caption for one image record.

Source code in modosaic/text/generators/impl/internvl_3_5_2b.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@override
@torch.inference_mode()
def generate(self, record: ImageRecord, prompt: str = CAPTIONING_PROMPT, ) -> str:
    """Generate a caption for one image record."""
    logger.debug(f"Generating text for record sample {record.sample_id}")
    with ImageService.temporary_image_file(record.image_bytes) as tmp_image_path:
        messages = InternVL352B._get_messages(tmp_image_path.resolve().as_posix())
        text_caption = self.pipeline(
            text=messages,
            max_new_tokens=MAX_CAPTIONING_TOKENS,
            max_length=MAX_CAPTIONING_TOKENS,
            return_full_text=False,
        )
        caption = text_caption[0]["generated_text"]
    logger.debug(f"Generated text for record sample {record.sample_id} with {len(caption)} characters")
    return caption