tatoy commited on
Commit
7dfaa48
·
1 Parent(s): 49af646
Files changed (1) hide show
  1. app.py +18 -0
app.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import requests
3
+ from transformers import Blip2Processor, Blip2Model, Blip2ForConditionalGeneration
4
+ import torch
5
+
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+
8
+ processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xxl", load_in_8bit=True, device_map="auto")
9
+
10
+ model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xxl", load_in_8bit=True, device_map="auto")
11
+
12
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
13
+ image = Image.open(requests.get(url, stream=True).raw)
14
+
15
+ inputs = processor(image, return_tensors="pt").to(device, torch.float16)
16
+
17
+ out = model.generate(**inputs, max_length=64, min_length=20)
18
+ print(i,': ',processor.decode(out[0], skip_special_tokens=True))