Views
No views yet
1HF_TOKEN=READ_WRITE_HF_TOKEN
2TOKENIZERS_PARALLELISM=false
3WANDB_API_KEY=WANDB_TOKEN_FOR_LOGGINGHF_TOKEN is needed for pushing the model to HF. WANDB_API_KEY is needed for evaluating the accuracy of fine tuning.requirements.txt dependencies. Since the file was created on MacOS, the installation might fail on Linux system. Make sure to install faker (pip install faker) for the script to run properly.python dataset_generator.py1from transformers import PaliGemmaForConditionalGeneration, AutoProcessor, BitsAndBytesConfig
2from PIL import Image
3import torch
4
5image = Image.open('path/to/image')
6
7bnb_config = BitsAndBytesConfig(
8 load_in_4bit=True,
9 bnb_4bit_quant_type="nf4",
10 bnb_4bit_compute_dtype=torch.bfloat16
11)
12model = PaliGemmaForConditionalGeneration.from_pretrained("theDisco/fluffy-koala", quantization_config=bnb_config)
13processor = AutoProcessor.from_pretrained("google/paligemma-3b-pt-448")
14
15inputs = processor(text="extract JSON.", images=image, return_tensors="pt")
16generated_ids = model.generate(**inputs, max_new_tokens=512)
17
18image_token_index = model.config.image_token_index
19num_image_tokens = len(generated_ids[generated_ids == image_token_index])
20num_text_tokens = len(processor.tokenizer.encode("extract JSON."))
21num_prompt_tokens = num_image_tokens + num_text_tokens + 2
22generated_text = processor.batch_decode(generated_ids[:, num_prompt_tokens:], skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
23
24print(generated_text)