imaginAIry/imaginairy/cli/imagine.py

209 lines
5.0 KiB
Python
Raw Normal View History

2023-02-25 20:32:50 +00:00
import click
from imaginairy.cli.clickshell_mod import ImagineColorsCommand
from imaginairy.cli.shared import _imagine_cmd, add_options, common_options
2023-02-25 20:32:50 +00:00
@click.command(
context_settings={"max_content_width": 140},
cls=ImagineColorsCommand,
name="imagine",
)
2023-02-25 20:32:50 +00:00
@click.argument("prompt_texts", nargs=-1)
@add_options(common_options)
@click.option(
"--control-image",
metavar="PATH|URL",
help=(
"Image used for control signal in image generation. "
"For example if control-mode is depth, then the generated image will match the depth map "
"extracted from the control image. "
"Defaults to the `--init-image`"
),
multiple=True,
2023-02-25 20:32:50 +00:00
)
@click.option(
"--control-image-raw",
metavar="PATH|URL",
help=(
"Preprocessed image used for control signal in image generation. Like `--control-image` but "
" expects the already extracted signal. For example the raw control image would be a depth map or"
"pose information."
),
multiple=True,
2023-02-25 20:32:50 +00:00
)
@click.option(
"--control-strength",
2023-12-07 05:51:36 +00:00
help=("Strength of the control signal."),
multiple=True,
)
2023-02-25 20:32:50 +00:00
@click.option(
"--control-mode",
default=None,
show_default=False,
type=click.Choice(
[
"",
"canny",
"depth",
"normal",
"hed",
"openpose",
"shuffle",
"edit",
"inpaint",
2023-05-05 09:59:53 +00:00
"details",
"colorize",
]
),
2023-02-25 20:32:50 +00:00
help="how the control image is used as signal",
multiple=True,
2023-02-25 20:32:50 +00:00
)
@click.option(
"--videogen",
is_flag=True,
default=False,
show_default=False,
help="Turns the generated photo into video",
)
2023-02-25 20:32:50 +00:00
@click.pass_context
def imagine_cmd(
ctx,
prompt_texts,
negative_prompt,
prompt_strength,
init_image,
init_image_strength,
outdir,
output_file_extension,
repeats,
height,
width,
size,
2023-02-25 20:32:50 +00:00
steps,
seed,
upscale,
fix_faces,
fix_faces_fidelity,
sampler_type,
log_level,
quiet,
show_work,
tile,
tile_x,
tile_y,
allow_compose_phase,
mask_image,
mask_prompt,
mask_mode,
mask_modify_original,
outpaint,
caption,
precision,
model_weights_path,
model_config_path,
prompt_library_path,
version,
2023-02-25 20:32:50 +00:00
make_gif,
make_compare_gif,
arg_schedules,
make_compilation_animation,
caption_text,
control_image,
control_image_raw,
control_strength,
2023-02-25 20:32:50 +00:00
control_mode,
videogen,
2023-02-25 20:32:50 +00:00
):
"""
Generate images via AI.
Can be invoked via either `aimg imagine` or just `imagine`.
"""
from imaginairy.schema import ControlNetInput, LazyLoadingImage
# hacky method of getting order of control images (mixing raw and normal images)
control_images = [
(o, path)
for o, path in ImagineColorsCommand._option_order
if o.name in ("control_image", "control_image_raw")
]
control_inputs = []
if control_mode:
for i, cm in enumerate(control_mode):
try:
option = control_images[i]
except IndexError:
option = None
if option is None:
control_image = None
control_image_raw = None
elif option[0].name == "control_image":
control_image = option[1]
control_image_raw = None
if control_image and control_image.startswith("http"):
control_image = LazyLoadingImage(url=control_image)
else:
control_image = None
control_image_raw = option[1]
if control_image_raw and control_image_raw.startswith("http"):
control_image_raw = LazyLoadingImage(url=control_image_raw)
control_inputs.append(
ControlNetInput(
image=control_image,
image_raw=control_image_raw,
strength=float(control_strength[i]),
mode=cm,
)
)
2023-02-25 20:32:50 +00:00
return _imagine_cmd(
ctx,
prompt_texts,
negative_prompt,
prompt_strength,
init_image,
init_image_strength,
outdir,
output_file_extension,
repeats,
height,
width,
size,
2023-02-25 20:32:50 +00:00
steps,
seed,
upscale,
fix_faces,
fix_faces_fidelity,
sampler_type,
log_level,
quiet,
show_work,
tile,
tile_x,
tile_y,
allow_compose_phase,
mask_image,
mask_prompt,
mask_mode,
mask_modify_original,
outpaint,
caption,
precision,
model_weights_path,
model_config_path,
prompt_library_path,
version,
2023-02-25 20:32:50 +00:00
make_gif,
make_compare_gif,
arg_schedules,
make_compilation_animation,
caption_text,
control_inputs=control_inputs,
videogen=videogen,
2023-02-25 20:32:50 +00:00
)
if __name__ == "__main__":
imagine_cmd()