telcom commited on
Commit
a344ac6
Β·
verified Β·
1 Parent(s): a290ddb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -26
app.py CHANGED
@@ -5,7 +5,7 @@
5
  # - @spaces.GPU MUST be used directly
6
  # ============================================================
7
 
8
- import spaces # MUST be first, no try/except
9
 
10
  import os
11
  import random
@@ -37,11 +37,8 @@ if HF_TOKEN:
37
  device = torch.device("cuda")
38
  dtype = torch.float16
39
 
 
40
  MAX_SEED = np.iinfo(np.int32).max
41
- MAX_IMAGE_SIZE = 1024
42
-
43
- pipe_txt2img = None
44
- pipe_img2img = None
45
 
46
  # ============================================================
47
  # Load model (once at startup)
@@ -53,7 +50,7 @@ pipe_txt2img = StableDiffusionPipeline.from_pretrained(
53
  safety_checker=None,
54
  ).to(device)
55
 
56
- # πŸ”‘ Force tokenizer + text encoder (fixes tokenize None bug)
57
  pipe_txt2img.tokenizer = CLIPTokenizer.from_pretrained(
58
  MODEL_ID, subfolder="tokenizer"
59
  )
@@ -68,7 +65,7 @@ pipe_txt2img.scheduler = EulerAncestralDiscreteScheduler.from_config(
68
  pipe_txt2img.scheduler.config
69
  )
70
 
71
- # Memory optimisations (safe on Spaces)
72
  pipe_txt2img.enable_attention_slicing()
73
  pipe_txt2img.enable_vae_slicing()
74
 
@@ -79,8 +76,10 @@ except Exception:
79
 
80
  pipe_txt2img.set_progress_bar_config(disable=True)
81
 
82
- # Img2Img pipeline (reuse components)
83
- pipe_img2img = StableDiffusionImg2ImgPipeline(**pipe_txt2img.components).to(device)
 
 
84
  pipe_img2img.scheduler = EulerAncestralDiscreteScheduler.from_config(
85
  pipe_img2img.scheduler.config
86
  )
@@ -94,16 +93,11 @@ def infer(
94
  negative_prompt,
95
  seed,
96
  randomize_seed,
97
- width,
98
- height,
99
  guidance_scale,
100
  num_inference_steps,
101
  init_image,
102
  strength,
103
  ):
104
- width = int(width)
105
- height = int(height)
106
-
107
  if randomize_seed:
108
  seed = random.randint(0, MAX_SEED)
109
 
@@ -125,8 +119,8 @@ def infer(
125
  image = pipe_txt2img(
126
  prompt=prompt,
127
  negative_prompt=negative_prompt,
128
- width=width,
129
- height=height,
130
  guidance_scale=float(guidance_scale),
131
  num_inference_steps=int(num_inference_steps),
132
  generator=generator,
@@ -141,25 +135,34 @@ def infer(
141
  # ============================================================
142
  # UI
143
  # ============================================================
144
- with gr.Blocks(title="Stable Diffusion (Unlearning Model)") as demo:
145
- gr.Markdown("## Stable Diffusion Generator (GPU)")
146
 
147
- prompt = gr.Textbox(label="Prompt", lines=2)
148
- init_image = gr.Image(label="Initial image (optional)", type="pil")
 
 
 
 
 
 
 
 
149
 
150
  run_button = gr.Button("Generate")
151
  result = gr.Image(label="Result")
152
  status = gr.Markdown("")
153
 
154
  with gr.Accordion("Advanced Settings", open=False):
155
- negative_prompt = gr.Textbox(label="Negative prompt", value="nsfw, (low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn, deform, paining, eyes misaligned, bad, disproportional, boring, fuzzy, asymmetric")
 
 
 
156
  seed = gr.Slider(0, MAX_SEED, step=1, value=0, label="Seed")
157
  randomize_seed = gr.Checkbox(True, label="Randomize seed")
158
- width = 512 #gr.Slider(256, MAX_IMAGE_SIZE, step=32, value=512, label="Width")
159
- height = 512 # gr.Slider(256, MAX_IMAGE_SIZE, step=32, value=512, label="Height")
160
  guidance_scale = gr.Slider(1, 20, step=0.5, value=7.5, label="Guidance scale")
161
  num_inference_steps = gr.Slider(1, 40, step=1, value=30, label="Steps")
162
- strength = gr.Slider(0.0, 1.0, step=0.05, value=0.7, label="Image strength")
163
 
164
  run_button.click(
165
  fn=infer,
@@ -168,8 +171,6 @@ with gr.Blocks(title="Stable Diffusion (Unlearning Model)") as demo:
168
  negative_prompt,
169
  seed,
170
  randomize_seed,
171
- width,
172
- height,
173
  guidance_scale,
174
  num_inference_steps,
175
  init_image,
 
5
  # - @spaces.GPU MUST be used directly
6
  # ============================================================
7
 
8
+ import spaces # MUST be first
9
 
10
  import os
11
  import random
 
37
  device = torch.device("cuda")
38
  dtype = torch.float16
39
 
40
+ IMAGE_SIZE = 512
41
  MAX_SEED = np.iinfo(np.int32).max
 
 
 
 
42
 
43
  # ============================================================
44
  # Load model (once at startup)
 
50
  safety_checker=None,
51
  ).to(device)
52
 
53
+ # πŸ”‘ Force tokenizer + text encoder
54
  pipe_txt2img.tokenizer = CLIPTokenizer.from_pretrained(
55
  MODEL_ID, subfolder="tokenizer"
56
  )
 
65
  pipe_txt2img.scheduler.config
66
  )
67
 
68
+ # Memory optimisations
69
  pipe_txt2img.enable_attention_slicing()
70
  pipe_txt2img.enable_vae_slicing()
71
 
 
76
 
77
  pipe_txt2img.set_progress_bar_config(disable=True)
78
 
79
+ # Img2Img pipeline
80
+ pipe_img2img = StableDiffusionImg2ImgPipeline(
81
+ **pipe_txt2img.components
82
+ ).to(device)
83
  pipe_img2img.scheduler = EulerAncestralDiscreteScheduler.from_config(
84
  pipe_img2img.scheduler.config
85
  )
 
93
  negative_prompt,
94
  seed,
95
  randomize_seed,
 
 
96
  guidance_scale,
97
  num_inference_steps,
98
  init_image,
99
  strength,
100
  ):
 
 
 
101
  if randomize_seed:
102
  seed = random.randint(0, MAX_SEED)
103
 
 
119
  image = pipe_txt2img(
120
  prompt=prompt,
121
  negative_prompt=negative_prompt,
122
+ width=IMAGE_SIZE,
123
+ height=IMAGE_SIZE,
124
  guidance_scale=float(guidance_scale),
125
  num_inference_steps=int(num_inference_steps),
126
  generator=generator,
 
135
  # ============================================================
136
  # UI
137
  # ============================================================
138
+ with gr.Blocks(title="Stable Diffusion (512Γ—512)") as demo:
139
+ gr.Markdown("## Stable Diffusion Generator (GPU, 512Γ—512)")
140
 
141
+ prompt = gr.Textbox(
142
+ label="Prompt",
143
+ placeholder="Describe the image you want to generate",
144
+ lines=2,
145
+ )
146
+
147
+ init_image = gr.Image(
148
+ label="Initial image (optional, enables img2img)",
149
+ type="pil",
150
+ )
151
 
152
  run_button = gr.Button("Generate")
153
  result = gr.Image(label="Result")
154
  status = gr.Markdown("")
155
 
156
  with gr.Accordion("Advanced Settings", open=False):
157
+ negative_prompt = gr.Textbox(
158
+ label="Negative prompt",
159
+ value="nsfw, (low quality, worst quality:1.2), watermark, signature, ugly, deformed",
160
+ )
161
  seed = gr.Slider(0, MAX_SEED, step=1, value=0, label="Seed")
162
  randomize_seed = gr.Checkbox(True, label="Randomize seed")
 
 
163
  guidance_scale = gr.Slider(1, 20, step=0.5, value=7.5, label="Guidance scale")
164
  num_inference_steps = gr.Slider(1, 40, step=1, value=30, label="Steps")
165
+ strength = gr.Slider(0.0, 1.0, step=0.05, value=0.7, label="Image strength (img2img)")
166
 
167
  run_button.click(
168
  fn=infer,
 
171
  negative_prompt,
172
  seed,
173
  randomize_seed,
 
 
174
  guidance_scale,
175
  num_inference_steps,
176
  init_image,