Views
No views yet
1# Sample code for loading and using this model
2from transformers import AutoProcessor, AutoModelForCausalLM
3from peft import PeftModel
4import torch
5from PIL import Image
6
7# Load base model and processor
8base_model_id = "unsloth/llama-3.2-11b-vision-instruct"
9processor = AutoProcessor.from_pretrained(base_model_id)
10model = AutoModelForCausalLM.from_pretrained(base_model_id, device_map="auto")
11
12# Load this adapter
13adapter_id = "saakshigupta/deepfake-explainer-1"
14model = PeftModel.from_pretrained(model, adapter_id)
15
16# Function to fix cross-attention masks
17def fix_processor_outputs(inputs):
18 if 'cross_attention_mask' in inputs and 0 in inputs['cross_attention_mask'].shape:
19 batch_size, seq_len, _, num_tiles = inputs['cross_attention_mask'].shape
20 visual_features = 6404 # Critical dimension
21 new_mask = torch.ones((batch_size, seq_len, visual_features, num_tiles),
22 device=inputs['cross_attention_mask'].device)
23 inputs['cross_attention_mask'] = new_mask
24 return inputs
25
26# Load and process image
27image_path = "path/to/your/image.jpg"
28image = Image.open(image_path).convert("RGB")
29prompt = "Analyze this image and tell me if it's a deepfake."
30
31# Process with fix
32inputs = processor(text=prompt, images=image, return_tensors="pt")
33inputs = fix_processor_outputs(inputs)
34inputs = {k: v.to(model.device) for k, v in inputs.items() if isinstance(v, torch.Tensor)}
35
36# Generate output
37with torch.no_grad():
38 output_ids = model.generate(**inputs, max_new_tokens=300)
39response = processor.decode(output_ids[0], skip_special_tokens=True)
40print(response)