Spaces:
Runtime error
Runtime error
File size: 4,943 Bytes
cd29368 8e80b38 cd29368 8e80b38 cd29368 8e80b38 cd29368 8e80b38 cd29368 8e80b38 cd29368 8e80b38 cd29368 |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
#!/usr/bin/env python
# coding: utf-8
import random
from typing import List
import gradio as gr
from collections import defaultdict
from functools import partial
from PIL import Image
block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")
SELECT_LABEL = "Select as seed"
selectors: List[gr.Radio] = []
def infer_seeded_image(prompt, seed):
return Image.open(f"sample_outputs/seeded_1.png")
def infer_grid(prompt):
response = defaultdict(list)
for i in range(1, 7):
response["images"].append(Image.open(f"sample_outputs/{i}.png"))
response["seeds"].append(random.randint(0, 2 ** 32 -1))
# TODO: only run when a selection exists
# TODO: get seed to reuse
return response["images"]
def infer(prompt):
"""
Outputs:
- Grid images (list)
- Seeded Image (Image or None)
- Grid Box with updated visibility
- Seeded Box with updated visibility
"""
grid_images = [None] * 6
image_with_seed = None
visible = (False, False)
if current_selection() > -1:
image_with_seed = infer_seeded_image(prompt, 7667)
visible = (False, True)
else:
grid_images = infer_grid(prompt)
visible = (True, False)
boxes = [gr.Box.update(visible=v) for v in visible]
return grid_images + [image_with_seed] + boxes
def image_block():
return gr.Image(
interactive=False, show_label=False
).style(
# border = (True, True, False, True),
rounded = (True, True, False, False),
)
selectors_state = [''] * 6
def did_select(radio: gr.Radio):
new_state = list(map(lambda r: SELECT_LABEL if r == radio else '', selectors))
return new_state
def update_state(radio: gr.Radio, *state):
global selectors_state
if list(state) != selectors_state:
selectors_state = did_select(radio)
return selectors_state
def current_selection():
try:
return selectors_state.index(SELECT_LABEL)
except:
return -1
def clear_seed():
"""Update state of Radio buttons, grid, seeded_box"""
global selectors_state
selectors_state = [''] * 6
return selectors_state + [gr.Box.update(visible=True), gr.Box.update(visible=False)]
def radio_block():
radio = gr.Radio(
choices=[SELECT_LABEL], interactive=True, show_label=False,
).style(
# border = (False, True, True, True),
# rounded = (False, False, True, True)
container=False
)
return radio
with block:
gr.Markdown("<h1><center>Latent Diffusion Demo</center></h1>")
with gr.Group():
with gr.Box():
with gr.Row().style(mobile_collapse=False, equal_height=True):
text = gr.Textbox(
label="Enter your prompt", show_label=False, max_lines=1
).style(
border=(True, False, True, True),
# margin=False,
rounded=(True, False, False, True),
container=False,
)
btn = gr.Button("Run").style(
margin=False,
rounded=(False, True, True, False),
)
## Can we create a Component with these, so it can participate as an output?
with (grid := gr.Box()):
with gr.Row():
with gr.Box().style(border=None):
image1 = image_block()
select1 = radio_block()
with gr.Box().style(border=None):
image2 = image_block()
select2 = radio_block()
with gr.Box().style(border=None):
image3 = image_block()
select3 = radio_block()
with gr.Row():
with gr.Box().style(border=None):
image4 = image_block()
select4 = radio_block()
with gr.Box().style(border=None):
image5 = image_block()
select5 = radio_block()
with gr.Box().style(border=None):
image6 = image_block()
select6 = radio_block()
images = [image1, image2, image3, image4, image5, image6]
selectors += [select1, select2, select3, select4, select5, select6]
for radio in selectors:
radio.change(fn=partial(update_state, radio), inputs=selectors, outputs=selectors)
with (seeded_box := gr.Box()):
seeded_image = image_block()
clear_seed_button = gr.Button("Clear Seed")
seeded_box.visible = False
clear_seed_button.click(clear_seed, inputs=[], outputs=selectors + [grid, seeded_box])
all_images = images + [seeded_image]
boxes = [grid, seeded_box]
infer_outputs = all_images + boxes
text.submit(infer, inputs=text, outputs=infer_outputs)
btn.click(infer, inputs=text, outputs=infer_outputs)
block.launch(enable_queue=False) |