Spaces:
Runtime error
Runtime error
File size: 2,110 Bytes
fd265dd 896e313 242d484 896e313 fd265dd 8b9def7 fd265dd 928539d fd265dd 5a8c12c 42522ad 928539d c76b17a c256949 fd265dd 928539d fd265dd e54acd2 fd265dd 5a8c12c fd265dd |
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 |
from PIL import Image
import gradio as gr
import numpy as np
import PIL.Image
import random
import cv2
import torch
from accelerate import Accelerator
from transformers import pipeline
from diffusers.utils import load_image
from diffusers import KandinskyV22PriorPipeline, KandinskyV22ControlnetPipeline
accelerator = Accelerator()
depth_estimator = accelerator.prepare(pipeline("depth-estimation",model="vinvino02/glpn-nyu"))
depth_estimator.to("cpu")
pipe_prior = accelerator.prepare(KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float32))
pipe_prior.to("cpu")
pipe = accelerator.prepare(KandinskyV22ControlnetPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float32))
pipe.to("cpu")
##pipe.unet.to(memory_format=torch.channels_last)
generator = torch.Generator(device="cpu").manual_seed(4096)
def make_hint(image):
image = depth_estimator(image)["depth"]
image = np.array(image)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
detected_map = torch.from_numpy(image).float() / 255.0
hint = detected_map.permute(2, 0, 1)
return hint
def plex(prompt,goof):
goof = load_image(goof)
goof = goof.convert("RGB")
goof.thumbnail((512, 512))
hint = make_hint(goof).unsqueeze(0).to("cpu")
negative_prior_prompt = "lowres,text,bad quality,jpeg artifacts,ugly,bad face,extra fingers,blurry,bad anatomy,extra limbs,fused fingers,long neck,watermark,signature"
image_emb, zero_image_emb = pipe_prior(prompt=prompt, negative_prompt=negative_prior_prompt, generator=generator).to_tuple()
images = pipe(
image_embeds=image_emb,
negative_image_embeds=zero_image_emb,
hint=hint,
num_inference_steps=10,
generator=generator,
height=512,
width=512,
).images[0]
return images
iface = gr.Interface(fn=plex,inputs=[gr.Textbox(),gr.Image(type="filepath")], outputs=gr.Image(), title="Img2Img_SkyV22CntrlNet_CPU", description="Running on CPU, very slow!")
iface.launch() |