import torch from transformers import pipeline import gradio as gr siglip_checkpoint = "nielsr/siglip-base-patch16-224" clip_checkpoint = "openai/clip-vit-base-patch16" siglip_detector = pipeline(model=siglip_checkpoint, task="zero-shot-image-classification") clip_detector = pipeline(model=clip_checkpoint, task="zero-shot-image-classification") def postprocess(output): return {out["label"]: float(out["score"]) for out in output} def infer(image, candidate_labels): candidate_labels = [label.lstrip(" ") for label in candidate_labels.split(",")] siglip_out = siglip_detector(image, candidate_labels=candidate_labels) clip_out = clip_detector(image, candidate_labels=candidate_labels) return postprocess(clip_out), postprocess(siglip_out) with gr.Blocks() as demo: gr.Markdown("# Compare CLIP and SigLIP") gr.Markdown("Compare the performance of CLIP and SigLIP on zero-shot classification in this Space 👇") with gr.Row(): with gr.Column(): image_input = gr.Image(type="pil") text_input = gr.Textbox(label="Input a list of labels") run_button = gr.Button("Run", visible=True) with gr.Column(): clip_output = gr.Label(label = "CLIP Output", num_top_classes=3) siglip_output = gr.Label(label = "SigLIP Output", num_top_classes=3) examples = [["./baklava.jpg", "baklava, souffle, tiramisu"]] gr.Examples( examples = examples, inputs=[image_input, text_input], outputs=[clip_output, siglip_output ], fn=infer, cache_examples=True ) run_button.click(fn=infer, inputs=[image_input, text_input], outputs=[clip_output, siglip_output ])