sasha HF Staff commited on
Commit
ab7a42f
·
1 Parent(s): f45f570

python version

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