Spaces:
Running on Zero
Running on Zero
Build prompt-free diffusion drawing canvas
Browse files
README.md
CHANGED
|
@@ -8,6 +8,7 @@ sdk_version: 5.49.1
|
|
| 8 |
app_file: app.py
|
| 9 |
python_version: '3.12'
|
| 10 |
pinned: false
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
# Automatic Drawing
|
|
@@ -38,4 +39,16 @@ prevent an old inference result from overwriting a paused or reset session.
|
|
| 38 |
checks deployed streaming, 13 cycles, renewal, resume, exports, and privacy.
|
| 39 |
`deploy_space.py` enforces private visibility and uploads an explicit allowlist.
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
python_version: '3.12'
|
| 10 |
pinned: false
|
| 11 |
+
hf_oauth: true
|
| 12 |
---
|
| 13 |
|
| 14 |
# Automatic Drawing
|
|
|
|
| 39 |
checks deployed streaming, 13 cycles, renewal, resume, exports, and privacy.
|
| 40 |
`deploy_space.py` enforces private visibility and uploads an explicit allowlist.
|
| 41 |
|
| 42 |
+
The built-in Hugging Face sign-in button requests basic profile identity only.
|
| 43 |
+
ZeroGPU quota is attributed by Hugging Face to the authenticated visitor, not a
|
| 44 |
+
shared owner API key. Sign-in does not override private Space access restrictions.
|
| 45 |
+
The Space does not use your computer's GPU.
|
| 46 |
+
|
| 47 |
+
Two compatible models are preloaded at startup: SD-Turbo and SDXL-Turbo. Each
|
| 48 |
+
session selects its own model. Switching pauses and resets that session's canvas;
|
| 49 |
+
it does not change another visitor's selection. Both use an empty prompt, four
|
| 50 |
+
configured steps, and the same noise visualization. Arbitrary model repositories
|
| 51 |
+
are not supported because schedulers and pipeline architectures differ.
|
| 52 |
+
|
| 53 |
+
Models: https://huggingface.co/stabilityai/sd-turbo and
|
| 54 |
+
https://huggingface.co/stabilityai/sdxl-turbo
|
app.py
CHANGED
|
@@ -9,8 +9,8 @@ from pathlib import Path
|
|
| 9 |
|
| 10 |
import gradio as gr
|
| 11 |
import torch
|
| 12 |
-
from diffusers import AutoPipelineForImage2Image, AutoPipelineForText2Image
|
| 13 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
class CaptureStart:
|
|
@@ -30,11 +30,26 @@ class DrawingPipeline(CaptureStart, StableDiffusionPipeline):
|
|
| 30 |
class DrawingFeedbackPipeline(CaptureStart, StableDiffusionImg2ImgPipeline):
|
| 31 |
pass
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def decode_preview(pipeline, latents):
|
|
@@ -45,8 +60,11 @@ def decode_preview(pipeline, latents):
|
|
| 45 |
return pipeline.image_processor.postprocess(pixels, output_type="pil")[0]
|
| 46 |
|
| 47 |
|
| 48 |
-
@spaces.GPU(duration=
|
| 49 |
-
def evolve(state, drift, renew):
|
|
|
|
|
|
|
|
|
|
| 50 |
state = dict(state or {})
|
| 51 |
seed = state.get("seed", secrets.randbelow(2**31))
|
| 52 |
image = state.get("image")
|
|
@@ -86,7 +104,7 @@ def evolve(state, drift, renew):
|
|
| 86 |
state = dict(seed=seed, image=image, frame=frame, history=history)
|
| 87 |
stages.append((image.copy(), "Completed image"))
|
| 88 |
elapsed = time.perf_counter()-started
|
| 89 |
-
return state, stages, f"Frame {frame} | seed {seed} | drift {effective_drift:g} | {elapsed:.2f}s | empty prompt"
|
| 90 |
|
| 91 |
|
| 92 |
class Session:
|
|
@@ -96,6 +114,7 @@ class Session:
|
|
| 96 |
self.revision = 0
|
| 97 |
self.wake = threading.Event()
|
| 98 |
self.lock = threading.Lock()
|
|
|
|
| 99 |
|
| 100 |
def __deepcopy__(self, memo):
|
| 101 |
return Session()
|
|
@@ -120,12 +139,20 @@ def reset(session):
|
|
| 120 |
return None, [], "Ready. Start a new drawing from random noise.", gr.Timer(active=False)
|
| 121 |
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
def animate(session, drift, pace, renew):
|
| 124 |
if not session.running or not session.lock.acquire(blocking=False):
|
| 125 |
return
|
| 126 |
revision = session.revision
|
| 127 |
try:
|
| 128 |
-
data, stages, message = evolve(session.data, drift, renew)
|
| 129 |
if revision != session.revision or not session.running:
|
| 130 |
return
|
| 131 |
session.data = data
|
|
@@ -168,6 +195,13 @@ with gr.Blocks(title="Automatic Drawing", css=CSS, theme=gr.themes.Soft()) as de
|
|
| 168 |
state = gr.State(Session())
|
| 169 |
timer = gr.Timer(.5, active=False)
|
| 170 |
gr.Markdown("# Automatic Drawing\nAn image finding its own way. No words, no instructions.", elem_id="title")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
canvas = gr.Image(label="Living canvas", interactive=False, height=640, elem_id="canvas", type="pil")
|
| 172 |
status = gr.Markdown("Ready. Start a new drawing from random noise.")
|
| 173 |
with gr.Row():
|
|
@@ -189,6 +223,8 @@ with gr.Blocks(title="Automatic Drawing", css=CSS, theme=gr.themes.Soft()) as de
|
|
| 189 |
start.click(start_session, [state], [timer, status], queue=False, api_name="start")
|
| 190 |
pause.click(pause_session, [state], [timer, status], queue=False, api_name="pause")
|
| 191 |
fresh.click(reset, [state], [canvas, gallery, status, timer], queue=False, api_name="reset")
|
|
|
|
|
|
|
| 192 |
timer.tick(animate, [state, drift, pace, renew], outputs, concurrency_limit=1,
|
| 193 |
concurrency_id="drawing", trigger_mode="once", api_name="cycle")
|
| 194 |
download.click(export, [state], [png, gif], api_name="export")
|
|
|
|
| 9 |
|
| 10 |
import gradio as gr
|
| 11 |
import torch
|
|
|
|
| 12 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
| 13 |
+
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
|
| 14 |
|
| 15 |
|
| 16 |
class CaptureStart:
|
|
|
|
| 30 |
class DrawingFeedbackPipeline(CaptureStart, StableDiffusionImg2ImgPipeline):
|
| 31 |
pass
|
| 32 |
|
| 33 |
+
|
| 34 |
+
class DrawingXLPipeline(CaptureStart, StableDiffusionXLPipeline):
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class DrawingXLFeedbackPipeline(CaptureStart, StableDiffusionXLImg2ImgPipeline):
|
| 39 |
+
pass
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
MODELS = {
|
| 43 |
+
"stabilityai/sd-turbo": (DrawingPipeline, DrawingFeedbackPipeline),
|
| 44 |
+
"stabilityai/sdxl-turbo": (DrawingXLPipeline, DrawingXLFeedbackPipeline),
|
| 45 |
+
}
|
| 46 |
+
PIPELINES = {}
|
| 47 |
+
for repo, (text_cls, image_cls) in MODELS.items():
|
| 48 |
+
print(f"Preloading {repo}", flush=True)
|
| 49 |
+
text_pipe = text_cls.from_pretrained(
|
| 50 |
+
repo, torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
| 51 |
+
text_pipe.to("cuda" if os.environ.get("SPACES_ZERO_GPU") or torch.cuda.is_available() else "cpu")
|
| 52 |
+
PIPELINES[repo] = (text_pipe, image_cls(**text_pipe.components))
|
| 53 |
|
| 54 |
|
| 55 |
def decode_preview(pipeline, latents):
|
|
|
|
| 60 |
return pipeline.image_processor.postprocess(pixels, output_type="pil")[0]
|
| 61 |
|
| 62 |
|
| 63 |
+
@spaces.GPU(duration=30)
|
| 64 |
+
def evolve(state, drift, renew, model):
|
| 65 |
+
if model not in PIPELINES:
|
| 66 |
+
raise gr.Error("Choose a supported model.")
|
| 67 |
+
pipe, feedback = PIPELINES[model]
|
| 68 |
state = dict(state or {})
|
| 69 |
seed = state.get("seed", secrets.randbelow(2**31))
|
| 70 |
image = state.get("image")
|
|
|
|
| 104 |
state = dict(seed=seed, image=image, frame=frame, history=history)
|
| 105 |
stages.append((image.copy(), "Completed image"))
|
| 106 |
elapsed = time.perf_counter()-started
|
| 107 |
+
return state, stages, f"{model.split('/')[-1]} | Frame {frame} | seed {seed} | drift {effective_drift:g} | {elapsed:.2f}s | empty prompt"
|
| 108 |
|
| 109 |
|
| 110 |
class Session:
|
|
|
|
| 114 |
self.revision = 0
|
| 115 |
self.wake = threading.Event()
|
| 116 |
self.lock = threading.Lock()
|
| 117 |
+
self.model = "stabilityai/sd-turbo"
|
| 118 |
|
| 119 |
def __deepcopy__(self, memo):
|
| 120 |
return Session()
|
|
|
|
| 139 |
return None, [], "Ready. Start a new drawing from random noise.", gr.Timer(active=False)
|
| 140 |
|
| 141 |
|
| 142 |
+
def select_model(session, model):
|
| 143 |
+
if model not in MODELS:
|
| 144 |
+
raise gr.Error("Choose a supported model.")
|
| 145 |
+
result = reset(session)
|
| 146 |
+
session.model = model
|
| 147 |
+
return result
|
| 148 |
+
|
| 149 |
+
|
| 150 |
def animate(session, drift, pace, renew):
|
| 151 |
if not session.running or not session.lock.acquire(blocking=False):
|
| 152 |
return
|
| 153 |
revision = session.revision
|
| 154 |
try:
|
| 155 |
+
data, stages, message = evolve(session.data, drift, renew, session.model)
|
| 156 |
if revision != session.revision or not session.running:
|
| 157 |
return
|
| 158 |
session.data = data
|
|
|
|
| 195 |
state = gr.State(Session())
|
| 196 |
timer = gr.Timer(.5, active=False)
|
| 197 |
gr.Markdown("# Automatic Drawing\nAn image finding its own way. No words, no instructions.", elem_id="title")
|
| 198 |
+
with gr.Row():
|
| 199 |
+
gr.LoginButton()
|
| 200 |
+
gr.Markdown("Runs on Hugging Face ZeroGPU. Sign in to use your personal GPU quota. This Space is private; sign-in does not grant access to it.")
|
| 201 |
+
model = gr.Dropdown(choices=[("SD-Turbo · faster", "stabilityai/sd-turbo"),
|
| 202 |
+
("SDXL-Turbo · larger model", "stabilityai/sdxl-turbo")],
|
| 203 |
+
value="stabilityai/sd-turbo", label="Drawing model",
|
| 204 |
+
info="Changing model pauses and starts a new drawing. Both models use empty prompts.")
|
| 205 |
canvas = gr.Image(label="Living canvas", interactive=False, height=640, elem_id="canvas", type="pil")
|
| 206 |
status = gr.Markdown("Ready. Start a new drawing from random noise.")
|
| 207 |
with gr.Row():
|
|
|
|
| 223 |
start.click(start_session, [state], [timer, status], queue=False, api_name="start")
|
| 224 |
pause.click(pause_session, [state], [timer, status], queue=False, api_name="pause")
|
| 225 |
fresh.click(reset, [state], [canvas, gallery, status, timer], queue=False, api_name="reset")
|
| 226 |
+
model.change(select_model, [state, model], [canvas, gallery, status, timer],
|
| 227 |
+
queue=False, api_name="select_model")
|
| 228 |
timer.tick(animate, [state, drift, pace, renew], outputs, concurrency_limit=1,
|
| 229 |
concurrency_id="drawing", trigger_mode="once", api_name="cycle")
|
| 230 |
download.click(export, [state], [png, gif], api_name="export")
|