Spaces:
Runtime error
Runtime error
File size: 6,695 Bytes
58b2866 | 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 | """
Depth ControlNet Logo Generator
--------------------------------
- Nimmt ein hochgeladenes Logo/Referenzbild
- Berechnet daraus eine Depth Map (Midas)
- Generiert per Stable Diffusion 1.5 + ControlNet-Depth ein neues,
strukturell ähnliches Bild nach Text-Prompt
- Läuft auf HuggingFace ZeroGPU (dynamisch zugewiesene GPU pro Request)
"""
import spaces
import gradio as gr
import numpy as np
import torch
from PIL import Image
from diffusers import (
StableDiffusionControlNetPipeline,
ControlNetModel,
UniPCMultistepScheduler,
)
from transformers import pipeline as hf_pipeline
# ---------------------------------------------------------------------------
# Konfiguration
# ---------------------------------------------------------------------------
SD_MODEL_ID = "runwayml/stable-diffusion-v1-5"
CONTROLNET_ID = "lllyasviel/sd-controlnet-depth"
DEPTH_MODEL_ID = "Intel/dpt-hybrid-midas" # gleicher Preprocessor wie beim ControlNet-Training
RESOLUTION = 512 # native SD1.5 Auflösung
device = "cuda"
dtype = torch.float16
# ---------------------------------------------------------------------------
# Modelle laden (einmalig beim Space-Start)
# Bei ZeroGPU: .to("cuda") hier ist ok, tatsächliche GPU wird erst bei
# @spaces.GPU-Aufrufen zugewiesen.
# ---------------------------------------------------------------------------
print("Lade Depth-Estimator ...")
depth_estimator = hf_pipeline("depth-estimation", model=DEPTH_MODEL_ID)
print("Lade ControlNet + Stable Diffusion Pipeline ...")
controlnet = ControlNetModel.from_pretrained(CONTROLNET_ID, torch_dtype=dtype)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
SD_MODEL_ID,
controlnet=controlnet,
torch_dtype=dtype,
safety_checker=None,
)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to(device)
try:
pipe.enable_xformers_memory_efficient_attention()
except Exception:
pass
# ---------------------------------------------------------------------------
# Hilfsfunktionen
# ---------------------------------------------------------------------------
def preprocess_image(image: Image.Image, resolution: int = RESOLUTION) -> Image.Image:
"""Logo auf quadratische Zielauflösung bringen."""
image = image.convert("RGB")
image = image.resize((resolution, resolution), Image.LANCZOS)
return image
def get_depth_map(image: Image.Image) -> Image.Image:
"""Depth Map aus dem Logo berechnen (das ist das ControlNet-Kontrollbild)."""
depth = depth_estimator(image)["depth"]
depth = np.array(depth)
depth = depth[:, :, None]
depth = np.concatenate([depth, depth, depth], axis=2)
return Image.fromarray(depth)
# ---------------------------------------------------------------------------
# Haupt-Generierungsfunktion (läuft auf der ZeroGPU-Instanz)
# ---------------------------------------------------------------------------
@spaces.GPU(duration=60)
def generate(
logo_image,
prompt,
negative_prompt,
controlnet_scale,
num_steps,
guidance_scale,
seed,
progress=gr.Progress(track_tqdm=True),
):
if logo_image is None:
raise gr.Error("Bitte zuerst ein Logo-/Referenzbild hochladen.")
if not prompt or prompt.strip() == "":
raise gr.Error("Bitte einen Prompt eingeben.")
logo_image = preprocess_image(logo_image)
depth_image = get_depth_map(logo_image)
seed = int(seed)
if seed < 0:
generator = None # zufälliger Seed
else:
generator = torch.Generator(device=device).manual_seed(seed)
result = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
image=depth_image,
num_inference_steps=int(num_steps),
guidance_scale=float(guidance_scale),
controlnet_conditioning_scale=float(controlnet_scale),
generator=generator,
).images[0]
return result, depth_image
# ---------------------------------------------------------------------------
# Gradio UI
# ---------------------------------------------------------------------------
with gr.Blocks(title="Depth ControlNet Logo Generator") as demo:
gr.Markdown(
"""
# 🎨 Depth ControlNet — Logo Generator
Lade ein bestehendes Logo hoch, gib einen neuen Stil-Prompt ein.
Die **Tiefenstruktur / Silhouette** des Logos bleibt erhalten,
während Stil, Farben und Textur komplett neu generiert werden.
"""
)
with gr.Row():
with gr.Column():
logo_input = gr.Image(
label="1️⃣ Logo / Referenzbild hochladen",
type="pil",
height=300,
)
prompt = gr.Textbox(
label="2️⃣ Prompt",
placeholder="z.B. vintage japanese emblem logo, ink brush style, minimal, black and red, flat vector, white background",
lines=3,
)
negative_prompt = gr.Textbox(
label="Negative Prompt",
value="blurry, low quality, watermark, text, extra elements, photo, 3d render",
lines=2,
)
with gr.Accordion("⚙️ Erweiterte Einstellungen", open=False):
controlnet_scale = gr.Slider(
0.0, 2.0, value=1.0, step=0.05,
label="ControlNet Conditioning Scale (Struktur-Treue)",
)
num_steps = gr.Slider(10, 50, value=25, step=1, label="Inference Steps")
guidance_scale = gr.Slider(1.0, 20.0, value=7.5, step=0.5, label="Guidance Scale")
seed = gr.Slider(-1, 999999, value=-1, step=1, label="Seed (-1 = zufällig)")
run_button = gr.Button("🚀 Generieren", variant="primary")
with gr.Column():
output_image = gr.Image(label="Ergebnis", height=400)
depth_preview = gr.Image(label="Erkannte Depth Map (Kontrollbild)", height=200)
run_button.click(
fn=generate,
inputs=[
logo_input,
prompt,
negative_prompt,
controlnet_scale,
num_steps,
guidance_scale,
seed,
],
outputs=[output_image, depth_preview],
)
gr.Markdown(
"""
---
💡 **Tipps:**
- Höherer *ControlNet Conditioning Scale* = Form/Struktur des Original-Logos wird strenger befolgt.
- Niedrigerer Wert = mehr kreative Freiheit, weniger Ähnlichkeit zur Vorlage.
- Für saubere Vektor-Optik: Begriffe wie "flat vector", "clean lines", "white background" im Prompt verwenden.
"""
)
demo.queue(max_size=20)
demo.launch()
|