um235 commited on
Commit
08ee6b2
·
verified ·
1 Parent(s): fc58ee6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -52
app.py CHANGED
@@ -2,8 +2,6 @@ import gradio as gr
2
  import numpy as np
3
  import random
4
  from peft import PeftModel, LoraConfig
5
-
6
- # import spaces #[uncomment to use ZeroGPU]
7
  from diffusers import DiffusionPipeline
8
  import torch
9
 
@@ -16,6 +14,23 @@ else:
16
  MAX_SEED = np.iinfo(np.int32).max
17
  MAX_IMAGE_SIZE = 1024
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # @spaces.GPU #[uncomment to use ZeroGPU]
21
  def infer(
@@ -44,18 +59,25 @@ def infer(
44
  generator = torch.Generator().manual_seed(seed)
45
 
46
  pipe = None
47
- if (model_id=="SD1.5 + lora Unet TextEncoder"):
48
- pipe=DiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch_dtype)
49
- pipe.unet = PeftModel.from_pretrained(pipe.unet,"um235/VanillaCat",subfolder="unet")
50
  pipe.safety_checker = None
51
- pipe.text_encoder= PeftModel.from_pretrained(pipe.text_encoder,"um235/VanillaCat",subfolder="text_encoder")
52
- elif (model_id=="SD1.5 + lora Unet"):
53
- pipe=DiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch_dtype)
54
  pipe.safety_checker = None
55
- pipe.unet = PeftModel.from_pretrained(pipe.unet,"um235/cartoon_cat_stickers")
56
  else:
57
- pipe=DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
58
  pipe.safety_checker = None
 
 
 
 
 
 
 
59
  pipe = pipe.to(device)
60
 
61
  image = pipe(
@@ -66,7 +88,9 @@ def infer(
66
  width=width,
67
  height=height,
68
  generator=generator,
69
- cross_attention_kwargs={"scale": lscale}
 
 
70
  ).images[0]
71
 
72
  return image, seed
@@ -86,40 +110,36 @@ css = """
86
  """
87
 
88
  def update_controlnet_visibility(controlnet_enabled):
89
- # Возвращаем два значения для обновления видимости control_strength и control_mode
90
  return gr.update(visible=controlnet_enabled), gr.update(visible=controlnet_enabled), gr.update(visible=controlnet_enabled)
91
 
92
  def update_ip_adapter_visibility(ip_adapter_enabled):
93
- # Возвращаем два значения для обновления видимости ip_adapter_scale и ip_adapter_image
94
  return gr.update(visible=ip_adapter_enabled), gr.update(visible=ip_adapter_enabled)
95
 
96
 
97
-
98
  with gr.Blocks(css=css) as demo:
99
  with gr.Column(elem_id="col-container"):
100
  gr.Markdown(" # UM235 DIFFUSION Space")
101
 
102
  model_id_input = gr.Dropdown(
103
- label="Choose Model",
104
- choices=[
105
- "stable-diffusion-v1-5/stable-diffusion-v1-5",
106
- "CompVis/stable-diffusion-v1-4",
107
- "SD1.5 + lora Unet TextEncoder",
108
- "SD1.5 + lora Unet"
109
- ],
110
- value="SD1.5 + lora Unet TextEncoder",
111
- show_label=True,
112
- type="value",
113
  )
114
  with gr.Row():
115
- lscale = gr.Slider(
116
- label="Lora scale",
117
- minimum=0,
118
- maximum=2,
119
- step=0.05,
120
- value=1,
121
- )
122
-
123
 
124
  with gr.Row():
125
  prompt = gr.Text(
@@ -134,20 +154,20 @@ with gr.Blocks(css=css) as demo:
134
  controlnet_enabled = gr.Checkbox(label="Enable ControlNet", value=False)
135
 
136
  with gr.Row():
137
- control_strength = gr.Slider(
138
- label="ControlNet scale",
139
- minimum=0.0,
140
- maximum=1.0,
141
- step=0.05,
142
- value=0.75,
143
- visible=False,
144
- )
145
 
146
- control_mode = gr.Dropdown(
147
- label="ControlNet Mode",
148
- choices=["edge_detection", "pose_estimation", "depth_estimation"],
149
- value="edge_detection",
150
- visible=False,
151
  )
152
 
153
  control_image = gr.Image(label="ControlNet Image", type="pil", visible=False)
@@ -168,7 +188,6 @@ with gr.Blocks(css=css) as demo:
168
  ip_adapter_image = gr.Image(label="IP-Adapter Image", type="pil", visible=False)
169
 
170
  with gr.Row():
171
-
172
  run_button = gr.Button("Run", scale=0, variant="primary")
173
 
174
  result = gr.Image(label="Result", show_label=False)
@@ -198,7 +217,7 @@ with gr.Blocks(css=css) as demo:
198
  minimum=256,
199
  maximum=MAX_IMAGE_SIZE,
200
  step=32,
201
- value=512, # Replace with defaults that work for your model
202
  )
203
 
204
  height = gr.Slider(
@@ -206,7 +225,7 @@ with gr.Blocks(css=css) as demo:
206
  minimum=256,
207
  maximum=MAX_IMAGE_SIZE,
208
  step=32,
209
- value=512, # Replace with defaults that work for your model
210
  )
211
 
212
  with gr.Row():
@@ -215,7 +234,7 @@ with gr.Blocks(css=css) as demo:
215
  minimum=0.0,
216
  maximum=10.0,
217
  step=0.1,
218
- value=9.0, # Replace with defaults that work for your model
219
  )
220
 
221
  num_inference_steps = gr.Slider(
@@ -223,10 +242,11 @@ with gr.Blocks(css=css) as demo:
223
  minimum=1,
224
  maximum=50,
225
  step=1,
226
- value=36, # Replace with defaults that work for your model
227
  )
228
 
229
  gr.Examples(examples=examples, inputs=[prompt])
 
230
  gr.on(
231
  triggers=[run_button.click, prompt.submit],
232
  fn=infer,
@@ -251,13 +271,13 @@ with gr.Blocks(css=css) as demo:
251
  ],
252
  outputs=[result, seed],
253
  )
 
254
  controlnet_enabled.change(
255
  fn=update_controlnet_visibility,
256
  inputs=[controlnet_enabled],
257
  outputs=[control_strength, control_mode, control_image],
258
  )
259
 
260
- # Updates visibility when the checkbox for IP-Adapter is toggled
261
  ip_adapter_enabled.change(
262
  fn=update_ip_adapter_visibility,
263
  inputs=[ip_adapter_enabled],
@@ -266,4 +286,4 @@ with gr.Blocks(css=css) as demo:
266
 
267
 
268
  if __name__ == "__main__":
269
- demo.launch()
 
2
  import numpy as np
3
  import random
4
  from peft import PeftModel, LoraConfig
 
 
5
  from diffusers import DiffusionPipeline
6
  import torch
7
 
 
14
  MAX_SEED = np.iinfo(np.int32).max
15
  MAX_IMAGE_SIZE = 1024
16
 
17
+ # ControlNet modes list with aliases
18
+ CONTROLNET_MODES = {
19
+ "Canny Edge Detection": "lllyasviel/control_v11p_sd15_canny",
20
+ "Pixel to Pixel": "lllyasviel/control_v11e_sd15_ip2p",
21
+ "Inpainting": "lllyasviel/control_v11p_sd15_inpaint",
22
+ "Multi-Level Line Segments": "lllyasviel/control_v11p_sd15_mlsd",
23
+ "Depth Estimation": "lllyasviel/control_v11f1p_sd15_depth",
24
+ "Surface Normal Estimation": "lllyasviel/control_v11p_sd15_normalbae",
25
+ "Image Segmentation": "lllyasviel/control_v11p_sd15_seg",
26
+ "Line Art Generation": "lllyasviel/control_v11p_sd15_lineart",
27
+ "Anime Line Art": "lllyasviel/control_v11p_sd15_lineart_anime",
28
+ "Human Pose Estimation": "lllyasviel/control_v11p_sd15_openpose",
29
+ "Scribble-Based Generation": "lllyasviel/control_v11p_sd15_scribble",
30
+ "Soft Edge Generation": "lllyasviel/control_v11p_sd15_softedge",
31
+ "Image Shuffling": "lllyasviel/control_v11e_sd15_shuffle",
32
+ "Image Tiling": "lllyasviel/control_v11f1e_sd15_tile",
33
+ }
34
 
35
  # @spaces.GPU #[uncomment to use ZeroGPU]
36
  def infer(
 
59
  generator = torch.Generator().manual_seed(seed)
60
 
61
  pipe = None
62
+ if model_id == "SD1.5 + lora Unet TextEncoder":
63
+ pipe = DiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch_dtype)
64
+ pipe.unet = PeftModel.from_pretrained(pipe.unet, "um235/VanillaCat", subfolder="unet")
65
  pipe.safety_checker = None
66
+ pipe.text_encoder = PeftModel.from_pretrained(pipe.text_encoder, "um235/VanillaCat", subfolder="text_encoder")
67
+ elif model_id == "SD1.5 + lora Unet":
68
+ pipe = DiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch_dtype)
69
  pipe.safety_checker = None
70
+ pipe.unet = PeftModel.from_pretrained(pipe.unet, "um235/cartoon_cat_stickers")
71
  else:
72
+ pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
73
  pipe.safety_checker = None
74
+
75
+ if controlnet_enabled:
76
+ controlnet_model = CONTROLNET_MODES.get(control_mode)
77
+ if controlnet_model:
78
+ controlnet_model = ControlNetModel.from_pretrained(controlnet_model)
79
+ pipe.controlnet = controlnet_model
80
+
81
  pipe = pipe.to(device)
82
 
83
  image = pipe(
 
88
  width=width,
89
  height=height,
90
  generator=generator,
91
+ cross_attention_kwargs={"scale": lscale},
92
+ control_image=control_image,
93
+ controlnet_conditioning_scale=control_strength
94
  ).images[0]
95
 
96
  return image, seed
 
110
  """
111
 
112
  def update_controlnet_visibility(controlnet_enabled):
 
113
  return gr.update(visible=controlnet_enabled), gr.update(visible=controlnet_enabled), gr.update(visible=controlnet_enabled)
114
 
115
  def update_ip_adapter_visibility(ip_adapter_enabled):
 
116
  return gr.update(visible=ip_adapter_enabled), gr.update(visible=ip_adapter_enabled)
117
 
118
 
 
119
  with gr.Blocks(css=css) as demo:
120
  with gr.Column(elem_id="col-container"):
121
  gr.Markdown(" # UM235 DIFFUSION Space")
122
 
123
  model_id_input = gr.Dropdown(
124
+ label="Choose Model",
125
+ choices=[
126
+ "stable-diffusion-v1-5/stable-diffusion-v1-5",
127
+ "CompVis/stable-diffusion-v1-4",
128
+ "SD1.5 + lora Unet TextEncoder",
129
+ "SD1.5 + lora Unet"
130
+ ],
131
+ value="SD1.5 + lora Unet TextEncoder",
132
+ show_label=True,
133
+ type="value",
134
  )
135
  with gr.Row():
136
+ lscale = gr.Slider(
137
+ label="Lora scale",
138
+ minimum=0,
139
+ maximum=2,
140
+ step=0.05,
141
+ value=1,
142
+ )
 
143
 
144
  with gr.Row():
145
  prompt = gr.Text(
 
154
  controlnet_enabled = gr.Checkbox(label="Enable ControlNet", value=False)
155
 
156
  with gr.Row():
157
+ control_strength = gr.Slider(
158
+ label="ControlNet scale",
159
+ minimum=0.0,
160
+ maximum=1.0,
161
+ step=0.05,
162
+ value=0.75,
163
+ visible=False,
164
+ )
165
 
166
+ control_mode = gr.Dropdown(
167
+ label="ControlNet Mode",
168
+ choices=list(CONTROLNET_MODES.keys()),
169
+ value="Canny Edge Detection",
170
+ visible=False,
171
  )
172
 
173
  control_image = gr.Image(label="ControlNet Image", type="pil", visible=False)
 
188
  ip_adapter_image = gr.Image(label="IP-Adapter Image", type="pil", visible=False)
189
 
190
  with gr.Row():
 
191
  run_button = gr.Button("Run", scale=0, variant="primary")
192
 
193
  result = gr.Image(label="Result", show_label=False)
 
217
  minimum=256,
218
  maximum=MAX_IMAGE_SIZE,
219
  step=32,
220
+ value=512,
221
  )
222
 
223
  height = gr.Slider(
 
225
  minimum=256,
226
  maximum=MAX_IMAGE_SIZE,
227
  step=32,
228
+ value=512,
229
  )
230
 
231
  with gr.Row():
 
234
  minimum=0.0,
235
  maximum=10.0,
236
  step=0.1,
237
+ value=9.0,
238
  )
239
 
240
  num_inference_steps = gr.Slider(
 
242
  minimum=1,
243
  maximum=50,
244
  step=1,
245
+ value=36,
246
  )
247
 
248
  gr.Examples(examples=examples, inputs=[prompt])
249
+
250
  gr.on(
251
  triggers=[run_button.click, prompt.submit],
252
  fn=infer,
 
271
  ],
272
  outputs=[result, seed],
273
  )
274
+
275
  controlnet_enabled.change(
276
  fn=update_controlnet_visibility,
277
  inputs=[controlnet_enabled],
278
  outputs=[control_strength, control_mode, control_image],
279
  )
280
 
 
281
  ip_adapter_enabled.change(
282
  fn=update_ip_adapter_visibility,
283
  inputs=[ip_adapter_enabled],
 
286
 
287
 
288
  if __name__ == "__main__":
289
+ demo.launch()