control_v11p_sd15s2_lineart_anime / control_net_lineart_anime.py
patrickvonplaten's picture
Duplicate from ControlNet-1-1-preview/control_v11p_sd15s2_lineart_anime
4d25a21
raw
history blame
1.68 kB
#!/usr/bin/env python3
import torch
import os
from huggingface_hub import HfApi
from pathlib import Path
from diffusers.utils import load_image
from controlnet_aux import LineartAnimeDetector
from transformers import CLIPTextModel
from diffusers import (
ControlNetModel,
StableDiffusionControlNetPipeline,
UniPCMultistepScheduler,
)
import sys
checkpoint = sys.argv[1]
url = "https://static.wikia.nocookie.net/unanything/images/a/a0/Unnamed.png/revision/latest/scale-to-width-down/350?cb=20230326002111"
image = load_image(url)
prompt = "warrior girl"
processor = LineartAnimeDetector.from_pretrained("lllyasviel/Annotators")
image = processor(image)
image.save("/home/patrick/images/check.png")
text_encoder = CLIPTextModel.from_pretrained("Linaqruf/anything-v3.0", subfolder="text_encoder", num_hidden_layers=11, torch_dtype=torch.float16)
controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, text_encoder=text_encoder, torch_dtype=torch.float16
)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
generator = torch.manual_seed(33)
out_image = pipe(prompt, num_inference_steps=25, generator=generator, image=image).images[0]
path = os.path.join(Path.home(), "images", "aa.png")
out_image.save(path)
api = HfApi()
api.upload_file(
path_or_fileobj=path,
path_in_repo=path.split("/")[-1],
repo_id="patrickvonplaten/images",
repo_type="dataset",
)
print("https://huggingface.co/datasets/patrickvonplaten/images/blob/main/aa.png")