Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,98 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# Load the
|
| 14 |
-
unet =
|
| 15 |
-
|
| 16 |
subfolder="unet",
|
| 17 |
-
torch_dtype=
|
| 18 |
)
|
| 19 |
-
unet.add_extra_conditions(["canny"])
|
| 20 |
|
| 21 |
-
# Add
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Load the ControlLoRA weights
|
| 25 |
pipe.load_lora_weights(
|
| 26 |
-
"models",
|
| 27 |
-
weight_name="40kHalf.safetensors"
|
| 28 |
-
adapter_name="control_lora"
|
| 29 |
)
|
| 30 |
|
| 31 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
with torch.no_grad():
|
| 33 |
-
# Get canny edges (you'll need to implement this)
|
| 34 |
-
# For now, let's assume the input image processing is handled separately
|
| 35 |
-
|
| 36 |
image = pipe(
|
| 37 |
prompt=prompt,
|
| 38 |
negative_prompt=negative_prompt,
|
| 39 |
num_inference_steps=steps,
|
| 40 |
guidance_scale=guidance_scale,
|
|
|
|
| 41 |
).images[0]
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
# Create the Gradio interface
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
with gr.Row():
|
| 47 |
with gr.Column():
|
|
|
|
| 48 |
prompt = gr.Textbox(label="Prompt")
|
| 49 |
negative_prompt = gr.Textbox(label="Negative Prompt")
|
|
|
|
|
|
|
|
|
|
| 50 |
guidance_scale = gr.Slider(minimum=1, maximum=20, value=7.5, label="Guidance Scale")
|
| 51 |
steps = gr.Slider(minimum=1, maximum=100, value=50, label="Steps")
|
| 52 |
generate = gr.Button("Generate")
|
| 53 |
|
| 54 |
with gr.Column():
|
|
|
|
| 55 |
result = gr.Image(label="Generated Image")
|
| 56 |
|
| 57 |
generate.click(
|
| 58 |
fn=generate_image,
|
| 59 |
-
inputs=[
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
from diffusers import StableDiffusionPipeline
|
| 6 |
+
from model import UNet2DConditionModelEx, StableDiffusionControlLoraV3Pipeline
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import os
|
| 9 |
+
from huggingface_hub import login
|
| 10 |
+
|
| 11 |
+
# Login using the token
|
| 12 |
+
login(token=os.environ.get("HF_TOKEN"))
|
| 13 |
+
|
| 14 |
+
# Initialize the models
|
| 15 |
+
base_model = "runwayml/stable-diffusion-v1-5"
|
| 16 |
+
dtype = torch.float32
|
| 17 |
|
| 18 |
+
# Load the custom UNet
|
| 19 |
+
unet = UNet2DConditionModelEx.from_pretrained(
|
| 20 |
+
base_model,
|
| 21 |
subfolder="unet",
|
| 22 |
+
torch_dtype=dtype
|
| 23 |
)
|
|
|
|
| 24 |
|
| 25 |
+
# Add conditioning
|
| 26 |
+
unet = unet.add_extra_conditions("ow-gbi-control-lora")
|
| 27 |
+
|
| 28 |
+
# Create the pipeline with custom UNet
|
| 29 |
+
pipe = StableDiffusionControlLoraV3Pipeline.from_pretrained(
|
| 30 |
+
base_model,
|
| 31 |
+
unet=unet,
|
| 32 |
+
torch_dtype=dtype
|
| 33 |
+
)
|
| 34 |
|
| 35 |
# Load the ControlLoRA weights
|
| 36 |
pipe.load_lora_weights(
|
| 37 |
+
"models",
|
| 38 |
+
weight_name="40kHalf.safetensors"
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
+
def get_canny_image(image, low_threshold=100, high_threshold=200):
|
| 42 |
+
if isinstance(image, Image.Image):
|
| 43 |
+
image = np.array(image)
|
| 44 |
+
|
| 45 |
+
if image.shape[2] == 4:
|
| 46 |
+
image = image[..., :3]
|
| 47 |
+
|
| 48 |
+
canny_image = cv2.Canny(image, low_threshold, high_threshold)
|
| 49 |
+
canny_image = np.stack([canny_image] * 3, axis=-1)
|
| 50 |
+
return Image.fromarray(canny_image)
|
| 51 |
+
|
| 52 |
+
def generate_image(input_image, prompt, negative_prompt, guidance_scale, steps, low_threshold, high_threshold):
|
| 53 |
+
canny_image = get_canny_image(input_image, low_threshold, high_threshold)
|
| 54 |
+
|
| 55 |
with torch.no_grad():
|
|
|
|
|
|
|
|
|
|
| 56 |
image = pipe(
|
| 57 |
prompt=prompt,
|
| 58 |
negative_prompt=negative_prompt,
|
| 59 |
num_inference_steps=steps,
|
| 60 |
guidance_scale=guidance_scale,
|
| 61 |
+
image=canny_image
|
| 62 |
).images[0]
|
| 63 |
+
|
| 64 |
+
return canny_image, image
|
| 65 |
|
| 66 |
# Create the Gradio interface
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
with gr.Row():
|
| 69 |
with gr.Column():
|
| 70 |
+
input_image = gr.Image(label="Input Image", type="numpy")
|
| 71 |
prompt = gr.Textbox(label="Prompt")
|
| 72 |
negative_prompt = gr.Textbox(label="Negative Prompt")
|
| 73 |
+
with gr.Row():
|
| 74 |
+
low_threshold = gr.Slider(minimum=1, maximum=255, value=100, label="Canny Low Threshold")
|
| 75 |
+
high_threshold = gr.Slider(minimum=1, maximum=255, value=200, label="Canny High Threshold")
|
| 76 |
guidance_scale = gr.Slider(minimum=1, maximum=20, value=7.5, label="Guidance Scale")
|
| 77 |
steps = gr.Slider(minimum=1, maximum=100, value=50, label="Steps")
|
| 78 |
generate = gr.Button("Generate")
|
| 79 |
|
| 80 |
with gr.Column():
|
| 81 |
+
canny_output = gr.Image(label="Canny Edge Detection")
|
| 82 |
result = gr.Image(label="Generated Image")
|
| 83 |
|
| 84 |
generate.click(
|
| 85 |
fn=generate_image,
|
| 86 |
+
inputs=[
|
| 87 |
+
input_image,
|
| 88 |
+
prompt,
|
| 89 |
+
negative_prompt,
|
| 90 |
+
guidance_scale,
|
| 91 |
+
steps,
|
| 92 |
+
low_threshold,
|
| 93 |
+
high_threshold
|
| 94 |
+
],
|
| 95 |
+
outputs=[canny_output, result]
|
| 96 |
)
|
| 97 |
|
| 98 |
demo.launch()
|