Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,16 @@ from PIL import Image
|
|
| 7 |
import imagehash
|
| 8 |
import requests
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def image_click(images, evt: gr.SelectData):
|
| 11 |
img_selected = images[evt.index]
|
| 12 |
return images[evt.index]['name']
|
|
@@ -14,30 +24,31 @@ def image_click(images, evt: gr.SelectData):
|
|
| 14 |
def swap_gallery(im, images):
|
| 15 |
#### name data is_file
|
| 16 |
#print(images[0].keys())
|
|
|
|
|
|
|
| 17 |
|
| 18 |
im_hash = imagehash.average_hash(Image.fromarray(im))
|
| 19 |
t2_list = sorted(images, key = lambda imm:
|
| 20 |
imagehash.average_hash(Image.open(imm["name"])) - im_hash, reverse = False)
|
| 21 |
return list(map(lambda x: x["name"], t2_list))
|
| 22 |
|
| 23 |
-
|
| 24 |
-
def lexica(prompt, limit_size = 128, resize = (512, 512)):
|
| 25 |
lex = Lexica(query=prompt).images()
|
| 26 |
lex = lex[:limit_size]
|
| 27 |
lex = list(map(lambda ele: Image.open(
|
| 28 |
requests.get(ele, stream = True).raw
|
| 29 |
), lex))
|
| 30 |
-
lex = list(map(lambda x: x
|
| 31 |
return lex
|
| 32 |
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
with gr.Row():
|
| 35 |
with gr.Tabs():
|
| 36 |
inputs = gr.Textbox(label = 'Enter prompt to search Lexica.art')
|
| 37 |
-
text_button = gr.Button("Retrieve Images")
|
| 38 |
#gr.Slider(label='Number of images ', minimum = 4, maximum = 20, step = 1, value = 4)]
|
| 39 |
-
with gr.Tabs():
|
| 40 |
i = gr.Image()
|
|
|
|
|
|
|
| 41 |
outputs = gr.Gallery(lable='Output gallery').style(grid=3,height=768,
|
| 42 |
allow_preview=False)
|
| 43 |
#gr.Dataframe(label='prompts for corresponding images')]
|
|
|
|
| 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)
|
| 13 |
+
h, w = map(lambda x: int(x * ratio), [h, w])
|
| 14 |
+
return ( ratio ,img.resize((w, h)) )
|
| 15 |
+
#return ( ratio ,img.resize((h, w)) )
|
| 16 |
+
|
| 17 |
+
#ratio_size = 512
|
| 18 |
+
#ratio, img_rs = min_dim_to_size(img, ratio_size)
|
| 19 |
+
|
| 20 |
def image_click(images, evt: gr.SelectData):
|
| 21 |
img_selected = images[evt.index]
|
| 22 |
return images[evt.index]['name']
|
|
|
|
| 24 |
def swap_gallery(im, images):
|
| 25 |
#### name data is_file
|
| 26 |
#print(images[0].keys())
|
| 27 |
+
if im is None:
|
| 28 |
+
return images
|
| 29 |
|
| 30 |
im_hash = imagehash.average_hash(Image.fromarray(im))
|
| 31 |
t2_list = sorted(images, key = lambda imm:
|
| 32 |
imagehash.average_hash(Image.open(imm["name"])) - im_hash, reverse = False)
|
| 33 |
return list(map(lambda x: x["name"], t2_list))
|
| 34 |
|
| 35 |
+
def lexica(prompt, limit_size = 128, ratio_size = 256):
|
|
|
|
| 36 |
lex = Lexica(query=prompt).images()
|
| 37 |
lex = lex[:limit_size]
|
| 38 |
lex = list(map(lambda ele: Image.open(
|
| 39 |
requests.get(ele, stream = True).raw
|
| 40 |
), lex))
|
| 41 |
+
lex = list(map(lambda x: min_dim_to_size(x, ratio_size)[1], lex))
|
| 42 |
return lex
|
| 43 |
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
with gr.Row():
|
| 46 |
with gr.Tabs():
|
| 47 |
inputs = gr.Textbox(label = 'Enter prompt to search Lexica.art')
|
|
|
|
| 48 |
#gr.Slider(label='Number of images ', minimum = 4, maximum = 20, step = 1, value = 4)]
|
|
|
|
| 49 |
i = gr.Image()
|
| 50 |
+
text_button = gr.Button("Retrieve Images")
|
| 51 |
+
with gr.Tabs():
|
| 52 |
outputs = gr.Gallery(lable='Output gallery').style(grid=3,height=768,
|
| 53 |
allow_preview=False)
|
| 54 |
#gr.Dataframe(label='prompts for corresponding images')]
|