Views
No views yet
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu124
pip3 install transformers pillow einops timm1import requests
2import torch
3from PIL import Image
4from transformers import AutoProcessor, AutoModelForCausalLM
5
6
7device = "cuda:0" if torch.cuda.is_available() else "cpu"
8torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
9
10model = AutoModelForCausalLM.from_pretrained("createveai/Florence-2-base-PromptGen-v1.5", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
11processor = AutoProcessor.from_pretrained("createveai/Florence-2-base-PromptGen-v1.5", trust_remote_code=True)
12
13# Examples include CAPTION>, <DETAILED_CAPTION>, <MORE_DETAILED_CAPTION>,<GENERATE_TAGS>, <MIXED_CAPTION>, <0D>
14prompt = "<CAPTION>"
15
16url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
17image = Image.open(requests.get(url, stream=True).raw)
18
19inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
20
21generated_ids = model.generate(
22 input_ids=inputs["input_ids"],
23 pixel_values=inputs["pixel_values"],
24 max_new_tokens=1024,
25 num_beams=3,
26 do_sample=False
27)
28generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
29parsed_answer = processor.post_process_generation(generated_text, task=prompt, image_size=(image.width, image.height))
30
31print(parsed_answer[prompt])