svjack commited on
Commit
d823d92
·
1 Parent(s): 1096f1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -27
app.py CHANGED
@@ -83,15 +83,26 @@ def enterpix(prompt, limit_size = 100, ratio_size = 256 + 128, use_key = "bigThu
83
  resp = list(map(lambda x: min_dim_to_size(x, ratio_size)[1], lex_))
84
  return resp
85
 
86
- def search(prompt, search_name):
87
  if search_name == "lexica":
88
- return lexica(prompt)
89
  else:
90
- return enterpix(prompt)
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  with gr.Blocks(css="custom.css") as demo:
93
  title = gr.HTML(
94
- """<h1><img src="https://i.imgur.com/52VJ8vS.png" alt="SD"> StableDiffusion Image Search </h1>""",
95
  elem_id="title",
96
  )
97
 
@@ -106,18 +117,7 @@ with gr.Blocks(css="custom.css") as demo:
106
  placeholder="Enter prompt to search", elem_id="prompt")
107
  #gr.Slider(label='Number of images ', minimum = 4, maximum = 20, step = 1, value = 4)]
108
  text_button = gr.Button("Retrieve Images", elem_id="run_button")
109
- i = gr.Image(elem_id="result-image")
110
-
111
- gr.Examples(
112
- [
113
- ["Chinese Traditional Culture", "lexica", "images/AbsoluteReality.png"],
114
- ["trending digital art", "lexica", "images/waifu.png"],
115
- ["beautiful home", "enterpix", "images/Cyberpunk_Anime.png"],
116
- ["interior design of living room", "enterpix", "images/DreamShaper.png"],
117
- ],
118
- inputs = [inputs, search_func_name, i],
119
- label = "Examples"
120
- )
121
 
122
  with gr.Column():
123
  title = gr.Markdown(
@@ -128,32 +128,44 @@ with gr.Blocks(css="custom.css") as demo:
128
  order_func_name = gr.Radio(choices=hash_func_name,
129
  value=hash_func_name[0], label="Order by", elem_id="order_radio")
130
 
131
- outputs = gr.Gallery(lable='Output gallery', elem_id="gallery",).style(grid=5,height=768 - 128,
132
  allow_preview=False, label = "retrieve Images")
133
  #gr.Dataframe(label='prompts for corresponding images')]
134
 
135
- #outputs.select(image_click, outputs, i, _js="(x) => x.splice(0,x.length)")
136
- outputs.select(image_click, outputs, i,)
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
- i.change(
139
  fn=swap_gallery,
140
  inputs=[i, outputs, order_func_name],
141
  outputs=outputs,
142
  queue=False
143
  )
144
- order_func_name.change(
145
  fn=swap_gallery,
146
  inputs=[i, outputs, order_func_name],
147
  outputs=outputs,
148
  queue=False
149
  )
150
- #### gr.Textbox().submit().success()
151
 
152
- ### lexica
153
- #text_button.click(lexica, inputs=inputs, outputs=outputs)
154
- ### enterpix
155
- #text_button.click(enterpix, inputs=inputs, outputs=outputs)
156
- text_button.click(search, inputs=[inputs, search_func_name], outputs=outputs)
157
 
158
 
159
 
 
83
  resp = list(map(lambda x: min_dim_to_size(x, ratio_size)[1], lex_))
84
  return resp
85
 
86
+ def search(prompt, search_name, im, func_name):
87
  if search_name == "lexica":
88
+ im_l = lexica(prompt)
89
  else:
90
+ im_l = enterpix(prompt)
91
+ if im is None:
92
+ return im_l
93
+
94
+ hash_func = getattr(imagehash, func_name)
95
+
96
+ im_hash = hash_func(Image.fromarray(im))
97
+ t2_list = sorted(im_l, key = lambda imm:
98
+ hash_func(imm) - im_hash, reverse = False)
99
+ return t2_list
100
+ #return list(map(lambda x: x["name"], t2_list))
101
+
102
 
103
  with gr.Blocks(css="custom.css") as demo:
104
  title = gr.HTML(
105
+ """<h1><img src="https://i.imgur.com/52VJ8vS.png" alt="SD"> StableDiffusion Search by Prompt order by Image</h1>""",
106
  elem_id="title",
107
  )
108
 
 
117
  placeholder="Enter prompt to search", elem_id="prompt")
118
  #gr.Slider(label='Number of images ', minimum = 4, maximum = 20, step = 1, value = 4)]
119
  text_button = gr.Button("Retrieve Images", elem_id="run_button")
120
+ i = gr.Image(elem_id="result-image", label = "Image upload or selected", height = 768 - 256 - 32)
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  with gr.Column():
123
  title = gr.Markdown(
 
128
  order_func_name = gr.Radio(choices=hash_func_name,
129
  value=hash_func_name[0], label="Order by", elem_id="order_radio")
130
 
131
+ outputs = gr.Gallery(lable='Output gallery', elem_id="gallery",).style(grid=5,height=768 - 256,
132
  allow_preview=False, label = "retrieve Images")
133
  #gr.Dataframe(label='prompts for corresponding images')]
134
 
135
+ with gr.Row():
136
+ gr.Examples(
137
+ [
138
+ ["chinese zodiac signs", "lexica", "images/chinese_zodiac_signs.png", "average_hash"],
139
+ ["trending digital art", "lexica", "images/trending_digital_art.png", "colorhash"],
140
+ ["beautiful home", "enterpix", "images/beautiful_home.png", "dhash"],
141
+ ["interior design of living room", "enterpix", "images/interior_design_of_living_room.png", "whash"],
142
+ ],
143
+ inputs = [inputs, search_func_name, i, order_func_name],
144
+ label = "Examples"
145
+ )
146
+
147
+ #outputs.select(image_click, outputs, i, _js="(x) => x.splice(0,x.length)")
148
+ outputs.select(image_click, outputs, i,)
149
 
150
+ i.change(
151
  fn=swap_gallery,
152
  inputs=[i, outputs, order_func_name],
153
  outputs=outputs,
154
  queue=False
155
  )
156
+ order_func_name.change(
157
  fn=swap_gallery,
158
  inputs=[i, outputs, order_func_name],
159
  outputs=outputs,
160
  queue=False
161
  )
162
+ #### gr.Textbox().submit().success()
163
 
164
+ ### lexica
165
+ #text_button.click(lexica, inputs=inputs, outputs=outputs)
166
+ ### enterpix
167
+ #text_button.click(enterpix, inputs=inputs, outputs=outputs)
168
+ text_button.click(search, inputs=[inputs, search_func_name, i, order_func_name], outputs=outputs)
169
 
170
 
171