Spaces:
Runtime error
Runtime error
adding new embs
Browse files- app.py +40 -39
- index_768_cosine.pickle +0 -3
- butts_1024_new.pickle → insectarium_768.pickle +2 -2
app.py
CHANGED
|
@@ -6,15 +6,15 @@ import wikipedia
|
|
| 6 |
|
| 7 |
|
| 8 |
# Only runs once when the script is first run.
|
| 9 |
-
with open("
|
| 10 |
index = pickle.load(handle)
|
| 11 |
|
| 12 |
# Load model for computing embeddings.
|
| 13 |
-
feature_extractor = AutoFeatureExtractor.from_pretrained("sasha/
|
| 14 |
-
model = AutoModel.from_pretrained("sasha/
|
| 15 |
|
| 16 |
# Candidate images.
|
| 17 |
-
dataset = load_dataset("sasha/
|
| 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 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
except:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
return images_with_captions, labels_with_probs, description
|
| 45 |
|
| 46 |
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 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:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d8ceee3f2f9388816bc6d37101be7cabe6b62cf140729bd9d13e4386e3265791
|
| 3 |
+
size 211226535
|