Spaces:
Running
on
Zero
Running
on
Zero
Tanut
commited on
Commit
·
b83d18f
1
Parent(s):
6a497cb
Test ZeroGPU
Browse files
app.py
CHANGED
|
@@ -1,30 +1,99 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import spaces
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
zero = torch.tensor([0.0])
|
| 7 |
-
print("startup device:", zero.device) #
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def greet(n: float):
|
| 11 |
-
|
| 12 |
-
print("inside greet, torch.cuda.is_available():", torch.cuda.is_available())
|
| 13 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
| 29 |
-
#
|
| 30 |
demo.launch()
|
|
|
|
| 1 |
+
import gc, random
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch, spaces
|
| 4 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
| 5 |
|
| 6 |
+
# ----- sanity: show CPU at startup (no GPU yet) -----
|
| 7 |
+
zero = torch.tensor([0.0])
|
| 8 |
+
print("startup device:", zero.device) # should print: cpu
|
| 9 |
|
| 10 |
+
# ====== SD 1.5 minimal loader (lazy) ======
|
| 11 |
+
MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 12 |
+
DTYPE = torch.float16
|
| 13 |
+
_PIPE = None
|
| 14 |
+
|
| 15 |
+
def get_pipe():
|
| 16 |
+
"""Create the pipeline on first use (safe for ZeroGPU)."""
|
| 17 |
+
global _PIPE
|
| 18 |
+
if _PIPE is None:
|
| 19 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 20 |
+
MODEL_ID,
|
| 21 |
+
torch_dtype=DTYPE,
|
| 22 |
+
safety_checker=None,
|
| 23 |
+
use_safetensors=True,
|
| 24 |
+
low_cpu_mem_usage=True,
|
| 25 |
+
)
|
| 26 |
+
# fast/stable scheduler
|
| 27 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(
|
| 28 |
+
pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="dpmsolver++"
|
| 29 |
+
)
|
| 30 |
+
# memory savers
|
| 31 |
+
pipe.enable_attention_slicing()
|
| 32 |
+
pipe.enable_vae_slicing()
|
| 33 |
+
pipe.enable_model_cpu_offload()
|
| 34 |
+
_PIPE = pipe
|
| 35 |
+
return _PIPE
|
| 36 |
+
|
| 37 |
+
def snap8(x: int) -> int:
|
| 38 |
+
x = max(256, min(1024, int(x)))
|
| 39 |
+
return x - (x % 8)
|
| 40 |
+
|
| 41 |
+
# ====== original ZeroGPU example ======
|
| 42 |
+
@spaces.GPU
|
| 43 |
def greet(n: float):
|
| 44 |
+
print("inside greet, cuda available:", torch.cuda.is_available())
|
|
|
|
| 45 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 46 |
+
t = zero.to(device) + torch.tensor([float(n)], device=device)
|
| 47 |
+
return f"Hello {t.item():.3f} Tensor (device: {t.device})"
|
| 48 |
+
|
| 49 |
+
# ====== SD 1.5 text -> image ======
|
| 50 |
+
@spaces.GPU(duration=120)
|
| 51 |
+
def generate(prompt: str, negative: str, steps: int, cfg: float, width: int, height: int, seed: int):
|
| 52 |
+
pipe = get_pipe()
|
| 53 |
+
w, h = snap8(width), snap8(height)
|
| 54 |
+
|
| 55 |
+
# seed
|
| 56 |
+
if int(seed) < 0:
|
| 57 |
+
seed = random.randint(0, 2**31 - 1)
|
| 58 |
+
gen = torch.Generator(device="cuda").manual_seed(int(seed))
|
| 59 |
+
|
| 60 |
+
if torch.cuda.is_available():
|
| 61 |
+
torch.cuda.empty_cache()
|
| 62 |
+
gc.collect()
|
| 63 |
|
| 64 |
+
with torch.autocast(device_type="cuda", dtype=DTYPE):
|
| 65 |
+
out = pipe(
|
| 66 |
+
prompt=str(prompt),
|
| 67 |
+
negative_prompt=str(negative or ""),
|
| 68 |
+
num_inference_steps=int(steps),
|
| 69 |
+
guidance_scale=float(cfg),
|
| 70 |
+
width=w, height=h,
|
| 71 |
+
generator=gen,
|
| 72 |
+
)
|
| 73 |
+
return out.images[0]
|
| 74 |
|
| 75 |
+
# ====== UI (two tabs: sanity + SD) ======
|
| 76 |
+
with gr.Blocks() as demo:
|
| 77 |
+
gr.Markdown("# ZeroGPU demo + Stable Diffusion 1.5 (minimal)")
|
| 78 |
|
| 79 |
+
with gr.Tab("ZeroGPU sanity"):
|
| 80 |
+
n = gr.Number(label="Add to zero", value=1.0)
|
| 81 |
+
hello = gr.Textbox(label="Result")
|
| 82 |
+
gr.Button("Greet").click(greet, n, hello)
|
| 83 |
|
| 84 |
+
with gr.Tab("SD 1.5: Text → Image"):
|
| 85 |
+
prompt = gr.Textbox(label="Prompt", value="a cozy reading nook, warm sunlight, cinematic lighting, highly detailed")
|
| 86 |
+
negative = gr.Textbox(label="Negative (optional)", value="lowres, blurry, watermark, text")
|
| 87 |
+
steps = gr.Slider(8, 40, value=28, step=1, label="Steps")
|
| 88 |
+
cfg = gr.Slider(1.0, 12.0, value=7.0, step=0.5, label="CFG")
|
| 89 |
+
width = gr.Slider(256, 1024, value=640, step=16, label="Width")
|
| 90 |
+
height = gr.Slider(256, 1024, value=640, step=16, label="Height")
|
| 91 |
+
seed = gr.Number(value=-1, precision=0, label="Seed (-1 random)")
|
| 92 |
+
out_img = gr.Image(label="Image", interactive=False)
|
| 93 |
+
gr.Button("Generate").click(
|
| 94 |
+
generate, [prompt, negative, steps, cfg, width, height, seed], out_img
|
| 95 |
+
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
+
# keep it minimal so the Space "just builds"
|
| 99 |
demo.launch()
|