diffusers/tools
1128
1#!/usr/bin/env python32import torch3import os4from huggingface_hub import HfApi5from pathlib import Path6from diffusers.utils import load_image7from controlnet_aux import CannyDetector8 9from diffusers import (10 ControlNetModel,11 StableDiffusionControlNetPipeline,12 UniPCMultistepScheduler,13)14import sys15 16checkpoint = sys.argv[1]17 18image = load_image(19 "https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png"20)21 22canny_detector = CannyDetector()23canny_image = canny_detector(image, low_threshold=100, high_threshold=200)24 25controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16)26pipe = StableDiffusionControlNetPipeline.from_pretrained(27 "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float1628)29 30pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)31pipe.enable_model_cpu_offload()32 33generator = torch.manual_seed(33)34out_image = pipe("a blue paradise bird in the jungle", num_inference_steps=20, generator=generator, image=canny_image).images[0]35 36path = os.path.join(Path.home(), "images", "aa.png")37out_image.save(path)38 39api = HfApi()40 41api.upload_file(42 path_or_fileobj=path,43 path_in_repo=path.split("/")[-1],44 repo_id="patrickvonplaten/images",45 repo_type="dataset",46)47print("https://huggingface.co/datasets/patrickvonplaten/images/blob/main/aa.png")48 