Tanut commited on
Commit
3ce6abd
·
1 Parent(s): af5a35d

Test ZeroGPU

Browse files
Files changed (1) hide show
  1. app.py +10 -80
app.py CHANGED
@@ -1,84 +1,14 @@
1
- import gc, random
2
  import gradio as gr
3
- import torch, spaces
4
- from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
5
 
6
- # ---- config ----
7
- MODEL_ID = "runwayml/stable-diffusion-v1-5"
8
- DTYPE = torch.float16 # ZeroGPU slice runs fp16 nicely
9
 
10
- # lazy cache
11
- _PIPE = None
 
 
12
 
13
- def get_pipe():
14
- global _PIPE
15
- if _PIPE is None:
16
- pipe = StableDiffusionPipeline.from_pretrained(
17
- MODEL_ID,
18
- torch_dtype=DTYPE,
19
- safety_checker=None,
20
- use_safetensors=True,
21
- low_cpu_mem_usage=True,
22
- )
23
- # fast, stable scheduler
24
- pipe.scheduler = DPMSolverMultistepScheduler.from_config(
25
- pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="dpmsolver++"
26
- )
27
- # memory savers (great for Spaces/ZeroGPU)
28
- pipe.enable_attention_slicing()
29
- pipe.enable_vae_slicing()
30
- pipe.enable_model_cpu_offload()
31
- _PIPE = pipe
32
- return _PIPE
33
-
34
- def snap8(x: int) -> int:
35
- x = max(256, min(1024, int(x)))
36
- return x - (x % 8)
37
-
38
- @spaces.GPU(duration=120)
39
- def generate(prompt: str, negative: str, steps: int, cfg: float, width: int, height: int, seed: int):
40
- pipe = get_pipe()
41
- w, h = snap8(width), snap8(height)
42
-
43
- # seed
44
- if int(seed) < 0:
45
- seed = random.randint(0, 2**31 - 1)
46
- gen = torch.Generator(device="cuda").manual_seed(int(seed))
47
-
48
- if torch.cuda.is_available():
49
- torch.cuda.empty_cache()
50
- gc.collect()
51
-
52
- with torch.autocast(device_type="cuda", dtype=DTYPE):
53
- out = pipe(
54
- prompt=str(prompt),
55
- negative_prompt=str(negative or ""),
56
- num_inference_steps=int(steps),
57
- guidance_scale=float(cfg),
58
- width=w, height=h,
59
- generator=gen,
60
- )
61
- return out.images[0]
62
-
63
- # -------- UI --------
64
- with gr.Blocks() as demo:
65
- gr.Markdown("# 🎨 Stable Diffusion 1.5 — ZeroGPU (public, minimal)")
66
-
67
- with gr.Row():
68
- with gr.Column():
69
- prompt = gr.Textbox(label="Prompt", value="a cozy reading nook, warm sunlight, cinematic lighting, highly detailed")
70
- negative = gr.Textbox(label="Negative (optional)", value="lowres, blurry, watermark, text")
71
- steps = gr.Slider(8, 40, value=28, step=1, label="Steps")
72
- cfg = gr.Slider(1.0, 12.0, value=7.0, step=0.5, label="CFG")
73
- width = gr.Slider(256, 1024, value=640, step=16, label="Width")
74
- height = gr.Slider(256, 1024, value=640, step=16, label="Height")
75
- seed = gr.Number(value=-1, precision=0, label="Seed (-1 random)")
76
- btn = gr.Button("Generate", variant="primary")
77
- with gr.Column():
78
- out = gr.Image(label="Result", interactive=False)
79
-
80
- btn.click(generate, [prompt, negative, steps, cfg, width, height, seed], out)
81
-
82
- if __name__ == "__main__":
83
- # Keep it plain so the Space builds cleanly
84
- demo.queue().launch(show_api=True, show_error=True, ssr_mode=False)
 
 
1
  import gradio as gr
2
+ import spaces
3
+ import torch
4
 
5
+ zero = torch.Tensor([0]).cuda()
6
+ print(zero.device) # <-- 'cpu' 🤔
 
7
 
8
+ @spaces.GPU
9
+ def greet(n):
10
+ print(zero.device) # <-- 'cuda:0' 🤗
11
+ return f"Hello {zero + n} Tensor"
12
 
13
+ demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
14
+ demo.launch()