Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,144 +1,135 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 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 |
-
d = gr.update(value=0)
|
| 61 |
-
tog = gr.update(value=0)
|
| 62 |
-
#print (f'passing: to: {to} et: {et}')
|
| 63 |
-
pass
|
| 64 |
-
return d, tog
|
| 65 |
-
|
| 66 |
-
def all_task_start():
|
| 67 |
-
print("\n\n\n\n\n\n\n")
|
| 68 |
-
t = time.gmtime()
|
| 69 |
-
t_stamp = time.time()
|
| 70 |
-
current_time = time.strftime("%H:%M:%S", t)
|
| 71 |
-
return gr.update(value=t_stamp), gr.update(value=t_stamp), gr.update(value=0)
|
| 72 |
-
|
| 73 |
-
def clear_fn():
|
| 74 |
-
nn = len(models)
|
| 75 |
-
return tuple([None, *[None for _ in range(nn)]])
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
with gr.Blocks(title="SD Models") as my_interface:
|
| 80 |
-
with gr.Column(scale=12):
|
| 81 |
-
# with gr.Row():
|
| 82 |
-
# gr.Markdown("""- Primary prompt: 你想画的内容(英文单词,如 a cat, 加英文逗号效果更好;点 Improve 按钮进行完善)\n- Real prompt: 完善后的提示词,出现后再点右边的 Run 按钮开始运行""")
|
| 83 |
with gr.Row():
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
with gr.Row():
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
with gr.Row(visible=False):
|
| 105 |
-
start_box=gr.Number(interactive=False)
|
| 106 |
-
end_box=gr.Number(interactive=False)
|
| 107 |
-
tog_box=gr.Textbox(value=0,interactive=False)
|
| 108 |
-
|
| 109 |
-
start_box.change(
|
| 110 |
-
all_task_end,
|
| 111 |
-
[start_box, end_box],
|
| 112 |
-
[start_box, tog_box],
|
| 113 |
-
every=1,
|
| 114 |
-
show_progress=True)
|
| 115 |
-
|
| 116 |
-
primary_prompt.submit(all_task_start, None, [start_box, end_box, tog_box])
|
| 117 |
-
run.click(all_task_start, None, [start_box, end_box, tog_box])
|
| 118 |
-
runs_dict = {}
|
| 119 |
-
model_idx = 1
|
| 120 |
-
for model_path in models:
|
| 121 |
-
runs_dict[model_idx] = run.click(model_functions[model_idx], inputs=[primary_prompt], outputs=[sd_outputs[model_idx]])
|
| 122 |
-
model_idx += 1
|
| 123 |
-
pass
|
| 124 |
-
pass
|
| 125 |
-
|
| 126 |
-
# improve_prompts_btn_clicked=improve_prompts_btn.click(
|
| 127 |
-
# get_prompts,
|
| 128 |
-
# inputs=[primary_prompt],
|
| 129 |
-
# outputs=[primary_prompt],
|
| 130 |
-
# cancels=list(runs_dict.values()))
|
| 131 |
-
clear_btn.click(
|
| 132 |
-
clear_fn,
|
| 133 |
-
None,
|
| 134 |
-
[primary_prompt, *list(sd_outputs.values())],
|
| 135 |
-
cancels=[*list(runs_dict.values())])
|
| 136 |
-
tog_box.change(
|
| 137 |
-
clear_it,
|
| 138 |
-
tog_box,
|
| 139 |
-
tog_box,
|
| 140 |
-
cancels=[*list(runs_dict.values())])
|
| 141 |
-
|
| 142 |
-
my_interface.queue(concurrency_count=600, status_update_rate=1)
|
| 143 |
-
my_interface.launch(inline=True, show_api=False)
|
| 144 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from random import randint
|
| 3 |
+
from all_models import models
|
| 4 |
+
|
| 5 |
+
from externalmod import gr_Interface_load, randomize_seed
|
| 6 |
+
|
| 7 |
+
import asyncio
|
| 8 |
+
import os
|
| 9 |
+
from threading import RLock
|
| 10 |
+
lock = RLock()
|
| 11 |
+
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.
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def load_fn(models):
|
| 15 |
+
global models_load
|
| 16 |
+
models_load = {}
|
| 17 |
+
|
| 18 |
+
for model in models:
|
| 19 |
+
if model not in models_load.keys():
|
| 20 |
+
try:
|
| 21 |
+
m = gr_Interface_load(f'models/{model}', hf_token=HF_TOKEN)
|
| 22 |
+
except Exception as error:
|
| 23 |
+
print(error)
|
| 24 |
+
m = gr.Interface(lambda: None, ['text'], ['image'])
|
| 25 |
+
models_load.update({model: m})
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
load_fn(models)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
num_models = 9
|
| 32 |
+
|
| 33 |
+
default_models = models[:num_models]
|
| 34 |
+
inference_timeout = 600
|
| 35 |
+
MAX_SEED=666666666
|
| 36 |
+
starting_seed = randint(666666000, 666666666)
|
| 37 |
+
|
| 38 |
+
def extend_choices(choices):
|
| 39 |
+
return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def update_imgbox(choices):
|
| 43 |
+
choices_plus = extend_choices(choices[:num_models])
|
| 44 |
+
return [gr.Image(None, label=m, visible=(m!='NA')) for m in choices_plus]
|
| 45 |
+
|
| 46 |
+
async def infer(model_str, prompt, seed=1, timeout=inference_timeout):
|
| 47 |
+
from pathlib import Path
|
| 48 |
+
kwargs = {}
|
| 49 |
+
noise = ""
|
| 50 |
+
kwargs["seed"] = seed
|
| 51 |
+
task = asyncio.create_task(asyncio.to_thread(models_load[model_str].fn,
|
| 52 |
+
prompt=f'{prompt} {noise}', **kwargs, token=HF_TOKEN))
|
| 53 |
+
await asyncio.sleep(0)
|
| 54 |
+
try:
|
| 55 |
+
result = await asyncio.wait_for(task, timeout=timeout)
|
| 56 |
+
except (Exception, asyncio.TimeoutError) as e:
|
| 57 |
+
print(e)
|
| 58 |
+
print(f"Task timed out: {model_str}")
|
| 59 |
+
if not task.done(): task.cancel()
|
| 60 |
+
result = None
|
| 61 |
+
if task.done() and result is not None:
|
| 62 |
+
with lock:
|
| 63 |
+
png_path = "image.png"
|
| 64 |
+
result.save(png_path)
|
| 65 |
+
image = str(Path(png_path).resolve())
|
| 66 |
+
return image
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def gen_fnseed(model_str, prompt, seed=1):
|
| 73 |
+
if model_str == 'NA':
|
| 74 |
+
return None
|
| 75 |
try:
|
| 76 |
+
loop = asyncio.new_event_loop()
|
| 77 |
+
result = loop.run_until_complete(infer(model_str, prompt, seed, inference_timeout))
|
| 78 |
+
except (Exception, asyncio.CancelledError) as e:
|
| 79 |
+
print(e)
|
| 80 |
+
print(f"Task aborted: {model_str}")
|
| 81 |
+
result = None
|
| 82 |
+
with lock:
|
| 83 |
+
image = "https://huggingface.co/spaces/Yntec/ToyWorld/resolve/main/error.png"
|
| 84 |
+
result = image
|
| 85 |
+
finally:
|
| 86 |
+
loop.close()
|
| 87 |
+
return result
|
| 88 |
+
|
| 89 |
+
with gr.Blocks(theme='Yntec/HaleyCH_Theme_Blue_Teal') as demo:
|
| 90 |
+
with gr.Tab('🤗 2024 is the year this space was launched not the number of models! (they were brand new back then!) 🤗'):
|
| 91 |
+
txt_input = gr.Textbox(label='Your prompt:', lines=4)
|
| 92 |
+
gen_button = gr.Button('Generate up to 9 images in up to 3 minutes total')
|
| 93 |
+
with gr.Row():
|
| 94 |
+
seed = gr.Slider(label="Use a seed to replicate the same image later (maximum 666666666)", minimum=0, maximum=MAX_SEED, step=1, value=starting_seed, scale=3)
|
| 95 |
+
seed_rand = gr.Button("Randomize Seed 🎲", size="sm", variant="secondary", scale=1)
|
| 96 |
+
seed_rand.click(randomize_seed, None, [seed], queue=False)
|
| 97 |
+
#stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
|
| 98 |
+
|
| 99 |
+
gen_button.click(lambda s: gr.update(interactive = True), None)
|
| 100 |
+
gr.HTML(
|
| 101 |
+
"""
|
| 102 |
+
<div style="text-align: center; max-width: 1200px; margin: 0 auto;">
|
| 103 |
+
<div>
|
| 104 |
+
<body>
|
| 105 |
+
<div class="center"><p style="margin-bottom: 10px; color: #000000;">Scroll down to see more images and select models.</p>
|
| 106 |
+
</div>
|
| 107 |
+
</body>
|
| 108 |
+
</div>
|
| 109 |
+
</div>
|
| 110 |
+
"""
|
| 111 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
with gr.Row():
|
| 113 |
+
output = [gr.Image(label = m, min_width=480) for m in default_models]
|
| 114 |
+
current_models = [gr.Textbox(m, visible = False) for m in default_models]
|
| 115 |
+
|
| 116 |
+
for m, o in zip(current_models, output):
|
| 117 |
+
gen_event = gr.on(triggers=[gen_button.click, txt_input.submit], fn=gen_fnseed,
|
| 118 |
+
inputs=[m, txt_input, seed], outputs=[o], concurrency_limit=None, queue=False)
|
| 119 |
+
#stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
|
| 120 |
+
with gr.Accordion('Model selection'):
|
| 121 |
+
model_choice = gr.CheckboxGroup(models, label = 'Untick the models you will not be using', value=default_models, interactive=True)
|
| 122 |
+
#model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 2 available! Untick them to only use one!', value = default_models, multiselect = True, max_choices = num_models, interactive = True, filterable = False)
|
| 123 |
+
model_choice.change(update_imgbox, model_choice, output)
|
| 124 |
+
model_choice.change(extend_choices, model_choice, current_models)
|
| 125 |
with gr.Row():
|
| 126 |
+
gr.HTML(
|
| 127 |
+
"""
|
| 128 |
+
<div class="footer">
|
| 129 |
+
<p> For more than a hundred times more models (that's not a typo) check out <a href="https://huggingface.co/spaces/Yntec/ToyWorld">Toy World</a>!</a>
|
| 130 |
+
</p>
|
| 131 |
+
"""
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
demo.queue(default_concurrency_limit=200, max_size=200)
|
| 135 |
+
demo.launch(show_api=False, max_threads=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|