Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,23 +8,11 @@ import io
|
|
| 8 |
import pickle
|
| 9 |
import random
|
| 10 |
|
| 11 |
-
def getRandID():
|
| 12 |
-
indx = random.randrange(0, 325667)
|
| 13 |
-
return indx_to_id_dict[indx]
|
| 14 |
-
|
| 15 |
-
def getTax(indx):
|
| 16 |
-
s = species[indx]
|
| 17 |
-
g = genus[indx]
|
| 18 |
-
f = family[indx]
|
| 19 |
-
str = "Species: " + s + "\nGenus: " + g + "\nFamily: " + f
|
| 20 |
-
return str
|
| 21 |
-
|
| 22 |
def get_image(image1, image2, dataset_image_mask, processid_to_index, idx):
|
| 23 |
if (idx < 162834):
|
| 24 |
image_enc_padded = image1[idx].astype(np.uint8)
|
| 25 |
elif(idx >= 162834):
|
| 26 |
image_enc_padded = image2[idx-162834].astype(np.uint8)
|
| 27 |
-
|
| 28 |
enc_length = dataset_image_mask[idx]
|
| 29 |
image_enc = image_enc_padded[:enc_length]
|
| 30 |
image = Image.open(io.BytesIO(image_enc))
|
|
@@ -55,7 +43,7 @@ def searchEmbeddings(id, mod1, mod2):
|
|
| 55 |
for indx in I[0]:
|
| 56 |
id = indx_to_id_dict[indx]
|
| 57 |
id_list.append(id)
|
| 58 |
-
|
| 59 |
# get images
|
| 60 |
image0 = get_image(dataset_image1, dataset_image2, dataset_image_mask, processid_to_index, original_indx)
|
| 61 |
image1 = get_image(dataset_image1, dataset_image2, dataset_image_mask, processid_to_index, I[0][0])
|
|
@@ -68,7 +56,7 @@ def searchEmbeddings(id, mod1, mod2):
|
|
| 68 |
image8 = get_image(dataset_image1, dataset_image2, dataset_image_mask, processid_to_index, I[0][7])
|
| 69 |
image9 = get_image(dataset_image1, dataset_image2, dataset_image_mask, processid_to_index, I[0][8])
|
| 70 |
image10 = get_image(dataset_image1, dataset_image2, dataset_image_mask, processid_to_index, I[0][9])
|
| 71 |
-
|
| 72 |
# get taxonomic information
|
| 73 |
s0 = getTax(original_indx)
|
| 74 |
s1 = getTax(I[0][0])
|
|
@@ -84,7 +72,18 @@ def searchEmbeddings(id, mod1, mod2):
|
|
| 84 |
|
| 85 |
return id_list, image0, image1, image2, image3, image4, image5, image6, image7, image8, image9, image10, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# open general files
|
| 89 |
with open("dataset_image1.pickle", "rb") as f:
|
| 90 |
dataset_image1 = pickle.load(f)
|
|
@@ -108,12 +107,9 @@ with gr.Blocks() as demo:
|
|
| 108 |
id_to_dna_emb_dict = pickle.load(f)
|
| 109 |
|
| 110 |
# open taxonomy files
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
genus= [item.decode("utf-8") for item in pickle.load(f)]
|
| 115 |
-
with open("species.pickle", "rb") as f:
|
| 116 |
-
species = [item.decode("utf-8") for item in pickle.load(f)]
|
| 117 |
|
| 118 |
with gr.Column():
|
| 119 |
process_id = gr.Textbox(label="ID:", info="Enter a sample ID to search for")
|
|
@@ -167,7 +163,7 @@ with gr.Blocks() as demo:
|
|
| 167 |
image10 = gr.Image(label=10)
|
| 168 |
tax10 = gr.Textbox(label="Taxonomy")
|
| 169 |
|
| 170 |
-
id_btn.click(fn=getRandID, inputs=[], outputs=rand_id)
|
| 171 |
search_btn.click(fn=searchEmbeddings, inputs=[process_id, mod1, mod2],
|
| 172 |
outputs=[process_id_list, image0, image1, image2, image3, image4, image5, image6, image7, image8, image9, image10,
|
| 173 |
tax0, tax1, tax2, tax3, tax4, tax5, tax6, tax7, tax8, tax9, tax10])
|
|
|
|
| 8 |
import pickle
|
| 9 |
import random
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def get_image(image1, image2, dataset_image_mask, processid_to_index, idx):
|
| 12 |
if (idx < 162834):
|
| 13 |
image_enc_padded = image1[idx].astype(np.uint8)
|
| 14 |
elif(idx >= 162834):
|
| 15 |
image_enc_padded = image2[idx-162834].astype(np.uint8)
|
|
|
|
| 16 |
enc_length = dataset_image_mask[idx]
|
| 17 |
image_enc = image_enc_padded[:enc_length]
|
| 18 |
image = Image.open(io.BytesIO(image_enc))
|
|
|
|
| 43 |
for indx in I[0]:
|
| 44 |
id = indx_to_id_dict[indx]
|
| 45 |
id_list.append(id)
|
| 46 |
+
|
| 47 |
# get images
|
| 48 |
image0 = get_image(dataset_image1, dataset_image2, dataset_image_mask, processid_to_index, original_indx)
|
| 49 |
image1 = get_image(dataset_image1, dataset_image2, dataset_image_mask, processid_to_index, I[0][0])
|
|
|
|
| 56 |
image8 = get_image(dataset_image1, dataset_image2, dataset_image_mask, processid_to_index, I[0][7])
|
| 57 |
image9 = get_image(dataset_image1, dataset_image2, dataset_image_mask, processid_to_index, I[0][8])
|
| 58 |
image10 = get_image(dataset_image1, dataset_image2, dataset_image_mask, processid_to_index, I[0][9])
|
| 59 |
+
|
| 60 |
# get taxonomic information
|
| 61 |
s0 = getTax(original_indx)
|
| 62 |
s1 = getTax(I[0][0])
|
|
|
|
| 72 |
|
| 73 |
return id_list, image0, image1, image2, image3, image4, image5, image6, image7, image8, image9, image10, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10
|
| 74 |
|
| 75 |
+
def getRandID():
|
| 76 |
+
indx = random.randrange(0, 325667)
|
| 77 |
+
return indx_to_id_dict[indx], indx
|
| 78 |
+
|
| 79 |
+
def getTax(indx):
|
| 80 |
+
s = species[indx]
|
| 81 |
+
g = genus[indx]
|
| 82 |
+
f = family[indx]
|
| 83 |
+
str = "Species: " + s + "\nGenus: " + g + "\nFamily: " + f
|
| 84 |
+
return str
|
| 85 |
+
|
| 86 |
+
with gr.Blocks(title="Bioscan-Clip") as demo:
|
| 87 |
# open general files
|
| 88 |
with open("dataset_image1.pickle", "rb") as f:
|
| 89 |
dataset_image1 = pickle.load(f)
|
|
|
|
| 107 |
id_to_dna_emb_dict = pickle.load(f)
|
| 108 |
|
| 109 |
# open taxonomy files
|
| 110 |
+
family = [item.decode("utf-8") for item in dataset_hdf5_all_key["family"][:]]
|
| 111 |
+
genus = [item.decode("utf-8") for item in dataset_hdf5_all_key["genus"][:]]
|
| 112 |
+
species = [item.decode("utf-8") for item in dataset_hdf5_all_key["species"][:]]
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
with gr.Column():
|
| 115 |
process_id = gr.Textbox(label="ID:", info="Enter a sample ID to search for")
|
|
|
|
| 163 |
image10 = gr.Image(label=10)
|
| 164 |
tax10 = gr.Textbox(label="Taxonomy")
|
| 165 |
|
| 166 |
+
id_btn.click(fn=getRandID, inputs=[], outputs=[rand_id, rand_id_indx])
|
| 167 |
search_btn.click(fn=searchEmbeddings, inputs=[process_id, mod1, mod2],
|
| 168 |
outputs=[process_id_list, image0, image1, image2, image3, image4, image5, image6, image7, image8, image9, image10,
|
| 169 |
tax0, tax1, tax2, tax3, tax4, tax5, tax6, tax7, tax8, tax9, tax10])
|