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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from settings import (
|
| 6 |
+
DEFAULT_IMAGE_RESOLUTION,
|
| 7 |
+
DEFAULT_NUM_IMAGES,
|
| 8 |
+
MAX_IMAGE_RESOLUTION,
|
| 9 |
+
MAX_NUM_IMAGES,
|
| 10 |
+
MAX_SEED,
|
| 11 |
+
)
|
| 12 |
+
from utils import randomize_seed_fn
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def create_demo(process):
|
| 16 |
+
with gr.Blocks() as demo:
|
| 17 |
+
gr.Markdown("## BRIA 2.2 ControlNet Canny")
|
| 18 |
+
gr.HTML('''
|
| 19 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
| 20 |
+
This is a demo for ControlNet Canny that using
|
| 21 |
+
<a href="https://huggingface.co/briaai/BRIA-2.2" target="_blank">BRIA 2.2 text-to-image model</a> as backbone.
|
| 22 |
+
Trained on licensed data, BRIA 2.2 provide full legal liability coverage for copyright and privacy infringement.
|
| 23 |
+
</p>
|
| 24 |
+
''')
|
| 25 |
+
with gr.Row():
|
| 26 |
+
with gr.Column():
|
| 27 |
+
input_image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
|
| 28 |
+
prompt = gr.Textbox(label="Prompt")
|
| 29 |
+
negative_prompt = gr.Textbox(label="Negative prompt", value="Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers")
|
| 30 |
+
num_steps = gr.Slider(label="Number of steps", minimum=25, maximum=100, value=50, step=1)
|
| 31 |
+
controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
|
| 32 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True,)
|
| 33 |
+
run_button = gr.Button(value="Run")
|
| 34 |
+
with gr.Column():
|
| 35 |
+
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[2], height='auto')
|
| 36 |
+
ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
|
| 37 |
+
|
| 38 |
+
run_button.click(
|
| 39 |
+
fn=process,
|
| 40 |
+
inputs=inputs,
|
| 41 |
+
outputs=result,
|
| 42 |
+
api_name="canny",
|
| 43 |
+
)
|
| 44 |
+
return demo
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
from model import Model
|
| 49 |
+
|
| 50 |
+
model = Model(task_name="Canny")
|
| 51 |
+
demo = create_demo(model.process_canny)
|
| 52 |
+
demo.queue().launch()
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
-----------------------------------------------------------------------
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL, EulerAncestralDiscreteScheduler
|
| 66 |
+
# from diffusers.utils import load_image
|
| 67 |
+
# from PIL import Image
|
| 68 |
+
# import torch
|
| 69 |
+
# import numpy as np
|
| 70 |
+
# import cv2
|
| 71 |
+
# import gradio as gr
|
| 72 |
+
# from torchvision import transforms
|
| 73 |
+
|
| 74 |
+
# controlnet = ControlNetModel.from_pretrained(
|
| 75 |
+
# "briaai/BRIA-2.2-ControlNet-Canny",
|
| 76 |
+
# torch_dtype=torch.float16
|
| 77 |
+
# ).to('cuda')
|
| 78 |
+
|
| 79 |
+
# pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 80 |
+
# "briaai/BRIA-2.2",
|
| 81 |
+
# controlnet=controlnet,
|
| 82 |
+
# torch_dtype=torch.float16,
|
| 83 |
+
# device_map='auto',
|
| 84 |
+
# low_cpu_mem_usage=True,
|
| 85 |
+
# offload_state_dict=True,
|
| 86 |
+
# ).to('cuda')
|
| 87 |
+
# pipe.scheduler = EulerAncestralDiscreteScheduler(
|
| 88 |
+
# beta_start=0.00085,
|
| 89 |
+
# beta_end=0.012,
|
| 90 |
+
# beta_schedule="scaled_linear",
|
| 91 |
+
# num_train_timesteps=1000,
|
| 92 |
+
# steps_offset=1
|
| 93 |
+
# )
|
| 94 |
+
# # pipe.enable_freeu(b1=1.1, b2=1.1, s1=0.5, s2=0.7)
|
| 95 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
| 96 |
+
# pipe.force_zeros_for_empty_prompt = False
|
| 97 |
+
|
| 98 |
+
# low_threshold = 100
|
| 99 |
+
# high_threshold = 200
|
| 100 |
+
|
| 101 |
+
# def resize_image(image):
|
| 102 |
+
# image = image.convert('RGB')
|
| 103 |
+
# current_size = image.size
|
| 104 |
+
# if current_size[0] > current_size[1]:
|
| 105 |
+
# center_cropped_image = transforms.functional.center_crop(image, (current_size[1], current_size[1]))
|
| 106 |
+
# else:
|
| 107 |
+
# center_cropped_image = transforms.functional.center_crop(image, (current_size[0], current_size[0]))
|
| 108 |
+
# resized_image = transforms.functional.resize(center_cropped_image, (1024, 1024))
|
| 109 |
+
# return resized_image
|
| 110 |
+
|
| 111 |
+
# def get_canny_filter(image):
|
| 112 |
+
|
| 113 |
+
# if not isinstance(image, np.ndarray):
|
| 114 |
+
# image = np.array(image)
|
| 115 |
+
|
| 116 |
+
# image = cv2.Canny(image, low_threshold, high_threshold)
|
| 117 |
+
# image = image[:, :, None]
|
| 118 |
+
# image = np.concatenate([image, image, image], axis=2)
|
| 119 |
+
# canny_image = Image.fromarray(image)
|
| 120 |
+
# return canny_image
|
| 121 |
+
|
| 122 |
+
# def process(input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed):
|
| 123 |
+
# generator = torch.manual_seed(seed)
|
| 124 |
+
|
| 125 |
+
# # resize input_image to 1024x1024
|
| 126 |
+
# input_image = resize_image(input_image)
|
| 127 |
+
|
| 128 |
+
# canny_image = get_canny_filter(input_image)
|
| 129 |
+
|
| 130 |
+
# images = pipe(
|
| 131 |
+
# prompt, negative_prompt=negative_prompt, image=canny_image, num_inference_steps=num_steps, controlnet_conditioning_scale=float(controlnet_conditioning_scale),
|
| 132 |
+
# generator=generator,
|
| 133 |
+
# ).images
|
| 134 |
+
|
| 135 |
+
# return [canny_image,images[0]]
|
| 136 |
+
|
| 137 |
+
# block = gr.Blocks().queue()
|
| 138 |
+
|
| 139 |
+
# with block:
|
| 140 |
+
# gr.Markdown("## BRIA 2.2 ControlNet Canny")
|
| 141 |
+
# gr.HTML('''
|
| 142 |
+
# <p style="margin-bottom: 10px; font-size: 94%">
|
| 143 |
+
# This is a demo for ControlNet Canny that using
|
| 144 |
+
# <a href="https://huggingface.co/briaai/BRIA-2.2" target="_blank">BRIA 2.2 text-to-image model</a> as backbone.
|
| 145 |
+
# Trained on licensed data, BRIA 2.2 provide full legal liability coverage for copyright and privacy infringement.
|
| 146 |
+
# </p>
|
| 147 |
+
# ''')
|
| 148 |
+
# with gr.Row():
|
| 149 |
+
# with gr.Column():
|
| 150 |
+
# input_image = gr.Image(sources=None, type="pil") # None for upload, ctrl+v and webcam
|
| 151 |
+
# prompt = gr.Textbox(label="Prompt")
|
| 152 |
+
# negative_prompt = gr.Textbox(label="Negative prompt", value="Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers")
|
| 153 |
+
# num_steps = gr.Slider(label="Number of steps", minimum=25, maximum=100, value=50, step=1)
|
| 154 |
+
# controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
|
| 155 |
+
# seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True,)
|
| 156 |
+
# run_button = gr.Button(value="Run")
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
# with gr.Column():
|
| 160 |
+
# result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[2], height='auto')
|
| 161 |
+
# ips = [input_image, prompt, negative_prompt, num_steps, controlnet_conditioning_scale, seed]
|
| 162 |
+
# run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
|
| 163 |
+
|
| 164 |
+
# block.launch(debug = True)
|