Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,46 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
---
|
4 |
+
|
5 |
+
```
|
6 |
+
pip install -q datasets flash_attn timm einops
|
7 |
+
```
|
8 |
+
|
9 |
+
```python
|
10 |
+
from transformers import AutoModelForCausalLM, AutoProcessor, AutoConfig
|
11 |
+
import torch
|
12 |
+
|
13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
+
|
15 |
+
model = AutoModelForCausalLM.from_pretrained("gokaygokay/Florence-2-SD3-Captioner", trust_remote_code=True).to(device).eval()
|
16 |
+
processor = AutoProcessor.from_pretrained("gokaygokay/Florence-2-SD3-Captioner", trust_remote_code=True)
|
17 |
+
|
18 |
+
# Function to run the model on an example
|
19 |
+
def run_example(task_prompt, text_input, image):
|
20 |
+
prompt = task_prompt + text_input
|
21 |
+
|
22 |
+
# Ensure the image is in RGB mode
|
23 |
+
if image.mode != "RGB":
|
24 |
+
image = image.convert("RGB")
|
25 |
+
|
26 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
|
27 |
+
generated_ids = model.generate(
|
28 |
+
input_ids=inputs["input_ids"],
|
29 |
+
pixel_values=inputs["pixel_values"],
|
30 |
+
max_new_tokens=1024,
|
31 |
+
num_beams=3
|
32 |
+
)
|
33 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
34 |
+
parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
|
35 |
+
return parsed_answer
|
36 |
+
|
37 |
+
from PIL import Image
|
38 |
+
import requests
|
39 |
+
import copy
|
40 |
+
|
41 |
+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
|
42 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
43 |
+
run_example("<DESCRIPTION>", "Describe this image in great detail.", image)
|
44 |
+
|
45 |
+
# {'<DESCRIPTION>': '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.'}
|
46 |
+
```
|