Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,13 @@ from PIL import Image
|
|
7 |
import imagehash
|
8 |
import requests
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def min_dim_to_size(img, size = 512):
|
11 |
h, w = img.size
|
12 |
ratio = size / max(h, w)
|
@@ -20,24 +27,34 @@ def image_click(images, evt: gr.SelectData):
|
|
20 |
img_selected = images[evt.index]
|
21 |
return images[evt.index]['name']
|
22 |
|
23 |
-
def swap_gallery(im, images):
|
24 |
#### name data is_file
|
25 |
#print(images[0].keys())
|
26 |
if im is None:
|
27 |
return list(map(lambda x: x["name"], images))
|
|
|
28 |
|
29 |
-
im_hash =
|
30 |
t2_list = sorted(images, key = lambda imm:
|
31 |
-
|
32 |
return list(map(lambda x: x["name"], t2_list))
|
33 |
|
34 |
def lexica(prompt, limit_size = 128, ratio_size = 256 + 128):
|
35 |
lex = Lexica(query=prompt).images()
|
36 |
lex = lex[:limit_size]
|
37 |
-
lex = list(map(lambda
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
return lex
|
42 |
|
43 |
def enterpix(prompt, limit_size = 100, ratio_size = 256 + 128, use_key = "bigThumbnailUrl"):
|
@@ -52,15 +69,37 @@ def enterpix(prompt, limit_size = 100, ratio_size = 256 + 128, use_key = "bigThu
|
|
52 |
)
|
53 |
resp = resp.json()
|
54 |
resp = list(map(lambda x: x[use_key], resp["images"]))
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
return resp
|
60 |
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
with gr.Row():
|
63 |
with gr.Column():
|
|
|
|
|
|
|
64 |
with gr.Row():
|
65 |
#inputs = gr.Textbox(label = 'Enter prompt to search Lexica.art')
|
66 |
inputs = gr.Textbox(label="Prompt", show_label=False, lines=1, max_lines=20, min_width = 256,
|
@@ -75,7 +114,10 @@ with gr.Blocks() as demo:
|
|
75 |
visible=True,
|
76 |
elem_id="selected_model",
|
77 |
)
|
78 |
-
|
|
|
|
|
|
|
79 |
allow_preview=False, label = "retrieve Images")
|
80 |
#gr.Dataframe(label='prompts for corresponding images')]
|
81 |
|
@@ -84,7 +126,13 @@ with gr.Blocks() as demo:
|
|
84 |
|
85 |
i.change(
|
86 |
fn=swap_gallery,
|
87 |
-
inputs=[i, outputs],
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
outputs=outputs,
|
89 |
queue=False
|
90 |
)
|
@@ -93,6 +141,8 @@ with gr.Blocks() as demo:
|
|
93 |
### lexica
|
94 |
#text_button.click(lexica, inputs=inputs, outputs=outputs)
|
95 |
### enterpix
|
96 |
-
text_button.click(enterpix, inputs=inputs, outputs=outputs)
|
|
|
|
|
97 |
|
98 |
demo.launch("0.0.0.0")
|
|
|
7 |
import imagehash
|
8 |
import requests
|
9 |
|
10 |
+
from time import sleep
|
11 |
+
sleep_time = 0.5
|
12 |
+
|
13 |
+
hash_func_name = list(filter(lambda x: x.endswith("hash") and
|
14 |
+
"hex" not in x ,dir(imagehash)))
|
15 |
+
hash_func_name = ['average_hash', 'colorhash', 'dhash', 'phash', 'whash', 'crop_resistant_hash',]
|
16 |
+
|
17 |
def min_dim_to_size(img, size = 512):
|
18 |
h, w = img.size
|
19 |
ratio = size / max(h, w)
|
|
|
27 |
img_selected = images[evt.index]
|
28 |
return images[evt.index]['name']
|
29 |
|
30 |
+
def swap_gallery(im, images, func_name):
|
31 |
#### name data is_file
|
32 |
#print(images[0].keys())
|
33 |
if im is None:
|
34 |
return list(map(lambda x: x["name"], images))
|
35 |
+
hash_func = getattr(imagehash, func_name)
|
36 |
|
37 |
+
im_hash = hash_func(Image.fromarray(im))
|
38 |
t2_list = sorted(images, key = lambda imm:
|
39 |
+
hash_func(Image.open(imm["name"])) - im_hash, reverse = False)
|
40 |
return list(map(lambda x: x["name"], t2_list))
|
41 |
|
42 |
def lexica(prompt, limit_size = 128, ratio_size = 256 + 128):
|
43 |
lex = Lexica(query=prompt).images()
|
44 |
lex = lex[:limit_size]
|
45 |
+
lex = list(map(lambda x: x.replace("full_jpg", "sm2"), lex))
|
46 |
+
lex_ = []
|
47 |
+
for ele in lex:
|
48 |
+
try:
|
49 |
+
im = Image.open(
|
50 |
+
requests.get(ele, stream = True).raw
|
51 |
+
)
|
52 |
+
lex_.append(im)
|
53 |
+
except:
|
54 |
+
print("err")
|
55 |
+
sleep(sleep_time)
|
56 |
+
assert lex_
|
57 |
+
lex = list(map(lambda x: min_dim_to_size(x, ratio_size)[1], lex_))
|
58 |
return lex
|
59 |
|
60 |
def enterpix(prompt, limit_size = 100, ratio_size = 256 + 128, use_key = "bigThumbnailUrl"):
|
|
|
69 |
)
|
70 |
resp = resp.json()
|
71 |
resp = list(map(lambda x: x[use_key], resp["images"]))
|
72 |
+
lex_ = []
|
73 |
+
for ele in resp:
|
74 |
+
try:
|
75 |
+
im = Image.open(
|
76 |
+
requests.get(ele, stream = True).raw
|
77 |
+
)
|
78 |
+
lex_.append(im)
|
79 |
+
except:
|
80 |
+
print("err")
|
81 |
+
sleep(sleep_time)
|
82 |
+
assert lex_
|
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 |
+
|
98 |
with gr.Row():
|
99 |
with gr.Column():
|
100 |
+
with gr.Row():
|
101 |
+
search_func_name = gr.Radio(choices=["lexica", "enterpix"],
|
102 |
+
value="lexica", label="Search by", elem_id="search_radio")
|
103 |
with gr.Row():
|
104 |
#inputs = gr.Textbox(label = 'Enter prompt to search Lexica.art')
|
105 |
inputs = gr.Textbox(label="Prompt", show_label=False, lines=1, max_lines=20, min_width = 256,
|
|
|
114 |
visible=True,
|
115 |
elem_id="selected_model",
|
116 |
)
|
117 |
+
order_func_name = gr.Radio(choices=hash_func_name,
|
118 |
+
value=hash_func_name[0], label="Order by", elem_id="order_radio")
|
119 |
+
|
120 |
+
outputs = gr.Gallery(lable='Output gallery', elem_id="gallery",).style(grid=5,height=768,
|
121 |
allow_preview=False, label = "retrieve Images")
|
122 |
#gr.Dataframe(label='prompts for corresponding images')]
|
123 |
|
|
|
126 |
|
127 |
i.change(
|
128 |
fn=swap_gallery,
|
129 |
+
inputs=[i, outputs, order_func_name],
|
130 |
+
outputs=outputs,
|
131 |
+
queue=False
|
132 |
+
)
|
133 |
+
order_func_name.change(
|
134 |
+
fn=swap_gallery,
|
135 |
+
inputs=[i, outputs, order_func_name],
|
136 |
outputs=outputs,
|
137 |
queue=False
|
138 |
)
|
|
|
141 |
### lexica
|
142 |
#text_button.click(lexica, inputs=inputs, outputs=outputs)
|
143 |
### enterpix
|
144 |
+
#text_button.click(enterpix, inputs=inputs, outputs=outputs)
|
145 |
+
text_button.click(search, inputs=[inputs, search_func_name], outputs=outputs)
|
146 |
+
|
147 |
|
148 |
demo.launch("0.0.0.0")
|