|
import gradio as gr |
|
import openai |
|
from dotenv import load_dotenv |
|
import os |
|
|
|
import torch |
|
|
|
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline, UniPCMultistepScheduler |
|
import cv2 |
|
from PIL import Image |
|
import numpy as np |
|
from diffusers.utils import load_image |
|
|
|
|
|
image = load_image( |
|
"https://hf.co/datasets/huggingface/documentation-images/resolve/main/diffusers/input_image_vermeer.png" |
|
) |
|
image = np.array(image) |
|
|
|
low_threshold = 100 |
|
high_threshold = 200 |
|
|
|
image = cv2.Canny(image, low_threshold, high_threshold) |
|
image = image[:, :, None] |
|
image = np.concatenate([image, image, image], axis=2) |
|
canny_image = Image.fromarray(image) |
|
|
|
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-scribble", torch_dtype=torch.float16) |
|
|
|
pipe = StableDiffusionControlNetPipeline.from_pretrained( |
|
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16 |
|
) |
|
|
|
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config) |
|
|
|
|
|
pipe.enable_model_cpu_offload() |
|
|
|
|
|
|
|
|
|
n_steps = 25 |
|
|
|
generator = torch.manual_seed(0) |
|
|
|
|
|
|
|
|
|
|
|
def predict(prompt,negative_prompt): |
|
|
|
image = pipe( |
|
prompt=prompt, |
|
negative_prompt=negative_prompt, |
|
num_inference_steps=n_steps, |
|
generator=generator, |
|
image=canny_image |
|
|
|
|
|
|
|
).images[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
return image |
|
|
|
|
|
demo = gr.Interface(fn=predict, inputs=[gr.Textbox(value="prompt"), gr.Textbox(value="negative prompt")], outputs="image") |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|