ariG23498 HF staff commited on
Commit
0553ee9
·
1 Parent(s): ba8b411

adding examples

Browse files
Files changed (3) hide show
  1. app.py +12 -9
  2. cat.jpg +0 -0
  3. requirements.txt +1 -2
app.py CHANGED
@@ -1,9 +1,10 @@
1
  """This space is taken and modified from https://huggingface.co/spaces/merve/compare_clip_siglip"""
2
  import torch
3
- from transformers import AutoModel, AutoProcessor
4
- import numpy as np
 
 
5
  import gradio as gr
6
- import spaces
7
 
8
  ################################################################################
9
  # Load the models
@@ -12,7 +13,7 @@ sg1_ckpt = "google/siglip-so400m-patch14-384"
12
  siglip1_model = AutoModel.from_pretrained(sg1_ckpt, device_map="auto").eval()
13
  siglip1_processor = AutoProcessor.from_pretrained(sg1_ckpt)
14
 
15
- sg2_ckpt = "s0225/siglip2-so400m-patch14-384"
16
  siglip2_model = AutoModel.from_pretrained(sg2_ckpt, device_map="auto").eval()
17
  siglip2_processor = AutoProcessor.from_pretrained(sg2_ckpt)
18
 
@@ -24,11 +25,10 @@ def postprocess(output):
24
 
25
 
26
  def postprocess_siglip(sg1_probs, sg2_probs, labels):
27
- sg1_output = {labels[i]: float(np.array(sg1_probs[0])[i]) for i in range(len(labels))}
28
- sg2_output = {labels[i]: float(np.array(sg2_probs[0])[i]) for i in range(len(labels))}
29
  return sg1_output, sg2_output
30
 
31
- @spaces.GPU
32
  def siglip_detector(image, texts):
33
  sg1_inputs = siglip1_processor(
34
  text=texts, images=image, return_tensors="pt", padding="max_length", max_length=64
@@ -73,13 +73,16 @@ with gr.Blocks() as demo:
73
  siglip1_output = gr.Label(label="SigLIP 1 Output", num_top_classes=3)
74
  siglip2_output = gr.Label(label="SigLIP 2 Output", num_top_classes=3)
75
 
76
- examples = [["./baklava.jpg", "baklava, souffle, tiramisu"]]
 
 
 
 
77
  gr.Examples(
78
  examples=examples,
79
  inputs=[image_input, text_input],
80
  outputs=[siglip1_output, siglip2_output],
81
  fn=infer,
82
- cache_examples=True,
83
  )
84
  run_button.click(
85
  fn=infer, inputs=[image_input, text_input], outputs=[siglip1_output, siglip2_output]
 
1
  """This space is taken and modified from https://huggingface.co/spaces/merve/compare_clip_siglip"""
2
  import torch
3
+ from transformers import (
4
+ AutoModel,
5
+ AutoProcessor
6
+ )
7
  import gradio as gr
 
8
 
9
  ################################################################################
10
  # Load the models
 
13
  siglip1_model = AutoModel.from_pretrained(sg1_ckpt, device_map="auto").eval()
14
  siglip1_processor = AutoProcessor.from_pretrained(sg1_ckpt)
15
 
16
+ sg2_ckpt = "google/siglip2-so400m-patch14-384"
17
  siglip2_model = AutoModel.from_pretrained(sg2_ckpt, device_map="auto").eval()
18
  siglip2_processor = AutoProcessor.from_pretrained(sg2_ckpt)
19
 
 
25
 
26
 
27
  def postprocess_siglip(sg1_probs, sg2_probs, labels):
28
+ sg1_output = {labels[i]: float(sg1_probs[0].cpu().numpy()[i]) for i in range(len(labels))}
29
+ sg2_output = {labels[i]: float(sg2_probs[0].cpu().numpy()[i]) for i in range(len(labels))}
30
  return sg1_output, sg2_output
31
 
 
32
  def siglip_detector(image, texts):
33
  sg1_inputs = siglip1_processor(
34
  text=texts, images=image, return_tensors="pt", padding="max_length", max_length=64
 
73
  siglip1_output = gr.Label(label="SigLIP 1 Output", num_top_classes=3)
74
  siglip2_output = gr.Label(label="SigLIP 2 Output", num_top_classes=3)
75
 
76
+ examples = [
77
+ ["./baklava.jpg", "desser on a plate, a serving of baklava, a plate and spoon"],
78
+ ["./baklava.jpg", "a cat, two cats, three cats"],
79
+ ["./baklava.jpg", "two sleeping cats, two cats playing, three cats laying down"],
80
+ ]
81
  gr.Examples(
82
  examples=examples,
83
  inputs=[image_input, text_input],
84
  outputs=[siglip1_output, siglip2_output],
85
  fn=infer,
 
86
  )
87
  run_button.click(
88
  fn=infer, inputs=[image_input, text_input], outputs=[siglip1_output, siglip2_output]
cat.jpg ADDED
requirements.txt CHANGED
@@ -4,5 +4,4 @@ git+https://github.com/huggingface/transformers@main
4
  sentencepiece
5
  pillow
6
  protobuf
7
- accelerate
8
- spaces
 
4
  sentencepiece
5
  pillow
6
  protobuf
7
+ accelerate