Spaces:
Sleeping
Sleeping
Update simple_app.py
Browse files- simple_app.py +149 -22
simple_app.py
CHANGED
@@ -1,33 +1,160 @@
|
|
1 |
import gradio as gr
|
2 |
from Lex import *
|
3 |
-
|
4 |
'''
|
5 |
lex = Lexica(query="man woman fire snow").images()
|
6 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
15 |
lex = Lexica(query=prompt).images()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
return lex
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from Lex import *
|
|
|
3 |
'''
|
4 |
lex = Lexica(query="man woman fire snow").images()
|
5 |
'''
|
6 |
+
from PIL import Image
|
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)
|
20 |
+
h, w = map(lambda x: int(x * ratio), [h, w])
|
21 |
+
return ( ratio ,img.resize((h, w)) )
|
22 |
+
|
23 |
+
#ratio_size = 512
|
24 |
+
#ratio, img_rs = min_dim_to_size(img, ratio_size)
|
25 |
|
26 |
+
def image_click(images, evt: gr.SelectData):
|
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"):
|
61 |
+
resp = requests.post(
|
62 |
+
url = "https://www.enterpix.app/enterpix/v1/image/prompt-search",
|
63 |
+
data= {
|
64 |
+
"length": limit_size,
|
65 |
+
"platform": "stable-diffusion,midjourney",
|
66 |
+
"prompt": prompt,
|
67 |
+
"start": 0
|
68 |
+
}
|
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,
|
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(
|
124 |
+
value="### Click on a Image in the gallery to select it, and the grid order will change",
|
125 |
+
visible=True,
|
126 |
+
elem_id="selected_model",
|
127 |
+
)
|
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 |
+
|
160 |
+
demo.launch("0.0.0.0")
|