jocoandonob commited on
Commit
2a6c924
·
1 Parent(s): 8db5115

Initial commitcq

Browse files
Files changed (1) hide show
  1. app.py +109 -28
app.py CHANGED
@@ -15,6 +15,7 @@ from PIL import Image
15
  import io
16
  import requests
17
  from transformers import DPTImageProcessor, DPTForDepthEstimation
 
18
 
19
  # Available models
20
  AVAILABLE_MODELS = {
@@ -28,6 +29,21 @@ AVAILABLE_LORAS = {
28
  "Papercut": "TheLastBen/Papercut_SDXL",
29
  }
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  def get_depth_map(image):
32
  # Initialize depth estimator
33
  depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas")
@@ -61,15 +77,20 @@ def load_image_from_url(url):
61
 
62
  def generate_image(prompt, seed, num_steps, guidance_scale, eta):
63
  try:
 
 
 
64
  # Initialize the pipeline
65
  base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
66
  tcd_lora_id = "h1t/TCD-SDXL-LoRA"
67
 
68
- # Use CPU for inference
69
  pipe = StableDiffusionXLPipeline.from_pretrained(
70
  base_model_id,
71
- torch_dtype=torch.float32 # Use float32 for CPU
72
- )
 
 
 
73
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
74
 
75
  # Load and fuse LoRA weights with prefix=None
@@ -77,7 +98,7 @@ def generate_image(prompt, seed, num_steps, guidance_scale, eta):
77
  pipe.fuse_lora()
78
 
79
  # Generate the image
80
- generator = torch.Generator().manual_seed(seed)
81
  image = pipe(
82
  prompt=prompt,
83
  num_inference_steps=num_steps,
@@ -86,21 +107,31 @@ def generate_image(prompt, seed, num_steps, guidance_scale, eta):
86
  generator=generator,
87
  ).images[0]
88
 
 
 
 
 
89
  return image, "Image generated successfully!"
90
  except Exception as e:
 
91
  return None, f"Error generating image: {str(e)}"
92
 
93
  def generate_community_image(prompt, model_name, seed, num_steps, guidance_scale, eta):
94
  try:
 
 
 
95
  # Initialize the pipeline
96
  base_model_id = AVAILABLE_MODELS[model_name]
97
  tcd_lora_id = "h1t/TCD-SDXL-LoRA"
98
 
99
- # Use CPU for inference
100
  pipe = StableDiffusionXLPipeline.from_pretrained(
101
  base_model_id,
102
- torch_dtype=torch.float32 # Use float32 for CPU
103
- )
 
 
 
104
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
105
 
106
  # Load and fuse LoRA weights with prefix=None
@@ -108,7 +139,7 @@ def generate_community_image(prompt, model_name, seed, num_steps, guidance_scale
108
  pipe.fuse_lora()
109
 
110
  # Generate the image
111
- generator = torch.Generator().manual_seed(seed)
112
  image = pipe(
113
  prompt=prompt,
114
  num_inference_steps=num_steps,
@@ -117,22 +148,32 @@ def generate_community_image(prompt, model_name, seed, num_steps, guidance_scale
117
  generator=generator,
118
  ).images[0]
119
 
 
 
 
 
120
  return image, "Image generated successfully!"
121
  except Exception as e:
 
122
  return None, f"Error generating image: {str(e)}"
123
 
124
  def generate_style_mix(prompt, seed, num_steps, guidance_scale, eta, style_weight):
125
  try:
 
 
 
126
  # Initialize the pipeline
127
  base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
128
  tcd_lora_id = "h1t/TCD-SDXL-LoRA"
129
  styled_lora_id = "TheLastBen/Papercut_SDXL"
130
 
131
- # Use CPU for inference
132
  pipe = StableDiffusionXLPipeline.from_pretrained(
133
  base_model_id,
134
- torch_dtype=torch.float32 # Use float32 for CPU
135
- )
 
 
 
136
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
137
 
138
  # Load multiple LoRA weights with prefix=None
@@ -143,7 +184,7 @@ def generate_style_mix(prompt, seed, num_steps, guidance_scale, eta, style_weigh
143
  pipe.set_adapters(["tcd", "style"], adapter_weights=[1.0, style_weight])
144
 
145
  # Generate the image
146
- generator = torch.Generator().manual_seed(seed)
147
  image = pipe(
148
  prompt=prompt,
149
  num_inference_steps=num_steps,
@@ -152,12 +193,20 @@ def generate_style_mix(prompt, seed, num_steps, guidance_scale, eta, style_weigh
152
  generator=generator,
153
  ).images[0]
154
 
 
 
 
 
155
  return image, "Image generated successfully!"
156
  except Exception as e:
 
157
  return None, f"Error generating image: {str(e)}"
158
 
159
  def generate_controlnet(prompt, init_image, seed, num_steps, guidance_scale, eta, controlnet_scale):
160
  try:
 
 
 
161
  # Initialize the pipeline
162
  base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
163
  controlnet_id = "diffusers/controlnet-depth-sdxl-1.0"
@@ -166,15 +215,20 @@ def generate_controlnet(prompt, init_image, seed, num_steps, guidance_scale, eta
166
  # Initialize ControlNet
167
  controlnet = ControlNetModel.from_pretrained(
168
  controlnet_id,
169
- torch_dtype=torch.float32 # Use float32 for CPU
170
- )
 
 
171
 
172
  # Initialize pipeline
173
  pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
174
  base_model_id,
175
  controlnet=controlnet,
176
- torch_dtype=torch.float32 # Use float32 for CPU
177
- )
 
 
 
178
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
179
 
180
  # Load and fuse LoRA weights with prefix=None
@@ -185,7 +239,7 @@ def generate_controlnet(prompt, init_image, seed, num_steps, guidance_scale, eta
185
  depth_image = get_depth_map(init_image)
186
 
187
  # Generate the image
188
- generator = torch.Generator().manual_seed(seed)
189
  image = pipe(
190
  prompt=prompt,
191
  image=depth_image,
@@ -198,21 +252,32 @@ def generate_controlnet(prompt, init_image, seed, num_steps, guidance_scale, eta
198
 
199
  # Create a grid of the depth map and result
200
  grid = make_image_grid([depth_image, image], rows=1, cols=2)
 
 
 
 
 
201
  return grid, "Image generated successfully!"
202
  except Exception as e:
 
203
  return None, f"Error generating image: {str(e)}"
204
 
205
  def inpaint_image(prompt, init_image, mask_image, seed, num_steps, guidance_scale, eta, strength):
206
  try:
 
 
 
207
  # Initialize the pipeline
208
  base_model_id = "diffusers/stable-diffusion-xl-1.0-inpainting-0.1"
209
  tcd_lora_id = "h1t/TCD-SDXL-LoRA"
210
 
211
- # Use CPU for inference
212
  pipe = AutoPipelineForInpainting.from_pretrained(
213
  base_model_id,
214
- torch_dtype=torch.float32 # Use float32 for CPU
215
- )
 
 
 
216
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
217
 
218
  # Load and fuse LoRA weights with prefix=None
@@ -220,7 +285,7 @@ def inpaint_image(prompt, init_image, mask_image, seed, num_steps, guidance_scal
220
  pipe.fuse_lora()
221
 
222
  # Generate the image
223
- generator = torch.Generator().manual_seed(seed)
224
  image = pipe(
225
  prompt=prompt,
226
  image=init_image,
@@ -234,12 +299,21 @@ def inpaint_image(prompt, init_image, mask_image, seed, num_steps, guidance_scal
234
 
235
  # Create a grid of the original image, mask, and result
236
  grid = make_image_grid([init_image, mask_image, image], rows=1, cols=3)
 
 
 
 
 
237
  return grid, "Image generated successfully!"
238
  except Exception as e:
 
239
  return None, f"Error generating image: {str(e)}"
240
 
241
  def generate_animation(prompt, seed, num_steps, guidance_scale, eta, num_frames, motion_scale):
242
  try:
 
 
 
243
  # Initialize the pipeline
244
  base_model_id = "frankjoshua/toonyou_beta6"
245
  motion_adapter_id = "guoyww/animatediff-motion-adapter-v1-5"
@@ -247,16 +321,17 @@ def generate_animation(prompt, seed, num_steps, guidance_scale, eta, num_frames,
247
  motion_lora_id = "guoyww/animatediff-motion-lora-zoom-in"
248
 
249
  # Load motion adapter
250
- adapter = MotionAdapter.from_pretrained(motion_adapter_id)
251
 
252
- # Initialize pipeline with CPU optimization
253
  pipe = AnimateDiffPipeline.from_pretrained(
254
  base_model_id,
255
  motion_adapter=adapter,
256
- torch_dtype=torch.float32, # Use float32 for CPU
257
- low_cpu_mem_usage=True, # Enable low CPU memory usage
258
- use_safetensors=False # Use standard PyTorch weights
259
- )
 
260
 
261
  # Set TCD scheduler
262
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
@@ -273,7 +348,7 @@ def generate_animation(prompt, seed, num_steps, guidance_scale, eta, num_frames,
273
  pipe.set_adapters(["tcd", "motion-lora"], adapter_weights=[1.0, motion_scale])
274
 
275
  # Generate animation
276
- generator = torch.Generator().manual_seed(seed)
277
  frames = pipe(
278
  prompt=prompt,
279
  num_inference_steps=num_steps,
@@ -287,8 +362,14 @@ def generate_animation(prompt, seed, num_steps, guidance_scale, eta, num_frames,
287
  # Export to GIF
288
  gif_path = "animation.gif"
289
  export_to_gif(frames, gif_path)
 
 
 
 
 
290
  return gif_path, "Animation generated successfully!"
291
  except Exception as e:
 
292
  return None, f"Error generating animation: {str(e)}"
293
 
294
  # Create the Gradio interface
 
15
  import io
16
  import requests
17
  from transformers import DPTImageProcessor, DPTForDepthEstimation
18
+ import gc
19
 
20
  # Available models
21
  AVAILABLE_MODELS = {
 
29
  "Papercut": "TheLastBen/Papercut_SDXL",
30
  }
31
 
32
+ def get_device():
33
+ if torch.cuda.is_available():
34
+ return "cuda"
35
+ return "cpu"
36
+
37
+ def get_dtype():
38
+ if torch.cuda.is_available():
39
+ return torch.float16
40
+ return torch.float32
41
+
42
+ def cleanup_memory():
43
+ gc.collect()
44
+ if torch.cuda.is_available():
45
+ torch.cuda.empty_cache()
46
+
47
  def get_depth_map(image):
48
  # Initialize depth estimator
49
  depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas")
 
77
 
78
  def generate_image(prompt, seed, num_steps, guidance_scale, eta):
79
  try:
80
+ device = get_device()
81
+ dtype = get_dtype()
82
+
83
  # Initialize the pipeline
84
  base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
85
  tcd_lora_id = "h1t/TCD-SDXL-LoRA"
86
 
 
87
  pipe = StableDiffusionXLPipeline.from_pretrained(
88
  base_model_id,
89
+ torch_dtype=dtype,
90
+ use_safetensors=True,
91
+ variant="fp16" if device == "cuda" else None
92
+ ).to(device)
93
+
94
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
95
 
96
  # Load and fuse LoRA weights with prefix=None
 
98
  pipe.fuse_lora()
99
 
100
  # Generate the image
101
+ generator = torch.Generator(device=device).manual_seed(seed)
102
  image = pipe(
103
  prompt=prompt,
104
  num_inference_steps=num_steps,
 
107
  generator=generator,
108
  ).images[0]
109
 
110
+ # Cleanup
111
+ del pipe
112
+ cleanup_memory()
113
+
114
  return image, "Image generated successfully!"
115
  except Exception as e:
116
+ cleanup_memory()
117
  return None, f"Error generating image: {str(e)}"
118
 
119
  def generate_community_image(prompt, model_name, seed, num_steps, guidance_scale, eta):
120
  try:
121
+ device = get_device()
122
+ dtype = get_dtype()
123
+
124
  # Initialize the pipeline
125
  base_model_id = AVAILABLE_MODELS[model_name]
126
  tcd_lora_id = "h1t/TCD-SDXL-LoRA"
127
 
 
128
  pipe = StableDiffusionXLPipeline.from_pretrained(
129
  base_model_id,
130
+ torch_dtype=dtype,
131
+ use_safetensors=True,
132
+ variant="fp16" if device == "cuda" else None
133
+ ).to(device)
134
+
135
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
136
 
137
  # Load and fuse LoRA weights with prefix=None
 
139
  pipe.fuse_lora()
140
 
141
  # Generate the image
142
+ generator = torch.Generator(device=device).manual_seed(seed)
143
  image = pipe(
144
  prompt=prompt,
145
  num_inference_steps=num_steps,
 
148
  generator=generator,
149
  ).images[0]
150
 
151
+ # Cleanup
152
+ del pipe
153
+ cleanup_memory()
154
+
155
  return image, "Image generated successfully!"
156
  except Exception as e:
157
+ cleanup_memory()
158
  return None, f"Error generating image: {str(e)}"
159
 
160
  def generate_style_mix(prompt, seed, num_steps, guidance_scale, eta, style_weight):
161
  try:
162
+ device = get_device()
163
+ dtype = get_dtype()
164
+
165
  # Initialize the pipeline
166
  base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
167
  tcd_lora_id = "h1t/TCD-SDXL-LoRA"
168
  styled_lora_id = "TheLastBen/Papercut_SDXL"
169
 
 
170
  pipe = StableDiffusionXLPipeline.from_pretrained(
171
  base_model_id,
172
+ torch_dtype=dtype,
173
+ use_safetensors=True,
174
+ variant="fp16" if device == "cuda" else None
175
+ ).to(device)
176
+
177
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
178
 
179
  # Load multiple LoRA weights with prefix=None
 
184
  pipe.set_adapters(["tcd", "style"], adapter_weights=[1.0, style_weight])
185
 
186
  # Generate the image
187
+ generator = torch.Generator(device=device).manual_seed(seed)
188
  image = pipe(
189
  prompt=prompt,
190
  num_inference_steps=num_steps,
 
193
  generator=generator,
194
  ).images[0]
195
 
196
+ # Cleanup
197
+ del pipe
198
+ cleanup_memory()
199
+
200
  return image, "Image generated successfully!"
201
  except Exception as e:
202
+ cleanup_memory()
203
  return None, f"Error generating image: {str(e)}"
204
 
205
  def generate_controlnet(prompt, init_image, seed, num_steps, guidance_scale, eta, controlnet_scale):
206
  try:
207
+ device = get_device()
208
+ dtype = get_dtype()
209
+
210
  # Initialize the pipeline
211
  base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
212
  controlnet_id = "diffusers/controlnet-depth-sdxl-1.0"
 
215
  # Initialize ControlNet
216
  controlnet = ControlNetModel.from_pretrained(
217
  controlnet_id,
218
+ torch_dtype=dtype,
219
+ use_safetensors=True,
220
+ variant="fp16" if device == "cuda" else None
221
+ ).to(device)
222
 
223
  # Initialize pipeline
224
  pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
225
  base_model_id,
226
  controlnet=controlnet,
227
+ torch_dtype=dtype,
228
+ use_safetensors=True,
229
+ variant="fp16" if device == "cuda" else None
230
+ ).to(device)
231
+
232
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
233
 
234
  # Load and fuse LoRA weights with prefix=None
 
239
  depth_image = get_depth_map(init_image)
240
 
241
  # Generate the image
242
+ generator = torch.Generator(device=device).manual_seed(seed)
243
  image = pipe(
244
  prompt=prompt,
245
  image=depth_image,
 
252
 
253
  # Create a grid of the depth map and result
254
  grid = make_image_grid([depth_image, image], rows=1, cols=2)
255
+
256
+ # Cleanup
257
+ del pipe, controlnet
258
+ cleanup_memory()
259
+
260
  return grid, "Image generated successfully!"
261
  except Exception as e:
262
+ cleanup_memory()
263
  return None, f"Error generating image: {str(e)}"
264
 
265
  def inpaint_image(prompt, init_image, mask_image, seed, num_steps, guidance_scale, eta, strength):
266
  try:
267
+ device = get_device()
268
+ dtype = get_dtype()
269
+
270
  # Initialize the pipeline
271
  base_model_id = "diffusers/stable-diffusion-xl-1.0-inpainting-0.1"
272
  tcd_lora_id = "h1t/TCD-SDXL-LoRA"
273
 
 
274
  pipe = AutoPipelineForInpainting.from_pretrained(
275
  base_model_id,
276
+ torch_dtype=dtype,
277
+ use_safetensors=True,
278
+ variant="fp16" if device == "cuda" else None
279
+ ).to(device)
280
+
281
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
282
 
283
  # Load and fuse LoRA weights with prefix=None
 
285
  pipe.fuse_lora()
286
 
287
  # Generate the image
288
+ generator = torch.Generator(device=device).manual_seed(seed)
289
  image = pipe(
290
  prompt=prompt,
291
  image=init_image,
 
299
 
300
  # Create a grid of the original image, mask, and result
301
  grid = make_image_grid([init_image, mask_image, image], rows=1, cols=3)
302
+
303
+ # Cleanup
304
+ del pipe
305
+ cleanup_memory()
306
+
307
  return grid, "Image generated successfully!"
308
  except Exception as e:
309
+ cleanup_memory()
310
  return None, f"Error generating image: {str(e)}"
311
 
312
  def generate_animation(prompt, seed, num_steps, guidance_scale, eta, num_frames, motion_scale):
313
  try:
314
+ device = get_device()
315
+ dtype = get_dtype()
316
+
317
  # Initialize the pipeline
318
  base_model_id = "frankjoshua/toonyou_beta6"
319
  motion_adapter_id = "guoyww/animatediff-motion-adapter-v1-5"
 
321
  motion_lora_id = "guoyww/animatediff-motion-lora-zoom-in"
322
 
323
  # Load motion adapter
324
+ adapter = MotionAdapter.from_pretrained(motion_adapter_id).to(device)
325
 
326
+ # Initialize pipeline with optimization
327
  pipe = AnimateDiffPipeline.from_pretrained(
328
  base_model_id,
329
  motion_adapter=adapter,
330
+ torch_dtype=dtype,
331
+ low_cpu_mem_usage=True,
332
+ use_safetensors=True,
333
+ variant="fp16" if device == "cuda" else None
334
+ ).to(device)
335
 
336
  # Set TCD scheduler
337
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
 
348
  pipe.set_adapters(["tcd", "motion-lora"], adapter_weights=[1.0, motion_scale])
349
 
350
  # Generate animation
351
+ generator = torch.Generator(device=device).manual_seed(seed)
352
  frames = pipe(
353
  prompt=prompt,
354
  num_inference_steps=num_steps,
 
362
  # Export to GIF
363
  gif_path = "animation.gif"
364
  export_to_gif(frames, gif_path)
365
+
366
+ # Cleanup
367
+ del pipe, adapter
368
+ cleanup_memory()
369
+
370
  return gif_path, "Animation generated successfully!"
371
  except Exception as e:
372
+ cleanup_memory()
373
  return None, f"Error generating animation: {str(e)}"
374
 
375
  # Create the Gradio interface