Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install git+https://github.com/openai/CLIP.git
|
2 |
+
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
|
6 |
+
from transformers import CLIPProcessor, CLIPModel
|
7 |
+
|
8 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
9 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
10 |
+
|
11 |
+
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
12 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
13 |
+
|
14 |
+
inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)
|
15 |
+
|
16 |
+
outputs = model(**inputs)
|
17 |
+
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
|
18 |
+
probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
|