update
Browse files
app.py
CHANGED
@@ -6,19 +6,36 @@ processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
|
|
6 |
|
7 |
|
8 |
def calculate_score(image, text):
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
|
21 |
|
22 |
if __name__ == "__main__":
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
def calculate_score(image, text):
|
9 |
+
labels = text.split(";")
|
10 |
+
labels = [l.strip() for l in labels]
|
11 |
+
labels = list(filter(None, labels))
|
12 |
+
if len(labels) == 0:
|
13 |
+
return dict()
|
14 |
+
inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
|
15 |
+
outputs = model(**inputs)
|
16 |
+
logits_per_image = outputs.logits_per_image.detach().numpy()
|
17 |
+
|
18 |
+
results_dict = {
|
19 |
+
label: score / 100.0 for label, score in zip(labels, logits_per_image[0])
|
20 |
+
}
|
21 |
+
return results_dict
|
22 |
|
23 |
|
24 |
if __name__ == "__main__":
|
25 |
+
cat_example = [
|
26 |
+
"cat.jpg",
|
27 |
+
"a cat stuck in a door; a cat in the air; a cat sitting; a cat standing; a cat is entering the matrix; a cat is entering the void",
|
28 |
+
]
|
29 |
+
|
30 |
+
demo = gr.Interface(
|
31 |
+
fn=calculate_score,
|
32 |
+
inputs=["image", "text"],
|
33 |
+
outputs="label",
|
34 |
+
examples=[cat_example],
|
35 |
+
allow_flagging="never",
|
36 |
+
description="# CLIP Score",
|
37 |
+
article="Calculate the [CLIP](https://openai.com/blog/clip/) score of a given image and text",
|
38 |
+
cache_examples=True,
|
39 |
+
)
|
40 |
+
|
41 |
+
demo.launch()
|
cat.jpg
ADDED