Bases: TextGenerator
InternVL 3 2B image-caption generator.
Load the pinned InternVL 3 pipeline.
Source code in modosaic/text/generators/impl/internvl_3_2b.py
24
25
26
27
28
29
30
31
32 | def __init__(self) -> None:
"""Load the pinned InternVL 3 pipeline."""
super().__init__()
self.model_specs = HFModelSpec(
model_id="OpenGVLab/InternVL3-2B-hf",
revision="cb57a075cb75a2e6d1b668b128d48bb00ae321d2",
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_2b.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 | @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 = InternVL32B._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
|