VarunSivamani commited on
Commit
c9bbb53
1 Parent(s): 5ef295c

added app file

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import CLIPProcessor, CLIPModel
3
+
4
+ model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
5
+ processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
6
+
7
+
8
+ def CLIP_calculate_score(image, text):
9
+ words = text.split(";")
10
+ words = [w.strip() for w in words]
11
+ words = list(filter(None, words))
12
+
13
+ if len(words) == 0:
14
+ return dict()
15
+
16
+ inputs = processor(text=words, images=image, return_tensors="pt", padding=True)
17
+ outputs = model(**inputs)
18
+ logits_per_image = outputs.logits_per_image.detach().numpy()
19
+
20
+ results_dict = {
21
+ label: score / 100.0
22
+ for label, score in zip(words, logits_per_image[0])
23
+ }
24
+
25
+ return results_dict
26
+
27
+
28
+ examples = [
29
+ ["images/girl_and_dog.jpg", "a dog playing in the beach; a dog and a girl playing in the beach; a girl playing in the beach"],
30
+ ["images/horse.jpg", "group of horses running; a dog playing; a horse standing"],
31
+ ["images/man_and_cat.jpg", "a man and a cat listening to music; a cat; a man"]
32
+ ]
33
+
34
+ demo = gr.Interface(
35
+ fn=CLIP_calculate_score,
36
+ inputs=["image", "text"],
37
+ outputs="label",
38
+ examples=examples,
39
+ # allow_flagging="never",
40
+ # cache_examples=True,
41
+ )
42
+
43
+ demo.launch()