Views
No views yet
pip install -q datasets flash_attn timm einops1from transformers import AutoModelForCausalLM, AutoProcessor, AutoConfig
2import torch
3
4device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5
6model = AutoModelForCausalLM.from_pretrained("gokaygokay/Florence-2-SD3-Captioner", trust_remote_code=True).to(device).eval()
7processor = AutoProcessor.from_pretrained("gokaygokay/Florence-2-SD3-Captioner", trust_remote_code=True)
8
9# Function to run the model on an example
10def run_example(task_prompt, text_input, image):
11 prompt = task_prompt + text_input
12
13 # Ensure the image is in RGB mode
14 if image.mode != "RGB":
15 image = image.convert("RGB")
16
17 inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
18 generated_ids = model.generate(
19 input_ids=inputs["input_ids"],
20 pixel_values=inputs["pixel_values"],
21 max_new_tokens=1024,
22 num_beams=3
23 )
24 generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
25 parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
26 return parsed_answer
27
28from PIL import Image
29import requests
30import copy
31
32url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
33image = Image.open(requests.get(url, stream=True).raw)
34answer = run_example("<DESCRIPTION>", "Describe this image in great detail.", image)
35final_answer = answer['<DESCRIPTION>']
36print(final_answer)
37
38# 'Captured at eye-level on a sunny day, a light blue Volkswagen Beetle is parked on a cobblestone street. The beetle is parked in front of a yellow building with two brown doors. The door on the right side of the frame is white, while the left side is a darker shade of blue. The car is facing the camera, and the car is positioned in the middle of the street.'