diffusers/tools
1128
1#!/usr/bin/env python32import torch3import os4from huggingface_hub import HfApi5from pathlib import Path6from diffusers.utils import load_image7from PIL import Image8import numpy as np9from controlnet_aux import PidiNetDetector, HEDdetector10 11from diffusers import (12 ControlNetModel,13 StableDiffusionControlNetPipeline,14 UniPCMultistepScheduler,15)16import sys17 18checkpoint = sys.argv[1]19 20image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-mlsd/resolve/main/images/room.png")21 22prompt = "royal chamber with fancy bed"23 24processor = HEDdetector.from_pretrained('lllyasviel/Annotators')25processor = PidiNetDetector.from_pretrained('lllyasviel/Annotators')26image = processor(image, safe=True)27 28controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16)29pipe = StableDiffusionControlNetPipeline.from_pretrained(30 "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float1631)32 33pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)34pipe.enable_model_cpu_offload()35 36generator = torch.manual_seed(0)37out_image = pipe(prompt, num_inference_steps=30, generator=generator, image=image).images[0]38 39path = os.path.join(Path.home(), "images", "aa.png")40out_image.save(path)41 42api = HfApi()43 44api.upload_file(45 path_or_fileobj=path,46 path_in_repo=path.split("/")[-1],47 repo_id="patrickvonplaten/images",48 repo_type="dataset",49)50print("https://huggingface.co/datasets/patrickvonplaten/images/blob/main/aa.png")51 