micole66 commited on
Commit
bf1110a
1 Parent(s): 99e3eb5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoProcessor, AutoModel
3
+
4
+ processor = AutoProcessor.from_pretrained("flax-community/clip-rsicd-v2")
5
+
6
+ model = AutoModel.from_pretrained("flax-community/clip-rsicd-v2")
7
+
8
+
9
+ def calculate_score(image, text):
10
+ labels = text.split(";")
11
+ labels = [l.strip() for l in labels]
12
+ labels = list(filter(None, labels))
13
+ if len(labels) == 0:
14
+ return dict()
15
+ inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
16
+ outputs = model(**inputs)
17
+ logits_per_image = outputs.logits_per_image.detach().numpy()
18
+
19
+ results_dict = {
20
+ label: score / 100.0 for label, score in zip(labels, logits_per_image[0])
21
+ }
22
+ return results_dict
23
+
24
+
25
+ if __name__ == "__main__":
26
+ cat_example = [
27
+ "cat.jpg",
28
+ "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",
29
+ ]
30
+
31
+ demo = gr.Interface(
32
+ fn=calculate_score,
33
+ inputs=["image", "text"],
34
+ outputs="label",
35
+ examples=[cat_example],
36
+ allow_flagging="never",
37
+ description="# CLIP Score",
38
+ article="Calculate the [CLIP](https://openai.com/blog/clip/) score of a given image and text",
39
+ cache_examples=True,
40
+ )
41
+
42
+ demo.launch()