import torch from transformers import AutoProcessor, Gemma3ForConditionalGeneration ckpt = "google/gemma-3-4b-it" model = Gemma3ForConditionalGeneration.from_pretrained( ckpt, device_map="auto", torch_dtype=torch.bfloat16, ) processor = AutoProcessor.from_pretrained(ckpt) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/spaces/big-vision/paligemma-hf/resolve/main/examples/password.jpg"}, {"type": "text", "text": "What is the password?"} ] } ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt" ).to(model.device) input_len = inputs["input_ids"].shape[-1] generation = model.generate(**inputs, max_new_tokens=100, do_sample=False) generation = generation[0][input_len:] decoded = processor.decode(generation, skip_special_tokens=True) print(decoded)