Spaces:
Running
Running
Upload 3 files
Browse files- README.md +1 -1
- app.py +34 -45
- multit2i.py +39 -16
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: ππ
|
|
4 |
colorFrom: blue
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
short_description: Text-to-Image
|
10 |
license: mit
|
|
|
4 |
colorFrom: blue
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.42.0
|
8 |
app_file: app.py
|
9 |
short_description: Text-to-Image
|
10 |
license: mit
|
app.py
CHANGED
@@ -1,24 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
from model import models
|
3 |
-
from multit2i import (
|
4 |
-
load_models, infer_fn, infer_rand_fn, save_gallery,
|
5 |
change_model, warm_model, get_model_info_md, loaded_models,
|
6 |
get_positive_prefix, get_positive_suffix, get_negative_prefix, get_negative_suffix,
|
7 |
-
get_recom_prompt_type, set_recom_prompt_preset, get_tag_type
|
8 |
-
|
9 |
-
|
10 |
-
predict_tags_wd, remove_specific_prompt, convert_danbooru_to_e621_prompt,
|
11 |
-
insert_recom_prompt, compose_prompt_to_copy,
|
12 |
-
)
|
13 |
from tagger.fl2sd3longcap import predict_tags_fl2_sd3
|
14 |
from tagger.v2 import V2_ALL_MODELS, v2_random_prompt
|
15 |
-
from tagger.utils import (
|
16 |
-
|
17 |
-
V2_LENGTH_OPTIONS, V2_IDENTITY_OPTIONS,
|
18 |
-
)
|
19 |
-
|
20 |
|
21 |
max_images = 8
|
|
|
22 |
load_models(models)
|
23 |
|
24 |
css = """
|
@@ -51,10 +45,13 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
|
|
51 |
prompt = gr.Text(label="Prompt", lines=2, max_lines=8, placeholder="1girl, solo, ...", show_copy_button=True)
|
52 |
neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="")
|
53 |
with gr.Accordion("Advanced options", open=False):
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
58 |
with gr.Accordion("Recommended Prompt", open=False):
|
59 |
recom_prompt_preset = gr.Radio(label="Set Presets", choices=get_recom_prompt_type(), value="Common")
|
60 |
with gr.Row():
|
@@ -63,12 +60,14 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
|
|
63 |
negative_prefix = gr.CheckboxGroup(label="Use Negative Prefix", choices=get_negative_prefix(), value=[])
|
64 |
negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"])
|
65 |
with gr.Accordion("Prompt Transformer", open=False):
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
72 |
v2_model = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
|
73 |
v2_copy = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
|
74 |
image_num = gr.Slider(label="Number of images", minimum=1, maximum=max_images, value=1, step=1, interactive=True, scale=1)
|
@@ -115,13 +114,13 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
|
|
115 |
img_i = gr.Number(i, visible=False)
|
116 |
image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=False)
|
117 |
gen_event = gr.on(triggers=[run_button.click, prompt.submit],
|
118 |
-
fn=lambda i, n, m, t1, t2, n1, n2, n3, n4, l1, l2, l3, l4: infer_fn(m, t1, t2, n1, n2, n3, n4, l1, l2, l3, l4) if (i < n) else None,
|
119 |
-
inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg,
|
120 |
positive_prefix, positive_suffix, negative_prefix, negative_suffix],
|
121 |
outputs=[o], queue=True, show_api=False)
|
122 |
gen_event2 = gr.on(triggers=[random_button.click],
|
123 |
-
fn=lambda i, n, m, t1, t2, n1, n2, n3, n4, l1, l2, l3, l4: infer_rand_fn(m, t1, t2, n1, n2, n3, n4, l1, l2, l3, l4) if (i < n) else None,
|
124 |
-
inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg,
|
125 |
positive_prefix, positive_suffix, negative_prefix, negative_suffix],
|
126 |
outputs=[o], queue=True, show_api=False)
|
127 |
o.change(save_gallery, [o, results], [results, image_files], show_api=False)
|
@@ -135,29 +134,19 @@ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", fill_width=True, css=css) as demo:
|
|
135 |
random_prompt.click(
|
136 |
v2_random_prompt, [prompt, v2_series, v2_character, v2_rating, v2_aspect_ratio, v2_length,
|
137 |
v2_identity, v2_ban_tags, v2_model], [prompt, v2_series, v2_character], show_api=False,
|
138 |
-
).success(
|
139 |
-
|
140 |
-
|
141 |
-
convert_danbooru_to_e621_prompt, [prompt, v2_tag_type], [prompt], queue=False, show_api=False,
|
142 |
-
)
|
143 |
-
tagger_generate_from_image.click(
|
144 |
-
lambda: ("", "", ""), None, [v2_series, v2_character, prompt], queue=False, show_api=False,
|
145 |
).success(
|
146 |
predict_tags_wd,
|
147 |
[tagger_image, prompt, tagger_algorithms, tagger_general_threshold, tagger_character_threshold],
|
148 |
[v2_series, v2_character, prompt, v2_copy],
|
149 |
show_api=False,
|
150 |
-
).success(
|
151 |
-
|
152 |
-
).success(
|
153 |
-
|
154 |
-
).success(
|
155 |
-
convert_danbooru_to_e621_prompt, [prompt, tagger_tag_type], [prompt], queue=False, show_api=False,
|
156 |
-
).success(
|
157 |
-
insert_recom_prompt, [prompt, neg_prompt, tagger_recom_prompt], [prompt, neg_prompt], queue=False, show_api=False,
|
158 |
-
).success(
|
159 |
-
compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False,
|
160 |
-
)
|
161 |
|
162 |
demo.queue()
|
163 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from model import models
|
3 |
+
from multit2i import (load_models, infer_fn, infer_rand_fn, save_gallery,
|
|
|
4 |
change_model, warm_model, get_model_info_md, loaded_models,
|
5 |
get_positive_prefix, get_positive_suffix, get_negative_prefix, get_negative_suffix,
|
6 |
+
get_recom_prompt_type, set_recom_prompt_preset, get_tag_type)
|
7 |
+
from tagger.tagger import (predict_tags_wd, remove_specific_prompt, convert_danbooru_to_e621_prompt,
|
8 |
+
insert_recom_prompt, compose_prompt_to_copy)
|
|
|
|
|
|
|
9 |
from tagger.fl2sd3longcap import predict_tags_fl2_sd3
|
10 |
from tagger.v2 import V2_ALL_MODELS, v2_random_prompt
|
11 |
+
from tagger.utils import (V2_ASPECT_RATIO_OPTIONS, V2_RATING_OPTIONS,
|
12 |
+
V2_LENGTH_OPTIONS, V2_IDENTITY_OPTIONS)
|
|
|
|
|
|
|
13 |
|
14 |
max_images = 8
|
15 |
+
MAX_SEED = 2**32-1
|
16 |
load_models(models)
|
17 |
|
18 |
css = """
|
|
|
45 |
prompt = gr.Text(label="Prompt", lines=2, max_lines=8, placeholder="1girl, solo, ...", show_copy_button=True)
|
46 |
neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="")
|
47 |
with gr.Accordion("Advanced options", open=False):
|
48 |
+
with gr.Row():
|
49 |
+
width = gr.Number(label="Width", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
50 |
+
height = gr.Number(label="Height", info="If 0, the default value is used.", maximum=1216, step=32, value=0)
|
51 |
+
with gr.Row():
|
52 |
+
steps = gr.Number(label="Number of inference steps", info="If 0, the default value is used.", maximum=100, step=1, value=0)
|
53 |
+
cfg = gr.Number(label="Guidance scale", info="If 0, the default value is used.", maximum=30.0, step=0.1, value=0)
|
54 |
+
seed = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
|
55 |
with gr.Accordion("Recommended Prompt", open=False):
|
56 |
recom_prompt_preset = gr.Radio(label="Set Presets", choices=get_recom_prompt_type(), value="Common")
|
57 |
with gr.Row():
|
|
|
60 |
negative_prefix = gr.CheckboxGroup(label="Use Negative Prefix", choices=get_negative_prefix(), value=[])
|
61 |
negative_suffix = gr.CheckboxGroup(label="Use Negative Suffix", choices=get_negative_suffix(), value=["Common"])
|
62 |
with gr.Accordion("Prompt Transformer", open=False):
|
63 |
+
with gr.Row():
|
64 |
+
v2_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="sfw")
|
65 |
+
v2_aspect_ratio = gr.Radio(label="Aspect ratio", info="The aspect ratio of the image.", choices=list(V2_ASPECT_RATIO_OPTIONS), value="square", visible=False)
|
66 |
+
v2_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="long")
|
67 |
+
with gr.Row():
|
68 |
+
v2_identity = gr.Radio(label="Keep identity", info="How strictly to keep the identity of the character or subject. If you specify the detail of subject in the prompt, you should choose `strict`. Otherwise, choose `none` or `lax`. `none` is very creative but sometimes ignores the input prompt.", choices=list(V2_IDENTITY_OPTIONS), value="lax")
|
69 |
+
v2_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
|
70 |
+
v2_tag_type = gr.Radio(label="Tag Type", info="danbooru for common, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru", visible=False)
|
71 |
v2_model = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
|
72 |
v2_copy = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
|
73 |
image_num = gr.Slider(label="Number of images", minimum=1, maximum=max_images, value=1, step=1, interactive=True, scale=1)
|
|
|
114 |
img_i = gr.Number(i, visible=False)
|
115 |
image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=False)
|
116 |
gen_event = gr.on(triggers=[run_button.click, prompt.submit],
|
117 |
+
fn=lambda i, n, m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4: infer_fn(m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4) if (i < n) else None,
|
118 |
+
inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
|
119 |
positive_prefix, positive_suffix, negative_prefix, negative_suffix],
|
120 |
outputs=[o], queue=True, show_api=False)
|
121 |
gen_event2 = gr.on(triggers=[random_button.click],
|
122 |
+
fn=lambda i, n, m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4: infer_rand_fn(m, t1, t2, n1, n2, n3, n4, n5, l1, l2, l3, l4) if (i < n) else None,
|
123 |
+
inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
|
124 |
positive_prefix, positive_suffix, negative_prefix, negative_suffix],
|
125 |
outputs=[o], queue=True, show_api=False)
|
126 |
o.change(save_gallery, [o, results], [results, image_files], show_api=False)
|
|
|
134 |
random_prompt.click(
|
135 |
v2_random_prompt, [prompt, v2_series, v2_character, v2_rating, v2_aspect_ratio, v2_length,
|
136 |
v2_identity, v2_ban_tags, v2_model], [prompt, v2_series, v2_character], show_api=False,
|
137 |
+
).success(get_tag_type, [positive_prefix, positive_suffix, negative_prefix, negative_suffix], [v2_tag_type], queue=False, show_api=False
|
138 |
+
).success(convert_danbooru_to_e621_prompt, [prompt, v2_tag_type], [prompt], queue=False, show_api=False)
|
139 |
+
tagger_generate_from_image.click(lambda: ("", "", ""), None, [v2_series, v2_character, prompt], queue=False, show_api=False,
|
|
|
|
|
|
|
|
|
140 |
).success(
|
141 |
predict_tags_wd,
|
142 |
[tagger_image, prompt, tagger_algorithms, tagger_general_threshold, tagger_character_threshold],
|
143 |
[v2_series, v2_character, prompt, v2_copy],
|
144 |
show_api=False,
|
145 |
+
).success(predict_tags_fl2_sd3, [tagger_image, prompt, tagger_algorithms], [prompt], show_api=False,
|
146 |
+
).success(remove_specific_prompt, [prompt, tagger_keep_tags], [prompt], queue=False, show_api=False,
|
147 |
+
).success(convert_danbooru_to_e621_prompt, [prompt, tagger_tag_type], [prompt], queue=False, show_api=False,
|
148 |
+
).success(insert_recom_prompt, [prompt, neg_prompt, tagger_recom_prompt], [prompt, neg_prompt], queue=False, show_api=False,
|
149 |
+
).success(compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
demo.queue()
|
152 |
demo.launch()
|
multit2i.py
CHANGED
@@ -6,7 +6,7 @@ from huggingface_hub import InferenceClient
|
|
6 |
import os
|
7 |
|
8 |
|
9 |
-
HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None
|
10 |
server_timeout = 600
|
11 |
inference_timeout = 300
|
12 |
|
@@ -33,22 +33,43 @@ def is_repo_name(s):
|
|
33 |
return re.fullmatch(r'^[^/]+?/[^/]+?$', s)
|
34 |
|
35 |
|
36 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
from huggingface_hub import HfApi
|
38 |
api = HfApi()
|
39 |
default_tags = ["diffusers"]
|
40 |
if not sort: sort = "last_modified"
|
|
|
41 |
models = []
|
42 |
try:
|
43 |
-
model_infos = api.list_models(author=author,
|
44 |
-
tags=list_uniq(default_tags + tags), cardData=True, sort=sort, limit=limit
|
45 |
except Exception as e:
|
46 |
print(f"Error: Failed to list models.")
|
47 |
print(e)
|
48 |
return models
|
49 |
for model in model_infos:
|
50 |
-
if not model.private and not model.gated
|
51 |
-
|
|
|
52 |
models.append(model.id)
|
53 |
if len(models) == limit: break
|
54 |
return models
|
@@ -333,13 +354,14 @@ def warm_model(model_name: str):
|
|
333 |
# https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
|
334 |
def infer_body(client: InferenceClient | gr.Interface, prompt: str, neg_prompt: str | None = None,
|
335 |
height: int | None = None, width: int | None = None,
|
336 |
-
steps: int | None = None, cfg: int | None = None):
|
337 |
png_path = "image.png"
|
338 |
kwargs = {}
|
339 |
if height is not None and height >= 256: kwargs["height"] = height
|
340 |
if width is not None and width >= 256: kwargs["width"] = width
|
341 |
if steps is not None and steps >= 1: kwargs["num_inference_steps"] = steps
|
342 |
if cfg is not None and cfg > 0: cfg = kwargs["guidance_scale"] = cfg
|
|
|
343 |
try:
|
344 |
if isinstance(client, InferenceClient):
|
345 |
image = client.text_to_image(prompt=prompt, negative_prompt=neg_prompt, **kwargs, token=HF_TOKEN)
|
@@ -355,17 +377,18 @@ def infer_body(client: InferenceClient | gr.Interface, prompt: str, neg_prompt:
|
|
355 |
|
356 |
async def infer(model_name: str, prompt: str, neg_prompt: str | None = None,
|
357 |
height: int | None = None, width: int | None = None,
|
358 |
-
steps: int | None = None, cfg: int | None = None,
|
359 |
save_path: str | None = None, timeout: float = inference_timeout):
|
360 |
import random
|
361 |
noise = ""
|
362 |
-
|
363 |
-
|
364 |
-
|
|
|
365 |
model = load_model(model_name)
|
366 |
if not model: return None
|
367 |
task = asyncio.create_task(asyncio.to_thread(infer_body, model, f"{prompt} {noise}", neg_prompt,
|
368 |
-
height, width, steps, cfg))
|
369 |
await asyncio.sleep(0)
|
370 |
try:
|
371 |
result = await asyncio.wait_for(task, timeout=timeout)
|
@@ -382,7 +405,7 @@ async def infer(model_name: str, prompt: str, neg_prompt: str | None = None,
|
|
382 |
|
383 |
|
384 |
def infer_fn(model_name: str, prompt: str, neg_prompt: str | None = None, height: int | None = None,
|
385 |
-
width: int | None = None, steps: int | None = None, cfg: int | None = None,
|
386 |
pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], save_path: str | None = None):
|
387 |
if model_name == 'NA':
|
388 |
return None
|
@@ -390,7 +413,7 @@ def infer_fn(model_name: str, prompt: str, neg_prompt: str | None = None, height
|
|
390 |
prompt, neg_prompt = recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
391 |
loop = asyncio.new_event_loop()
|
392 |
result = loop.run_until_complete(infer(model_name, prompt, neg_prompt, height, width,
|
393 |
-
steps, cfg, save_path, inference_timeout))
|
394 |
except (Exception, asyncio.CancelledError) as e:
|
395 |
print(e)
|
396 |
print(f"Task aborted: {model_name}")
|
@@ -401,7 +424,7 @@ def infer_fn(model_name: str, prompt: str, neg_prompt: str | None = None, height
|
|
401 |
|
402 |
|
403 |
def infer_rand_fn(model_name_dummy: str, prompt: str, neg_prompt: str | None = None, height: int | None = None,
|
404 |
-
width: int | None = None, steps: int | None = None, cfg: int | None = None,
|
405 |
pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], save_path: str | None = None):
|
406 |
import random
|
407 |
if model_name_dummy == 'NA':
|
@@ -412,7 +435,7 @@ def infer_rand_fn(model_name_dummy: str, prompt: str, neg_prompt: str | None = N
|
|
412 |
prompt, neg_prompt = recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
413 |
loop = asyncio.new_event_loop()
|
414 |
result = loop.run_until_complete(infer(model_name, prompt, neg_prompt, height, width,
|
415 |
-
steps, cfg, save_path, inference_timeout))
|
416 |
except (Exception, asyncio.CancelledError) as e:
|
417 |
print(e)
|
418 |
print(f"Task aborted: {model_name}")
|
|
|
6 |
import os
|
7 |
|
8 |
|
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 |
server_timeout = 600
|
11 |
inference_timeout = 300
|
12 |
|
|
|
33 |
return re.fullmatch(r'^[^/]+?/[^/]+?$', s)
|
34 |
|
35 |
|
36 |
+
def get_status(model_name: str):
|
37 |
+
from huggingface_hub import InferenceClient
|
38 |
+
client = InferenceClient(timeout=10)
|
39 |
+
return client.get_model_status(model_name)
|
40 |
+
|
41 |
+
|
42 |
+
def is_loadable(model_name: str, force_gpu: bool = False):
|
43 |
+
try:
|
44 |
+
status = get_status(model_name)
|
45 |
+
except Exception as e:
|
46 |
+
print(e)
|
47 |
+
print(f"Couldn't load {model_name}.")
|
48 |
+
return False
|
49 |
+
gpu_state = isinstance(status.compute_type, dict) and "gpu" in status.compute_type.keys()
|
50 |
+
if status is None or status.state not in ["Loadable", "Loaded"] or (force_gpu and not gpu_state):
|
51 |
+
print(f"Couldn't load {model_name}. Model state:'{status.state}', GPU:{gpu_state}")
|
52 |
+
return status is not None and status.state in ["Loadable", "Loaded"] and (not force_gpu or gpu_state)
|
53 |
+
|
54 |
+
|
55 |
+
def find_model_list(author: str="", tags: list[str]=[], not_tag="", sort: str="last_modified", limit: int=30, force_gpu=False, check_status=False):
|
56 |
from huggingface_hub import HfApi
|
57 |
api = HfApi()
|
58 |
default_tags = ["diffusers"]
|
59 |
if not sort: sort = "last_modified"
|
60 |
+
limit = limit * 20 if check_status and force_gpu else limit * 5
|
61 |
models = []
|
62 |
try:
|
63 |
+
model_infos = api.list_models(author=author, task="text-to-image",
|
64 |
+
tags=list_uniq(default_tags + tags), cardData=True, sort=sort, limit=limit)
|
65 |
except Exception as e:
|
66 |
print(f"Error: Failed to list models.")
|
67 |
print(e)
|
68 |
return models
|
69 |
for model in model_infos:
|
70 |
+
if not model.private and not model.gated:
|
71 |
+
loadable = is_loadable(model.id, force_gpu) if check_status else True
|
72 |
+
if not_tag and not_tag in model.tags or not loadable: continue
|
73 |
models.append(model.id)
|
74 |
if len(models) == limit: break
|
75 |
return models
|
|
|
354 |
# https://huggingface.co/docs/huggingface_hub/package_reference/inference_client
|
355 |
def infer_body(client: InferenceClient | gr.Interface, prompt: str, neg_prompt: str | None = None,
|
356 |
height: int | None = None, width: int | None = None,
|
357 |
+
steps: int | None = None, cfg: int | None = None, seed: int = -1):
|
358 |
png_path = "image.png"
|
359 |
kwargs = {}
|
360 |
if height is not None and height >= 256: kwargs["height"] = height
|
361 |
if width is not None and width >= 256: kwargs["width"] = width
|
362 |
if steps is not None and steps >= 1: kwargs["num_inference_steps"] = steps
|
363 |
if cfg is not None and cfg > 0: cfg = kwargs["guidance_scale"] = cfg
|
364 |
+
if seed >= 0: kwargs["seed"] = seed
|
365 |
try:
|
366 |
if isinstance(client, InferenceClient):
|
367 |
image = client.text_to_image(prompt=prompt, negative_prompt=neg_prompt, **kwargs, token=HF_TOKEN)
|
|
|
377 |
|
378 |
async def infer(model_name: str, prompt: str, neg_prompt: str | None = None,
|
379 |
height: int | None = None, width: int | None = None,
|
380 |
+
steps: int | None = None, cfg: int | None = None, seed: int = -1,
|
381 |
save_path: str | None = None, timeout: float = inference_timeout):
|
382 |
import random
|
383 |
noise = ""
|
384 |
+
if seed < 0:
|
385 |
+
rand = random.randint(1, 500)
|
386 |
+
for i in range(rand):
|
387 |
+
noise += " "
|
388 |
model = load_model(model_name)
|
389 |
if not model: return None
|
390 |
task = asyncio.create_task(asyncio.to_thread(infer_body, model, f"{prompt} {noise}", neg_prompt,
|
391 |
+
height, width, steps, cfg, seed))
|
392 |
await asyncio.sleep(0)
|
393 |
try:
|
394 |
result = await asyncio.wait_for(task, timeout=timeout)
|
|
|
405 |
|
406 |
|
407 |
def infer_fn(model_name: str, prompt: str, neg_prompt: str | None = None, height: int | None = None,
|
408 |
+
width: int | None = None, steps: int | None = None, cfg: int | None = None, seed: int = -1,
|
409 |
pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], save_path: str | None = None):
|
410 |
if model_name == 'NA':
|
411 |
return None
|
|
|
413 |
prompt, neg_prompt = recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
414 |
loop = asyncio.new_event_loop()
|
415 |
result = loop.run_until_complete(infer(model_name, prompt, neg_prompt, height, width,
|
416 |
+
steps, cfg, seed, save_path, inference_timeout))
|
417 |
except (Exception, asyncio.CancelledError) as e:
|
418 |
print(e)
|
419 |
print(f"Task aborted: {model_name}")
|
|
|
424 |
|
425 |
|
426 |
def infer_rand_fn(model_name_dummy: str, prompt: str, neg_prompt: str | None = None, height: int | None = None,
|
427 |
+
width: int | None = None, steps: int | None = None, cfg: int | None = None, seed: int = -1,
|
428 |
pos_pre: list = [], pos_suf: list = [], neg_pre: list = [], neg_suf: list = [], save_path: str | None = None):
|
429 |
import random
|
430 |
if model_name_dummy == 'NA':
|
|
|
435 |
prompt, neg_prompt = recom_prompt(prompt, neg_prompt, pos_pre, pos_suf, neg_pre, neg_suf)
|
436 |
loop = asyncio.new_event_loop()
|
437 |
result = loop.run_until_complete(infer(model_name, prompt, neg_prompt, height, width,
|
438 |
+
steps, cfg, seed, save_path, inference_timeout))
|
439 |
except (Exception, asyncio.CancelledError) as e:
|
440 |
print(e)
|
441 |
print(f"Task aborted: {model_name}")
|