Views
No views yet
1
2from transformers import PaliGemmaForConditionalGeneration, PaliGemmaProcessor
3from PIL import Image
4
5
6model_id = "Fer14/paligemma_coffee_machine_caption"
7
8model = PaliGemmaForConditionalGeneration.from_pretrained(model_id)
9processor = PaliGemmaProcessor.from_pretrained(model_id)
10
11
12image = Image.open("path to your image").convert("RGB")
13
14prompt = (
15 f"Generate a caption for the following coffee maker image. The caption has to be of the following structure:\n"
16 "\"A <color> <type>, <accessories>, <shape> shaped, with <screen> and <number> <b_color> butons\"\n\n"
17 "in which:\n"
18 "- color: red, black, blue...\n"
19 "- type: coffee machine, coffee maker, espresso coffee machine...\n"
20 "- accessories: a list of accessories like the ones described above\n"
21 "- shape: cubed, round...\n"
22 "- screen: screen, no screen.\n"
23 "- number: amount of buttons to add\n"
24 "- b_color: color of the buttons"
25 )
26
27inputs = processor(
28 text=prompt,
29 images=image,
30 return_tensors="pt",
31 padding="longest",
32 )
33
34output = model.generate(**inputs, max_length=1000)
35
36decoded_output = processor.decode(output[0], skip_special_tokens=True)[len(prompt) :]
37