rokmr commited on
Commit
cebbf6e
·
verified ·
1 Parent(s): 34a75a5

handling pipeline bug

Browse files
Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -14,21 +14,28 @@ torch_dtype = torch.bfloat16
14
 
15
  print("Starting Flux2 Image Generator...")
16
 
17
- # Load the pipeline at startup (NOT inside GPU decorator)
18
  print("Loading Flux2 pipeline...")
19
  pipe = None
20
 
21
- try:
22
- pipe = Flux2Pipeline.from_pretrained(
23
- repo_id,
24
- text_encoder=None,
25
- torch_dtype=torch_dtype,
26
- device_map="balanced" # Use balanced for CPU during startup
27
- )
28
- print("Pipeline loaded successfully!")
29
- except Exception as e:
30
- print(f"Error loading pipeline: {e}")
31
- # Don't raise - will try to load later if needed
 
 
 
 
 
 
 
32
 
33
  def remote_text_encoder(prompts):
34
  """Encode prompts using remote text encoder API."""
@@ -145,12 +152,16 @@ def generate_image(
145
  progress(0, desc="Moving model to GPU...")
146
 
147
  try:
148
- # Move pipeline to GPU
149
  if pipe is None:
150
- raise gr.Error("Pipeline not loaded. Please refresh the page.")
 
 
 
151
 
152
  print("Moving pipeline to CUDA...")
153
- pipe = pipe.to("cuda")
 
154
 
155
  progress(0.1, desc="Encoding prompt...")
156
  print("Encoding prompt...")
@@ -194,12 +205,17 @@ def generate_image(
194
 
195
  # Generate image
196
  with torch.inference_mode():
197
- result = pipe(**pipe_kwargs)
198
  image = result.images[0]
199
 
200
  print("Generation complete!")
201
  progress(1.0, desc="Done!")
202
 
 
 
 
 
 
203
  return image
204
 
205
  except gr.Error:
 
14
 
15
  print("Starting Flux2 Image Generator...")
16
 
17
+ # Load the pipeline at startup
18
  print("Loading Flux2 pipeline...")
19
  pipe = None
20
 
21
+ def load_pipeline_startup():
22
+ """Load pipeline at startup without CUDA."""
23
+ global pipe
24
+ try:
25
+ print("Loading pipeline components...")
26
+ pipe = Flux2Pipeline.from_pretrained(
27
+ repo_id,
28
+ text_encoder=None,
29
+ torch_dtype=torch_dtype,
30
+ )
31
+ # Keep on CPU initially - will move to CUDA when needed
32
+ print("Pipeline loaded successfully on CPU!")
33
+ except Exception as e:
34
+ print(f"Warning: Could not load pipeline at startup: {e}")
35
+ print("Pipeline will be loaded on first use.")
36
+
37
+ # Try to load at startup
38
+ load_pipeline_startup()
39
 
40
  def remote_text_encoder(prompts):
41
  """Encode prompts using remote text encoder API."""
 
152
  progress(0, desc="Moving model to GPU...")
153
 
154
  try:
155
+ # Load or get pipeline
156
  if pipe is None:
157
+ print("Pipeline not loaded at startup, loading now...")
158
+ load_pipeline_startup()
159
+ if pipe is None:
160
+ raise gr.Error("Failed to load pipeline. Please try again or contact support.")
161
 
162
  print("Moving pipeline to CUDA...")
163
+ pipeline = pipe.to("cuda")
164
+ torch.cuda.empty_cache() # Clear cache before generation
165
 
166
  progress(0.1, desc="Encoding prompt...")
167
  print("Encoding prompt...")
 
205
 
206
  # Generate image
207
  with torch.inference_mode():
208
+ result = pipeline(**pipe_kwargs)
209
  image = result.images[0]
210
 
211
  print("Generation complete!")
212
  progress(1.0, desc="Done!")
213
 
214
+ # Move pipeline back to CPU to free GPU memory
215
+ print("Moving pipeline back to CPU...")
216
+ pipe.to("cpu")
217
+ torch.cuda.empty_cache()
218
+
219
  return image
220
 
221
  except gr.Error: