Views
No views yet
1!pip install peft trl datasets accelerate bitsandbytes
2!pip install transformers --upgrade1from peft import get_peft_model, LoraConfig,prepare_model_for_kbit_training
2from transformers import TrainingArguments, Trainer , PaliGemmaForConditionalGeneration , AutoProcessor,BitsAndBytesConfig,AutoTokenizer
3from peft import PeftModel, PeftConfig
4from datasets import load_dataset
5import torch
6from datasets import load_dataset
7
8dataset = load_dataset("damerajee/clean_hin_vqa",split='train')
9test_example = dataset[10000]
10test_image = test_example["image"]
11text = test_example['question']
12
13device_index = torch.cuda.current_device()
14print("device_index:",device_index)
15base_model = PaliGemmaForConditionalGeneration.from_pretrained("BhashaAI/ViLaH",device_map={"": device_index},torch_dtype=torch.float16,low_cpu_mem_usage=True)
16processor = AutoProcessor.from_pretrained("BhashaAI/ViLaH")
17
18inputs = processor(text=text, images=test_image, return_tensors="pt").to("cuda")
19for k,v in inputs.items():
20 print(k,v.shape)
21
22MAX_LENGTH = 200
23# Autoregressively generate
24# We use greedy decoding here, for more fancy methods see https://huggingface.co/blog/how-to-generate
25generated_ids = base_model.generate(**inputs, max_new_tokens=MAX_LENGTH,temperature=0.7,repetition_penalty=2.0,do_sample=True)
26
27# Next we turn each predicted token ID back into a string using the decode method
28# We chop of the prompt, which consists of image tokens and our text prompt
29image_token_index = base_model.config.image_token_index
30num_image_tokens = len(generated_ids[generated_ids==image_token_index])
31num_text_tokens = len(processor.tokenizer.encode(text))
32num_prompt_tokens = num_image_tokens + num_text_tokens + 2
33generated_text = processor.batch_decode(generated_ids[:, num_prompt_tokens:], skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
34generated_text
351from peft import get_peft_model, LoraConfig,prepare_model_for_kbit_training
2from transformers import TrainingArguments, Trainer , PaliGemmaForConditionalGeneration , AutoProcessor,BitsAndBytesConfig,AutoTokenizer
3from peft import PeftModel, PeftConfig
4from datasets import load_dataset
5import torch
6from datasets import load_dataset
7
8dataset = load_dataset("damerajee/clean_hin_vqa",split='train')
9test_example = dataset[10000]
10test_image = test_example["image"]
11text = test_example['question']
12
13device_index = torch.cuda.current_device()
14print("device_index:",device_index)
15quantization_config = BitsAndBytesConfig(load_in_4bit=True)
16base_model = PaliGemmaForConditionalGeneration.from_pretrained("BhashaAI/ViLaH",device_map={"": device_index},quantization_config=quantization_config,torch_dtype=torch.float16,low_cpu_mem_usage=True)
17processor = AutoProcessor.from_pretrained("BhashaAI/ViLaH")
18
19inputs = processor(text=text, images=test_image, return_tensors="pt").to("cuda")
20for k,v in inputs.items():
21 print(k,v.shape)
22
23MAX_LENGTH = 200
24# Autoregressively generate
25# We use greedy decoding here, for more fancy methods see https://huggingface.co/blog/how-to-generate
26generated_ids = base_model.generate(**inputs, max_new_tokens=MAX_LENGTH,temperature=0.7,repetition_penalty=2.0,do_sample=True)
27
28# Next we turn each predicted token ID back into a string using the decode method
29# We chop of the prompt, which consists of image tokens and our text prompt
30image_token_index = base_model.config.image_token_index
31num_image_tokens = len(generated_ids[generated_ids==image_token_index])
32num_text_tokens = len(processor.tokenizer.encode(text))
33num_prompt_tokens = num_image_tokens + num_text_tokens + 2
34generated_text = processor.batch_decode(generated_ids[:, num_prompt_tokens:], skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
35generated_text