File size: 2,274 Bytes
811d1c6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#!/usr/bin/env python3
from diffusers import AutoPipelineForInpainting, AutoPipelineForImage2Image
from diffusers.utils import load_image
import torch
from pathlib import Path
import os
from huggingface_hub import HfApi
torch.backends.cuda.matmul.allow_tf32 = True
torch_device = "cuda" if torch.cuda.is_available() else "cpu"
api = HfApi()
pipe = AutoPipelineForInpainting.from_pretrained("runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
pipe = pipe.to(torch_device)
pipe.enable_xformers_memory_efficient_attention()
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
image = load_image(img_url)
mask_image = load_image(mask_url)
prompt = "dslr photography of an empty bench, high quality"
generator = torch.Generator(device="cuda").manual_seed(0)
image = pipe(
prompt=prompt,
image=image,
mask_image=mask_image,
guidance_scale=8.0,
num_inference_steps=20,
generator=generator,
).images[0]
image = image.resize((1024, 1024))
pipe = AutoPipelineForInpainting.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
pipe.to("cuda")
pipe.enable_xformers_memory_efficient_attention()
image = pipe(
prompt=prompt,
image=image,
mask_image=mask_image,
guidance_scale=8.0,
num_inference_steps=100,
strength=0.2,
generator=generator,
).images[0]
pipe = AutoPipelineForImage2Image.from_pipe(pipe)
pipe.enable_xformers_memory_efficient_attention()
image = pipe(
prompt=prompt,
image=image,
guidance_scale=8.0,
num_inference_steps=100,
strength=0.2,
generator=generator,
).images[0]
file_name = f"aaa"
path = os.path.join(Path.home(), "images", "ediffi_sdxl", f"{file_name}.png")
image.save(path)
api.upload_file(
path_or_fileobj=path,
path_in_repo=path.split("/")[-1],
repo_id="patrickvonplaten/images",
repo_type="dataset",
)
print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")
|