Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,84 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
MAX_SEED = 2**32-1
|
| 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 |
-
for i, o in enumerate(output):
|
| 86 |
-
img_i = gr.Number(i, visible=False)
|
| 87 |
-
image_num.change(lambda i, n: gr.update(visible = (i < n)), [img_i, image_num], o, show_api=False)
|
| 88 |
-
gen_event = gr.on(triggers=[run_button.click, prompt.submit],
|
| 89 |
-
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,
|
| 90 |
-
inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
|
| 91 |
-
positive_prefix, positive_suffix, negative_prefix, negative_suffix],
|
| 92 |
-
outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
|
| 93 |
-
gen_event2 = gr.on(triggers=[random_button.click],
|
| 94 |
-
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,
|
| 95 |
-
inputs=[img_i, image_num, model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
|
| 96 |
-
positive_prefix, positive_suffix, negative_prefix, negative_suffix],
|
| 97 |
-
outputs=[o], queue=False, show_api=False) # Be sure to delete ", queue=False" when activating the stop button
|
| 98 |
-
o.change(save_gallery, [o, results], [results, image_files], show_api=False)
|
| 99 |
-
#stop_button.click(lambda: gr.update(interactive=False), None, stop_button, cancels=[gen_event, gen_event2], show_api=False)
|
| 100 |
-
|
| 101 |
-
clear_prompt.click(lambda: None, None, [prompt], queue=False, show_api=False)
|
| 102 |
-
clear_results.click(lambda: (None, None), None, [results, image_files], queue=False, show_api=False)
|
| 103 |
-
recom_prompt_preset.change(set_recom_prompt_preset, [recom_prompt_preset],
|
| 104 |
-
[positive_prefix, positive_suffix, negative_prefix, negative_suffix], queue=False, show_api=False)
|
| 105 |
-
seed_rand.click(randomize_seed, None, [seed], queue=False, show_api=False)
|
| 106 |
-
trans_prompt.click(translate_to_en, [prompt], [prompt], queue=False, show_api=False)\
|
| 107 |
-
.then(translate_to_en, [neg_prompt], [neg_prompt], queue=False, show_api=False)
|
| 108 |
-
|
| 109 |
-
demo.queue(default_concurrency_limit=200, max_size=200)
|
| 110 |
-
demo.launch(max_threads=400)
|
| 111 |
-
# https://github.com/gradio-app/gradio/issues/6339
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import random
|
| 3 |
+
from model import models
|
| 4 |
+
from multit2i import (load_models, infer_fn, infer_rand_fn, save_gallery, change_model,
|
| 5 |
+
warm_model, get_model_info_md, loaded_models, get_positive_prefix, get_positive_suffix,
|
| 6 |
+
get_negative_prefix, get_negative_suffix, get_recom_prompt_type, set_recom_prompt_preset,
|
| 7 |
+
randomize_seed, translate_to_en)
|
| 8 |
+
|
| 9 |
+
MAX_SEED = 2**32 - 1
|
| 10 |
+
max_images = 6
|
| 11 |
+
|
| 12 |
+
def generate_image(args):
|
| 13 |
+
load_models(models)
|
| 14 |
+
|
| 15 |
+
model_name = args.model_name
|
| 16 |
+
prompt = args.prompt
|
| 17 |
+
neg_prompt = args.neg_prompt
|
| 18 |
+
width = args.width
|
| 19 |
+
height = args.height
|
| 20 |
+
steps = args.steps
|
| 21 |
+
cfg = args.cfg
|
| 22 |
+
seed = args.seed if args.seed != -1 else random.randint(0, MAX_SEED)
|
| 23 |
+
|
| 24 |
+
positive_prefix = args.positive_prefix
|
| 25 |
+
positive_suffix = args.positive_suffix
|
| 26 |
+
negative_prefix = args.negative_prefix
|
| 27 |
+
negative_suffix = args.negative_suffix
|
| 28 |
+
|
| 29 |
+
images = infer_fn(
|
| 30 |
+
model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
|
| 31 |
+
positive_prefix, positive_suffix, negative_prefix, negative_suffix
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
save_gallery(images, args.output)
|
| 35 |
+
|
| 36 |
+
def random_model_image(args):
|
| 37 |
+
load_models(models)
|
| 38 |
+
|
| 39 |
+
model_name = random.choice(list(loaded_models.keys()))
|
| 40 |
+
prompt = args.prompt
|
| 41 |
+
neg_prompt = args.neg_prompt
|
| 42 |
+
width = args.width
|
| 43 |
+
height = args.height
|
| 44 |
+
steps = args.steps
|
| 45 |
+
cfg = args.cfg
|
| 46 |
+
seed = random.randint(0, MAX_SEED)
|
| 47 |
+
|
| 48 |
+
positive_prefix = args.positive_prefix
|
| 49 |
+
positive_suffix = args.positive_suffix
|
| 50 |
+
negative_prefix = args.negative_prefix
|
| 51 |
+
negative_suffix = args.negative_suffix
|
| 52 |
+
|
| 53 |
+
images = infer_rand_fn(
|
| 54 |
+
model_name, prompt, neg_prompt, height, width, steps, cfg, seed,
|
| 55 |
+
positive_prefix, positive_suffix, negative_prefix, negative_suffix
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
save_gallery(images, args.output)
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
parser = argparse.ArgumentParser(description="Image generation script using preloaded models.")
|
| 62 |
+
|
| 63 |
+
parser.add_argument('--model_name', type=str, default=list(loaded_models.keys())[0], help='The model to use for image generation.')
|
| 64 |
+
parser.add_argument('--prompt', type=str, required=True, help='The prompt for image generation.')
|
| 65 |
+
parser.add_argument('--neg_prompt', type=str, default="", help='Negative prompt to avoid unwanted elements.')
|
| 66 |
+
parser.add_argument('--width', type=int, default=0, help='Image width, 0 for default.')
|
| 67 |
+
parser.add_argument('--height', type=int, default=0, help='Image height, 0 for default.')
|
| 68 |
+
parser.add_argument('--steps', type=int, default=0, help='Number of inference steps.')
|
| 69 |
+
parser.add_argument('--cfg', type=float, default=0, help='Guidance scale (CFG).')
|
| 70 |
+
parser.add_argument('--seed', type=int, default=-1, help='Random seed, -1 for random.')
|
| 71 |
+
parser.add_argument('--output', type=str, default="output_gallery", help='Output directory for generated images.')
|
| 72 |
+
parser.add_argument('--positive_prefix', nargs='*', default=[], help='Positive prefix for the prompt.')
|
| 73 |
+
parser.add_argument('--positive_suffix', nargs='*', default=["Common"], help='Positive suffix for the prompt.')
|
| 74 |
+
parser.add_argument('--negative_prefix', nargs='*', default=[], help='Negative prefix for the prompt.')
|
| 75 |
+
parser.add_argument('--negative_suffix', nargs='*', default=["Common"], help='Negative suffix for the prompt.')
|
| 76 |
+
|
| 77 |
+
parser.add_argument('--random_model', action='store_true', help='Use random model for image generation.')
|
| 78 |
+
|
| 79 |
+
args = parser.parse_args()
|
| 80 |
+
|
| 81 |
+
if args.random_model:
|
| 82 |
+
random_model_image(args)
|
| 83 |
+
else:
|
| 84 |
+
generate_image(args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|