2022-09-10 05:14:04 +00:00
|
|
|
import hashlib
|
2022-09-11 06:27:22 +00:00
|
|
|
import json
|
2022-09-16 06:06:59 +00:00
|
|
|
import logging
|
|
|
|
import os.path
|
2022-09-10 05:14:04 +00:00
|
|
|
import random
|
2022-09-11 06:27:22 +00:00
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
2022-11-26 22:52:28 +00:00
|
|
|
from imaginairy import config
|
2022-09-10 05:14:04 +00:00
|
|
|
|
2022-09-16 06:06:59 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidUrlError(ValueError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class LazyLoadingImage:
|
|
|
|
def __init__(self, *, filepath=None, url=None):
|
|
|
|
if not filepath and not url:
|
|
|
|
raise ValueError("You must specify a url or filepath")
|
|
|
|
if filepath and url:
|
|
|
|
raise ValueError("You cannot specify a url and filepath")
|
|
|
|
|
|
|
|
# validate file exists
|
|
|
|
if filepath and not os.path.exists(filepath):
|
|
|
|
raise FileNotFoundError(f"File does not exist: {filepath}")
|
|
|
|
|
|
|
|
# validate url is valid url
|
|
|
|
if url:
|
2023-02-03 05:43:04 +00:00
|
|
|
from urllib3.exceptions import LocationParseError
|
|
|
|
from urllib3.util import parse_url
|
|
|
|
|
2022-09-16 06:06:59 +00:00
|
|
|
try:
|
|
|
|
parsed_url = parse_url(url)
|
|
|
|
except LocationParseError:
|
2022-09-16 16:24:24 +00:00
|
|
|
raise InvalidUrlError(f"Invalid url: {url}") # noqa
|
2022-09-16 06:06:59 +00:00
|
|
|
if parsed_url.scheme not in {"http", "https"} or not parsed_url.host:
|
|
|
|
raise InvalidUrlError(f"Invalid url: {url}")
|
|
|
|
|
|
|
|
self._lazy_filepath = filepath
|
|
|
|
self._lazy_url = url
|
|
|
|
self._img = None
|
|
|
|
|
|
|
|
def __getattr__(self, key):
|
|
|
|
if key == "_img":
|
|
|
|
# http://nedbatchelder.com/blog/201010/surprising_getattr_recursion.html
|
|
|
|
raise AttributeError()
|
2022-09-17 05:34:42 +00:00
|
|
|
if self._img:
|
|
|
|
return getattr(self._img, key)
|
2023-02-03 05:43:04 +00:00
|
|
|
from PIL import Image, ImageOps
|
2022-09-16 06:06:59 +00:00
|
|
|
|
|
|
|
if self._lazy_filepath:
|
|
|
|
self._img = Image.open(self._lazy_filepath)
|
2023-01-01 22:54:49 +00:00
|
|
|
logger.debug(
|
2022-09-16 06:06:59 +00:00
|
|
|
f"Loaded input 🖼 of size {self._img.size} from {self._lazy_filepath}"
|
|
|
|
)
|
|
|
|
elif self._lazy_url:
|
2023-02-03 05:43:04 +00:00
|
|
|
import requests
|
|
|
|
|
2022-09-17 05:21:20 +00:00
|
|
|
self._img = Image.open(
|
|
|
|
requests.get(self._lazy_url, stream=True, timeout=60).raw
|
|
|
|
)
|
2023-01-01 22:54:49 +00:00
|
|
|
logger.debug(
|
2022-09-16 06:06:59 +00:00
|
|
|
f"Loaded input 🖼 of size {self._img.size} from {self._lazy_url}"
|
|
|
|
)
|
2022-09-24 05:58:48 +00:00
|
|
|
# fix orientation
|
|
|
|
self._img = ImageOps.exif_transpose(self._img)
|
2022-09-16 06:06:59 +00:00
|
|
|
|
|
|
|
return getattr(self._img, key)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self._lazy_filepath or self._lazy_url
|
|
|
|
|
2022-09-10 05:14:04 +00:00
|
|
|
|
|
|
|
class WeightedPrompt:
|
|
|
|
def __init__(self, text, weight=1):
|
|
|
|
self.text = text
|
|
|
|
self.weight = weight
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.weight}*({self.text})"
|
|
|
|
|
|
|
|
|
|
|
|
class ImaginePrompt:
|
2022-09-18 13:07:07 +00:00
|
|
|
class MaskMode:
|
|
|
|
KEEP = "keep"
|
|
|
|
REPLACE = "replace"
|
|
|
|
|
2022-10-07 00:21:01 +00:00
|
|
|
DEFAULT_FACE_FIDELITY = 0.2
|
|
|
|
|
2022-09-10 05:14:04 +00:00
|
|
|
def __init__(
|
2022-09-17 19:24:27 +00:00
|
|
|
self,
|
|
|
|
prompt=None,
|
2023-01-21 21:23:48 +00:00
|
|
|
negative_prompt=None,
|
2022-09-17 19:24:27 +00:00
|
|
|
prompt_strength=7.5,
|
|
|
|
init_image=None, # Pillow Image, LazyLoadingImage, or filepath str
|
2023-02-12 07:42:19 +00:00
|
|
|
init_image_strength=None,
|
|
|
|
control_image=None,
|
|
|
|
control_image_raw=None,
|
|
|
|
control_mode=None,
|
2022-09-18 13:07:07 +00:00
|
|
|
mask_prompt=None,
|
|
|
|
mask_image=None,
|
|
|
|
mask_mode=MaskMode.REPLACE,
|
2022-09-25 20:07:27 +00:00
|
|
|
mask_modify_original=True,
|
2023-01-17 06:48:27 +00:00
|
|
|
outpaint=None,
|
2022-09-17 19:24:27 +00:00
|
|
|
seed=None,
|
2022-11-26 22:52:28 +00:00
|
|
|
steps=None,
|
|
|
|
height=None,
|
|
|
|
width=None,
|
2022-09-17 19:24:27 +00:00
|
|
|
upscale=False,
|
|
|
|
fix_faces=False,
|
2022-10-07 00:21:01 +00:00
|
|
|
fix_faces_fidelity=DEFAULT_FACE_FIDELITY,
|
2022-11-26 22:52:28 +00:00
|
|
|
sampler_type=config.DEFAULT_SAMPLER,
|
2022-09-17 19:24:27 +00:00
|
|
|
conditioning=None,
|
2022-12-20 05:32:41 +00:00
|
|
|
tile_mode="",
|
2023-02-16 16:11:31 +00:00
|
|
|
allow_compose_phase=True,
|
2022-11-26 22:52:28 +00:00
|
|
|
model=config.DEFAULT_MODEL,
|
2023-01-01 22:54:49 +00:00
|
|
|
model_config_path=None,
|
2023-01-25 16:55:05 +00:00
|
|
|
is_intermediate=False,
|
2023-01-28 01:18:42 +00:00
|
|
|
collect_progress_latents=False,
|
2022-09-10 05:14:04 +00:00
|
|
|
):
|
2023-01-29 01:16:47 +00:00
|
|
|
self.prompts = prompt
|
|
|
|
self.negative_prompt = negative_prompt
|
2022-09-11 06:27:22 +00:00
|
|
|
self.prompt_strength = prompt_strength
|
2022-09-10 05:14:04 +00:00
|
|
|
self.init_image = init_image
|
|
|
|
self.init_image_strength = init_image_strength
|
2023-02-12 07:42:19 +00:00
|
|
|
self.control_image = control_image
|
|
|
|
self.control_image_raw = control_image_raw
|
|
|
|
self.control_mode = control_mode
|
2023-01-29 01:16:47 +00:00
|
|
|
self._orig_seed = seed
|
|
|
|
self.seed = seed
|
2022-09-10 05:14:04 +00:00
|
|
|
self.steps = steps
|
|
|
|
self.height = height
|
|
|
|
self.width = width
|
|
|
|
self.upscale = upscale
|
|
|
|
self.fix_faces = fix_faces
|
2023-01-29 01:16:47 +00:00
|
|
|
self.fix_faces_fidelity = fix_faces_fidelity
|
|
|
|
self.sampler_type = sampler_type
|
2022-09-17 05:21:20 +00:00
|
|
|
self.conditioning = conditioning
|
2022-09-18 13:07:07 +00:00
|
|
|
self.mask_prompt = mask_prompt
|
|
|
|
self.mask_image = mask_image
|
|
|
|
self.mask_mode = mask_mode
|
2022-09-25 20:07:27 +00:00
|
|
|
self.mask_modify_original = mask_modify_original
|
2023-01-17 06:48:27 +00:00
|
|
|
self.outpaint = outpaint
|
2022-09-21 05:57:03 +00:00
|
|
|
self.tile_mode = tile_mode
|
2023-02-16 16:11:31 +00:00
|
|
|
self.allow_compose_phase = allow_compose_phase
|
2022-10-24 05:42:17 +00:00
|
|
|
self.model = model
|
2023-01-21 21:23:48 +00:00
|
|
|
self.model_config_path = model_config_path
|
2023-02-16 16:11:31 +00:00
|
|
|
|
2023-01-25 16:55:05 +00:00
|
|
|
# we don't want to save intermediate images
|
|
|
|
self.is_intermediate = is_intermediate
|
2023-01-28 01:18:42 +00:00
|
|
|
self.collect_progress_latents = collect_progress_latents
|
2022-09-10 05:14:04 +00:00
|
|
|
|
2023-01-29 01:16:47 +00:00
|
|
|
self.validate()
|
|
|
|
|
|
|
|
def validate(self):
|
2023-02-03 05:43:04 +00:00
|
|
|
from imaginairy.samplers import SAMPLER_LOOKUP, SamplerName
|
|
|
|
|
2023-01-29 01:16:47 +00:00
|
|
|
self.prompts = self.process_prompt_input(self.prompts)
|
|
|
|
|
|
|
|
if self.tile_mode is True:
|
|
|
|
self.tile_mode = "xy"
|
|
|
|
elif self.tile_mode is False:
|
|
|
|
self.tile_mode = ""
|
|
|
|
else:
|
|
|
|
self.tile_mode = self.tile_mode.lower()
|
|
|
|
assert self.tile_mode in ("", "x", "y", "xy")
|
|
|
|
|
2023-02-12 07:42:19 +00:00
|
|
|
if isinstance(self.control_image, str):
|
|
|
|
if not self.control_image.startswith("*prev."):
|
|
|
|
self.control_image = LazyLoadingImage(filepath=self.control_image)
|
|
|
|
|
2023-01-29 01:16:47 +00:00
|
|
|
if isinstance(self.init_image, str):
|
|
|
|
if not self.init_image.startswith("*prev."):
|
|
|
|
self.init_image = LazyLoadingImage(filepath=self.init_image)
|
|
|
|
|
|
|
|
if isinstance(self.mask_image, str):
|
|
|
|
if not self.mask_image.startswith("*prev."):
|
|
|
|
self.mask_image = LazyLoadingImage(filepath=self.mask_image)
|
|
|
|
|
2023-02-12 07:42:19 +00:00
|
|
|
if self.control_image is not None and self.control_image_raw is not None:
|
|
|
|
raise ValueError(
|
|
|
|
"You can only set one of `control_image` and `control_image_raw`"
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.control_image is not None and self.init_image is None:
|
|
|
|
self.init_image = self.control_image
|
|
|
|
|
|
|
|
if (
|
|
|
|
self.control_mode
|
|
|
|
and self.control_image is None
|
|
|
|
and self.init_image is not None
|
|
|
|
):
|
|
|
|
self.control_image = self.init_image
|
|
|
|
|
|
|
|
if self.control_mode and not self.control_image:
|
|
|
|
raise ValueError("You must set `control_image` when using `control_mode`")
|
|
|
|
|
2023-01-29 01:16:47 +00:00
|
|
|
if self.mask_image is not None and self.mask_prompt is not None:
|
|
|
|
raise ValueError("You can only set one of `mask_image` and `mask_prompt`")
|
2023-02-12 07:42:19 +00:00
|
|
|
|
2023-01-29 01:16:47 +00:00
|
|
|
if self.model is None:
|
|
|
|
self.model = config.DEFAULT_MODEL
|
|
|
|
|
2023-02-12 07:42:19 +00:00
|
|
|
if self.init_image_strength is None:
|
|
|
|
if self.control_mode is not None:
|
|
|
|
self.init_image_strength = 0.0
|
|
|
|
elif self.outpaint or self.mask_image or self.mask_prompt:
|
|
|
|
self.init_image_strength = 0.0
|
|
|
|
else:
|
|
|
|
self.init_image_strength = 0.2
|
|
|
|
|
2023-01-29 01:16:47 +00:00
|
|
|
self.seed = random.randint(1, 1_000_000_000) if self.seed is None else self.seed
|
|
|
|
|
|
|
|
self.sampler_type = self.sampler_type.lower()
|
|
|
|
|
|
|
|
self.fix_faces_fidelity = (
|
|
|
|
self.fix_faces_fidelity
|
|
|
|
if self.fix_faces_fidelity
|
|
|
|
else self.DEFAULT_FACE_FIDELITY
|
|
|
|
)
|
|
|
|
|
2022-11-26 22:52:28 +00:00
|
|
|
if self.height is None or self.width is None or self.steps is None:
|
2023-02-03 05:43:04 +00:00
|
|
|
from imaginairy.model_manager import get_model_default_image_size
|
|
|
|
|
2022-11-26 22:52:28 +00:00
|
|
|
SamplerCls = SAMPLER_LOOKUP[self.sampler_type]
|
|
|
|
self.steps = self.steps or SamplerCls.default_steps
|
|
|
|
self.width = self.width or get_model_default_image_size(self.model)
|
|
|
|
self.height = self.height or get_model_default_image_size(self.model)
|
|
|
|
|
2023-01-29 01:16:47 +00:00
|
|
|
if self.negative_prompt is None:
|
2023-01-21 21:23:48 +00:00
|
|
|
model_config = config.MODEL_CONFIG_SHORTCUTS.get(self.model, None)
|
|
|
|
if model_config:
|
2023-01-29 01:16:47 +00:00
|
|
|
self.negative_prompt = model_config.default_negative_prompt
|
2023-01-21 21:23:48 +00:00
|
|
|
else:
|
2023-01-29 01:16:47 +00:00
|
|
|
self.negative_prompt = config.DEFAULT_NEGATIVE_PROMPT
|
2023-01-21 21:23:48 +00:00
|
|
|
|
2023-01-29 01:16:47 +00:00
|
|
|
self.negative_prompt = self.process_prompt_input(self.negative_prompt)
|
2023-01-21 21:23:48 +00:00
|
|
|
|
2022-11-26 23:37:45 +00:00
|
|
|
if self.model == "SD-2.0-v" and self.sampler_type == SamplerName.PLMS:
|
|
|
|
raise ValueError("PLMS sampler is not supported for SD-2.0-v model.")
|
2023-01-21 21:23:48 +00:00
|
|
|
|
|
|
|
if self.model == "edit" and self.sampler_type in (
|
|
|
|
SamplerName.PLMS,
|
|
|
|
SamplerName.DDIM,
|
|
|
|
):
|
|
|
|
raise ValueError(
|
|
|
|
"PLMS and DDIM samplers are not supported for pix2pix edit model."
|
|
|
|
)
|
2022-11-26 23:37:45 +00:00
|
|
|
|
2022-09-10 05:14:04 +00:00
|
|
|
@property
|
|
|
|
def prompt_text(self):
|
|
|
|
if len(self.prompts) == 1:
|
|
|
|
return self.prompts[0].text
|
|
|
|
return "|".join(str(p) for p in self.prompts)
|
|
|
|
|
2022-12-13 18:05:24 +00:00
|
|
|
@property
|
|
|
|
def negative_prompt_text(self):
|
|
|
|
if len(self.negative_prompt) == 1:
|
|
|
|
return self.negative_prompt[0].text
|
|
|
|
return "|".join(str(p) for p in self.negative_prompt)
|
|
|
|
|
2022-09-11 06:27:22 +00:00
|
|
|
def prompt_description(self):
|
|
|
|
return (
|
2022-10-11 03:13:32 +00:00
|
|
|
f'"{self.prompt_text}" {self.width}x{self.height}px '
|
2022-12-13 18:05:24 +00:00
|
|
|
f'negative-prompt:"{self.negative_prompt_text}" '
|
2022-09-11 06:27:22 +00:00
|
|
|
f"seed:{self.seed} prompt-strength:{self.prompt_strength} steps:{self.steps} sampler-type:{self.sampler_type}"
|
|
|
|
)
|
|
|
|
|
|
|
|
def as_dict(self):
|
|
|
|
prompts = [(p.weight, p.text) for p in self.prompts]
|
2022-12-13 18:05:24 +00:00
|
|
|
negative_prompts = [(p.weight, p.text) for p in self.negative_prompt]
|
2022-09-11 06:27:22 +00:00
|
|
|
return {
|
2022-12-13 18:05:24 +00:00
|
|
|
"software": "imaginAIry",
|
|
|
|
"model": self.model,
|
2022-09-11 06:27:22 +00:00
|
|
|
"prompts": prompts,
|
|
|
|
"prompt_strength": self.prompt_strength,
|
2022-12-13 18:05:24 +00:00
|
|
|
"negative_prompt": negative_prompts,
|
2022-09-16 06:06:59 +00:00
|
|
|
"init_image": str(self.init_image),
|
2022-09-11 06:27:22 +00:00
|
|
|
"init_image_strength": self.init_image_strength,
|
2023-02-12 02:23:45 +00:00
|
|
|
# "seed": self.seed,
|
2022-09-11 06:27:22 +00:00
|
|
|
"steps": self.steps,
|
|
|
|
"height": self.height,
|
|
|
|
"width": self.width,
|
|
|
|
"upscale": self.upscale,
|
|
|
|
"fix_faces": self.fix_faces,
|
|
|
|
"sampler_type": self.sampler_type,
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:49:13 +00:00
|
|
|
def process_prompt_input(self, prompt_input):
|
|
|
|
prompt_input = prompt_input if prompt_input is not None else ""
|
|
|
|
|
|
|
|
if isinstance(prompt_input, str):
|
|
|
|
prompt_input = [WeightedPrompt(prompt_input, 1)]
|
|
|
|
|
|
|
|
prompt_input.sort(key=lambda p: p.weight, reverse=True)
|
|
|
|
return prompt_input
|
|
|
|
|
2022-09-11 06:27:22 +00:00
|
|
|
|
|
|
|
class ExifCodes:
|
2023-01-02 04:14:22 +00:00
|
|
|
"""https://www.awaresystems.be/imaging/tiff/tifftags/baseline.html."""
|
2022-09-11 07:35:57 +00:00
|
|
|
|
2022-09-11 06:27:22 +00:00
|
|
|
ImageDescription = 0x010E
|
|
|
|
Software = 0x0131
|
|
|
|
DateTime = 0x0132
|
|
|
|
HostComputer = 0x013C
|
|
|
|
UserComment = 0x9286
|
|
|
|
|
2022-09-10 05:14:04 +00:00
|
|
|
|
|
|
|
class ImagineResult:
|
2022-09-24 05:58:48 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
img,
|
|
|
|
prompt: ImaginePrompt,
|
|
|
|
is_nsfw,
|
2022-10-10 08:22:11 +00:00
|
|
|
safety_score,
|
2022-09-24 05:58:48 +00:00
|
|
|
upscaled_img=None,
|
2022-09-26 04:55:25 +00:00
|
|
|
modified_original=None,
|
2022-09-25 20:07:27 +00:00
|
|
|
mask_binary=None,
|
|
|
|
mask_grayscale=None,
|
2023-02-15 16:02:36 +00:00
|
|
|
result_images=None,
|
2022-11-11 18:16:59 +00:00
|
|
|
timings=None,
|
2023-01-28 01:18:42 +00:00
|
|
|
progress_latents=None,
|
2022-09-24 05:58:48 +00:00
|
|
|
):
|
2023-02-15 16:02:36 +00:00
|
|
|
import torch
|
|
|
|
|
2023-02-16 16:11:31 +00:00
|
|
|
from imaginairy.img_utils import (
|
|
|
|
model_latent_to_pillow_img,
|
|
|
|
torch_img_to_pillow_img,
|
|
|
|
)
|
2023-02-03 05:43:04 +00:00
|
|
|
from imaginairy.utils import get_device, get_hardware_description
|
|
|
|
|
2022-09-25 20:07:27 +00:00
|
|
|
self.prompt = prompt
|
|
|
|
|
|
|
|
self.images = {"generated": img}
|
|
|
|
|
|
|
|
if upscaled_img:
|
|
|
|
self.images["upscaled"] = upscaled_img
|
|
|
|
|
2022-09-26 04:55:25 +00:00
|
|
|
if modified_original:
|
|
|
|
self.images["modified_original"] = modified_original
|
2022-09-25 20:07:27 +00:00
|
|
|
|
|
|
|
if mask_binary:
|
|
|
|
self.images["mask_binary"] = mask_binary
|
|
|
|
|
|
|
|
if mask_grayscale:
|
|
|
|
self.images["mask_grayscale"] = mask_grayscale
|
|
|
|
|
2023-02-15 16:02:36 +00:00
|
|
|
for img_type, r_img in result_images.items():
|
|
|
|
if isinstance(r_img, torch.Tensor):
|
2023-02-16 16:11:31 +00:00
|
|
|
if r_img.shape[1] == 4:
|
|
|
|
r_img = model_latent_to_pillow_img(r_img)
|
|
|
|
else:
|
|
|
|
r_img = torch_img_to_pillow_img(r_img)
|
2023-02-15 16:02:36 +00:00
|
|
|
self.images[img_type] = r_img
|
2022-12-20 09:43:04 +00:00
|
|
|
|
2022-11-11 18:16:59 +00:00
|
|
|
self.timings = timings
|
2023-01-28 01:18:42 +00:00
|
|
|
self.progress_latents = progress_latents
|
2022-11-11 18:16:59 +00:00
|
|
|
|
2022-09-25 20:07:27 +00:00
|
|
|
# for backward compat
|
2022-09-10 05:14:04 +00:00
|
|
|
self.img = img
|
2022-09-13 07:27:53 +00:00
|
|
|
self.upscaled_img = upscaled_img
|
2022-09-25 20:07:27 +00:00
|
|
|
|
2022-09-15 02:40:50 +00:00
|
|
|
self.is_nsfw = is_nsfw
|
2022-10-10 08:22:11 +00:00
|
|
|
self.safety_score = safety_score
|
2022-09-11 06:27:22 +00:00
|
|
|
self.created_at = datetime.utcnow().replace(tzinfo=timezone.utc)
|
|
|
|
self.torch_backend = get_device()
|
2022-10-04 22:07:40 +00:00
|
|
|
self.hardware_name = get_hardware_description(get_device())
|
2022-09-10 05:14:04 +00:00
|
|
|
|
|
|
|
def md5(self):
|
|
|
|
return hashlib.md5(self.img.tobytes()).hexdigest()
|
2022-09-11 06:27:22 +00:00
|
|
|
|
|
|
|
def metadata_dict(self):
|
|
|
|
return {
|
|
|
|
"prompt": self.prompt.as_dict(),
|
|
|
|
}
|
|
|
|
|
2022-11-11 18:16:59 +00:00
|
|
|
def timings_str(self):
|
|
|
|
if not self.timings:
|
|
|
|
return ""
|
|
|
|
return " ".join(f"{k}:{v:.2f}s" for k, v in self.timings.items())
|
|
|
|
|
2022-09-13 07:27:53 +00:00
|
|
|
def _exif(self):
|
2023-02-03 05:43:04 +00:00
|
|
|
from PIL import Image
|
|
|
|
|
2022-09-16 06:06:59 +00:00
|
|
|
exif = Image.Exif()
|
2022-09-11 06:27:22 +00:00
|
|
|
exif[ExifCodes.ImageDescription] = self.prompt.prompt_description()
|
|
|
|
exif[ExifCodes.UserComment] = json.dumps(self.metadata_dict())
|
|
|
|
# help future web scrapes not ingest AI generated art
|
2022-11-26 22:52:28 +00:00
|
|
|
sd_version = self.prompt.model
|
|
|
|
if len(sd_version) > 20:
|
|
|
|
sd_version = "custom weights"
|
2022-11-27 22:02:07 +00:00
|
|
|
exif[ExifCodes.Software] = f"Imaginairy / Stable Diffusion {sd_version}"
|
2022-09-11 06:27:22 +00:00
|
|
|
exif[ExifCodes.DateTime] = self.created_at.isoformat(sep=" ")[:19]
|
|
|
|
exif[ExifCodes.HostComputer] = f"{self.torch_backend}:{self.hardware_name}"
|
2022-09-13 07:27:53 +00:00
|
|
|
return exif
|
|
|
|
|
2022-09-25 20:07:27 +00:00
|
|
|
def save(self, save_path, image_type="generated"):
|
|
|
|
img = self.images.get(image_type, None)
|
|
|
|
if img is None:
|
|
|
|
raise ValueError(
|
|
|
|
f"Image of type {image_type} not stored. Options are: {self.images.keys()}"
|
|
|
|
)
|
2022-09-17 05:34:42 +00:00
|
|
|
|
2022-09-25 20:07:27 +00:00
|
|
|
img.convert("RGB").save(save_path, exif=self._exif())
|
2022-09-24 05:58:48 +00:00
|
|
|
|
2022-09-17 05:34:42 +00:00
|
|
|
|
2023-02-03 05:43:04 +00:00
|
|
|
class SafetyMode:
|
|
|
|
STRICT = "strict"
|
|
|
|
RELAXED = "relaxed"
|