Spaces:
Sleeping
Sleeping
MikeTrizna
commited on
Commit
•
a017c81
1
Parent(s):
843dcad
Initial commit
Browse files- .gitattributes +2 -0
- README.md +2 -2
- bhl_flickr_list.json +3 -0
- bhl_index.annoy +3 -0
- requirements.txt +5 -0
- streamlit_app.py +134 -0
.gitattributes
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
|
|
2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
@@ -33,3 +34,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
1 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.annoy filter=lfs diff=lfs merge=lfs -text
|
3 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
4 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
5 |
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
|
|
34 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
35 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
36 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
37 |
+
bhl_flickr_list.json filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
emoji: 🐢
|
4 |
colorFrom: indigo
|
5 |
colorTo: blue
|
@@ -10,4 +10,4 @@ pinned: false
|
|
10 |
license: mit
|
11 |
---
|
12 |
|
13 |
-
|
|
|
1 |
---
|
2 |
+
title: BHL Flickr Search
|
3 |
emoji: 🐢
|
4 |
colorFrom: indigo
|
5 |
colorTo: blue
|
|
|
10 |
license: mit
|
11 |
---
|
12 |
|
13 |
+
This is a Hugging Face implementation of https://github.com/miketrizna/bhl_flickr_search
|
bhl_flickr_list.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3409a9e5895eccaeec5c0f133d5a28be0cf868dbf04c096d9dd22ef6baec14a6
|
3 |
+
size 21832723
|
bhl_index.annoy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:734c1933e58f75c2dac87cb31c26a311559fcea142f6b86e5c4bccd7b848d1fb
|
3 |
+
size 702894660
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
annoy
|
3 |
+
sentence-transformers
|
4 |
+
pillow
|
5 |
+
ftfy
|
streamlit_app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from annoy import AnnoyIndex
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
import json
|
5 |
+
from PIL import Image
|
6 |
+
import os
|
7 |
+
import urllib
|
8 |
+
|
9 |
+
st.set_page_config(
|
10 |
+
page_title="BHL Flickr Image Search",
|
11 |
+
page_icon="🖼️",
|
12 |
+
layout="wide"
|
13 |
+
)
|
14 |
+
|
15 |
+
@st.cache_resource
|
16 |
+
def load_clip_model():
|
17 |
+
return SentenceTransformer('clip-ViT-B-32')
|
18 |
+
|
19 |
+
@st.cache_resource
|
20 |
+
def load_annoy_index():
|
21 |
+
annoy_index = AnnoyIndex(512, metric='angular')
|
22 |
+
annoy_index.load('bhl_index.annoy')
|
23 |
+
return annoy_index
|
24 |
+
|
25 |
+
@st.cache_data
|
26 |
+
def load_flickr_data():
|
27 |
+
with open('bhl_flickr_list.json') as json_in:
|
28 |
+
bhl_flickr_ids = json.load(json_in)
|
29 |
+
return bhl_flickr_ids
|
30 |
+
|
31 |
+
def bhl_annoy_search(mode, query, k=5):
|
32 |
+
if mode == 'id':
|
33 |
+
for idx, row in enumerate(bhl_flickr_ids):
|
34 |
+
if str(row['flickr_id']) == query:
|
35 |
+
matching_row = idx
|
36 |
+
neighbors = bhl_index.get_nns_by_item(matching_row, k,
|
37 |
+
include_distances=True)
|
38 |
+
elif mode == 'text':
|
39 |
+
query_emb = model.encode([query], show_progress_bar=False)
|
40 |
+
neighbors = bhl_index.get_nns_by_vector(query_emb[0], k,
|
41 |
+
include_distances=True)
|
42 |
+
elif mode == 'image':
|
43 |
+
query_emb = model.encode([query], show_progress_bar=False)
|
44 |
+
neighbors = bhl_index.get_nns_by_vector(query_emb[0], k,
|
45 |
+
include_distances=True)
|
46 |
+
return neighbors
|
47 |
+
|
48 |
+
#DEPLOY_MODE = 'streamlit_share'
|
49 |
+
#DEPLOY_MODE = 'hf_spaces'
|
50 |
+
DEPLOY_MODE = 'localhost'
|
51 |
+
|
52 |
+
if DEPLOY_MODE == 'localhost':
|
53 |
+
BASE_URL = 'http://localhost:8501/'
|
54 |
+
elif DEPLOY_MODE == 'streamlit_share':
|
55 |
+
BASE_URL = 'https://share.streamlit.io/miketrizna/bhl_flickr_search'
|
56 |
+
elif DEPLOY_MODE == 'hf_spaces':
|
57 |
+
BASE_URL = 'https://huggingface.co/spaces/MikeTrizna/bhl_flickr_search'
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
st.markdown("# BHL Flickr Image Search")
|
61 |
+
with st.expander("How does this work?", expanded=False):
|
62 |
+
st.write('placeholder')
|
63 |
+
|
64 |
+
st.sidebar.markdown('### Search Mode')
|
65 |
+
|
66 |
+
query_params = st.experimental_get_query_params()
|
67 |
+
mode_index = 0
|
68 |
+
if 'mode' in query_params:
|
69 |
+
if query_params['mode'][0] == 'text_search':
|
70 |
+
mode_index = 0
|
71 |
+
elif query_params['mode'][0] == 'flickr_id':
|
72 |
+
mode_index = 2
|
73 |
+
|
74 |
+
app_mode = st.sidebar.radio("How would you like to search?",
|
75 |
+
['Text search','Upload Image', 'BHL Flickr ID'],
|
76 |
+
index = mode_index)
|
77 |
+
|
78 |
+
model = load_clip_model()
|
79 |
+
bhl_index = load_annoy_index()
|
80 |
+
bhl_flickr_ids = load_flickr_data()
|
81 |
+
|
82 |
+
if app_mode == 'Text search':
|
83 |
+
search_text = 'a watercolor illustration of an insect with flowers'
|
84 |
+
if 'mode' in query_params:
|
85 |
+
if query_params['mode'][0] == 'text_search':
|
86 |
+
if 'query' in query_params:
|
87 |
+
search_text = query_params['query'][0]
|
88 |
+
else:
|
89 |
+
st.experimental_set_query_params()
|
90 |
+
query = st.text_input('Text query',search_text)
|
91 |
+
search_mode = 'text'
|
92 |
+
#closest_k_idx, closest_k_dist = bhl_text_search(text_query, 100)
|
93 |
+
|
94 |
+
elif app_mode == 'BHL Flickr ID':
|
95 |
+
search_id = '5974846748'
|
96 |
+
if 'mode' in st.experimental_get_query_params():
|
97 |
+
if st.experimental_get_query_params()['mode'][0] == 'flickr_id':
|
98 |
+
if 'query' in st.experimental_get_query_params():
|
99 |
+
search_id = st.experimental_get_query_params()['query'][0]
|
100 |
+
else:
|
101 |
+
st.experimental_set_query_params()
|
102 |
+
query = st.text_input('Query ID', search_id)
|
103 |
+
search_mode = 'id'
|
104 |
+
#closest_k_idx, closest_k_dist = bhl_id_search(id_query, 100)
|
105 |
+
|
106 |
+
elif app_mode == 'Upload Image':
|
107 |
+
st.experimental_set_query_params()
|
108 |
+
query = None
|
109 |
+
image_file = st.file_uploader("Upload Image", type=["png","jpg","jpeg"])
|
110 |
+
search_mode = 'image'
|
111 |
+
#closest_k_idx = []
|
112 |
+
if image_file is not None:
|
113 |
+
query = Image.open(image_file)
|
114 |
+
st.image(query,width=100,caption='Query image')
|
115 |
+
#closest_k_idx, closest_k_dist = bhl_image_search(img, 100)
|
116 |
+
|
117 |
+
if query:
|
118 |
+
closest_k_idx, closest_k_dist = bhl_annoy_search(search_mode, query, 100)
|
119 |
+
|
120 |
+
col_list = st.columns(5)
|
121 |
+
|
122 |
+
if len(closest_k_idx):
|
123 |
+
for idx, annoy_idx in enumerate(closest_k_idx):
|
124 |
+
bhl_ids = bhl_flickr_ids[annoy_idx]
|
125 |
+
bhl_url = f"https://live.staticflickr.com/{bhl_ids['server']}/{bhl_ids['flickr_id']}_{bhl_ids['secret']}.jpg"
|
126 |
+
col_list[idx%5].image(bhl_url, use_column_width=True)
|
127 |
+
|
128 |
+
flickr_url = f"https://www.flickr.com/photos/biodivlibrary/{bhl_ids['flickr_id']}/"
|
129 |
+
neighbors_url = f"{BASE_URL}?mode=flickr_id&query={bhl_ids['flickr_id']}"
|
130 |
+
link_html = f'<a href="{flickr_url}" target="_blank">Flickr Link</a> | <a href="{neighbors_url}">Neighbors</a>'
|
131 |
+
col_list[idx%5].markdown(link_html, unsafe_allow_html=True)
|
132 |
+
col_list[idx%5].markdown("---")
|
133 |
+
|
134 |
+
|