Spaces:
Runtime error
Runtime error
import gradio as gr | |
import jax | |
import numpy as np | |
import jax.numpy as jnp | |
from flax.jax_utils import replicate | |
from flax.training.common_utils import shard | |
from PIL import Image | |
from diffusers import FlaxStableDiffusionControlNetPipeline, FlaxControlNetModel | |
import cv2 | |
def create_key(seed=0): | |
return jax.random.PRNGKey(seed) | |
def canny_filter(image): | |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0) | |
edges_image = cv2.Canny(blurred_image, 50, 200) | |
return edges_image | |
# load control net and stable diffusion v1-5 | |
controlnet, controlnet_params = FlaxControlNetModel.from_pretrained( | |
"tsungtao/controlnet-mlsd-202305011046", from_flax=True, dtype=jnp.bfloat16 | |
) | |
#controlnet.save_pretrained("tsungtao/controlnet-mlsd-202305011046",params=controlnet_params) | |
pipe, params = FlaxStableDiffusionControlNetPipeline.from_pretrained( | |
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, revision="flax", dtype=jnp.bfloat16 | |
) | |
def infer(prompts, negative_prompts, image): | |
params["controlnet"] = controlnet_params | |
num_samples = 1 #jax.device_count() | |
rng = create_key(0) | |
rng = jax.random.split(rng, jax.device_count()) | |
im = canny_filter(image) | |
canny_image = Image.fromarray(im) | |
prompt_ids = pipe.prepare_text_inputs([prompts] * num_samples) | |
negative_prompt_ids = pipe.prepare_text_inputs([negative_prompts] * num_samples) | |
processed_image = pipe.prepare_image_inputs([canny_image] * num_samples) | |
p_params = replicate(params) | |
prompt_ids = shard(prompt_ids) | |
negative_prompt_ids = shard(negative_prompt_ids) | |
processed_image = shard(processed_image) | |
output = pipe( | |
prompt_ids=prompt_ids, | |
image=processed_image, | |
params=p_params, | |
prng_seed=rng, | |
num_inference_steps=50, | |
neg_prompt_ids=negative_prompt_ids, | |
jit=True, | |
).images | |
output_images = pipe.numpy_to_pil(np.asarray(output.reshape((num_samples,) + output.shape[-3:]))) | |
return output_images | |
title = "ControlNet MLSD" | |
description = "This is a demo on ControlNet MLSD." | |
examples = [["living room with TV", "fan", "image_01.jpg"], | |
["a living room with hardwood floors and a flat screen tv", "sea", "image_02.jpg"], | |
["a living room with a fireplace and a view of the ocean", "pendant", "image_03.jpg"] | |
] | |
with gr.Blocks() as demo: | |
gr.Interface(infer, inputs=["text", "text", "image"], outputs="gallery", title = title, description = description, examples = examples, theme='gradio/soft') | |
gr.Markdown( | |
""" | |
* * * | |
* [Dataset](https://huggingface.co/datasets/tsungtao/diffusers-testing) | |
* [Diffusers model](https://huggingface.co/runwayml/stable-diffusion-v1-5) | |
* [Training Report](https://wandb.ai/tsungtao0311/controlnet-mlsd-202305011046/runs/ezfn6bkz?workspace=user-tsungtao0311) | |
""") | |
# with gr.Accordion("Open for More!"): | |
# gr.Markdown("Team:https://huggingface.co/ellljoy, https://huggingface.co/zenkig, https://huggingface.co/aze555, https://huggingface.co/tsungtao, https://huggingface.co/Mayyu") | |
gr.Markdown("* * *") | |
# gr.Markdown(""" <img src='https://huggingface.co/spaces/tsungtao/tsungtao-controlnet-mlsd-202305011046/blob/main/test.png' /> """) | |
demo.launch() |