konerusudhir commited on
Commit
2626cde
·
1 Parent(s): 54ae4c0

Added examples for text and reduced image results from 18 to 8

Browse files
Files changed (1) hide show
  1. app.py +73 -130
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 = 9
94
  TEXT_MODEL_INPUT_BATCH_SIZE = 10
95
- SEARCH_RESULTS_DISPLAY_COUNT = 9
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
- examples_paths = [f"{EXAMPLES_FOLDER}/e{n}.jpeg" for n in range(4)]
640
- # examples=gr.Examples()
 
 
 
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="query_text", value="")
646
- query_image = gr.Image(label="query_image")
647
  search_btn = gr.Button("Search")
648
- examples=gr.Examples(
649
- examples=examples_paths,
650
- inputs=query_image)
 
 
 
 
 
651
  with gr.Column(scale=10):
652
- with gr.Tab("Image Index Results"):
653
- with gr.Row():
654
- with gr.Column():
655
- img_result_0_image = gr.Image(label="img_result_0_image") # type="pil",
656
- img_result_0_label = gr.HTML(value = '-')
657
- img_result_0_btn = gr.Button("Related Images")
658
- with gr.Column():
659
- img_result_1_image = gr.Image(label="img_result_1_image")
660
- img_result_1_label = gr.HTML(value = '-')
661
- img_result_1_btn = gr.Button("Related Images")
662
- with gr.Column():
663
- img_result_2_image = gr.Image(label="img_result_2_image")
664
- img_result_2_label = gr.HTML(value = '-')
665
- img_result_2_btn = gr.Button("Related Images")
666
- with gr.Column():
667
- img_result_3_image = gr.Image(label="img_result_3_image")
668
- img_result_3_label = gr.HTML(value = '-')
669
- img_result_3_btn = gr.Button("Related Images")
670
- with gr.Column():
671
- img_result_4_image = gr.Image(label="img_result_4_image")
672
- img_result_4_label = gr.HTML(value = '-')
673
- img_result_4_btn = gr.Button("Related Images")
674
- with gr.Column():
675
- img_result_5_image = gr.Image(label="img_result_5_image")
676
- img_result_5_label = gr.HTML(value = '-')
677
- img_result_5_btn = gr.Button("Related Images")
678
- with gr.Column():
679
- img_result_6_image = gr.Image(label="img_result_6_image")
680
- img_result_6_label = gr.HTML(value = '-')
681
- img_result_6_btn = gr.Button("Related Images")
682
- with gr.Column():
683
- img_result_7_image = gr.Image(label="img_result_7_image")
684
- img_result_7_label = gr.HTML(value = '-')
685
- img_result_7_btn = gr.Button("Related Images")
686
- with gr.Column():
687
- img_result_8_image = gr.Image(label="img_result_8_image")
688
- img_result_8_label = gr.HTML(value = '-')
689
- img_result_8_btn = gr.Button("Related Images")
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 = [result_0_btn,result_1_btn,result_2_btn,result_3_btn,
732
- result_4_btn,result_5_btn,result_6_btn,result_7_btn,
733
- result_8_btn,
734
- img_result_0_btn,img_result_1_btn,img_result_2_btn,img_result_3_btn,
735
- img_result_4_btn,img_result_5_btn,img_result_6_btn,img_result_7_btn,
736
- img_result_8_btn
737
- ]
738
- labels = [ result_0_label,result_1_label,result_2_label,result_3_label,
739
- result_4_label,result_5_label,result_6_label,result_7_label,
740
- result_8_label,
741
- img_result_0_label,img_result_1_label,img_result_2_label,img_result_3_label,
742
- img_result_4_label,img_result_5_label,img_result_6_label,img_result_7_label,
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
- # demo.launch(share=True, debug=True,)
762
  demo.launch(debug=True)
763
 
764
- # demo.close()
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)