2022-09-11 06:27:22 +00:00
|
|
|
import logging.config
|
2022-09-09 05:22:55 +00:00
|
|
|
|
|
|
|
import click
|
2022-09-24 08:56:19 +00:00
|
|
|
from click_shell import shell
|
2022-09-11 20:58:14 +00:00
|
|
|
|
2022-09-20 04:15:38 +00:00
|
|
|
from imaginairy import LazyLoadingImage, generate_caption
|
2022-09-16 16:24:24 +00:00
|
|
|
from imaginairy.api import imagine_image_files, load_model
|
2022-09-14 16:37:45 +00:00
|
|
|
from imaginairy.samplers.base import SAMPLER_TYPE_OPTIONS
|
2022-09-16 16:24:24 +00:00
|
|
|
from imaginairy.schema import ImaginePrompt
|
2022-09-16 06:06:59 +00:00
|
|
|
from imaginairy.suppress_logs import suppress_annoying_logs_and_warnings
|
2022-09-11 06:27:22 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def configure_logging(level="INFO"):
|
|
|
|
fmt = "%(message)s"
|
|
|
|
if level == "DEBUG":
|
|
|
|
fmt = "%(asctime)s [%(levelname)s] %(name)s:%(lineno)d: %(message)s"
|
2022-09-09 05:22:55 +00:00
|
|
|
|
2022-09-11 06:27:22 +00:00
|
|
|
LOGGING_CONFIG = {
|
|
|
|
"version": 1,
|
|
|
|
"disable_existing_loggers": True,
|
|
|
|
"formatters": {
|
|
|
|
"standard": {"format": fmt},
|
|
|
|
},
|
|
|
|
"handlers": {
|
|
|
|
"default": {
|
|
|
|
"level": "INFO",
|
|
|
|
"formatter": "standard",
|
|
|
|
"class": "logging.StreamHandler",
|
|
|
|
"stream": "ext://sys.stdout", # Default is stderr
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"loggers": {
|
|
|
|
"": { # root logger
|
|
|
|
"handlers": ["default"],
|
|
|
|
"level": "WARNING",
|
|
|
|
"propagate": False,
|
|
|
|
},
|
|
|
|
"imaginairy": {"handlers": ["default"], "level": level, "propagate": False},
|
|
|
|
"transformers.modeling_utils": {
|
|
|
|
"handlers": ["default"],
|
|
|
|
"level": "ERROR",
|
|
|
|
"propagate": False,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
logging.config.dictConfig(LOGGING_CONFIG)
|
2022-09-09 05:22:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.command()
|
2022-09-11 06:27:22 +00:00
|
|
|
@click.argument("prompt_texts", nargs=-1)
|
2022-09-12 01:00:40 +00:00
|
|
|
@click.option(
|
|
|
|
"--prompt-strength",
|
|
|
|
default=7.5,
|
|
|
|
show_default=True,
|
|
|
|
help="How closely to follow the prompt. Image looks unnatural at higher values",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--init-image",
|
2022-09-16 06:06:59 +00:00
|
|
|
help="Starting image. filepath or url",
|
2022-09-12 01:00:40 +00:00
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--init-image-strength",
|
2022-09-12 04:36:14 +00:00
|
|
|
default=0.6,
|
|
|
|
show_default=True,
|
2022-09-12 01:00:40 +00:00
|
|
|
help="Starting image.",
|
|
|
|
)
|
2022-09-09 05:22:55 +00:00
|
|
|
@click.option("--outdir", default="./outputs", help="where to write results to")
|
2022-09-10 05:14:04 +00:00
|
|
|
@click.option(
|
2022-09-11 06:27:22 +00:00
|
|
|
"-r",
|
|
|
|
"--repeats",
|
|
|
|
default=1,
|
|
|
|
type=int,
|
|
|
|
help="How many times to repeat the renders. If you provide two prompts and --repeat=3 then six images will be generated",
|
2022-09-10 05:14:04 +00:00
|
|
|
)
|
2022-09-09 05:22:55 +00:00
|
|
|
@click.option(
|
|
|
|
"-h",
|
|
|
|
"--height",
|
|
|
|
default=512,
|
|
|
|
type=int,
|
|
|
|
help="image height. should be multiple of 64",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"-w", "--width", default=512, type=int, help="image width. should be multiple of 64"
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--steps",
|
2022-09-11 06:27:22 +00:00
|
|
|
default=40,
|
2022-09-09 05:22:55 +00:00
|
|
|
type=int,
|
2022-09-11 06:27:22 +00:00
|
|
|
show_default=True,
|
2022-09-09 05:22:55 +00:00
|
|
|
help="How many diffusion steps to run. More steps, more detail, but with diminishing returns",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--seed",
|
|
|
|
default=None,
|
|
|
|
type=int,
|
|
|
|
help="What seed to use for randomness. Allows reproducible image renders",
|
|
|
|
)
|
2022-09-13 07:27:53 +00:00
|
|
|
@click.option("--upscale", is_flag=True)
|
|
|
|
@click.option("--fix-faces", is_flag=True)
|
2022-09-11 06:27:22 +00:00
|
|
|
@click.option(
|
|
|
|
"--sampler-type",
|
2022-09-14 07:40:25 +00:00
|
|
|
default="plms",
|
2022-09-14 16:37:45 +00:00
|
|
|
type=click.Choice(SAMPLER_TYPE_OPTIONS),
|
2022-09-11 06:27:22 +00:00
|
|
|
help="What sampling strategy to use",
|
|
|
|
)
|
2022-09-09 05:22:55 +00:00
|
|
|
@click.option("--ddim-eta", default=0.0, type=float)
|
2022-09-11 06:27:22 +00:00
|
|
|
@click.option(
|
|
|
|
"--log-level",
|
|
|
|
default="INFO",
|
|
|
|
type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR"]),
|
|
|
|
help="What level of logs to show.",
|
|
|
|
)
|
2022-09-24 08:56:19 +00:00
|
|
|
@click.option(
|
2022-09-24 21:41:25 +00:00
|
|
|
"--quiet",
|
|
|
|
"-q",
|
2022-09-24 08:56:19 +00:00
|
|
|
is_flag=True,
|
|
|
|
help="Alias of `--log-level ERROR`",
|
|
|
|
)
|
2022-09-11 06:27:22 +00:00
|
|
|
@click.option(
|
|
|
|
"--show-work",
|
2022-09-24 18:21:53 +00:00
|
|
|
default=False,
|
2022-09-24 08:56:19 +00:00
|
|
|
is_flag=True,
|
|
|
|
help="Output a debug images to `steps` folder.",
|
2022-09-11 06:27:22 +00:00
|
|
|
)
|
2022-09-11 20:56:41 +00:00
|
|
|
@click.option(
|
|
|
|
"--tile",
|
|
|
|
is_flag=True,
|
2022-09-21 05:57:03 +00:00
|
|
|
help="Any images rendered will be tileable.",
|
2022-09-11 20:56:41 +00:00
|
|
|
)
|
2022-09-18 13:07:07 +00:00
|
|
|
@click.option(
|
|
|
|
"--mask-image",
|
|
|
|
help="A mask to use for inpainting. White gets painted, Black is left alone.",
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--mask-prompt",
|
2022-09-24 05:58:48 +00:00
|
|
|
help=(
|
|
|
|
"Describe what you want masked and the AI will mask it for you. "
|
|
|
|
"You can describe complex masks with AND, OR, NOT keywords and parentheses. "
|
|
|
|
"The strength of each mask can be modified with {*1.5} notation. \n\n"
|
|
|
|
"Examples: \n"
|
|
|
|
"car AND (wheels{*1.1} OR trunk OR engine OR windows OR headlights) AND NOT (truck OR headlights){*10}\n"
|
|
|
|
"fruit|fruit stem"
|
|
|
|
),
|
2022-09-18 13:07:07 +00:00
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--mask-mode",
|
|
|
|
default="replace",
|
|
|
|
type=click.Choice(["keep", "replace"]),
|
|
|
|
help="Should we replace the masked area or keep it?",
|
|
|
|
)
|
2022-09-24 08:56:19 +00:00
|
|
|
@click.option(
|
|
|
|
"--mask-modify-original",
|
|
|
|
default=True,
|
|
|
|
is_flag=True,
|
|
|
|
help="After the inpainting is done",
|
|
|
|
)
|
2022-09-20 04:15:38 +00:00
|
|
|
@click.option(
|
|
|
|
"--caption",
|
|
|
|
default=False,
|
|
|
|
is_flag=True,
|
|
|
|
help="Generate a text description of the generated image",
|
|
|
|
)
|
2022-09-22 05:03:12 +00:00
|
|
|
@click.option(
|
|
|
|
"--precision",
|
|
|
|
help="evaluate at this precision",
|
|
|
|
type=click.Choice(["full", "autocast"]),
|
|
|
|
default="autocast",
|
|
|
|
)
|
2022-09-20 04:15:38 +00:00
|
|
|
@click.pass_context
|
2022-09-09 05:22:55 +00:00
|
|
|
def imagine_cmd(
|
2022-09-20 04:15:38 +00:00
|
|
|
ctx,
|
2022-09-09 05:22:55 +00:00
|
|
|
prompt_texts,
|
2022-09-12 01:00:40 +00:00
|
|
|
prompt_strength,
|
|
|
|
init_image,
|
|
|
|
init_image_strength,
|
2022-09-09 05:22:55 +00:00
|
|
|
outdir,
|
|
|
|
repeats,
|
|
|
|
height,
|
|
|
|
width,
|
|
|
|
steps,
|
|
|
|
seed,
|
2022-09-13 07:27:53 +00:00
|
|
|
upscale,
|
|
|
|
fix_faces,
|
2022-09-09 05:22:55 +00:00
|
|
|
sampler_type,
|
|
|
|
ddim_eta,
|
2022-09-11 06:27:22 +00:00
|
|
|
log_level,
|
2022-09-24 08:56:19 +00:00
|
|
|
quiet,
|
2022-09-11 06:27:22 +00:00
|
|
|
show_work,
|
2022-09-11 20:56:41 +00:00
|
|
|
tile,
|
2022-09-18 13:07:07 +00:00
|
|
|
mask_image,
|
|
|
|
mask_prompt,
|
|
|
|
mask_mode,
|
2022-09-24 18:21:53 +00:00
|
|
|
mask_modify_original,
|
2022-09-20 04:15:38 +00:00
|
|
|
caption,
|
2022-09-22 05:03:12 +00:00
|
|
|
precision,
|
2022-09-09 05:22:55 +00:00
|
|
|
):
|
2022-09-20 04:15:38 +00:00
|
|
|
"""Have the AI generate images. alias:imagine"""
|
|
|
|
if ctx.invoked_subcommand is not None:
|
|
|
|
return
|
2022-09-16 06:06:59 +00:00
|
|
|
suppress_annoying_logs_and_warnings()
|
2022-09-24 08:56:19 +00:00
|
|
|
if quiet:
|
|
|
|
log_level = "ERROR"
|
2022-09-11 06:27:22 +00:00
|
|
|
configure_logging(log_level)
|
2022-09-11 20:56:41 +00:00
|
|
|
|
2022-09-11 06:27:22 +00:00
|
|
|
total_image_count = len(prompt_texts) * repeats
|
|
|
|
logger.info(
|
2022-09-13 07:27:53 +00:00
|
|
|
f"🤖🧠 imaginAIry received {len(prompt_texts)} prompt(s) and will repeat them {repeats} times to create {total_image_count} images."
|
2022-09-11 06:27:22 +00:00
|
|
|
)
|
|
|
|
|
2022-09-16 06:06:59 +00:00
|
|
|
if init_image and init_image.startswith("http"):
|
|
|
|
init_image = LazyLoadingImage(url=init_image)
|
|
|
|
|
2022-09-24 05:58:48 +00:00
|
|
|
if mask_image and mask_image.startswith("http"):
|
|
|
|
mask_image = LazyLoadingImage(url=mask_image)
|
|
|
|
|
2022-09-09 05:22:55 +00:00
|
|
|
prompts = []
|
2022-09-21 05:57:03 +00:00
|
|
|
load_model()
|
2022-09-09 05:22:55 +00:00
|
|
|
for _ in range(repeats):
|
|
|
|
for prompt_text in prompt_texts:
|
|
|
|
prompt = ImaginePrompt(
|
|
|
|
prompt_text,
|
2022-09-12 01:00:40 +00:00
|
|
|
prompt_strength=prompt_strength,
|
|
|
|
init_image=init_image,
|
|
|
|
init_image_strength=init_image_strength,
|
2022-09-09 05:22:55 +00:00
|
|
|
seed=seed,
|
|
|
|
sampler_type=sampler_type,
|
|
|
|
steps=steps,
|
|
|
|
height=height,
|
|
|
|
width=width,
|
2022-09-18 13:07:07 +00:00
|
|
|
mask_image=mask_image,
|
|
|
|
mask_prompt=mask_prompt,
|
|
|
|
mask_mode=mask_mode,
|
2022-09-13 07:27:53 +00:00
|
|
|
upscale=upscale,
|
|
|
|
fix_faces=fix_faces,
|
2022-09-21 05:57:03 +00:00
|
|
|
tile_mode=tile,
|
2022-09-09 05:22:55 +00:00
|
|
|
)
|
|
|
|
prompts.append(prompt)
|
|
|
|
|
2022-09-10 07:32:31 +00:00
|
|
|
imagine_image_files(
|
2022-09-09 05:22:55 +00:00
|
|
|
prompts,
|
|
|
|
outdir=outdir,
|
|
|
|
ddim_eta=ddim_eta,
|
2022-09-24 18:21:53 +00:00
|
|
|
record_step_images=show_work,
|
2022-09-13 07:27:53 +00:00
|
|
|
output_file_extension="png",
|
2022-09-20 04:15:38 +00:00
|
|
|
print_caption=caption,
|
2022-09-22 05:03:12 +00:00
|
|
|
precision=precision,
|
2022-09-24 21:41:25 +00:00
|
|
|
mask_modify_original=mask_modify_original,
|
2022-09-09 05:22:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-09-24 08:56:19 +00:00
|
|
|
@shell(prompt="aimg > ", intro="Starting imaginAIry...")
|
2022-09-20 04:15:38 +00:00
|
|
|
def aimg():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@click.argument("image_filepaths", nargs=-1)
|
|
|
|
@aimg.command()
|
|
|
|
def describe(image_filepaths):
|
|
|
|
"""Generate text descriptions of images"""
|
|
|
|
imgs = []
|
|
|
|
for p in image_filepaths:
|
|
|
|
if p.startswith("http"):
|
|
|
|
img = LazyLoadingImage(url=p)
|
|
|
|
else:
|
|
|
|
img = LazyLoadingImage(filepath=p)
|
|
|
|
imgs.append(img)
|
|
|
|
for img in imgs:
|
|
|
|
print(generate_caption(img.copy()))
|
|
|
|
|
|
|
|
|
2022-09-24 08:56:19 +00:00
|
|
|
aimg.add_command(imagine_cmd, name="imagine")
|
2022-09-20 04:15:38 +00:00
|
|
|
|
2022-09-09 05:22:55 +00:00
|
|
|
if __name__ == "__main__":
|
2022-09-11 20:56:41 +00:00
|
|
|
imagine_cmd() # noqa
|