Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,33 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from transformers import CLIPProcessor, CLIPModel
|
2 |
|
3 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
4 |
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
5 |
|
|
|
|
|
6 |
|
7 |
+
def inference(input_img, captions):
|
8 |
+
captions_list = captions.split(",")
|
9 |
+
#url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
10 |
+
#image = Image.open(requests.get(url, stream=True).raw)
|
11 |
+
inputs = processor(text=captions_list, images=input_img, return_tensors="pt", padding=True)
|
12 |
+
outputs = model(**inputs)
|
13 |
+
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
|
14 |
+
probs = logits_per_image.softmax(dim=1)
|
15 |
+
probabilities_percentages = ', '.join(['{:.2f}%'.format(prob.item() * 100) for prob in probs[0]])
|
16 |
+
return probabilities_percentages
|
17 |
+
|
18 |
+
title = "TSAI S18 Assignment: Use a pretrained CLIP model and give a demo on its workig"
|
19 |
+
description = "A simple Gradio interface that accepts an image and some captions, and gives a score as to how much the caption describes the image "
|
20 |
+
|
21 |
+
examples = [["cats.jpg","a photo of a cat, a photo of a dog"]
|
22 |
+
]
|
23 |
+
|
24 |
+
demo = gr.Interface(
|
25 |
+
inference,
|
26 |
+
inputs = [gr.Image(shape=(416, 416), label="Input Image"), gr.Textbox(placeholder="Enter different captions for image, separated by comma")],
|
27 |
+
outputs = [gr.Textbox(label="Probability score of captions")],
|
28 |
+
title = title,
|
29 |
+
description = description,
|
30 |
+
examples = examples,
|
31 |
+
)
|
32 |
|
33 |
+
demo.launch()
|
|
|
|