Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,78 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline
|
| 4 |
import spaces
|
|
|
|
|
|
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
-
# Load the
|
| 9 |
controlnet = ControlNetModel.from_pretrained(
|
| 10 |
"briaai/BRIA-2.2-ControlNet-Recoloring",
|
| 11 |
torch_dtype=torch.float16
|
| 12 |
-
)
|
|
|
|
| 13 |
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 14 |
"briaai/BRIA-2.2",
|
| 15 |
controlnet=controlnet,
|
| 16 |
torch_dtype=torch.float16,
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Function to transform the image based on a prompt
|
| 20 |
@spaces.GPU(enable_queue=True)
|
| 21 |
-
def generate_image(
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
results = pipe(prompt=prompt, negative_prompt=negative_prompt, image=recoloring_image, controlnet_conditioning_scale=1.0, height=1024, width=1024)
|
| 31 |
-
return results.images[0]
|
| 32 |
|
| 33 |
# Gradio Interface
|
| 34 |
-
description = ""
|
| 35 |
-
Anything to Anything, a workflow by Angrypenguinpng using the Bria Recolor ControlNet, check it out here: https://huggingface.co/briaai/BRIA-2.2-ControlNet-Recoloring
|
| 36 |
-
"""
|
| 37 |
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
gr.Markdown("<h1><center>Image Transformation with Bria Recolor ControlNet</center></h1>")
|
| 40 |
gr.Markdown(description)
|
| 41 |
-
with gr.
|
| 42 |
-
with gr.
|
| 43 |
-
|
| 44 |
-
prompt = gr.Textbox(label='Enter your prompt'
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
demo.queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import spaces
|
| 2 |
+
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, EulerAncestralDiscreteScheduler
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
+
# Load the models
|
| 9 |
controlnet = ControlNetModel.from_pretrained(
|
| 10 |
"briaai/BRIA-2.2-ControlNet-Recoloring",
|
| 11 |
torch_dtype=torch.float16
|
| 12 |
+
).to('cuda')
|
| 13 |
+
|
| 14 |
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 15 |
"briaai/BRIA-2.2",
|
| 16 |
controlnet=controlnet,
|
| 17 |
torch_dtype=torch.float16,
|
| 18 |
+
device_map='auto',
|
| 19 |
+
low_cpu_mem_usage=True,
|
| 20 |
+
offload_state_dict=True,
|
| 21 |
+
).to('cuda')
|
| 22 |
+
|
| 23 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler(
|
| 24 |
+
beta_start=0.00085,
|
| 25 |
+
beta_end=0.012,
|
| 26 |
+
beta_schedule="scaled_linear",
|
| 27 |
+
num_train_timesteps=1000,
|
| 28 |
+
steps_offset=1
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
pipe.force_zeros_for_empty_prompt = False
|
| 32 |
+
|
| 33 |
+
def resize_image(image):
|
| 34 |
+
image = image.convert('RGB')
|
| 35 |
+
current_size = image.size
|
| 36 |
+
transform = gr.Image(height=1024, width=1024, keep_aspect_ratio=True, source="upload", tool="editor")
|
| 37 |
+
resized_image = transform.postprocess(image)
|
| 38 |
+
return resized_image
|
| 39 |
|
|
|
|
| 40 |
@spaces.GPU(enable_queue=True)
|
| 41 |
+
def generate_image(input_image, prompt, controlnet_conditioning_scale):
|
| 42 |
+
# Always use a random seed for diversity in outputs
|
| 43 |
+
seed = np.random.randint(2147483647)
|
| 44 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
| 45 |
+
|
| 46 |
+
# Resize and prepare the image
|
| 47 |
+
input_image = resize_image(input_image)
|
| 48 |
+
grayscale_image = input_image.convert('L').convert('RGB')
|
| 49 |
|
| 50 |
+
# Generate the image with fixed 30 steps
|
| 51 |
+
images = pipe(
|
| 52 |
+
prompt=prompt,
|
| 53 |
+
image=grayscale_image,
|
| 54 |
+
num_inference_steps=30,
|
| 55 |
+
controlnet_conditioning_scale=float(controlnet_conditioning_scale),
|
| 56 |
+
generator=generator,
|
| 57 |
+
).images
|
| 58 |
|
| 59 |
+
return images[0]
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Gradio Interface
|
| 62 |
+
description = "Anything to Anything. Transform anything to anything. Allow an adjuster for controlnet scale."
|
|
|
|
|
|
|
| 63 |
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
gr.Markdown("<h1><center>Image Transformation with Bria Recolor ControlNet</center></h1>")
|
| 66 |
gr.Markdown(description)
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
input_image = gr.Image(label='Upload your image', type="pil")
|
| 70 |
+
prompt = gr.Textbox(label='Enter your prompt')
|
| 71 |
+
controlnet_conditioning_scale = gr.Slider(label="ControlNet conditioning scale", minimum=0.1, maximum=2.0, value=1.0, step=0.05)
|
| 72 |
+
submit_button = gr.Button('Transform Image')
|
| 73 |
+
with gr.Column():
|
| 74 |
+
output_image = gr.Image(label='Transformed Image')
|
| 75 |
+
|
| 76 |
+
submit_button.click(fn=generate_image, inputs=[input_image, prompt, controlnet_conditioning_scale], outputs=output_image)
|
| 77 |
|
| 78 |
demo.queue().launch()
|