|
|
|
|
|
import torch |
|
from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler |
|
from PIL import Image |
|
import random |
|
|
|
|
|
ckpt_path = "nyaflow-xl-alpha.safetensors" |
|
ckpt_path = "noobaiXLNAIXL_vPred10Version.safetensors" |
|
pipe = StableDiffusionXLPipeline.from_single_file( |
|
ckpt_path, |
|
use_safetensors=True, |
|
torch_dtype=torch.float16, |
|
) |
|
scheduler_args = {"prediction_type": "v_prediction", "rescale_betas_zero_snr": True} |
|
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, **scheduler_args) |
|
pipe.enable_xformers_memory_efficient_attention() |
|
pipe = pipe.to("cuda") |
|
|
|
|
|
PRESET_Q = "year_2022, best quality, high quality, very aesthetic" |
|
NEGATIVE_PROMPT = "lowres, worst quality, displeasing, bad anatomy, text, error, extra digit, cropped, error, fewer, extra, missing, worst quality, jpeg artifacts, censored, ai-generated worst quality displeasing, bad quality" |
|
|
|
def generate_image( |
|
prompt: str, |
|
preset: str = PRESET_Q, |
|
height: int = 1216, |
|
width: int = 832, |
|
negative_prompt: str = NEGATIVE_PROMPT, |
|
guidance_scale: float = 4.0, |
|
randomize_seed: bool = True, |
|
seed: int = 42, |
|
inference_steps: int = 25, |
|
) -> Image: |
|
|
|
prompt = prompt.strip() + ", " + preset.strip() |
|
negative_prompt = negative_prompt.strip() if negative_prompt and negative_prompt.strip() else None |
|
|
|
|
|
if randomize_seed: |
|
seed = random.randint(0, 9007199254740991) |
|
|
|
|
|
generator = torch.Generator(device="cuda").manual_seed(seed) |
|
|
|
|
|
if inference_steps > 50: |
|
inference_steps = 50 |
|
|
|
|
|
image = pipe( |
|
prompt, |
|
height=height, |
|
width=width, |
|
negative_prompt=negative_prompt, |
|
guidance_scale=guidance_scale, |
|
generator=generator, |
|
num_inference_steps=inference_steps |
|
).images[0] |
|
|
|
return image |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
prompt = "zhongli" |
|
image = generate_image(prompt) |
|
image |
|
|
|
prompt = "Neuvillette" |
|
image = generate_image(prompt) |
|
image |
|
|