sasha HF Staff commited on
Commit
45effd2
·
1 Parent(s): 6ed48fa

adding new embs

Browse files
app.py CHANGED
@@ -6,15 +6,15 @@ import wikipedia
6
 
7
 
8
  # Only runs once when the script is first run.
9
- with open("butts_1024_new.pickle", "rb") as handle:
10
  index = pickle.load(handle)
11
 
12
  # Load model for computing embeddings.
13
- feature_extractor = AutoFeatureExtractor.from_pretrained("sasha/autotrain-butterfly-similarity-2490576840")
14
- model = AutoModel.from_pretrained("sasha/autotrain-butterfly-similarity-2490576840")
15
 
16
  # Candidate images.
17
- dataset = load_dataset("sasha/butterflies_10k_names_multiple")
18
  ds = dataset["train"]
19
 
20
 
@@ -27,45 +27,46 @@ def query(image, top_k=4):
27
  logits = results[1][0].tolist()
28
  images = ds.select(inx)["image"]
29
  captions = ds.select(inx)["name"]
30
- images_with_captions = [(i, c) for i, c in zip(images,captions)]
31
- labels_with_probs = dict(zip(captions,logits))
32
- labels_with_probs = {k: 1- v for k, v in labels_with_probs.items()}
33
  try:
34
- description = wikipedia.summary(captions[0], sentences = 1)
35
- description = "### " + description
36
- url = wikipedia.page(captions[0]).url
37
- url = " You can learn more about your butterfly [here](" + str(url) + ")!"
38
- description = description + url
39
  except:
40
- description = "### Butterflies are insects in the order Lepidoptera, which also includes moths. Adult butterflies have large, often brightly coloured wings."
41
- url = "https://en.wikipedia.org/wiki/Butterfly"
42
- url = " You can learn more about butterflies [here](" + str(url) + ")!"
43
- description = description + url
44
  return images_with_captions, labels_with_probs, description
45
 
46
 
47
  with gr.Blocks() as demo:
48
- gr.Markdown("# Find my Butterfly 🦋")
49
- gr.Markdown("## Use this Space to find your butterfly, based on the [iNaturalist butterfly dataset](https://huggingface.co/datasets/huggan/inat_butterflies_top10k)!")
50
- with gr.Row():
51
- with gr.Column(scale=1):
52
- inputs = gr.Image()
53
- btn = gr.Button("Find my butterfly!")
54
- description = gr.Markdown()
55
-
56
- with gr.Column(scale=2):
57
- outputs=gr.Gallery(rows=2)
58
- labels = gr.Label()
59
-
60
- gr.Markdown("### Image Examples")
61
- gr.Examples(
62
- examples=["elton.jpg", "ken.jpg", "gaga.jpg", "taylor.jpg"],
63
- inputs=inputs,
64
- outputs=[outputs,labels],
65
- fn=query,
66
- cache_examples=True,
67
- )
68
- btn.click(query, inputs, [outputs, labels, description])
69
-
 
 
70
  demo.launch()
71
-
 
6
 
7
 
8
  # Only runs once when the script is first run.
9
+ with open("insectarium_768.pickle", "rb") as handle:
10
  index = pickle.load(handle)
11
 
12
  # Load model for computing embeddings.
13
+ feature_extractor = AutoFeatureExtractor.from_pretrained("sasha/vit-base-butterflies")
14
+ model = AutoModel.from_pretrained("sasha/vit-base-butterflies")
15
 
16
  # Candidate images.
17
+ dataset = load_dataset("sasha/insectarium-butterflies")
18
  ds = dataset["train"]
19
 
20
 
 
27
  logits = results[1][0].tolist()
28
  images = ds.select(inx)["image"]
29
  captions = ds.select(inx)["name"]
30
+ images_with_captions = [(i, c) for i, c in zip(images, captions)]
31
+ labels_with_probs = dict(zip(captions, logits))
32
+ labels_with_probs = {k: 1 - v for k, v in labels_with_probs.items()}
33
  try:
34
+ description = wikipedia.summary(captions[0], sentences=1)
35
+ description = "### " + description
36
+ url = wikipedia.page(captions[0]).url
37
+ url = " You can learn more about your butterfly [here](" + str(url) + ")!"
38
+ description = description + url
39
  except:
40
+ description = "### Butterflies are insects in the order Lepidoptera, which also includes moths. Adult butterflies have large, often brightly coloured wings."
41
+ url = "https://en.wikipedia.org/wiki/Butterfly"
42
+ url = " You can learn more about butterflies [here](" + str(url) + ")!"
43
+ description = description + url
44
  return images_with_captions, labels_with_probs, description
45
 
46
 
47
  with gr.Blocks() as demo:
48
+ gr.Markdown("# Find my Butterfly 🦋")
49
+ gr.Markdown(
50
+ "## Use this Space to find your butterfly, based on the [iNaturalist butterfly dataset](https://huggingface.co/datasets/huggan/inat_butterflies_top10k)!"
51
+ )
52
+ with gr.Row():
53
+ with gr.Column(scale=1):
54
+ inputs = gr.Image()
55
+ btn = gr.Button("Find my butterfly!")
56
+ description = gr.Markdown()
57
+
58
+ with gr.Column(scale=2):
59
+ outputs = gr.Gallery(rows=2)
60
+ labels = gr.Label()
61
+
62
+ gr.Markdown("### Image Examples")
63
+ gr.Examples(
64
+ examples=["elton.jpg", "ken.jpg", "gaga.jpg", "taylor.jpg"],
65
+ inputs=inputs,
66
+ outputs=[outputs, labels],
67
+ fn=query,
68
+ cache_examples=True,
69
+ )
70
+ btn.click(query, inputs, [outputs, labels, description])
71
+
72
  demo.launch()
 
index_768_cosine.pickle DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:864fe29de71f0e5b56ca87b04d559ea707d4cd3798429f80f04b8a58a07e3721
3
- size 53168791
 
 
 
 
butts_1024_new.pickle → insectarium_768.pickle RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:d5150f73211c6bac942b724c0bd56320ad99a0a0142d2c42199cab6bf719a1db
3
- size 52935099
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d8ceee3f2f9388816bc6d37101be7cabe6b62cf140729bd9d13e4386e3265791
3
+ size 211226535