Spaces:
Runtime error
Runtime error
add code files
Browse files- README.md +7 -6
- app.py +122 -0
- prepare_index.py +105 -0
- requirements.txt +6 -0
README.md
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
-
sdk:
|
|
|
|
|
7 |
pinned: false
|
8 |
-
license: mit
|
9 |
---
|
10 |
|
11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Bioscan Updated Ids
|
3 |
+
emoji: π
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: gray
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 5.5.0
|
8 |
+
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
import h5py
|
5 |
+
import faiss
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
+
import pickle
|
9 |
+
import random
|
10 |
+
|
11 |
+
def getRandID():
|
12 |
+
indx = random.randrange(0, 396503)
|
13 |
+
return indx_to_id_dict[indx], indx
|
14 |
+
|
15 |
+
def chooseImageIndex(indexType):
|
16 |
+
if (indexType == "FlatIP(default)"):
|
17 |
+
return image_index_IP
|
18 |
+
elif (indexType == "FlatL2"):
|
19 |
+
return image_index_L2
|
20 |
+
elif (indexType == "HNSWFlat"):
|
21 |
+
return image_index_HNSW
|
22 |
+
elif (indexType == "IVFFlat"):
|
23 |
+
return image_index_IVF
|
24 |
+
elif (indexType == "LSH"):
|
25 |
+
return image_index_LSH
|
26 |
+
|
27 |
+
def chooseDNAIndex(indexType):
|
28 |
+
if (indexType == "FlatIP(default)"):
|
29 |
+
return dna_index_IP
|
30 |
+
elif (indexType == "FlatL2"):
|
31 |
+
return dna_index_L2
|
32 |
+
elif (indexType == "HNSWFlat"):
|
33 |
+
return dna_index_HNSW
|
34 |
+
elif (indexType == "IVFFlat"):
|
35 |
+
return dna_index_IVF
|
36 |
+
elif (indexType == "LSH"):
|
37 |
+
return dna_index_LSH
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
def searchEmbeddings(id, mod1, mod2, indexType):
|
42 |
+
# variable and index initialization
|
43 |
+
dim = 768
|
44 |
+
count = 0
|
45 |
+
num_neighbors = 10
|
46 |
+
|
47 |
+
index = faiss.IndexFlatIP(dim)
|
48 |
+
|
49 |
+
# get index
|
50 |
+
if (mod2 == "Image"):
|
51 |
+
index = chooseImageIndex(indexType)
|
52 |
+
elif (mod2 == "DNA"):
|
53 |
+
index = chooseDNAIndex(indexType)
|
54 |
+
|
55 |
+
|
56 |
+
# search for query
|
57 |
+
if (mod1 == "Image"):
|
58 |
+
query = id_to_image_emb_dict[id]
|
59 |
+
elif (mod1 == "DNA"):
|
60 |
+
query = id_to_dna_emb_dict[id]
|
61 |
+
query = query.astype(np.float32)
|
62 |
+
D, I = index.search(query, num_neighbors)
|
63 |
+
|
64 |
+
id_list = []
|
65 |
+
i = 1
|
66 |
+
for indx in I[0]:
|
67 |
+
id = indx_to_id_dict[indx]
|
68 |
+
id_list.append(id)
|
69 |
+
|
70 |
+
return id_list
|
71 |
+
|
72 |
+
with gr.Blocks() as demo:
|
73 |
+
|
74 |
+
# for hf: change all file paths, indx_to_id_dict as well
|
75 |
+
|
76 |
+
# load indexes
|
77 |
+
image_index_IP = faiss.read_index("big_image_index_FlatIP.index")
|
78 |
+
image_index_L2 = faiss.read_index("big_image_index_FlatL2.index")
|
79 |
+
image_index_HNSW = faiss.read_index("big_image_index_HNSWFlat.index")
|
80 |
+
image_index_IVF = faiss.read_index("big_image_index_IVFFlat.index")
|
81 |
+
image_index_LSH = faiss.read_index("big_image_index_LSH.index")
|
82 |
+
|
83 |
+
dna_index_IP = faiss.read_index("big_dna_index_FlatIP.index")
|
84 |
+
dna_index_L2 = faiss.read_index("big_dna_index_FlatL2.index")
|
85 |
+
dna_index_HNSW = faiss.read_index("big_dna_index_HNSWFlat.index")
|
86 |
+
dna_index_IVF = faiss.read_index("big_dna_index_IVFFlat.index")
|
87 |
+
dna_index_LSH = faiss.read_index("big_dna_index_LSH.index")
|
88 |
+
|
89 |
+
with open("dataset_processid_list.pickle", "rb") as f:
|
90 |
+
dataset_processid_list = pickle.load(f)
|
91 |
+
with open("processid_to_index.pickle", "rb") as f:
|
92 |
+
processid_to_index = pickle.load(f)
|
93 |
+
with open("big_indx_to_id_dict.pickle", "rb") as f:
|
94 |
+
indx_to_id_dict = pickle.load(f)
|
95 |
+
|
96 |
+
# initialize both possible dicts
|
97 |
+
with open("big_id_to_image_emb_dict.pickle", "rb") as f:
|
98 |
+
id_to_image_emb_dict = pickle.load(f)
|
99 |
+
with open("big_id_to_dna_emb_dict.pickle", "rb") as f:
|
100 |
+
id_to_dna_emb_dict = pickle.load(f)
|
101 |
+
|
102 |
+
with gr.Column():
|
103 |
+
with gr.Row():
|
104 |
+
with gr.Column():
|
105 |
+
rand_id = gr.Textbox(label="Random ID:")
|
106 |
+
rand_id_indx = gr.Textbox(label="Index:")
|
107 |
+
id_btn = gr.Button("Get Random ID")
|
108 |
+
with gr.Column():
|
109 |
+
mod1 = gr.Radio(choices=["DNA", "Image"], label="Search From:")
|
110 |
+
mod2 = gr.Radio(choices=["DNA", "Image"], label="Search To:")
|
111 |
+
|
112 |
+
indexType = gr.Radio(choices=["FlatIP(default)", "FlatL2", "HNSWFlat", "IVFFlat", "LSH"], label="Index:", value="FlatIP(default)")
|
113 |
+
process_id = gr.Textbox(label="ID:", info="Enter a sample ID to search for")
|
114 |
+
process_id_list = gr.Textbox(label="Closest 10 matches:" )
|
115 |
+
search_btn = gr.Button("Search")
|
116 |
+
id_btn.click(fn=getRandID, inputs=[], outputs=[rand_id, rand_id_indx])
|
117 |
+
|
118 |
+
search_btn.click(fn=searchEmbeddings, inputs=[process_id, mod1, mod2, indexType],
|
119 |
+
outputs=[process_id_list])
|
120 |
+
|
121 |
+
|
122 |
+
demo.launch()
|
prepare_index.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
|
3 |
+
import click
|
4 |
+
import faiss
|
5 |
+
import h5py
|
6 |
+
|
7 |
+
ALL_KEY_TYPES = ["dna", "image"]
|
8 |
+
ALL_INDEX_TYPES = ["IndexFlatIP", "IndexFlatL2", "IndexIVFFlat", "IndexHNSWFlat", "IndexLSH"]
|
9 |
+
EMBEDDING_SIZE = 768
|
10 |
+
|
11 |
+
|
12 |
+
def process(input: Path, output: Path, key_type: str, index_type: str):
|
13 |
+
# load embeddings
|
14 |
+
all_keys = h5py.File(input / "extracted_features_of_all_keys.hdf5", "r", libver="latest")[
|
15 |
+
f"encoded_{key_type}_feature"
|
16 |
+
][:]
|
17 |
+
seen_test = h5py.File(input / "extracted_features_of_seen_test.hdf5", "r", libver="latest")[
|
18 |
+
f"encoded_{key_type}_feature"
|
19 |
+
][:]
|
20 |
+
unseen_test = h5py.File(input / "extracted_features_of_unseen_test.hdf5", "r", libver="latest")[
|
21 |
+
f"encoded_{key_type}_feature"
|
22 |
+
][:]
|
23 |
+
seen_val = h5py.File(input / "extracted_features_of_seen_val.hdf5", "r", libver="latest")[
|
24 |
+
f"encoded_{key_type}_feature"
|
25 |
+
][:]
|
26 |
+
unseen_val = h5py.File(input / "extracted_features_of_unseen_val.hdf5", "r", libver="latest")[
|
27 |
+
f"encoded_{key_type}_feature"
|
28 |
+
][:]
|
29 |
+
|
30 |
+
# FlatIP and FlatL2
|
31 |
+
if index_type == "IndexFlatIP":
|
32 |
+
test_index = faiss.IndexFlatIP(EMBEDDING_SIZE)
|
33 |
+
elif index_type == "IndexFlatL2":
|
34 |
+
test_index = faiss.IndexFlatL2(EMBEDDING_SIZE)
|
35 |
+
elif index_type == "IndexIVFFlat":
|
36 |
+
# IVFFlat
|
37 |
+
quantizer = faiss.IndexFlatIP(EMBEDDING_SIZE)
|
38 |
+
test_index = faiss.IndexIVFFlat(quantizer, EMBEDDING_SIZE, 128)
|
39 |
+
test_index.train(all_keys)
|
40 |
+
test_index.train(seen_test)
|
41 |
+
test_index.train(unseen_test)
|
42 |
+
test_index.train(seen_val)
|
43 |
+
test_index.train(unseen_val)
|
44 |
+
elif index_type == "IndexHNSWFlat":
|
45 |
+
# HNSW
|
46 |
+
# 16: connections for each vertex. efSearch: depth of search during search. efConstruction: depth of search during build
|
47 |
+
test_index = faiss.IndexHNSWFlat(EMBEDDING_SIZE, 16)
|
48 |
+
test_index.hnsw.efSearch = 32
|
49 |
+
test_index.hnsw.efConstruction = 64
|
50 |
+
elif index_type == "IndexLSH":
|
51 |
+
# LSH
|
52 |
+
test_index = faiss.IndexLSH(EMBEDDING_SIZE, EMBEDDING_SIZE * 2)
|
53 |
+
else:
|
54 |
+
raise ValueError(f"Index type {index_type} is not supported")
|
55 |
+
|
56 |
+
test_index.add(all_keys)
|
57 |
+
test_index.add(seen_test)
|
58 |
+
test_index.add(unseen_test)
|
59 |
+
test_index.add(seen_val)
|
60 |
+
test_index.add(unseen_val)
|
61 |
+
|
62 |
+
faiss.write_index(test_index, str(output / f"bioscan_5m_{key_type}_{index_type}.index"))
|
63 |
+
print("Saved index to", output / f"bioscan_5m_{key_type}_{index_type}.index")
|
64 |
+
|
65 |
+
|
66 |
+
@click.command()
|
67 |
+
@click.option(
|
68 |
+
"--input",
|
69 |
+
type=click.Path(path_type=Path),
|
70 |
+
default="bioscan-clip-scripts/extracted_features",
|
71 |
+
help="Path to extracted features",
|
72 |
+
)
|
73 |
+
@click.option(
|
74 |
+
"--output", type=click.Path(path_type=Path), default="bioscan-clip-scripts/index", help="Path to save the index"
|
75 |
+
)
|
76 |
+
@click.option(
|
77 |
+
"--key-type", "key_type", type=click.Choice(["all", *ALL_KEY_TYPES]), default="all", help="Type of key to use"
|
78 |
+
)
|
79 |
+
@click.option(
|
80 |
+
"--index-type",
|
81 |
+
"index_type",
|
82 |
+
type=click.Choice(["all", *ALL_INDEX_TYPES]),
|
83 |
+
default="all",
|
84 |
+
help="Type of index to use",
|
85 |
+
)
|
86 |
+
def main(input, output, key_type, index_type):
|
87 |
+
output.mkdir(parents=True, exist_ok=True)
|
88 |
+
|
89 |
+
if key_type == "all":
|
90 |
+
key_types = ALL_KEY_TYPES
|
91 |
+
else:
|
92 |
+
key_types = [key_type]
|
93 |
+
|
94 |
+
if index_type == "all":
|
95 |
+
index_types = ALL_INDEX_TYPES
|
96 |
+
else:
|
97 |
+
index_types = [index_type]
|
98 |
+
|
99 |
+
for key_type in key_types:
|
100 |
+
for index_type in index_types:
|
101 |
+
process(input, output, key_type, index_type)
|
102 |
+
|
103 |
+
|
104 |
+
if __name__ == "__main__":
|
105 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
numpy~=1.26.0
|
3 |
+
faiss-cpu==1.8.0
|
4 |
+
h5py~=3.9.0
|
5 |
+
gradio==5.7.1
|
6 |
+
click==8.1.7
|