Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,159 +1,206 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import sys
|
| 4 |
-
from pathlib import Path
|
| 5 |
from all_models import models
|
| 6 |
from externalmod import gr_Interface_load
|
| 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 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from random import randint
|
|
|
|
|
|
|
| 3 |
from all_models import models
|
| 4 |
from externalmod import gr_Interface_load
|
| 5 |
+
import asyncio
|
| 6 |
+
import os
|
| 7 |
+
from threading import RLock
|
| 8 |
+
lock = RLock()
|
| 9 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None # If private or gated models aren't used, ENV setting is unnecessary.
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def load_fn(models):
|
| 13 |
+
global models_load
|
| 14 |
+
models_load = {}
|
| 15 |
+
for model in models:
|
| 16 |
+
if model not in models_load.keys():
|
| 17 |
+
try:
|
| 18 |
+
m = gr_Interface_load(f'models/{model}', hf_token=HF_TOKEN)
|
| 19 |
+
except Exception as error:
|
| 20 |
+
print(error)
|
| 21 |
+
m = gr.Interface(lambda: None, ['text'], ['image'])
|
| 22 |
+
models_load.update({model: m})
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
load_fn(models)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
num_models = 6
|
| 29 |
+
max_images = 6
|
| 30 |
+
inference_timeout = 300
|
| 31 |
+
default_models = models[:num_models]
|
| 32 |
+
MAX_SEED = 2**32-1
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def extend_choices(choices):
|
| 36 |
+
return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def update_imgbox(choices):
|
| 40 |
+
choices_plus = extend_choices(choices[:num_models])
|
| 41 |
+
return [gr.Image(None, label = m, visible = (m != 'NA')) for m in choices_plus]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def random_choices():
|
| 45 |
+
import random
|
| 46 |
+
random.seed()
|
| 47 |
+
return random.choices(models, k = num_models)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# https://huggingface.co/docs/api-inference/detailed_parameters
|
| 51 |
+
# https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
|
| 52 |
+
async def infer(model_str, prompt, nprompt="", height=None, width=None, steps=None, cfg=None, seed=-1, timeout=inference_timeout):
|
| 53 |
+
from pathlib import Path
|
| 54 |
+
kwargs = {}
|
| 55 |
+
if height is not None and height >= 256: kwargs["height"] = height
|
| 56 |
+
if width is not None and width >= 256: kwargs["width"] = width
|
| 57 |
+
if steps is not None and steps >= 1: kwargs["num_inference_steps"] = steps
|
| 58 |
+
if cfg is not None and cfg > 0: cfg = kwargs["guidance_scale"] = cfg
|
| 59 |
+
noise = ""
|
| 60 |
+
if seed >= 0: kwargs["seed"] = seed
|
| 61 |
+
else:
|
| 62 |
+
rand = randint(1, 500)
|
| 63 |
+
for i in range(rand):
|
| 64 |
+
noise += " "
|
| 65 |
+
task = asyncio.create_task(asyncio.to_thread(models_load[model_str].fn,
|
| 66 |
+
prompt=f'{prompt} {noise}', negative_prompt=nprompt, **kwargs, token=HF_TOKEN))
|
| 67 |
+
await asyncio.sleep(0)
|
| 68 |
+
try:
|
| 69 |
+
result = await asyncio.wait_for(task, timeout=timeout)
|
| 70 |
+
except (Exception, asyncio.TimeoutError) as e:
|
| 71 |
+
print(e)
|
| 72 |
+
print(f"Task timed out: {model_str}")
|
| 73 |
+
if not task.done(): task.cancel()
|
| 74 |
+
result = None
|
| 75 |
+
if task.done() and result is not None:
|
| 76 |
+
with lock:
|
| 77 |
+
png_path = "image.png"
|
| 78 |
+
result.save(png_path)
|
| 79 |
+
image = str(Path(png_path).resolve())
|
| 80 |
+
return image
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def gen_fn(model_str, prompt, nprompt="", height=None, width=None, steps=None, cfg=None, seed=-1):
|
| 85 |
+
if model_str == 'NA':
|
| 86 |
+
return None
|
| 87 |
+
try:
|
| 88 |
+
loop = asyncio.new_event_loop()
|
| 89 |
+
result = loop.run_until_complete(infer(model_str, prompt, nprompt,
|
| 90 |
+
height, width, steps, cfg, seed, inference_timeout))
|
| 91 |
+
except (Exception, asyncio.CancelledError) as e:
|
| 92 |
+
print(e)
|
| 93 |
+
print(f"Task aborted: {model_str}")
|
| 94 |
+
result = None
|
| 95 |
+
finally:
|
| 96 |
+
loop.close()
|
| 97 |
+
return result
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def add_gallery(image, model_str, gallery):
|
| 101 |
+
if gallery is None: gallery = []
|
| 102 |
+
with lock:
|
| 103 |
+
if image is not None: gallery.insert(0, (image, model_str))
|
| 104 |
+
return gallery
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
CSS="""
|
| 108 |
+
#container { max-width: 1200px; margin: 0 auto; !important; }
|
| 109 |
+
.output { width=112px; height=112px; !important; }
|
| 110 |
+
.gallery { width=100%; min_height=768px; !important; }
|
| 111 |
+
.guide { text-align: center; !important; }
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=CSS) as demo:
|
| 115 |
+
with gr.Tab('The Dream'):
|
| 116 |
+
with gr.Column(scale=2):
|
| 117 |
+
with gr.Group():
|
| 118 |
+
txt_input = gr.Textbox(label='Your prompt:', lines=4)
|
| 119 |
+
neg_input = gr.Textbox(label='Negative prompt:', lines=1)
|
| 120 |
+
with gr.Accordion("Advanced", open=False, visible=True):
|
| 121 |
+
with gr.Row():
|
| 122 |
+
width = gr.Number(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
| 123 |
+
height = gr.Number(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
| 124 |
+
with gr.Row():
|
| 125 |
+
steps = gr.Number(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
| 126 |
+
cfg = gr.Number(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
| 127 |
+
seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
| 128 |
+
with gr.Row():
|
| 129 |
+
gen_button = gr.Button(f'Generate up to {int(num_models)} images in up to 3 minutes total', scale=3)
|
| 130 |
+
random_button = gr.Button(f'Random {int(num_models)} 🎲', variant='secondary', scale=1)
|
| 131 |
+
stop_button = gr.Button('Stop', variant='secondary', interactive=False, scale=1)
|
| 132 |
+
gen_button.click(lambda: gr.update(interactive=True), None, stop_button)
|
| 133 |
+
gr.Markdown("Scroll down to see more images and select models.", elem_classes="guide")
|
| 134 |
+
|
| 135 |
+
with gr.Column(scale=1):
|
| 136 |
+
with gr.Group():
|
| 137 |
+
with gr.Row():
|
| 138 |
+
output = [gr.Image(label=m, show_download_button=True, elem_classes="output",
|
| 139 |
+
interactive=False, min_width=80, show_share_button=False, format=".png",
|
| 140 |
+
visible=True) for m in default_models]
|
| 141 |
+
current_models = [gr.Textbox(m, visible=False) for m in default_models]
|
| 142 |
+
|
| 143 |
+
with gr.Column(scale=2):
|
| 144 |
+
gallery = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
|
| 145 |
+
interactive=False, show_share_button=True, container=True, format="png",
|
| 146 |
+
preview=True, object_fit="cover", columns=2, rows=2)
|
| 147 |
+
|
| 148 |
+
for m, o in zip(current_models, output):
|
| 149 |
+
gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fn,
|
| 150 |
+
inputs=[m, txt_input, neg_input, height, width, steps, cfg, seed], outputs=[o])
|
| 151 |
+
o.change(add_gallery, [o, m, gallery], [gallery])
|
| 152 |
+
stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event])
|
| 153 |
+
|
| 154 |
+
with gr.Column(scale=4):
|
| 155 |
+
with gr.Accordion('Model selection'):
|
| 156 |
+
model_choice = gr.CheckboxGroup(models, label = f'Choose up to {int(num_models)} different models from the {len(models)} available!', value=default_models, interactive=True)
|
| 157 |
+
model_choice.change(update_imgbox, model_choice, output)
|
| 158 |
+
model_choice.change(extend_choices, model_choice, current_models)
|
| 159 |
+
random_button.click(random_choices, None, model_choice)
|
| 160 |
+
|
| 161 |
+
with gr.Tab('Single model'):
|
| 162 |
+
with gr.Column(scale=2):
|
| 163 |
+
model_choice2 = gr.Dropdown(models, label='Choose model', value=models[0])
|
| 164 |
+
with gr.Group():
|
| 165 |
+
txt_input2 = gr.Textbox(label='Your prompt:', lines=4)
|
| 166 |
+
neg_input2 = gr.Textbox(label='Negative prompt:', lines=1)
|
| 167 |
+
with gr.Accordion("Advanced", open=False, visible=True):
|
| 168 |
+
with gr.Row():
|
| 169 |
+
width2 = gr.Number(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
| 170 |
+
height2 = gr.Number(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
| 171 |
+
with gr.Row():
|
| 172 |
+
steps2 = gr.Number(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
| 173 |
+
cfg2 = gr.Number(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
| 174 |
+
seed2 = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
| 175 |
+
num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
|
| 176 |
+
with gr.Row():
|
| 177 |
+
gen_button2 = gr.Button('Generate', scale=2)
|
| 178 |
+
stop_button2 = gr.Button('Stop', variant='secondary', interactive=False, scale=1)
|
| 179 |
+
gen_button2.click(lambda: gr.update(interactive=True), None, stop_button2)
|
| 180 |
+
|
| 181 |
+
with gr.Column(scale=1):
|
| 182 |
+
with gr.Group():
|
| 183 |
+
with gr.Row():
|
| 184 |
+
output2 = [gr.Image(label='', show_download_button=True, elem_classes="output",
|
| 185 |
+
interactive=False, min_width=80, visible=True, format=".png",
|
| 186 |
+
show_share_button=False, show_label=False) for _ in range(max_images)]
|
| 187 |
+
|
| 188 |
+
with gr.Column(scale=2):
|
| 189 |
+
gallery2 = gr.Gallery(label="Output", show_download_button=True, elem_classes="gallery",
|
| 190 |
+
interactive=False, show_share_button=True, container=True, format="png",
|
| 191 |
+
preview=True, object_fit="cover", columns=2, rows=2)
|
| 192 |
+
|
| 193 |
+
for i, o in enumerate(output2):
|
| 194 |
+
img_i = gr.Number(i, visible = False)
|
| 195 |
+
num_images.change(lambda i, n: gr.update(visible = (i < n)), [img_i, num_images], o)
|
| 196 |
+
gen_event2 = gr.on(triggers=[gen_button2.click, txt_input2.submit],
|
| 197 |
+
fn=lambda i, n, m, t1, t2, n1, n2, n3, n4, n5: gen_fn(m, t1, t2, n1, n2, n3, n4, n5) if (i < n) else None,
|
| 198 |
+
inputs=[img_i, num_images, model_choice2, txt_input2, neg_input2,
|
| 199 |
+
height2, width2, steps2, cfg2, seed2], outputs=[o])
|
| 200 |
+
o.change(add_gallery, [o, model_choice2, gallery2], [gallery2])
|
| 201 |
+
stop_button2.click(lambda: gr.update(interactive=False), None, stop_button2, cancels=[gen_event2])
|
| 202 |
+
|
| 203 |
+
gr.Markdown("Based on the [TestGen](https://huggingface.co/spaces/derwahnsinn/TestGen) Space by derwahnsinn, the [SpacIO](https://huggingface.co/spaces/RdnUser77/SpacIO_v1) Space by RdnUser77 and Omnibus's Maximum Multiplier!")
|
| 204 |
+
|
| 205 |
+
demo.queue()
|
| 206 |
+
demo.launch()
|