Spaces:
Running
Running
python version
Browse files
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(
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
|
|
|
| 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()
|
|
|