2023-01-28 23:09:19 +00:00
|
|
|
from logging import getLogger
|
2023-01-28 14:37:17 +00:00
|
|
|
|
2023-02-05 23:24:08 +00:00
|
|
|
import numpy as np
|
2023-02-05 23:55:04 +00:00
|
|
|
import torch
|
2023-02-05 13:53:26 +00:00
|
|
|
from diffusers import OnnxStableDiffusionPipeline
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
from ..diffusion.load import get_latents_from_seed, load_pipeline
|
|
|
|
from ..params import ImageParams, Size, StageParams
|
2023-02-26 05:49:39 +00:00
|
|
|
from ..server import ServerContext
|
2023-02-26 20:15:30 +00:00
|
|
|
from ..worker import ProgressCallback, WorkerContext
|
2023-01-28 14:37:17 +00:00
|
|
|
|
2023-01-28 23:09:19 +00:00
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2023-01-28 14:37:17 +00:00
|
|
|
|
2023-01-28 18:42:02 +00:00
|
|
|
def source_txt2img(
|
2023-02-26 05:49:39 +00:00
|
|
|
job: WorkerContext,
|
2023-02-05 03:17:39 +00:00
|
|
|
server: ServerContext,
|
2023-02-14 00:04:46 +00:00
|
|
|
_stage: StageParams,
|
2023-01-28 14:37:17 +00:00
|
|
|
params: ImageParams,
|
2023-02-19 04:11:44 +00:00
|
|
|
_source: Image.Image,
|
2023-01-28 14:37:17 +00:00
|
|
|
*,
|
|
|
|
size: Size,
|
2023-02-12 18:17:36 +00:00
|
|
|
callback: ProgressCallback = None,
|
2023-01-29 04:31:34 +00:00
|
|
|
**kwargs,
|
2023-01-28 18:42:02 +00:00
|
|
|
) -> Image.Image:
|
2023-02-18 22:27:48 +00:00
|
|
|
params = params.with_args(**kwargs)
|
|
|
|
size = size.with_args(**kwargs)
|
2023-02-18 23:59:13 +00:00
|
|
|
logger.info(
|
|
|
|
"generating image using txt2img, %s steps: %s", params.steps, params.prompt
|
|
|
|
)
|
2023-01-28 14:37:17 +00:00
|
|
|
|
2023-02-19 04:11:44 +00:00
|
|
|
if "stage_source" in kwargs:
|
2023-02-05 13:53:26 +00:00
|
|
|
logger.warn(
|
|
|
|
"a source image was passed to a txt2img stage, but will be discarded"
|
|
|
|
)
|
2023-01-28 14:37:17 +00:00
|
|
|
|
2023-02-05 23:36:00 +00:00
|
|
|
latents = get_latents_from_seed(params.seed, size)
|
2023-02-05 13:53:26 +00:00
|
|
|
pipe = load_pipeline(
|
2023-02-14 00:04:46 +00:00
|
|
|
server,
|
2023-02-05 23:55:04 +00:00
|
|
|
OnnxStableDiffusionPipeline,
|
|
|
|
params.model,
|
|
|
|
params.scheduler,
|
|
|
|
job.get_device(),
|
|
|
|
params.lpw,
|
2023-02-22 05:08:13 +00:00
|
|
|
params.inversion,
|
2023-02-05 13:53:26 +00:00
|
|
|
)
|
2023-02-05 23:36:00 +00:00
|
|
|
|
2023-02-05 23:15:37 +00:00
|
|
|
if params.lpw:
|
2023-02-05 23:55:04 +00:00
|
|
|
logger.debug("using LPW pipeline for txt2img")
|
2023-02-05 23:24:08 +00:00
|
|
|
rng = torch.manual_seed(params.seed)
|
2023-02-05 23:36:00 +00:00
|
|
|
result = pipe.text2img(
|
2023-02-18 22:27:48 +00:00
|
|
|
params.prompt,
|
2023-02-05 23:36:00 +00:00
|
|
|
height=size.height,
|
|
|
|
width=size.width,
|
|
|
|
generator=rng,
|
|
|
|
guidance_scale=params.cfg,
|
|
|
|
latents=latents,
|
|
|
|
negative_prompt=params.negative_prompt,
|
|
|
|
num_inference_steps=params.steps,
|
2023-02-12 18:17:36 +00:00
|
|
|
callback=callback,
|
2023-02-05 23:36:00 +00:00
|
|
|
)
|
2023-02-05 23:24:08 +00:00
|
|
|
else:
|
|
|
|
rng = np.random.RandomState(params.seed)
|
2023-02-05 23:36:00 +00:00
|
|
|
result = pipe(
|
2023-02-18 22:27:48 +00:00
|
|
|
params.prompt,
|
2023-02-05 23:36:00 +00:00
|
|
|
height=size.height,
|
|
|
|
width=size.width,
|
|
|
|
generator=rng,
|
|
|
|
guidance_scale=params.cfg,
|
|
|
|
latents=latents,
|
|
|
|
negative_prompt=params.negative_prompt,
|
|
|
|
num_inference_steps=params.steps,
|
2023-02-12 18:17:36 +00:00
|
|
|
callback=callback,
|
2023-02-05 23:36:00 +00:00
|
|
|
)
|
2023-01-28 14:37:17 +00:00
|
|
|
|
2023-01-28 14:44:24 +00:00
|
|
|
output = result.images[0]
|
2023-01-28 14:37:17 +00:00
|
|
|
|
2023-02-05 13:53:26 +00:00
|
|
|
logger.info("final output image size: %sx%s", output.width, output.height)
|
2023-01-28 14:37:17 +00:00
|
|
|
return output
|