Views
No views yet
1import torch
2import requests
3
4from io import BytesIO
5from PIL import Image
6from transformers import AutoProcessor, LlavaForConditionalGeneration
7
8model_path = 'swap-uniba/LLaVA-LLaMA-2-base-ES-FT'
9
10processor = AutoProcessor.from_pretrained(
11 model_path
12)
13
14model = LlavaForConditionalGeneration.from_pretrained(
15 model_path, torch_dtype=torch.bfloat16, device_map="auto"
16)
17
18conv_str = f"[INST] ¿Cuántos gatos hay en esta imagen?\n<image>[/INST]"
19
20# Tokenize the texts and process the images
21batch = processor(images=[Image.open(BytesIO(requests.get("https://farm1.staticflickr.com/36/100071458_515d1884d1_z.jpg").content))], text=[conv_str], return_tensors="pt", padding=True, truncation=True, max_length=1024)
22outs = model.generate(**batch, do_sample=False, num_beams=1, max_new_tokens=512)
23
24print(processor.tokenizer.decode(outs[0, batch.input_ids.shape[-1]:], skip_special_tokens=True))