ariG23498's picture
ariG23498 HF Staff
run on cpu
a852975
raw
history blame
2.61 kB
"""This space is taken and modified from https://huggingface.co/spaces/merve/compare_clip_siglip"""
from transformers import pipeline
import gradio as gr
################################################################################
# Load the models
################################################################################
sg1_ckpt = "google/siglip-so400m-patch14-384"
sg1_pipe = pipeline(task="zero-shot-image-classification", model=sg1_ckpt, device="cpu")
sg2_ckpt = "google/siglip2-so400m-patch14-384"
sg2_pipe = pipeline(task="zero-shot-image-classification", model=sg2_ckpt, device="cpu")
################################################################################
# Run inference
################################################################################
def infer(image, candidate_labels):
candidate_labels = [label.lstrip(" ") for label in candidate_labels.split(",")]
sg1_socres = sg1_pipe(image, candidate_labels=candidate_labels)
sg2_socres = sg2_pipe(image, candidate_labels=candidate_labels)
sg1_outputs = {element["label"]:element["score"] for element in sg1_socres}
sg2_outputs = {element["label"]:element["score"] for element in sg2_socres}
return sg1_outputs, sg2_outputs
################################################################################
# Gradio App
################################################################################
with gr.Blocks() as demo:
gr.Markdown("# Compare SigLIP 1 and SigLIP 2")
gr.Markdown(
"Compare the performance of SigLIP 1 and SigLIP 2 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 (comma seperated)")
run_button = gr.Button("Run", visible=True)
with gr.Column():
siglip1_output = gr.Label(label="SigLIP 1 Output", num_top_classes=3)
siglip2_output = gr.Label(label="SigLIP 2 Output", num_top_classes=3)
examples = [
["./baklava.jpg", "desser on a plate, a serving of baklava, a plate and spoon"],
["./baklava.jpg", "a cat, two cats, three cats"],
["./baklava.jpg", "two sleeping cats, two cats playing, three cats laying down"],
]
gr.Examples(
examples=examples,
inputs=[image_input, text_input],
outputs=[siglip1_output, siglip2_output],
fn=infer,
)
run_button.click(
fn=infer, inputs=[image_input, text_input], outputs=[siglip1_output, siglip2_output]
)
demo.launch()