Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -153,8 +153,35 @@ opt.prep_time = -3
|
|
| 153 |
|
| 154 |
|
| 155 |
### Shared memory hack for ZeroGPU
|
| 156 |
-
opt.address = ('localhost', 6000)
|
| 157 |
-
opt.authkey = b'secret password'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
|
| 160 |
### Event handlers
|
|
@@ -215,6 +242,43 @@ def select_palette(state, button, idx):
|
|
| 215 |
return updates
|
| 216 |
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
def change_prompt_strength(state, strength):
|
| 219 |
if state.current_palette == 0:
|
| 220 |
return state
|
|
@@ -497,31 +561,6 @@ def draw(state, drawpad):
|
|
| 497 |
# conn.send(data)
|
| 498 |
# conn.close()
|
| 499 |
|
| 500 |
-
### Load examples
|
| 501 |
-
|
| 502 |
-
root = pathlib.Path(__file__).parent
|
| 503 |
-
example_root = os.path.join(root, 'examples')
|
| 504 |
-
example_images = glob.glob(os.path.join(example_root, '*.png'))
|
| 505 |
-
example_images = [Image.open(i) for i in example_images]
|
| 506 |
-
|
| 507 |
-
with open(os.path.join(example_root, 'prompt_background.txt')) as f:
|
| 508 |
-
prompts_background = [l.strip() for l in f.readlines() if l.strip() != '']
|
| 509 |
-
|
| 510 |
-
# with open(os.path.join(example_root, 'prompt_girl.txt')) as f:
|
| 511 |
-
# prompts_girl = [l.strip() for l in f.readlines() if l.strip() != '']
|
| 512 |
-
|
| 513 |
-
# with open(os.path.join(example_root, 'prompt_boy.txt')) as f:
|
| 514 |
-
# prompts_boy = [l.strip() for l in f.readlines() if l.strip() != '']
|
| 515 |
-
|
| 516 |
-
with open(os.path.join(example_root, 'prompt_props.txt')) as f:
|
| 517 |
-
prompts_props = [l.strip() for l in f.readlines() if l.strip() != '']
|
| 518 |
-
prompts_props = {l.split(',')[0].strip(): ','.join(l.split(',')[1:]).strip() for l in prompts_props}
|
| 519 |
-
|
| 520 |
-
prompt_background = lambda: random.choice(prompts_background)
|
| 521 |
-
# prompt_girl = lambda: random.choice(prompts_girl)
|
| 522 |
-
# prompt_boy = lambda: random.choice(prompts_boy)
|
| 523 |
-
prompt_props = lambda: np.random.choice(list(prompts_props.keys()), size=(opt.max_palettes - 1), replace=False).tolist()
|
| 524 |
-
|
| 525 |
|
| 526 |
### Main application
|
| 527 |
|
|
@@ -656,19 +695,18 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, head=head) as demo:
|
|
| 656 |
state.active_palettes = 5
|
| 657 |
|
| 658 |
# Front-end initialized to the default values.
|
| 659 |
-
prompt_props_ = prompt_props()
|
| 660 |
state.prompt_names = [
|
| 661 |
'🌄 Background',
|
| 662 |
'👧 Girl',
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
] +
|
| 666 |
state.prompts = [
|
| 667 |
'',
|
| 668 |
'A girl smiling at viewer',
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
] + [
|
| 672 |
state.neg_prompts = [
|
| 673 |
opt.default_negative_prompt
|
| 674 |
+ (', humans, humans, humans' if i == 0 else '')
|
|
@@ -760,6 +798,11 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, head=head) as demo:
|
|
| 760 |
visible=(state.value.active_palettes < opt.max_palettes),
|
| 761 |
)
|
| 762 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 763 |
with gr.Accordion(label='Import/Export Semantic Palette', open=True):
|
| 764 |
iface.tbox_state_import = gr.Textbox(label='Put Palette JSON Here To Import')
|
| 765 |
iface.json_state_export = gr.JSON(label='Exported Palette')
|
|
@@ -1030,6 +1073,21 @@ Deadline animation originally by <a href="https://codepen.io/jtrancozo/pen/mEoEV
|
|
| 1030 |
api_name='create_new',
|
| 1031 |
)
|
| 1032 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1033 |
run_event = iface.btn_generate.click(
|
| 1034 |
fn=hide_element,
|
| 1035 |
inputs=None,
|
|
|
|
| 153 |
|
| 154 |
|
| 155 |
### Shared memory hack for ZeroGPU
|
| 156 |
+
# opt.address = ('localhost', 6000)
|
| 157 |
+
# opt.authkey = b'secret password'
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
### Load examples
|
| 161 |
+
|
| 162 |
+
root = pathlib.Path(__file__).parent
|
| 163 |
+
example_root = os.path.join(root, 'examples')
|
| 164 |
+
example_images = glob.glob(os.path.join(example_root, '*.png'))
|
| 165 |
+
example_images = [Image.open(i) for i in example_images]
|
| 166 |
+
|
| 167 |
+
with open(os.path.join(example_root, 'prompt_background.txt')) as f:
|
| 168 |
+
prompts_background = [l.strip() for l in f.readlines() if l.strip() != '']
|
| 169 |
+
|
| 170 |
+
# with open(os.path.join(example_root, 'prompt_girl.txt')) as f:
|
| 171 |
+
# prompts_girl = [l.strip() for l in f.readlines() if l.strip() != '']
|
| 172 |
+
|
| 173 |
+
# with open(os.path.join(example_root, 'prompt_boy.txt')) as f:
|
| 174 |
+
# prompts_boy = [l.strip() for l in f.readlines() if l.strip() != '']
|
| 175 |
+
|
| 176 |
+
with open(os.path.join(example_root, 'prompt_props.txt')) as f:
|
| 177 |
+
prompts_props = [l.strip() for l in f.readlines() if l.strip() != '']
|
| 178 |
+
prompts_props = {l.split(',')[0].strip(): ','.join(l.split(',')[1:]).strip() for l in prompts_props}
|
| 179 |
+
|
| 180 |
+
prompt_background = lambda: random.choice(prompts_background)
|
| 181 |
+
# prompt_girl = lambda: random.choice(prompts_girl)
|
| 182 |
+
# prompt_boy = lambda: random.choice(prompts_boy)
|
| 183 |
+
prompt_props = lambda: np.random.choice(list(prompts_props.keys()), size=(opt.max_palettes - 1), replace=False).tolist()
|
| 184 |
+
|
| 185 |
|
| 186 |
|
| 187 |
### Event handlers
|
|
|
|
| 242 |
return updates
|
| 243 |
|
| 244 |
|
| 245 |
+
def random_palette(state):
|
| 246 |
+
girlboy = random.choice(['A girl smiling at viewer', 'A boy smiling at viewer'])
|
| 247 |
+
girlboy_name = '👧 Girl' if 'girl' in girlboy else '👦 Boy'
|
| 248 |
+
prompt_props_ = prompt_props()
|
| 249 |
+
state.prompt_names = ['🌄 Background'] + [girlboy_name] + prompt_props_
|
| 250 |
+
state.prompts = [''] + girlboy + [prompts_props[k] for k in prompt_props_]
|
| 251 |
+
state.neg_prompts = [
|
| 252 |
+
opt.default_negative_prompt
|
| 253 |
+
+ (', humans, humans, humans' if i == 0 else '')
|
| 254 |
+
for i in range(opt.max_palettes + 1)
|
| 255 |
+
]
|
| 256 |
+
state.prompt_strengths = [opt.default_prompt_strength for _ in range(opt.max_palettes)]
|
| 257 |
+
state.mask_strengths = [opt.default_mask_strength for _ in range(opt.max_palettes)]
|
| 258 |
+
state.mask_stds = [opt.default_mask_std for _ in range(opt.max_palettes)]
|
| 259 |
+
|
| 260 |
+
updates = [state] + [
|
| 261 |
+
gr.update(value=state.prompt_names[state.current_palette]),
|
| 262 |
+
gr.update(value=state.prompts[state.current_palette]),
|
| 263 |
+
gr.update(value=state.neg_prompts[state.current_palette]),
|
| 264 |
+
(
|
| 265 |
+
gr.update(value=state.mask_strengths[state.current_palette - 1], interactive=True) if state.current_palette > 0 else
|
| 266 |
+
gr.update(value=opt.default_mask_strength, interactive=False)
|
| 267 |
+
),
|
| 268 |
+
(
|
| 269 |
+
gr.update(value=state.prompt_strengths[state.current_palette - 1], interactive=True) if state.current_palette > 0 else
|
| 270 |
+
gr.update(value=opt.default_prompt_strength, interactive=False)
|
| 271 |
+
),
|
| 272 |
+
(
|
| 273 |
+
gr.update(value=state.mask_stds[state.current_palette - 1], interactive=True) if state.current_palette > 0 else
|
| 274 |
+
gr.update(value=opt.default_mask_std, interactive=False)
|
| 275 |
+
),
|
| 276 |
+
] + [
|
| 277 |
+
gr.update(value=v) for v in state.prompt_names[1:]
|
| 278 |
+
]
|
| 279 |
+
return updates
|
| 280 |
+
|
| 281 |
+
|
| 282 |
def change_prompt_strength(state, strength):
|
| 283 |
if state.current_palette == 0:
|
| 284 |
return state
|
|
|
|
| 561 |
# conn.send(data)
|
| 562 |
# conn.close()
|
| 563 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
|
| 565 |
### Main application
|
| 566 |
|
|
|
|
| 695 |
state.active_palettes = 5
|
| 696 |
|
| 697 |
# Front-end initialized to the default values.
|
|
|
|
| 698 |
state.prompt_names = [
|
| 699 |
'🌄 Background',
|
| 700 |
'👧 Girl',
|
| 701 |
+
'🐶 Dog',
|
| 702 |
+
'💐 Garden',
|
| 703 |
+
] + ['🎨 New Palette' for _ in range(opt.max_palettes - 3)]
|
| 704 |
state.prompts = [
|
| 705 |
'',
|
| 706 |
'A girl smiling at viewer',
|
| 707 |
+
'Doggy body part',
|
| 708 |
+
'Flower garden',
|
| 709 |
+
] + ['' for _ in range(opt.max_palettes - 3)]
|
| 710 |
state.neg_prompts = [
|
| 711 |
opt.default_negative_prompt
|
| 712 |
+ (', humans, humans, humans' if i == 0 else '')
|
|
|
|
| 798 |
visible=(state.value.active_palettes < opt.max_palettes),
|
| 799 |
)
|
| 800 |
|
| 801 |
+
iface.btn_random_palette = gr.Button(
|
| 802 |
+
value='Random Semantic Palette',
|
| 803 |
+
variant='primary',
|
| 804 |
+
)
|
| 805 |
+
|
| 806 |
with gr.Accordion(label='Import/Export Semantic Palette', open=True):
|
| 807 |
iface.tbox_state_import = gr.Textbox(label='Put Palette JSON Here To Import')
|
| 808 |
iface.json_state_export = gr.JSON(label='Exported Palette')
|
|
|
|
| 1073 |
api_name='create_new',
|
| 1074 |
)
|
| 1075 |
|
| 1076 |
+
iface.btn_random_palette.click(
|
| 1077 |
+
fn=random_palette,
|
| 1078 |
+
inputs=state,
|
| 1079 |
+
output=[
|
| 1080 |
+
state,
|
| 1081 |
+
iface.tbox_name,
|
| 1082 |
+
iface.tbox_prompt,
|
| 1083 |
+
iface.tbox_neg_prompt,
|
| 1084 |
+
iface.slider_alpha,
|
| 1085 |
+
iface.slider_strength,
|
| 1086 |
+
iface.slider_std,
|
| 1087 |
+
] + iface.btn_semantics[1:],
|
| 1088 |
+
api_name='random_palette',
|
| 1089 |
+
)
|
| 1090 |
+
|
| 1091 |
run_event = iface.btn_generate.click(
|
| 1092 |
fn=hide_element,
|
| 1093 |
inputs=None,
|