File size: 5,714 Bytes
e29a7a0 13e41c1 84ab83e 7ad3690 13e41c1 e14967b 097fdcb 13e41c1 097fdcb 13e41c1 097fdcb fb70850 13e41c1 fb70850 e14967b e29a7a0 13e41c1 e14967b 13e41c1 fb70850 84ab83e fb70850 13e41c1 fb70850 e29a7a0 097fdcb 13e41c1 097fdcb 13e41c1 e29a7a0 7ad3690 097fdcb e29a7a0 13e41c1 e29a7a0 097fdcb e29a7a0 097fdcb e29a7a0 13e41c1 097fdcb 13e41c1 e29a7a0 097fdcb e29a7a0 13e41c1 e29a7a0 097fdcb e29a7a0 13e41c1 097fdcb 13e41c1 e29a7a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
import gradio as gr
import torch
import numpy as np
import cv2
from diffusers import StableDiffusionPipeline, UniPCMultistepScheduler
from model import UNet2DConditionModelEx
from pipeline import StableDiffusionControlLoraV3Pipeline
from PIL import Image
import os
from huggingface_hub import login
import spaces
import random
from pathlib import Path
# Login using the token
login(token=os.environ.get("HF_TOKEN"))
# For deterministic generation
torch.manual_seed(42)
torch.backends.cudnn.deterministic = True
# Initialize the models
base_model = "runwayml/stable-diffusion-v1-5"
dtype = torch.float16
# Load the custom UNet
unet = UNet2DConditionModelEx.from_pretrained(
base_model,
subfolder="unet",
torch_dtype=dtype
)
unet = unet.add_extra_conditions("ow-gbi-control-lora")
pipe = StableDiffusionControlLoraV3Pipeline.from_pretrained(
base_model,
unet=unet,
torch_dtype=dtype
)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.load_lora_weights(
"models",
weight_name="40kHalf.safetensors"
)
def get_random_condition_image():
conditions_dir = Path("conditions")
if conditions_dir.exists():
image_files = list(conditions_dir.glob("*.[jp][pn][g]")) # matches .jpg, .png, .jpeg
if image_files:
random_image = random.choice(image_files)
return str(random_image)
return None
def get_canny_image(image, low_threshold=100, high_threshold=200):
if isinstance(image, Image.Image):
image = np.array(image)
if image.shape[2] == 4:
image = image[..., :3]
canny_image = cv2.Canny(image, low_threshold, high_threshold)
canny_image = np.stack([canny_image] * 3, axis=-1)
return Image.fromarray(canny_image)
@spaces.GPU(duration=120)
def generate_image(input_image, prompt, negative_prompt, guidance_scale, steps, low_threshold, high_threshold, seed):
if seed is not None and seed != "":
try:
generator = torch.Generator().manual_seed(int(seed))
except ValueError:
generator = torch.Generator()
else:
generator = torch.Generator()
canny_image = get_canny_image(input_image, low_threshold, high_threshold)
with torch.no_grad():
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=steps,
guidance_scale=guidance_scale,
image=canny_image,
extra_condition_scale=1.0,
generator=generator
).images[0]
return canny_image, image
def random_image_click():
image_path = get_random_condition_image()
if image_path:
return Image.open(image_path)
return None
# Example data
examples = [
[
"conditions/example1.jpg", # Replace with actual paths
"a futuristic cyberpunk city",
"blurry, bad quality",
7.5,
50,
100,
200,
42
],
[
"conditions/example2.jpg", # Replace with actual paths
"a serene mountain landscape",
"dark, gloomy",
7.0,
40,
120,
180,
123
]
]
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown(
"""
# Control LoRA v3 Demo
⚠️ Warning: This is a demo of Control LoRA v3. Please be aware that generation can take several minutes.
The model uses edge detection to guide the image generation process.
"""
)
with gr.Row():
with gr.Column():
input_image = gr.Image(label="Input Image", type="numpy")
random_image_btn = gr.Button("Load Random Reference Image")
prompt = gr.Textbox(
label="Prompt",
placeholder="Enter your prompt here... (e.g., 'a futuristic cyberpunk city')"
)
negative_prompt = gr.Textbox(
label="Negative Prompt",
placeholder="Enter things you don't want to see... (e.g., 'blurry, bad quality')"
)
with gr.Row():
low_threshold = gr.Slider(minimum=1, maximum=255, value=100, label="Canny Low Threshold")
high_threshold = gr.Slider(minimum=1, maximum=255, value=200, label="Canny High Threshold")
guidance_scale = gr.Slider(minimum=1, maximum=20, value=7.5, label="Guidance Scale")
steps = gr.Slider(minimum=1, maximum=100, value=50, label="Steps")
seed = gr.Textbox(label="Seed (empty for random)", placeholder="Enter a number for reproducible results")
generate = gr.Button("Generate")
with gr.Column():
canny_output = gr.Image(label="Canny Edge Detection")
result = gr.Image(label="Generated Image")
# Set up example gallery
gr.Examples(
examples=examples,
inputs=[
input_image,
prompt,
negative_prompt,
guidance_scale,
steps,
low_threshold,
high_threshold,
seed
],
outputs=[canny_output, result],
fn=generate_image,
cache_examples=True
)
# Handle the random image button
random_image_btn.click(
fn=random_image_click,
outputs=input_image
)
# Handle the generate button
generate.click(
fn=generate_image,
inputs=[
input_image,
prompt,
negative_prompt,
guidance_scale,
steps,
low_threshold,
high_threshold,
seed
],
outputs=[canny_output, result]
)
demo.launch() |