imgen-diffusers/flux-schnell-local.py

53 lines
1.5 KiB
Python
Raw Normal View History

2024-08-11 13:05:14 -06:00
from diffusers import FluxPipeline
import torch
from pathlib import Path
import re
from datetime import datetime
def slugify(text):
# remove non-word characters and foreign characters
text = re.sub(r"[^\w\s]", "", text)
text = re.sub(r"\s+", "-", text)
return text
prompt = "dirt highway road running through verdant grassy plains sparsely dotted with very broad tall trees with a mountain range and forest far in the distance"
2024-08-11 13:05:14 -06:00
height, width = 720, 1280
ckpt_id = "./FLUX.1-schnell"
DIR_NAME="/home/tonydero/remdirs/immich/"
dirpath = Path(DIR_NAME)
# create parent dir if doesn't exist
# dirpath.mkdir(parents=True, exist_ok=True)
# denoising
pipe = FluxPipeline.from_pretrained(
ckpt_id,
torch_dtype=torch.bfloat16,
use_safetensors=True,
)
pipe.vae.enable_tiling()
pipe.vae.enable_slicing()
pipe.enable_sequential_cpu_offload() # offloads modules to CPU on a submodule level (rather than model level)
output = pipe(
prompt,
height=height,
width=width,
num_images_per_prompt=8,
num_inference_steps=4,
max_sequence_length=128,
guidance_scale=0.0,
)
# print('Max mem allocated (GB) while denoising:', torch.cuda.max_memory_allocated() / (1024 ** 3))
# import matplotlib.pyplot as plt
# plt.imshow(image)
# image.save("./whitehenge.png")
# plt.show()
for idx, image in enumerate(output.images):
timestamp = datetime.now().strftime("%Y%m%d%-H%M%S")
image_name = f'{slugify(prompt[:64])}-{idx}-{timestamp}.png'
image_path = dirpath / image_name
image.save(image_path)