Spaces:
Runtime error
Runtime error
Samuel Schmidt
commited on
Commit
·
26c2408
1
Parent(s):
0ab69ad
Add: Saving index to faiss file
Browse files- src/app.py +17 -22
src/app.py
CHANGED
@@ -23,13 +23,27 @@ def index_dataset(dataset):
|
|
23 |
|
24 |
# Add index
|
25 |
dataset_with_embeddings.add_faiss_index(column='color_embeddings')
|
|
|
|
|
26 |
dataset_with_embeddings.add_faiss_index(column='clip_embeddings')
|
|
|
27 |
|
28 |
|
29 |
print(dataset_with_embeddings)
|
30 |
return dataset_with_embeddings
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# Main function, to find similar images
|
35 |
# TODO: implement different distance measures
|
@@ -66,35 +80,16 @@ def get_neighbors(query_image, selected_descriptor, top_k=5):
|
|
66 |
|
67 |
# Define the Gradio Interface
|
68 |
|
69 |
-
|
70 |
-
iface = gr.Interface(
|
71 |
-
fn=get_neighbors,
|
72 |
-
inputs=[
|
73 |
-
gr.Image(type="pil", label="Your Image"),
|
74 |
-
gr.CheckboxGroup(["Color Descriptor", "LBP", "CLIP"], label="Descriptor method?"),
|
75 |
-
],
|
76 |
-
outputs=gr.Gallery(),
|
77 |
-
title="Image Similarity Gallery",
|
78 |
-
description="Upload an image and get similar images",
|
79 |
-
allow_flagging="never"
|
80 |
-
)
|
81 |
-
|
82 |
-
# Launch the Gradio interface
|
83 |
-
iface.launch()
|
84 |
-
|
85 |
with gr.Blocks() as demo:
|
86 |
image_input = gr.Image(type="pil", label="Please upload an image")
|
87 |
checkboxes_descr = gr.CheckboxGroup(["Color Descriptor", "LBP", "CLIP"], label="Please choose an descriptor"))
|
88 |
|
89 |
btn = gr.Button(value="Submit")
|
90 |
gallery_output = gr.Gallery()
|
|
|
91 |
|
92 |
-
btn.click(get_neighbors,
|
93 |
-
inputs=[image_input, checkboxes_descr],
|
94 |
-
outputs=[gallery_output]
|
95 |
-
)
|
96 |
btn_index = gr.Button(value="Re-index Dataset")
|
97 |
-
|
98 |
|
99 |
if __name__ == "__main__":
|
100 |
demo.launch()
|
|
|
23 |
|
24 |
# Add index
|
25 |
dataset_with_embeddings.add_faiss_index(column='color_embeddings')
|
26 |
+
dataset_with_embeddings.save_faiss_index('color_embeddings', 'color_index.faiss')
|
27 |
+
|
28 |
dataset_with_embeddings.add_faiss_index(column='clip_embeddings')
|
29 |
+
ds_with_embeddings.save_faiss_index('clip_embeddings', 'clip_index.faiss')
|
30 |
|
31 |
|
32 |
print(dataset_with_embeddings)
|
33 |
return dataset_with_embeddings
|
34 |
|
35 |
+
|
36 |
+
def check_index(ds):
|
37 |
+
index_path = "my_index.faiss"
|
38 |
+
if os.path.isfile('color_index.faiss') and os.path.isfile('clip_index.faiss'):
|
39 |
+
ds.load_faiss_index('color_embeddings', 'color_index.faiss')
|
40 |
+
return ds.load_faiss_index('clip_embeddings', 'clip_index.faiss')
|
41 |
+
|
42 |
+
else:
|
43 |
+
return index_dataset(ds)
|
44 |
+
|
45 |
+
|
46 |
+
dataset_with_embeddings = check_index(candidate_subset)
|
47 |
|
48 |
# Main function, to find similar images
|
49 |
# TODO: implement different distance measures
|
|
|
80 |
|
81 |
# Define the Gradio Interface
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
with gr.Blocks() as demo:
|
84 |
image_input = gr.Image(type="pil", label="Please upload an image")
|
85 |
checkboxes_descr = gr.CheckboxGroup(["Color Descriptor", "LBP", "CLIP"], label="Please choose an descriptor"))
|
86 |
|
87 |
btn = gr.Button(value="Submit")
|
88 |
gallery_output = gr.Gallery()
|
89 |
+
btn.click(get_neighbors, inputs=[image_input, checkboxes_descr], outputs=[gallery_output])
|
90 |
|
|
|
|
|
|
|
|
|
91 |
btn_index = gr.Button(value="Re-index Dataset")
|
92 |
+
btn_index.click(index_dataset)
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
demo.launch()
|