Views
No views yet
1from transformers import AutoProcessor, Gemma3ForConditionalGeneration
2import torch
3
4
5model_id = "GaMS-Beta/SVILA-1-4B"
6model = Gemma3ForConditionalGeneration.from_pretrained(
7 model_id, device_map="auto"
8).eval()
9
10processor = AutoProcessor.from_pretrained(model_id)
11
12
13messages = [
14 {
15 "role": "system",
16 "content": [{"type": "text", "text": ""}]
17 },
18 {
19 "role": "user",
20 "content": [
21 {"type": "image", "image": "https://www.dangerous-business.com/wp-content/uploads/2024/02/DSC02109.jpg"},
22 {"type": "text", "text": "Kaj je na sliki?"}
23 ]
24 }
25]
26
27print(processor.apply_chat_template(messages, tokenize=False))
28
29inputs = processor.apply_chat_template(
30 messages, add_generation_prompt=True, tokenize=True,
31 return_dict=True, return_tensors="pt"
32).to(model.device, dtype=torch.bfloat16)
33
34
35input_len = inputs["input_ids"].shape[-1]
36
37with torch.inference_mode():
38 generation = model.generate(**inputs, max_new_tokens=500)
39 generation = generation[0][input_len:]
40
41decoded = processor.decode(generation, skip_special_tokens=True)
42print(decoded)