sayshara commited on
Commit
90af8b7
Β·
1 Parent(s): 769c814

Trying to speed up

Browse files
Files changed (2) hide show
  1. app.py +10 -95
  2. diffqrcoder_wrapper.py +1 -1
app.py CHANGED
@@ -1,17 +1,10 @@
1
  # app.py
2
  import gradio as gr
3
  import spaces
4
- from diffqrcoder_wrapper import generate_qr_art
 
5
 
6
- DEFAULT_PROMPT = (
7
- "whimsical biomimetic blueprint, iridescent inks swirling through "
8
- "mechanical petals, soft gears woven with luminescent filigree"
9
- )
10
-
11
- DEFAULT_NEG = "easynegative"
12
-
13
-
14
- @spaces.GPU # ZeroGPU: attach GPU only for this call
15
  def infer(
16
  url_or_text: str,
17
  prompt: str,
@@ -21,100 +14,22 @@ def infer(
21
  perceptual_guidance_scale: float,
22
  srmpgd_iters: int,
23
  ):
24
- # Map 0 β†’ None for SR-MPGD iteration (as in original script)
 
 
 
 
 
25
  srmpgd_num_iteration = None if srmpgd_iters == 0 else srmpgd_iters
26
 
27
  img = generate_qr_art(
 
28
  url_or_text=url_or_text,
29
  prompt=prompt,
30
- neg_prompt=DEFAULT_NEG,
31
  num_inference_steps=num_inference_steps,
32
- qrcode_module_size=20,
33
- qrcode_padding=78,
34
  controlnet_conditioning_scale=controlnet_scale,
35
  scanning_robust_guidance_scale=scanning_robust_guidance_scale,
36
  perceptual_guidance_scale=perceptual_guidance_scale,
37
  srmpgd_num_iteration=srmpgd_num_iteration,
38
- srmpgd_lr=0.1,
39
- seed=1,
40
  )
41
  return img
42
-
43
-
44
- with gr.Blocks() as demo:
45
- gr.Markdown(
46
- r"""
47
- # DiffQRCoder – ZeroGPU demo
48
-
49
- Generate aesthetic, scanning-robust QR codes using the **DiffQRCoder** pipeline
50
- ([WACV 2025](https://openaccess.thecvf.com/content/WACV2025/html/Liao_DiffQRCoder_Diffusion-Based_Aesthetic_QR_Code_Generation_with_Scanning_Robustness_Guided_WACV_2025_paper.html)) πŸš€
51
- """
52
- )
53
-
54
- with gr.Row():
55
- url = gr.Textbox(
56
- label="QR contents (URL or text)",
57
- value="https://example.com",
58
- )
59
-
60
- prompt = gr.Textbox(
61
- label="Style prompt",
62
- value=DEFAULT_PROMPT,
63
- lines=3,
64
- )
65
-
66
- with gr.Accordion("Advanced parameters", open=False):
67
- steps = gr.Slider(
68
- minimum=10,
69
- maximum=60,
70
- value=40,
71
- step=1,
72
- label="Diffusion steps (num_inference_steps)",
73
- )
74
- control_scale = gr.Slider(
75
- minimum=0.5,
76
- maximum=2.0,
77
- value=1.35,
78
- step=0.05,
79
- label="ControlNet conditioning scale",
80
- )
81
- srg_scale = gr.Slider(
82
- minimum=0,
83
- maximum=800,
84
- value=500,
85
- step=10,
86
- label="Scanning-robust guidance scale (srg)",
87
- )
88
- pg_scale = gr.Slider(
89
- minimum=0,
90
- maximum=10,
91
- value=2,
92
- step=0.5,
93
- label="Perceptual guidance scale (pg)",
94
- )
95
- srmpgd_iters = gr.Slider(
96
- minimum=0,
97
- maximum=64,
98
- value=0,
99
- step=1,
100
- label="SR-MPGD iterations (0 = disabled)",
101
- )
102
-
103
- btn = gr.Button("Generate QR Art ✨", variant="primary")
104
- out = gr.Image(label="Output QR art", type="pil")
105
-
106
- btn.click(
107
- fn=infer,
108
- inputs=[
109
- url,
110
- prompt,
111
- steps,
112
- control_scale,
113
- srg_scale,
114
- pg_scale,
115
- srmpgd_iters,
116
- ],
117
- outputs=[out],
118
- )
119
-
120
- demo.launch()
 
1
  # app.py
2
  import gradio as gr
3
  import spaces
4
+ from diffqrcoder_wrapper import generate_qr_art, load_pipeline
5
+ import torch
6
 
7
+ @spaces.GPU
 
 
 
 
 
 
 
 
8
  def infer(
9
  url_or_text: str,
10
  prompt: str,
 
14
  perceptual_guidance_scale: float,
15
  srmpgd_iters: int,
16
  ):
17
+ # πŸ”Ή Make sure pipeline is loaded *once* and then just moved to GPU
18
+ pipe = load_pipeline()
19
+
20
+ # move to GPU *here*, with an explicit non-blocking call
21
+ pipe.to("cuda")
22
+
23
  srmpgd_num_iteration = None if srmpgd_iters == 0 else srmpgd_iters
24
 
25
  img = generate_qr_art(
26
+ pipe,
27
  url_or_text=url_or_text,
28
  prompt=prompt,
 
29
  num_inference_steps=num_inference_steps,
 
 
30
  controlnet_conditioning_scale=controlnet_scale,
31
  scanning_robust_guidance_scale=scanning_robust_guidance_scale,
32
  perceptual_guidance_scale=perceptual_guidance_scale,
33
  srmpgd_num_iteration=srmpgd_num_iteration,
 
 
34
  )
35
  return img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
diffqrcoder_wrapper.py CHANGED
@@ -83,6 +83,7 @@ def load_pipeline():
83
 
84
 
85
  def generate_qr_art(
 
86
  url_or_text: str,
87
  prompt: str,
88
  neg_prompt: str = "easynegative",
@@ -96,7 +97,6 @@ def generate_qr_art(
96
  srmpgd_lr: float = 0.1,
97
  seed: int = 1,
98
  ) -> Image.Image:
99
- pipe = load_pipeline()
100
 
101
  generator = torch.Generator(device=DEVICE).manual_seed(seed)
102
 
 
83
 
84
 
85
  def generate_qr_art(
86
+ pipe: DiffQRCoderPipeline,
87
  url_or_text: str,
88
  prompt: str,
89
  neg_prompt: str = "easynegative",
 
97
  srmpgd_lr: float = 0.1,
98
  seed: int = 1,
99
  ) -> Image.Image:
 
100
 
101
  generator = torch.Generator(device=DEVICE).manual_seed(seed)
102