svjack commited on
Commit
1613a0b
·
1 Parent(s): 64b5828

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def image_click(images, evt: gr.SelectData):
11
+ img_selected = images[evt.index]
12
+ return images[evt.index]['name']
13
+
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):
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
+ return lex
31
+
32
+ with gr.Blocks() as demo:
33
+ with gr.Row():
34
+ with gr.Tabs():
35
+ inputs = gr.Textbox(label = 'Enter prompt to search Lexica.art')
36
+ text_button = gr.Button("Retrieve Images")
37
+ #gr.Slider(label='Number of images ', minimum = 4, maximum = 20, step = 1, value = 4)]
38
+ with gr.Tabs():
39
+ i = gr.Image()
40
+ outputs = gr.Gallery(lable='Output gallery').style(grid=3,height=768,
41
+ allow_preview=False)
42
+ #gr.Dataframe(label='prompts for corresponding images')]
43
+
44
+ #outputs.select(image_click, outputs, i, _js="(x) => x.splice(0,x.length)")
45
+ outputs.select(image_click, outputs, i,)
46
+
47
+ i.change(
48
+ fn=swap_gallery,
49
+ inputs=[i, outputs],
50
+ outputs=outputs,
51
+ queue=False
52
+ )
53
+ #### gr.Textbox().submit().success()
54
+
55
+ text_button.click(lexica, inputs=inputs, outputs=outputs)
56
+
57
+ demo.launch("0.0.0.0" ,debug = True)