diffused.generate

 1from typing import NotRequired, TypedDict, Unpack
 2
 3import diffusers
 4import torch
 5from PIL import Image
 6
 7
 8class Generate(TypedDict):
 9    model: str
10    prompt: str
11    negative_prompt: NotRequired[str]
12    image: NotRequired[str]
13    mask_image: NotRequired[str]
14    width: NotRequired[int]
15    height: NotRequired[int]
16    num_images_per_prompt: NotRequired[int]
17    guidance_scale: NotRequired[float]
18    num_inference_steps: NotRequired[int]
19    strength: NotRequired[float]
20    seed: NotRequired[int]
21    device: NotRequired[str]
22    use_safetensors: NotRequired[bool]
23
24
25def generate(**kwargs: Unpack[Generate]) -> list[Image.Image]:
26    """
27    Generate image with diffusion model.
28
29    Args:
30        model (str): Diffusion model.
31        prompt (str): Text prompt.
32        negative_prompt (str): What to exclude from the generated image.
33        image (str): Input image path or URL.
34        mask_image (str): Mask image path or URL.
35        width (int): Generated image width in pixels.
36        height (int): Generated image height in pixels.
37        num_images_per_prompt (int): Number of images per prompt.
38        guidance_scale (float): How much the prompt influences image generation.
39        num_inference_steps (int): Number of diffusion steps used for generation.
40        strength (float): How much noise is added to the input image.
41        seed (int): Seed for generating reproducible images.
42        device (str): Device to accelerate computation (cpu, cuda, mps).
43        use_safetensors (bool): Whether to load safetensors.
44
45    Returns:
46        images (list[PIL.Image.Image]): Pillow images.
47    """
48    pipeline_args = {
49        "prompt": kwargs.get("prompt"),
50        "negative_prompt": kwargs.get("negative_prompt"),
51        "width": kwargs.get("width"),
52        "height": kwargs.get("height"),
53        "num_images_per_prompt": kwargs.get("num_images_per_prompt", 1),
54        "use_safetensors": kwargs.get("use_safetensors", True),
55    }
56
57    guidance_scale = kwargs.get("guidance_scale")
58    if guidance_scale is not None and guidance_scale >= 0:
59        pipeline_args["guidance_scale"] = guidance_scale
60
61    num_inference_steps = kwargs.get("num_inference_steps")
62    if num_inference_steps is not None and num_inference_steps >= 0:
63        pipeline_args["num_inference_steps"] = num_inference_steps
64
65    strength = kwargs.get("strength")
66    if strength is not None and strength >= 0:
67        pipeline_args["strength"] = strength
68
69    Pipeline = diffusers.AutoPipelineForText2Image
70
71    if kwargs.get("image"):
72        pipeline_args["image"] = diffusers.utils.load_image(kwargs.get("image"))
73        Pipeline = diffusers.AutoPipelineForImage2Image
74
75    if kwargs.get("mask_image"):
76        pipeline_args["mask_image"] = diffusers.utils.load_image(
77            kwargs.get("mask_image")
78        )
79        Pipeline = diffusers.AutoPipelineForInpainting
80
81    pipeline = Pipeline.from_pretrained(kwargs.get("model"))
82
83    device = kwargs.get("device")
84    if device:
85        pipeline.to(device)
86
87    seed = kwargs.get("seed")
88    if isinstance(seed, int):
89        pipeline_args["generator"] = torch.Generator(device=device).manual_seed(seed)
90
91    images = pipeline(**pipeline_args).images
92    return images
class Generate(typing.TypedDict):
 9class Generate(TypedDict):
10    model: str
11    prompt: str
12    negative_prompt: NotRequired[str]
13    image: NotRequired[str]
14    mask_image: NotRequired[str]
15    width: NotRequired[int]
16    height: NotRequired[int]
17    num_images_per_prompt: NotRequired[int]
18    guidance_scale: NotRequired[float]
19    num_inference_steps: NotRequired[int]
20    strength: NotRequired[float]
21    seed: NotRequired[int]
22    device: NotRequired[str]
23    use_safetensors: NotRequired[bool]
model: str
prompt: str
negative_prompt: NotRequired[str]
image: NotRequired[str]
mask_image: NotRequired[str]
width: NotRequired[int]
height: NotRequired[int]
num_images_per_prompt: NotRequired[int]
guidance_scale: NotRequired[float]
num_inference_steps: NotRequired[int]
strength: NotRequired[float]
seed: NotRequired[int]
device: NotRequired[str]
use_safetensors: NotRequired[bool]
def generate(**kwargs: Unpack[Generate]) -> list[PIL.Image.Image]:
26def generate(**kwargs: Unpack[Generate]) -> list[Image.Image]:
27    """
28    Generate image with diffusion model.
29
30    Args:
31        model (str): Diffusion model.
32        prompt (str): Text prompt.
33        negative_prompt (str): What to exclude from the generated image.
34        image (str): Input image path or URL.
35        mask_image (str): Mask image path or URL.
36        width (int): Generated image width in pixels.
37        height (int): Generated image height in pixels.
38        num_images_per_prompt (int): Number of images per prompt.
39        guidance_scale (float): How much the prompt influences image generation.
40        num_inference_steps (int): Number of diffusion steps used for generation.
41        strength (float): How much noise is added to the input image.
42        seed (int): Seed for generating reproducible images.
43        device (str): Device to accelerate computation (cpu, cuda, mps).
44        use_safetensors (bool): Whether to load safetensors.
45
46    Returns:
47        images (list[PIL.Image.Image]): Pillow images.
48    """
49    pipeline_args = {
50        "prompt": kwargs.get("prompt"),
51        "negative_prompt": kwargs.get("negative_prompt"),
52        "width": kwargs.get("width"),
53        "height": kwargs.get("height"),
54        "num_images_per_prompt": kwargs.get("num_images_per_prompt", 1),
55        "use_safetensors": kwargs.get("use_safetensors", True),
56    }
57
58    guidance_scale = kwargs.get("guidance_scale")
59    if guidance_scale is not None and guidance_scale >= 0:
60        pipeline_args["guidance_scale"] = guidance_scale
61
62    num_inference_steps = kwargs.get("num_inference_steps")
63    if num_inference_steps is not None and num_inference_steps >= 0:
64        pipeline_args["num_inference_steps"] = num_inference_steps
65
66    strength = kwargs.get("strength")
67    if strength is not None and strength >= 0:
68        pipeline_args["strength"] = strength
69
70    Pipeline = diffusers.AutoPipelineForText2Image
71
72    if kwargs.get("image"):
73        pipeline_args["image"] = diffusers.utils.load_image(kwargs.get("image"))
74        Pipeline = diffusers.AutoPipelineForImage2Image
75
76    if kwargs.get("mask_image"):
77        pipeline_args["mask_image"] = diffusers.utils.load_image(
78            kwargs.get("mask_image")
79        )
80        Pipeline = diffusers.AutoPipelineForInpainting
81
82    pipeline = Pipeline.from_pretrained(kwargs.get("model"))
83
84    device = kwargs.get("device")
85    if device:
86        pipeline.to(device)
87
88    seed = kwargs.get("seed")
89    if isinstance(seed, int):
90        pipeline_args["generator"] = torch.Generator(device=device).manual_seed(seed)
91
92    images = pipeline(**pipeline_args).images
93    return images

Generate image with diffusion model.

Args: model (str): Diffusion model. prompt (str): Text prompt. negative_prompt (str): What to exclude from the generated image. image (str): Input image path or URL. mask_image (str): Mask image path or URL. width (int): Generated image width in pixels. height (int): Generated image height in pixels. num_images_per_prompt (int): Number of images per prompt. guidance_scale (float): How much the prompt influences image generation. num_inference_steps (int): Number of diffusion steps used for generation. strength (float): How much noise is added to the input image. seed (int): Seed for generating reproducible images. device (str): Device to accelerate computation (cpu, cuda, mps). use_safetensors (bool): Whether to load safetensors.

Returns: images (list[PIL.Image.Image]): Pillow images.