Spaces:
Runtime error
Runtime error
Commit
·
2626cde
1
Parent(s):
54ae4c0
Added examples for text and reduced image results from 18 to 8
Browse files
app.py
CHANGED
@@ -90,9 +90,9 @@ IPFS_IMAGE_IDS_PATH = os.path.join(INDEX_FOLDER, IPFS_IMAGE_IDS_FILE_NAME)
|
|
90 |
MIN_TOKEN_ID = 1
|
91 |
MAX_TOKEN_ID = 20000
|
92 |
GATEWAY_QUERY_BATCH_SIZE = 100
|
93 |
-
VISION_MODEL_INPUT_BATCH_SIZE =
|
94 |
TEXT_MODEL_INPUT_BATCH_SIZE = 10
|
95 |
-
SEARCH_RESULTS_DISPLAY_COUNT =
|
96 |
LOG_DISPLAY_THRESHOLD = 1000
|
97 |
RANDOM_SEED = 7
|
98 |
np.random.seed(RANDOM_SEED)
|
@@ -557,34 +557,16 @@ def read_ipfs_image_ids(image_ids_path):
|
|
557 |
|
558 |
import gradio as gr
|
559 |
|
560 |
-
# Download some images from ipfs ids to boot strap image search App
|
561 |
-
# loaded_ipfs_image_ids = list(read_ipfs_image_ids(IPFS_IMAGE_IDS_PATH))
|
562 |
-
# download_images(loaded_ipfs_image_ids[:30])
|
563 |
-
|
564 |
-
# # Load Image index
|
565 |
-
# image_index_prefix = index_name.replace(',', '_')
|
566 |
-
# image_index_file_name = f"{image_index_prefix}.index"
|
567 |
-
# index_path = os.path.join(INDEX_FOLDER, image_index_file_name)
|
568 |
-
# loaded_image_index = faiss.read_index(index_path)
|
569 |
-
# # Load Image Id map
|
570 |
-
# id_map_file_name = f"{image_index_prefix}_image_ids.pickle"
|
571 |
-
# id_map_path = os.path.join(INDEX_FOLDER, id_map_file_name)
|
572 |
-
# with open(id_map_path, 'rb') as f:
|
573 |
-
# loaded_image_ids_map = pickle.load(f)
|
574 |
-
|
575 |
print(f"Text Index size: {text_index.ntotal}")
|
576 |
print(f"Image Index size: {image_index.ntotal}")
|
577 |
print(f"Ids size: {len(content_ids_map)}")
|
578 |
|
579 |
-
# def load_resized_image(img_path, max_width = 224, max_height=224):
|
580 |
-
# img = Image.open(img_path).convert('RGB')
|
581 |
-
# # img.thumbnail((max_width,max_height))
|
582 |
-
# img = np.asarray(img)
|
583 |
-
# return img
|
584 |
-
|
585 |
def search_images_by_image(query_image):
|
586 |
return search_images('', query_image)
|
587 |
|
|
|
|
|
|
|
588 |
def search_images(query_text, query_image):
|
589 |
images_to_download = []
|
590 |
# print(f"Received {query_text}:{query_image}")
|
@@ -633,124 +615,85 @@ def search_images(query_text, query_image):
|
|
633 |
diatance_html = f"Distance: {distance_rounded}"
|
634 |
|
635 |
image_objects.append(f"{diatance_html} - {link_html}")
|
|
|
636 |
|
637 |
return image_objects
|
638 |
|
639 |
-
|
640 |
-
|
|
|
|
|
|
|
641 |
with gr.Blocks() as demo:
|
642 |
gr.Markdown(value = '# Makers Place NFTs search')
|
643 |
with gr.Row():
|
644 |
with gr.Column(scale=0.5):
|
645 |
-
query_text = gr.Textbox(label="
|
646 |
-
query_image = gr.Image(label="
|
647 |
search_btn = gr.Button("Search")
|
648 |
-
|
649 |
-
examples=
|
650 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
651 |
with gr.Column(scale=10):
|
652 |
-
with gr.
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
|
678 |
-
|
679 |
-
|
680 |
-
|
681 |
-
|
682 |
-
|
683 |
-
|
684 |
-
|
685 |
-
|
686 |
-
|
687 |
-
|
688 |
-
|
689 |
-
|
690 |
-
with gr.Tab("Text Index Results"):
|
691 |
-
with gr.Row():
|
692 |
-
with gr.Column():
|
693 |
-
result_0_image = gr.Image(label="result_0_image") # type="pil",
|
694 |
-
result_0_label = gr.HTML(value = '-')
|
695 |
-
result_0_btn = gr.Button("Related Images")
|
696 |
-
with gr.Column():
|
697 |
-
result_1_image = gr.Image(label="result_1_image")
|
698 |
-
result_1_label = gr.HTML(value = '-')
|
699 |
-
result_1_btn = gr.Button("Related Images")
|
700 |
-
with gr.Column():
|
701 |
-
result_2_image = gr.Image(label="result_2_image")
|
702 |
-
result_2_label = gr.HTML(value = '-')
|
703 |
-
result_2_btn = gr.Button("Related Images")
|
704 |
-
with gr.Column():
|
705 |
-
result_3_image = gr.Image(label="result_3_image")
|
706 |
-
result_3_label = gr.HTML(value = '-')
|
707 |
-
result_3_btn = gr.Button("Related Images")
|
708 |
-
with gr.Column():
|
709 |
-
result_4_image = gr.Image(label="result_4_image")
|
710 |
-
result_4_label = gr.HTML(value = '', show_progress=False)
|
711 |
-
result_4_btn = gr.Button("Related Images")
|
712 |
-
with gr.Column():
|
713 |
-
result_5_image = gr.Image(label="result_5_image")
|
714 |
-
result_5_label = gr.HTML(value = '-')
|
715 |
-
result_5_btn = gr.Button("Related Images")
|
716 |
-
with gr.Column():
|
717 |
-
result_6_image = gr.Image(label="result_6_image")
|
718 |
-
result_6_label = gr.HTML(value = '-')
|
719 |
-
result_6_btn = gr.Button("Related Images")
|
720 |
-
with gr.Column():
|
721 |
-
result_7_image = gr.Image(label="result_7_image")
|
722 |
-
result_7_label = gr.HTML(value = '-')
|
723 |
-
result_7_btn = gr.Button("Related Images")
|
724 |
-
with gr.Column():
|
725 |
-
result_8_image = gr.Image(label="result_8_image")
|
726 |
-
result_8_label = gr.HTML(value = '-')
|
727 |
-
result_8_btn = gr.Button("Related Images")
|
728 |
-
|
729 |
|
730 |
inputs = [query_text, query_image]
|
731 |
-
buttons = [
|
732 |
-
|
733 |
-
|
734 |
-
|
735 |
-
|
736 |
-
|
737 |
-
|
738 |
-
|
739 |
-
|
740 |
-
|
741 |
-
|
742 |
-
|
743 |
-
img_result_8_label,
|
744 |
-
]
|
745 |
-
images = [result_0_image,result_1_image,result_2_image,result_3_image,
|
746 |
-
result_4_image,result_5_image,result_6_image,result_7_image,result_8_image,
|
747 |
-
img_result_0_image,img_result_1_image,img_result_2_image,img_result_3_image,
|
748 |
-
img_result_4_image,img_result_5_image,img_result_6_image,img_result_7_image,
|
749 |
-
img_result_8_image]
|
750 |
outputs = images + labels
|
751 |
|
752 |
-
# examples.
|
753 |
-
|
754 |
search_btn.click(fn=search_images, inputs=inputs, outputs=outputs)
|
755 |
|
756 |
for i in range(len(buttons)):
|
@@ -758,10 +701,10 @@ with gr.Blocks() as demo:
|
|
758 |
inputs=images[i], outputs=outputs)
|
759 |
|
760 |
|
761 |
-
|
762 |
demo.launch(debug=True)
|
763 |
|
764 |
-
|
765 |
|
766 |
# file_name = "mp_nft_data_np_array_12k.pickle"
|
767 |
# file_path = os.path.join(INDEX_FOLDER, file_name)
|
|
|
90 |
MIN_TOKEN_ID = 1
|
91 |
MAX_TOKEN_ID = 20000
|
92 |
GATEWAY_QUERY_BATCH_SIZE = 100
|
93 |
+
VISION_MODEL_INPUT_BATCH_SIZE = 4
|
94 |
TEXT_MODEL_INPUT_BATCH_SIZE = 10
|
95 |
+
SEARCH_RESULTS_DISPLAY_COUNT = 4
|
96 |
LOG_DISPLAY_THRESHOLD = 1000
|
97 |
RANDOM_SEED = 7
|
98 |
np.random.seed(RANDOM_SEED)
|
|
|
557 |
|
558 |
import gradio as gr
|
559 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
560 |
print(f"Text Index size: {text_index.ntotal}")
|
561 |
print(f"Image Index size: {image_index.ntotal}")
|
562 |
print(f"Ids size: {len(content_ids_map)}")
|
563 |
|
|
|
|
|
|
|
|
|
|
|
|
|
564 |
def search_images_by_image(query_image):
|
565 |
return search_images('', query_image)
|
566 |
|
567 |
+
def search_images_by_text(query_text):
|
568 |
+
return search_images(query_text, None)
|
569 |
+
|
570 |
def search_images(query_text, query_image):
|
571 |
images_to_download = []
|
572 |
# print(f"Received {query_text}:{query_image}")
|
|
|
615 |
diatance_html = f"Distance: {distance_rounded}"
|
616 |
|
617 |
image_objects.append(f"{diatance_html} - {link_html}")
|
618 |
+
# image_objects.append(query_text, query_image)
|
619 |
|
620 |
return image_objects
|
621 |
|
622 |
+
example_images = [f"{EXAMPLES_FOLDER}/e{n}.jpeg" for n in range(4)]
|
623 |
+
example_text = ["surrealism art","3d art", "psychedelic art","Glitch Art"] #,"Photo of a Unicorn"
|
624 |
+
examples_queries = [example_images, example_text]
|
625 |
+
# print(examples_queries)
|
626 |
+
outputs = []
|
627 |
with gr.Blocks() as demo:
|
628 |
gr.Markdown(value = '# Makers Place NFTs search')
|
629 |
with gr.Row():
|
630 |
with gr.Column(scale=0.5):
|
631 |
+
query_text = gr.Textbox(label="Text", value="")
|
632 |
+
query_image = gr.Image(label="Image")
|
633 |
search_btn = gr.Button("Search")
|
634 |
+
examples_text_display=gr.Examples(
|
635 |
+
examples=example_text,
|
636 |
+
inputs=query_text
|
637 |
+
)
|
638 |
+
examples_image_display=gr.Examples(
|
639 |
+
examples=example_images,
|
640 |
+
inputs=query_image
|
641 |
+
)
|
642 |
with gr.Column(scale=10):
|
643 |
+
with gr.Row():
|
644 |
+
gr.HTML(value="<font size='+3'>Image Index Results</font>")
|
645 |
+
with gr.Row():
|
646 |
+
with gr.Column(min_width=224):
|
647 |
+
img_result_0_image = gr.Image(label="img_result_0_image") # type="pil",
|
648 |
+
img_result_0_label = gr.HTML(value = '-')
|
649 |
+
img_result_0_btn = gr.Button("Related Images")
|
650 |
+
with gr.Column(min_width=224):
|
651 |
+
img_result_1_image = gr.Image(label="img_result_1_image")
|
652 |
+
img_result_1_label = gr.HTML(value = '-')
|
653 |
+
img_result_1_btn = gr.Button("Related Images")
|
654 |
+
with gr.Column(min_width=224):
|
655 |
+
img_result_2_image = gr.Image(label="img_result_2_image")
|
656 |
+
img_result_2_label = gr.HTML(value = '-')
|
657 |
+
img_result_2_btn = gr.Button("Related Images")
|
658 |
+
with gr.Column(min_width=224):
|
659 |
+
img_result_3_image = gr.Image(label="img_result_3_image")
|
660 |
+
img_result_3_label = gr.HTML(value = '-')
|
661 |
+
img_result_3_btn = gr.Button("Related Images")
|
662 |
+
with gr.Row():
|
663 |
+
gr.HTML(value="<br><br> <font size='+3'>Text Index Results</font>")
|
664 |
+
with gr.Row():
|
665 |
+
with gr.Column(min_width=224):
|
666 |
+
result_0_image = gr.Image(label="result_0_image") # type="pil",
|
667 |
+
result_0_label = gr.HTML(value = '-')
|
668 |
+
result_0_btn = gr.Button("Related Images")
|
669 |
+
with gr.Column(min_width=224):
|
670 |
+
result_1_image = gr.Image(label="result_1_image")
|
671 |
+
result_1_label = gr.HTML(value = '-')
|
672 |
+
result_1_btn = gr.Button("Related Images")
|
673 |
+
with gr.Column(min_width=224):
|
674 |
+
result_2_image = gr.Image(label="result_2_image")
|
675 |
+
result_2_label = gr.HTML(value = '-')
|
676 |
+
result_2_btn = gr.Button("Related Images")
|
677 |
+
with gr.Column(min_width=224):
|
678 |
+
result_3_image = gr.Image(label="result_3_image")
|
679 |
+
result_3_label = gr.HTML(value = '-')
|
680 |
+
result_3_btn = gr.Button("Related Images")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
681 |
|
682 |
inputs = [query_text, query_image]
|
683 |
+
buttons = [
|
684 |
+
result_0_btn,result_1_btn,result_2_btn,result_3_btn,
|
685 |
+
img_result_0_btn,img_result_1_btn,img_result_2_btn,img_result_3_btn,
|
686 |
+
]
|
687 |
+
labels = [
|
688 |
+
result_0_label,result_1_label,result_2_label,result_3_label,
|
689 |
+
img_result_0_label,img_result_1_label,img_result_2_label,img_result_3_label,
|
690 |
+
]
|
691 |
+
images = [
|
692 |
+
result_0_image,result_1_image,result_2_image,result_3_image,
|
693 |
+
img_result_0_image,img_result_1_image,img_result_2_image,img_result_3_image
|
694 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
695 |
outputs = images + labels
|
696 |
|
|
|
|
|
697 |
search_btn.click(fn=search_images, inputs=inputs, outputs=outputs)
|
698 |
|
699 |
for i in range(len(buttons)):
|
|
|
701 |
inputs=images[i], outputs=outputs)
|
702 |
|
703 |
|
704 |
+
demo.queue(concurrency_count=2, max_size=5)
|
705 |
demo.launch(debug=True)
|
706 |
|
707 |
+
demo.close()
|
708 |
|
709 |
# file_name = "mp_nft_data_np_array_12k.pickle"
|
710 |
# file_path = os.path.join(INDEX_FOLDER, file_name)
|