Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import (
|
2 |
+
PaliGemmaProcessor,
|
3 |
+
PaliGemmaForConditionalGeneration,
|
4 |
+
)
|
5 |
+
from transformers.image_utils import load_image
|
6 |
+
import torch
|
7 |
+
|
8 |
+
model_id = "google/paligemma2-3b-pt-448"
|
9 |
+
|
10 |
+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg"
|
11 |
+
image = load_image(url)
|
12 |
+
|
13 |
+
model = PaliGemmaForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto").eval()
|
14 |
+
processor = PaliGemmaProcessor.from_pretrained(model_id)
|
15 |
+
|
16 |
+
# Leaving the prompt blank for pre-trained models
|
17 |
+
prompt = ""
|
18 |
+
model_inputs = processor(text=prompt, images=image, return_tensors="pt").to(torch.bfloat16).to(model.device)
|
19 |
+
input_len = model_inputs["input_ids"].shape[-1]
|
20 |
+
|
21 |
+
with torch.inference_mode():
|
22 |
+
generation = model.generate(**model_inputs, max_new_tokens=100, do_sample=False)
|
23 |
+
generation = generation[0][input_len:]
|
24 |
+
decoded = processor.decode(generation, skip_special_tokens=True)
|
25 |
+
print(decoded)
|