Gemma4Inference#
- class rapidtools.models.Gemma4Inference(model_id: str = 'google/gemma-4-E2B-it', device: str = 'auto', temperature: float = 0.4, max_tokens: int = 2048)[source]#
Bases:
BaseLocalInferenceModelLocal inference model for Google’s Gemma 4 vision-language models.
Supports every instruction-tuned Gemma 4 size (31B, 26B-A4B, 12B, E4B, E2B). Images are loaded with the robust PIL loader of
BaseLocalInferenceModeland generation runs sequentially (or in explicit batches throughrun_inference_batch()) to protect GPU memory.Example
>>> from rapidtools.models import Gemma4Inference >>> Gemma4Inference.list_available_models()[-1] 'google/gemma-4-E2B-it' >>> model = Gemma4Inference('google/gemma-4-E4B-it', device='cuda') >>> out = model.run_inference( ... 'house.jpg', 'Rate the damage from 0 (none) to 5 (destroyed).' ... ) >>> out.text '4'
Methods
Return the curated instruction-tuned Gemma 4 checkpoints.
run_batchProcess assets sequentially to avoid GPU out-of-memory crashes.
Run multimodal inference on a single prompt and image(s).
Run batched multimodal inference on several prompt/image sets.
- classmethod list_available_models(auth_key: str | None = None) list[str][source]#
Return the curated instruction-tuned Gemma 4 checkpoints.
- Parameters:
auth_key – Unused; present for signature parity with API wrappers.
- Returns:
list[str] – Repository IDs, largest first.
Example
>>> from rapidtools.models import Gemma4Inference >>> Gemma4Inference.list_available_models()[0] 'google/gemma-4-31B-it'
- run_inference(image_inputs: str | Path | list[str | Path] | None, prompt: str | Path, **kwargs: Any) ModelOutput | None[source]#
Run multimodal inference on a single prompt and image(s).
- Parameters:
image_inputs – A path/URL, a list of them, or
None/[]for a text-only prompt.prompt – Prompt text or path to a prompt file.
**kwargs –
temperature,max_tokens,json_modeoverrides or aconfig=GenerationConfig(...).
- Returns:
ModelOutput | None – Generated text and raw response, or
Nonewhen every requested image failed to load or generation raised.
Example
>>> from rapidtools.models import Gemma4Inference >>> model = Gemma4Inference() >>> model.run_inference([], 'Reply with OK.').text 'OK'
- run_inference_batch(batch_image_inputs: list[list[str | Path]], batch_prompts: list[str], **kwargs: Any) list[ModelOutput | None][source]#
Run batched multimodal inference on several prompt/image sets.
Assets whose images all fail to load receive
Noneat their index so the output aligns with the input. If the batched call itself raises, every index isNone.- Parameters:
batch_image_inputs – One list of image paths/URLs per asset.
batch_prompts – One prompt per asset (same length).
**kwargs –
temperatureandmax_tokensoverrides.
- Returns:
list[ModelOutput | None] – Results aligned with the inputs.
Example
>>> from rapidtools.models import Gemma4Inference >>> model = Gemma4Inference() >>> outs = model.run_inference_batch( ... [['a.jpg'], ['b.jpg']], ['Rate damage 0-5.'] * 2 ... ) >>> [o.text for o in outs] ['1', '4']