Plana-Archive commited on
Commit
e64f9d3
·
verified ·
1 Parent(s): 3abc8a0

Upload compare_clip_siglip/app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. compare_clip_siglip/app.py +46 -0
compare_clip_siglip/app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import pipeline, SiglipModel, AutoProcessor
3
+ import numpy as np
4
+ import gradio as gr
5
+
6
+
7
+ clip_checkpoint = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"
8
+ clip_detector = pipeline(model=clip_checkpoint, task="zero-shot-image-classification")
9
+
10
+
11
+ def postprocess(output):
12
+ return {out["label"]: float(out["score"]) for out in output}
13
+
14
+
15
+ def infer(image, candidate_labels):
16
+ candidate_labels = [label.lstrip(" ") for label in candidate_labels.split(",")]
17
+ clip_out = clip_detector(image, candidate_labels=candidate_labels)
18
+ return postprocess(clip_out)
19
+
20
+ with gr.Blocks() as demo:
21
+ gr.Markdown("# Compare CLIP and SigLIP")
22
+ gr.Markdown("Compare the performance of CLIP and SigLIP on zero-shot classification in this Space 👇")
23
+ with gr.Row():
24
+ with gr.Column():
25
+ image_input = gr.Image(type="pil")
26
+ text_input = gr.Textbox(label="Input a list of labels")
27
+ run_button = gr.Button("Run", visible=True)
28
+
29
+ with gr.Column():
30
+ clip_output = gr.Label(label = "CLIP Output", num_top_classes=15)
31
+
32
+ examples = [["./baklava.jpg", "baklava, souffle, tiramisu"]]
33
+ gr.Examples(
34
+ examples = examples,
35
+ inputs=[image_input, text_input],
36
+ outputs=[clip_output,
37
+ ],
38
+ fn=infer,
39
+ cache_examples=True
40
+ )
41
+ run_button.click(fn=infer,
42
+ inputs=[image_input, text_input],
43
+ outputs=[clip_output,
44
+ ])
45
+
46
+ demo.launch()