ysharma HF staff commited on
Commit
a6aa0d1
·
1 Parent(s): 81827f6

update gallery code

Browse files
Files changed (1) hide show
  1. app.py +32 -3
app.py CHANGED
@@ -20,6 +20,12 @@ hf = HuggingFaceEmbeddings(model_name=model_name)
20
  #PlaygrondAI open-sourced dataset is a collection of around 1.3 mil generated images and caption pairs
21
  with open("search_index0.pickle", "rb") as f:
22
  search_index = pickle.load(f)
 
 
 
 
 
 
23
 
24
  #Defining methods for inference
25
  def encode(img):
@@ -45,14 +51,36 @@ def Image_similarity_search(image_in, search_query):
45
  else:
46
  img_caption = search_query
47
  print(f"Image caption from Blip2 Gradio Space or the search_query is - {img_caption}")
 
48
  #Searching the vector space
49
  search_result = search_index.similarity_search(img_caption)[0]
 
 
 
 
50
  #Formatting the search results
51
  pai_prompt = list(search_result)[0][1]
 
 
 
52
  pai_img_link = list(search_result)[-2][-1]['source']
 
 
 
 
53
  #formatting html output for displaying image
54
- html_tag = "<img src='"+pai_img_link+"' alt='"+img_caption+"' height='512' style='display: block; margin: auto;'>"
55
- return pai_prompt, html_tag
 
 
 
 
 
 
 
 
 
 
56
 
57
 
58
  #Defining Gradio Blocks
@@ -60,6 +88,8 @@ with gr.Blocks(css = """#label_mid {padding-top: 2px; padding-bottom: 2px;}
60
  #label_results {padding-top: 5px; padding-bottom: 1px;}
61
  #col-container {max-width: 580px; margin-left: auto; margin-right: auto;}
62
  #accordion {max-width: 580px; margin-left: auto; margin-right: auto;}
 
 
63
  """) as demo:
64
  gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
65
  <div
@@ -95,7 +125,6 @@ with gr.Blocks(css = """#label_mid {padding-top: 2px; padding-bottom: 2px;}
95
  search_query = gr.Textbox(placeholder="Example: A small cat sitting", label="", elem_id="search_query", value='')
96
  label_results = gr.HTML(value= "<p style='text-align: center; color: blue; font-weight: bold;'>👇These Search results are from PlaygroundAI 'Liked_Images' dataset available on <a href='https://github.com/playgroundai/liked_images' _target='blank'>github</a></center></p>", elem_id="label_results")
97
  img_search = gr.HTML(label = 'Image search results from PlaygroundAI dataset', elem_id="img_search")
98
- #with gr.Row():
99
  pai_prompt = gr.Textbox(label="Image prompt from PlaygroundAI dataset", elem_id="pai_prompt")
100
  #b1 = gr.Button("Retry").style(full_width=False)
101
 
 
20
  #PlaygrondAI open-sourced dataset is a collection of around 1.3 mil generated images and caption pairs
21
  with open("search_index0.pickle", "rb") as f:
22
  search_index = pickle.load(f)
23
+ with open("search_index1.pickle", "rb") as f:
24
+ search_index1 = pickle.load(f)
25
+ with open("search_index2.pickle", "rb") as f:
26
+ search_index2 = pickle.load(f)
27
+ with open("search_index3.pickle", "rb") as f:
28
+ search_index3 = pickle.load(f)
29
 
30
  #Defining methods for inference
31
  def encode(img):
 
51
  else:
52
  img_caption = search_query
53
  print(f"Image caption from Blip2 Gradio Space or the search_query is - {img_caption}")
54
+
55
  #Searching the vector space
56
  search_result = search_index.similarity_search(img_caption)[0]
57
+ search_result1 = search_index1.similarity_search(img_caption)[0]
58
+ search_result2 = search_index2.similarity_search(img_caption)[0]
59
+ search_result3 = search_index3.similarity_search(img_caption)[0]
60
+
61
  #Formatting the search results
62
  pai_prompt = list(search_result)[0][1]
63
+ pai_prompt1 = list(search_result1)[0][1]
64
+ pai_prompt2 = list(search_result2)[0][1]
65
+ pai_prompt3 = list(search_result3)[0][1]
66
  pai_img_link = list(search_result)[-2][-1]['source']
67
+ pai_img_link1 = list(search_result1)[-2][-1]['source']
68
+ pai_img_link2 = list(search_result2)[-2][-1]['source']
69
+ pai_img_link3 = list(search_result3)[-2][-1]['source']
70
+
71
  #formatting html output for displaying image
72
+ #html_tag = "<img src='"+pai_img_link+"' alt='"+img_caption+"' height='512' style='display: block; margin: auto;'>"
73
+ #html_tag1 = "<img src='"+pai_img_link1+"' alt='"+img_caption+"' height='512' style='display: block; margin: auto;'>"
74
+ #html_tag2 = "<img src='"+pai_img_link2+"' alt='"+img_caption+"' height='512' style='display: block; margin: auto;'>"
75
+ #html_tag3 = "<img src='"+pai_img_link3+"' alt='"+img_caption+"' height='512' style='display: block; margin: auto;'>"
76
+ html_tag = """<div class='gallery'>
77
+ <img src='"+pai_img_link+"' alt='"+img_caption+"' height='512' style='display: block; margin: auto;'>
78
+ <img src='"+pai_img_link1+"' alt='"+img_caption+"' height='512' style='display: block; margin: auto;'>
79
+ <img src='"+pai_img_link2+"' alt='"+img_caption+"' height='512' style='display: block; margin: auto;'>
80
+ <img src='"+pai_img_link3+"' alt='"+img_caption+"' height='512' style='display: block; margin: auto;'>
81
+ </div>"""
82
+
83
+ return pai_prompt, html_tag #[pai_prompt, pai_prompt1, pai_prompt2, pai_prompt3], [html_tag, html_tag1, html_tag2, html_tag3]
84
 
85
 
86
  #Defining Gradio Blocks
 
88
  #label_results {padding-top: 5px; padding-bottom: 1px;}
89
  #col-container {max-width: 580px; margin-left: auto; margin-right: auto;}
90
  #accordion {max-width: 580px; margin-left: auto; margin-right: auto;}
91
+ #img_search.gallery {display: flex; flex-wrap: wrap; justify-content: center;}
92
+ #img_search.gallery img {margin: 10px; max-width: 300px; max-height: 300px;}
93
  """) as demo:
94
  gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
95
  <div
 
125
  search_query = gr.Textbox(placeholder="Example: A small cat sitting", label="", elem_id="search_query", value='')
126
  label_results = gr.HTML(value= "<p style='text-align: center; color: blue; font-weight: bold;'>👇These Search results are from PlaygroundAI 'Liked_Images' dataset available on <a href='https://github.com/playgroundai/liked_images' _target='blank'>github</a></center></p>", elem_id="label_results")
127
  img_search = gr.HTML(label = 'Image search results from PlaygroundAI dataset', elem_id="img_search")
 
128
  pai_prompt = gr.Textbox(label="Image prompt from PlaygroundAI dataset", elem_id="pai_prompt")
129
  #b1 = gr.Button("Retry").style(full_width=False)
130