Spaces:
Sleeping
Sleeping
Pedro Cuenca
commited on
Commit
·
8e80b38
1
Parent(s):
cd29368
UI mockup that toggles between grid / detail modes.
Browse files- app.py +69 -27
- sample_outputs/seeded_1.png +0 -0
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
#!/usr/bin/env python
|
2 |
# coding: utf-8
|
3 |
-
import os
|
4 |
import random
|
5 |
from typing import List
|
6 |
import gradio as gr
|
@@ -13,15 +12,41 @@ block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")
|
|
13 |
SELECT_LABEL = "Select as seed"
|
14 |
selectors: List[gr.Radio] = []
|
15 |
|
16 |
-
def
|
17 |
-
|
|
|
|
|
18 |
response = defaultdict(list)
|
19 |
for i in range(1, 7):
|
20 |
response["images"].append(Image.open(f"sample_outputs/{i}.png"))
|
21 |
response["seeds"].append(random.randint(0, 2 ** 32 -1))
|
22 |
-
|
|
|
|
|
23 |
return response["images"]
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
def image_block():
|
27 |
return gr.Image(
|
@@ -48,6 +73,12 @@ def current_selection():
|
|
48 |
except:
|
49 |
return -1
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def radio_block():
|
52 |
radio = gr.Radio(
|
53 |
choices=[SELECT_LABEL], interactive=True, show_label=False,
|
@@ -77,34 +108,45 @@ with block:
|
|
77 |
)
|
78 |
|
79 |
## Can we create a Component with these, so it can participate as an output?
|
80 |
-
with gr.
|
81 |
-
with gr.
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
with gr.
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
100 |
|
101 |
images = [image1, image2, image3, image4, image5, image6]
|
102 |
selectors += [select1, select2, select3, select4, select5, select6]
|
103 |
|
104 |
for radio in selectors:
|
105 |
radio.change(fn=partial(update_state, radio), inputs=selectors, outputs=selectors)
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
block.launch(enable_queue=False)
|
|
|
1 |
#!/usr/bin/env python
|
2 |
# coding: utf-8
|
|
|
3 |
import random
|
4 |
from typing import List
|
5 |
import gradio as gr
|
|
|
12 |
SELECT_LABEL = "Select as seed"
|
13 |
selectors: List[gr.Radio] = []
|
14 |
|
15 |
+
def infer_seeded_image(prompt, seed):
|
16 |
+
return Image.open(f"sample_outputs/seeded_1.png")
|
17 |
+
|
18 |
+
def infer_grid(prompt):
|
19 |
response = defaultdict(list)
|
20 |
for i in range(1, 7):
|
21 |
response["images"].append(Image.open(f"sample_outputs/{i}.png"))
|
22 |
response["seeds"].append(random.randint(0, 2 ** 32 -1))
|
23 |
+
|
24 |
+
# TODO: only run when a selection exists
|
25 |
+
# TODO: get seed to reuse
|
26 |
return response["images"]
|
27 |
|
28 |
+
def infer(prompt):
|
29 |
+
"""
|
30 |
+
Outputs:
|
31 |
+
- Grid images (list)
|
32 |
+
- Seeded Image (Image or None)
|
33 |
+
- Grid Box with updated visibility
|
34 |
+
- Seeded Box with updated visibility
|
35 |
+
"""
|
36 |
+
grid_images = [None] * 6
|
37 |
+
image_with_seed = None
|
38 |
+
visible = (False, False)
|
39 |
+
|
40 |
+
if current_selection() > -1:
|
41 |
+
image_with_seed = infer_seeded_image(prompt, 7667)
|
42 |
+
visible = (False, True)
|
43 |
+
else:
|
44 |
+
grid_images = infer_grid(prompt)
|
45 |
+
visible = (True, False)
|
46 |
+
|
47 |
+
boxes = [gr.Box.update(visible=v) for v in visible]
|
48 |
+
return grid_images + [image_with_seed] + boxes
|
49 |
+
|
50 |
|
51 |
def image_block():
|
52 |
return gr.Image(
|
|
|
73 |
except:
|
74 |
return -1
|
75 |
|
76 |
+
def clear_seed():
|
77 |
+
"""Update state of Radio buttons, grid, seeded_box"""
|
78 |
+
global selectors_state
|
79 |
+
selectors_state = [''] * 6
|
80 |
+
return selectors_state + [gr.Box.update(visible=True), gr.Box.update(visible=False)]
|
81 |
+
|
82 |
def radio_block():
|
83 |
radio = gr.Radio(
|
84 |
choices=[SELECT_LABEL], interactive=True, show_label=False,
|
|
|
108 |
)
|
109 |
|
110 |
## Can we create a Component with these, so it can participate as an output?
|
111 |
+
with (grid := gr.Box()):
|
112 |
+
with gr.Row():
|
113 |
+
with gr.Box().style(border=None):
|
114 |
+
image1 = image_block()
|
115 |
+
select1 = radio_block()
|
116 |
+
with gr.Box().style(border=None):
|
117 |
+
image2 = image_block()
|
118 |
+
select2 = radio_block()
|
119 |
+
with gr.Box().style(border=None):
|
120 |
+
image3 = image_block()
|
121 |
+
select3 = radio_block()
|
122 |
+
with gr.Row():
|
123 |
+
with gr.Box().style(border=None):
|
124 |
+
image4 = image_block()
|
125 |
+
select4 = radio_block()
|
126 |
+
with gr.Box().style(border=None):
|
127 |
+
image5 = image_block()
|
128 |
+
select5 = radio_block()
|
129 |
+
with gr.Box().style(border=None):
|
130 |
+
image6 = image_block()
|
131 |
+
select6 = radio_block()
|
132 |
|
133 |
images = [image1, image2, image3, image4, image5, image6]
|
134 |
selectors += [select1, select2, select3, select4, select5, select6]
|
135 |
|
136 |
for radio in selectors:
|
137 |
radio.change(fn=partial(update_state, radio), inputs=selectors, outputs=selectors)
|
138 |
+
|
139 |
+
with (seeded_box := gr.Box()):
|
140 |
+
seeded_image = image_block()
|
141 |
+
clear_seed_button = gr.Button("Clear Seed")
|
142 |
+
seeded_box.visible = False
|
143 |
+
clear_seed_button.click(clear_seed, inputs=[], outputs=selectors + [grid, seeded_box])
|
144 |
+
|
145 |
+
all_images = images + [seeded_image]
|
146 |
+
boxes = [grid, seeded_box]
|
147 |
+
infer_outputs = all_images + boxes
|
148 |
+
|
149 |
+
text.submit(infer, inputs=text, outputs=infer_outputs)
|
150 |
+
btn.click(infer, inputs=text, outputs=infer_outputs)
|
151 |
|
152 |
block.launch(enable_queue=False)
|
sample_outputs/seeded_1.png
ADDED
![]() |