dvir-bria's picture
Create app.py
95a9f0f
raw
history blame
2.03 kB
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
from diffusers.utils import load_image
from PIL import Image
import torch
import numpy as np
import cv2
import gradio as gr
controlnet_conditioning_scale = 0.5 # recommended for good generalization
controlnet = ControlNetModel.from_pretrained(
"briaai/ControlNet-Canny",
torch_dtype=torch.float16
)
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
"briaai/BRIA-2.0",
controlnet=controlnet,
vae=vae,
torch_dtype=torch.float16,
)
pipe.enable_model_cpu_offload()
low_threshold = 100
high_threshold = 200
def get_canny_filter(image):
if not isinstance(image, np.ndarray):
image = np.array(image)
image = cv2.Canny(image, low_threshold, high_threshold)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
canny_image = Image.fromarray(image)
return canny_image
def process(input_image, prompt):
canny_image = get_canny_filter(input_image)
images = pipe(
prompt,image=canny_image, controlnet_conditioning_scale=controlnet_conditioning_scale,
).images
return [canny_image,images[0]]
block = gr.Blocks().queue()
with block:
gr.Markdown("## BRIA 2.0 ControlNet Canny")
gr.HTML('''
<p style="margin-bottom: 10px; font-size: 94%">
This is a demo for BRIA 2.0 ControlNet Canny, a fully legal and safe T2I model.
</p>
''')
with gr.Row():
with gr.Column():
input_image = gr.Image(source='upload', type="numpy")
prompt = gr.Textbox(label="Prompt")
run_button = gr.Button(label="Run")
with gr.Column():
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid_cols=2, height='auto')
ips = [input_image, prompt]
run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
block.launch(debug = True)