Spaces:
Running
Running
Upload 2 files
Browse files- app.py +17 -20
- externalmod.py +27 -0
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
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
|
@@ -49,21 +48,16 @@ def random_choices():
|
|
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=
|
53 |
-
from pathlib import Path
|
54 |
kwargs = {}
|
55 |
-
if height
|
56 |
-
if width
|
57 |
-
if steps
|
58 |
-
if cfg
|
59 |
-
|
60 |
-
|
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=
|
67 |
await asyncio.sleep(0)
|
68 |
try:
|
69 |
result = await asyncio.wait_for(task, timeout=timeout)
|
@@ -72,22 +66,21 @@ async def infer(model_str, prompt, nprompt="", height=None, width=None, steps=No
|
|
72 |
print(f"Task timed out: {model_str}")
|
73 |
if not task.done(): task.cancel()
|
74 |
result = None
|
75 |
-
raise Exception(f"Task timed out: {model_str}")
|
76 |
except Exception as e:
|
77 |
print(e)
|
78 |
if not task.done(): task.cancel()
|
79 |
result = None
|
80 |
-
raise Exception(e
|
81 |
if task.done() and result is not None and not isinstance(result, tuple):
|
82 |
with lock:
|
83 |
png_path = "image.png"
|
84 |
-
result
|
85 |
-
image = str(Path(png_path).resolve())
|
86 |
return image
|
87 |
return None
|
88 |
|
89 |
|
90 |
-
def gen_fn(model_str, prompt, nprompt="", height=
|
91 |
try:
|
92 |
loop = asyncio.new_event_loop()
|
93 |
result = loop.run_until_complete(infer(model_str, prompt, nprompt,
|
@@ -131,6 +124,8 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=CSS) as demo:
|
|
131 |
steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
132 |
cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
133 |
seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
|
|
|
|
134 |
with gr.Row():
|
135 |
gen_button = gr.Button(f'Generate up to {int(num_models)} images in up to 3 minutes total', variant='primary', scale=3)
|
136 |
random_button = gr.Button(f'Random {int(num_models)} π²', variant='secondary', scale=1)
|
@@ -179,6 +174,8 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=CSS) as demo:
|
|
179 |
steps2 = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
180 |
cfg2 = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
181 |
seed2 = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
|
|
|
|
182 |
num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
|
183 |
with gr.Row():
|
184 |
gen_button2 = gr.Button('Generate', variant='primary', scale=2)
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from all_models import models
|
3 |
+
from externalmod import gr_Interface_load, save_image, randomize_seed
|
4 |
import asyncio
|
5 |
import os
|
6 |
from threading import RLock
|
|
|
48 |
|
49 |
# https://huggingface.co/docs/api-inference/detailed_parameters
|
50 |
# https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
|
51 |
+
async def infer(model_str, prompt, nprompt="", height=0, width=0, steps=0, cfg=0, seed=-1, timeout=inference_timeout):
|
|
|
52 |
kwargs = {}
|
53 |
+
if height > 0: kwargs["height"] = height
|
54 |
+
if width > 0: kwargs["width"] = width
|
55 |
+
if steps > 0: kwargs["num_inference_steps"] = steps
|
56 |
+
if cfg > 0: cfg = kwargs["guidance_scale"] = cfg
|
57 |
+
if seed == -1: kwargs["seed"] = randomize_seed()
|
58 |
+
else: kwargs["seed"] = seed
|
|
|
|
|
|
|
|
|
59 |
task = asyncio.create_task(asyncio.to_thread(models_load[model_str].fn,
|
60 |
+
prompt=prompt, negative_prompt=nprompt, **kwargs, token=HF_TOKEN))
|
61 |
await asyncio.sleep(0)
|
62 |
try:
|
63 |
result = await asyncio.wait_for(task, timeout=timeout)
|
|
|
66 |
print(f"Task timed out: {model_str}")
|
67 |
if not task.done(): task.cancel()
|
68 |
result = None
|
69 |
+
raise Exception(f"Task timed out: {model_str}") from e
|
70 |
except Exception as e:
|
71 |
print(e)
|
72 |
if not task.done(): task.cancel()
|
73 |
result = None
|
74 |
+
raise Exception() from e
|
75 |
if task.done() and result is not None and not isinstance(result, tuple):
|
76 |
with lock:
|
77 |
png_path = "image.png"
|
78 |
+
image = save_image(result, png_path, model_str, prompt, nprompt, height, width, steps, cfg, seed)
|
|
|
79 |
return image
|
80 |
return None
|
81 |
|
82 |
|
83 |
+
def gen_fn(model_str, prompt, nprompt="", height=0, width=0, steps=0, cfg=0, seed=-1):
|
84 |
try:
|
85 |
loop = asyncio.new_event_loop()
|
86 |
result = loop.run_until_complete(infer(model_str, prompt, nprompt,
|
|
|
124 |
steps = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
125 |
cfg = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
126 |
seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
127 |
+
seed_rand = gr.Button("Randomize Seed π²", size="sm", variant="secondary")
|
128 |
+
seed_rand.click(randomize_seed, None, [seed], queue=False)
|
129 |
with gr.Row():
|
130 |
gen_button = gr.Button(f'Generate up to {int(num_models)} images in up to 3 minutes total', variant='primary', scale=3)
|
131 |
random_button = gr.Button(f'Random {int(num_models)} π²', variant='secondary', scale=1)
|
|
|
174 |
steps2 = gr.Slider(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
175 |
cfg2 = gr.Slider(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
176 |
seed2 = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
177 |
+
seed_rand2 = gr.Button("Randomize Seed π²", size="sm", variant="secondary")
|
178 |
+
seed_rand2.click(randomize_seed, None, [seed2], queue=False)
|
179 |
num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
|
180 |
with gr.Row():
|
181 |
gen_button2 = gr.Button('Generate', variant='primary', scale=2)
|
externalmod.py
CHANGED
@@ -583,3 +583,30 @@ def find_model_list(author: str="", tags: list[str]=[], not_tag="", sort: str="l
|
|
583 |
models.append(model.id)
|
584 |
if len(models) == limit: break
|
585 |
return models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
583 |
models.append(model.id)
|
584 |
if len(models) == limit: break
|
585 |
return models
|
586 |
+
|
587 |
+
|
588 |
+
def save_image(image, savefile, modelname, prompt, nprompt, height=0, width=0, steps=0, cfg=0, seed=-1):
|
589 |
+
from PIL import Image, PngImagePlugin
|
590 |
+
import json
|
591 |
+
try:
|
592 |
+
metadata = {"prompt": prompt, "negative_prompt": nprompt, "Model": {"Model": modelname.split("/")[-1]}}
|
593 |
+
if steps > 0: metadata["num_inference_steps"] = steps
|
594 |
+
if cfg > 0: metadata["guidance_scale"] = cfg
|
595 |
+
if seed != -1: metadata["seed"] = seed
|
596 |
+
if width > 0 and height > 0: metadata["resolution"] = f"{width} x {height}"
|
597 |
+
metadata_str = json.dumps(metadata)
|
598 |
+
info = PngImagePlugin.PngInfo()
|
599 |
+
info.add_text("metadata", metadata_str)
|
600 |
+
image.save(savefile, "PNG", pnginfo=info)
|
601 |
+
return str(Path(savefile).resolve())
|
602 |
+
except Exception as e:
|
603 |
+
print(f"Failed to save image file: {e}")
|
604 |
+
raise Exception(f"Failed to save image file:") from e
|
605 |
+
|
606 |
+
|
607 |
+
def randomize_seed():
|
608 |
+
from random import seed, randint
|
609 |
+
MAX_SEED = 2**32-1
|
610 |
+
seed()
|
611 |
+
rseed = randint(0, MAX_SEED)
|
612 |
+
return rseed
|