Spaces:
Sleeping
Sleeping
File size: 2,256 Bytes
1613a0b e12ff12 1613a0b e12ff12 1613a0b e12ff12 1613a0b e12ff12 1613a0b e12ff12 1613a0b bc0be02 1613a0b bc0be02 1613a0b bc0be02 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import gradio as gr
from Lex import *
'''
lex = Lexica(query="man woman fire snow").images()
'''
from PIL import Image
import imagehash
import requests
def min_dim_to_size(img, size = 512):
h, w = img.size
ratio = size / max(h, w)
h, w = map(lambda x: int(x * ratio), [h, w])
return ( ratio ,img.resize((w, h)) )
#return ( ratio ,img.resize((h, w)) )
#ratio_size = 512
#ratio, img_rs = min_dim_to_size(img, ratio_size)
def image_click(images, evt: gr.SelectData):
img_selected = images[evt.index]
return images[evt.index]['name']
def swap_gallery(im, images):
#### name data is_file
#print(images[0].keys())
if im is None:
return images
im_hash = imagehash.average_hash(Image.fromarray(im))
t2_list = sorted(images, key = lambda imm:
imagehash.average_hash(Image.open(imm["name"])) - im_hash, reverse = False)
return list(map(lambda x: x["name"], t2_list))
def lexica(prompt, limit_size = 128, ratio_size = 256):
lex = Lexica(query=prompt).images()
lex = lex[:limit_size]
lex = list(map(lambda ele: Image.open(
requests.get(ele, stream = True).raw
), lex))
lex = list(map(lambda x: min_dim_to_size(x, ratio_size)[1], lex))
return lex
with gr.Blocks() as demo:
with gr.Row():
with gr.Tabs():
inputs = gr.Textbox(label = 'Enter prompt to search Lexica.art')
#gr.Slider(label='Number of images ', minimum = 4, maximum = 20, step = 1, value = 4)]
i = gr.Image()
text_button = gr.Button("Retrieve Images")
with gr.Tabs():
outputs = gr.Gallery(lable='Output gallery').style(grid=3,height=768,
allow_preview=False)
#gr.Dataframe(label='prompts for corresponding images')]
#outputs.select(image_click, outputs, i, _js="(x) => x.splice(0,x.length)")
outputs.select(image_click, outputs, i,)
i.change(
fn=swap_gallery,
inputs=[i, outputs],
outputs=outputs,
queue=False
)
#### gr.Textbox().submit().success()
text_button.click(lexica, inputs=inputs, outputs=outputs)
demo.launch("0.0.0.0" ,debug = True)
|