Borcherding commited on
Commit
97a48f9
·
verified ·
1 Parent(s): a436bfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -72
app.py CHANGED
@@ -3,7 +3,6 @@ import numpy as np
3
  import spaces
4
  import torch
5
  import random
6
- import gc
7
  from peft import PeftModel
8
  from diffusers import FluxControlPipeline, FluxTransformer2DModel
9
  from image_gen_aux import DepthPreprocessor
@@ -11,107 +10,69 @@ from image_gen_aux import DepthPreprocessor
11
  MAX_SEED = np.iinfo(np.int32).max
12
  MAX_IMAGE_SIZE = 2048
13
 
14
- def init_pipeline():
15
- """Initialize pipeline with memory-efficient settings"""
16
- pipe = FluxControlPipeline.from_pretrained(
17
- "black-forest-labs/FLUX.1-Depth-dev",
18
- torch_dtype=torch.bfloat16,
19
- low_cpu_mem_usage=True,
20
- use_safetensors=True
21
- )
22
- return pipe
23
-
24
- # Initialize models without moving to CUDA
25
- pipe = init_pipeline()
26
  processor = DepthPreprocessor.from_pretrained("LiheYoung/depth-anything-large-hf")
27
 
28
- def cleanup_memory():
29
- """Aggressive memory cleanup"""
30
- if torch.cuda.is_available():
31
- with torch.cuda.device('cuda'):
32
- torch.cuda.empty_cache()
33
- torch.cuda.ipc_collect()
34
- gc.collect()
35
-
36
- def reinit_pipeline():
37
- """Reinitialize the pipeline if needed"""
38
- global pipe
39
- cleanup_memory()
40
- pipe = init_pipeline()
41
- cleanup_memory()
42
-
43
  @spaces.GPU
44
  def load_lora(lora_path):
45
- global pipe
46
  if not lora_path.strip():
47
  return "Please provide a valid LoRA path"
48
  try:
49
- cleanup_memory()
 
50
 
51
- # Reinitialize pipeline
52
- reinit_pipeline()
53
-
54
- # Enable sequential CPU offload
55
- pipe.enable_sequential_cpu_offload()
56
-
57
- # Load LoRA weights
58
  pipe.load_lora_weights(lora_path)
59
-
60
- cleanup_memory()
61
  return f"Successfully loaded LoRA weights from {lora_path}"
62
  except Exception as e:
63
- cleanup_memory()
64
  return f"Error loading LoRA weights: {str(e)}"
65
 
66
  @spaces.GPU
67
  def unload_lora():
68
- global pipe
69
  try:
70
- cleanup_memory()
71
- reinit_pipeline()
72
- pipe.enable_sequential_cpu_offload()
73
  pipe.unload_lora_weights()
74
- cleanup_memory()
75
  return "Successfully unloaded LoRA weights"
76
  except Exception as e:
77
- cleanup_memory()
78
  return f"Error unloading LoRA weights: {str(e)}"
79
 
80
- def round_to_multiple(number, multiple):
81
- return multiple * round(number / multiple)
82
-
83
  @spaces.GPU
84
  def infer(control_image, prompt, seed=42, randomize_seed=False, width=1024, height=1024,
85
  guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
 
 
 
 
86
  try:
87
- cleanup_memory()
88
-
89
- if randomize_seed:
90
- seed = random.randint(0, MAX_SEED)
91
-
92
- # Ensure dimensions are divisible by 16
93
- width = round_to_multiple(width, 16)
94
- height = round_to_multiple(height, 16)
95
 
96
  # Process control image
97
  control_image = processor(control_image)[0].convert("RGB")
98
 
99
- # Generate image with memory optimization
100
- with torch.inference_mode(), torch.cuda.amp.autocast():
101
- image = pipe(
102
- prompt=prompt,
103
- control_image=control_image,
104
- height=height,
105
- width=width,
106
- num_inference_steps=num_inference_steps,
107
- guidance_scale=guidance_scale,
108
- generator=torch.Generator("cuda").manual_seed(seed),
109
- ).images[0]
110
 
111
- cleanup_memory()
112
  return image, seed
113
  except Exception as e:
114
- cleanup_memory()
115
  return None, f"Error during inference: {str(e)}"
116
 
117
  css="""
@@ -122,6 +83,7 @@ css="""
122
  """
123
 
124
  with gr.Blocks(css=css) as demo:
 
125
  with gr.Column(elem_id="col-container"):
126
  gr.Markdown(f"""# FLUX.1 Depth [dev] with LoRA Support
127
  12B param rectified flow transformer structural conditioning tuned, guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
@@ -169,7 +131,7 @@ with gr.Blocks(css=css) as demo:
169
  label="Width",
170
  minimum=256,
171
  maximum=MAX_IMAGE_SIZE,
172
- step=16,
173
  value=1024,
174
  )
175
 
@@ -177,7 +139,7 @@ with gr.Blocks(css=css) as demo:
177
  label="Height",
178
  minimum=256,
179
  maximum=MAX_IMAGE_SIZE,
180
- step=16,
181
  value=1024,
182
  )
183
 
 
3
  import spaces
4
  import torch
5
  import random
 
6
  from peft import PeftModel
7
  from diffusers import FluxControlPipeline, FluxTransformer2DModel
8
  from image_gen_aux import DepthPreprocessor
 
10
  MAX_SEED = np.iinfo(np.int32).max
11
  MAX_IMAGE_SIZE = 2048
12
 
13
+ # Initialize models without moving to CUDA yet
14
+ pipe = FluxControlPipeline.from_pretrained(
15
+ "black-forest-labs/FLUX.1-Depth-dev",
16
+ torch_dtype=torch.bfloat16
17
+ )
 
 
 
 
 
 
 
18
  processor = DepthPreprocessor.from_pretrained("LiheYoung/depth-anything-large-hf")
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  @spaces.GPU
21
  def load_lora(lora_path):
 
22
  if not lora_path.strip():
23
  return "Please provide a valid LoRA path"
24
  try:
25
+ # Move to GPU within the wrapped function
26
+ pipe.to("cuda")
27
 
28
+ # Unload any existing LoRA weights first
29
+ try:
30
+ pipe.unload_lora_weights()
31
+ except:
32
+ pass
33
+
34
+ # Load new LoRA weights
35
  pipe.load_lora_weights(lora_path)
 
 
36
  return f"Successfully loaded LoRA weights from {lora_path}"
37
  except Exception as e:
 
38
  return f"Error loading LoRA weights: {str(e)}"
39
 
40
  @spaces.GPU
41
  def unload_lora():
 
42
  try:
43
+ pipe.to("cuda")
 
 
44
  pipe.unload_lora_weights()
 
45
  return "Successfully unloaded LoRA weights"
46
  except Exception as e:
 
47
  return f"Error unloading LoRA weights: {str(e)}"
48
 
 
 
 
49
  @spaces.GPU
50
  def infer(control_image, prompt, seed=42, randomize_seed=False, width=1024, height=1024,
51
  guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
52
+
53
+ if randomize_seed:
54
+ seed = random.randint(0, MAX_SEED)
55
+
56
  try:
57
+ # Move pipeline to GPU within the wrapped function
58
+ pipe.to("cuda")
 
 
 
 
 
 
59
 
60
  # Process control image
61
  control_image = processor(control_image)[0].convert("RGB")
62
 
63
+ # Generate image
64
+ image = pipe(
65
+ prompt=prompt,
66
+ control_image=control_image,
67
+ height=height,
68
+ width=width,
69
+ num_inference_steps=num_inference_steps,
70
+ guidance_scale=guidance_scale,
71
+ generator=torch.Generator("cuda").manual_seed(seed),
72
+ ).images[0]
 
73
 
 
74
  return image, seed
75
  except Exception as e:
 
76
  return None, f"Error during inference: {str(e)}"
77
 
78
  css="""
 
83
  """
84
 
85
  with gr.Blocks(css=css) as demo:
86
+
87
  with gr.Column(elem_id="col-container"):
88
  gr.Markdown(f"""# FLUX.1 Depth [dev] with LoRA Support
89
  12B param rectified flow transformer structural conditioning tuned, guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
 
131
  label="Width",
132
  minimum=256,
133
  maximum=MAX_IMAGE_SIZE,
134
+ step=32,
135
  value=1024,
136
  )
137
 
 
139
  label="Height",
140
  minimum=256,
141
  maximum=MAX_IMAGE_SIZE,
142
+ step=32,
143
  value=1024,
144
  )
145