alexnasa commited on
Commit
fcd1803
·
verified ·
1 Parent(s): a8cd550

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -418,36 +418,40 @@ def apply_prompt(meta_data, new_prompt):
418
 
419
  @spaces.GPU(duration=30)
420
  def on_image_change(filepath):
421
- # Extract the filename without extension
422
  filename = os.path.splitext(os.path.basename(filepath))[0]
423
 
424
- if filename in ["example1", "example3", "example4"]:
 
425
 
426
- meta_data_raw = load_state_from_file(f"assets/{filename}-turbo.pkl")
427
-
428
- global weights
429
- _, _, _, weights = meta_data_raw
430
-
431
- global num_inference_steps
432
- num_inference_steps = 10
433
- scale_value = 7
434
 
435
- if filename == "example1":
436
- scale_value = 8
437
- new_prompt = "a photo of a tree, summer, colourful"
438
 
439
- elif filename == "example3":
440
- scale_value = 6
441
- new_prompt = "a realistic photo of a female warrior, flowing dark purple or black hair, bronze shoulder armour, leather chest piece, sky background with clouds"
442
 
443
- elif filename == "example4":
444
- scale_value = 13
445
- new_prompt = "a photo of plastic bottle on some sand, beach background, sky background"
 
 
 
 
 
 
 
 
446
 
447
- update_scale(scale_value)
448
- img = apply_prompt(meta_data_raw, new_prompt)
449
 
450
- return filepath, img, meta_data_raw, num_inference_steps, scale_value, scale_value
 
451
 
452
 
453
  def update_value(value, layer_type, resolution, depth):
 
418
 
419
  @spaces.GPU(duration=30)
420
  def on_image_change(filepath):
 
421
  filename = os.path.splitext(os.path.basename(filepath))[0]
422
 
423
+ if filename not in ["example1","example3","example4"]:
424
+ return filepath, None, None, None, None, None
425
 
426
+ # 1) load the raw state (might contain CUDA tensors!)
427
+ caption, real_latents, inversed_latents, saved_weights = load_state_from_file(
428
+ f"assets/{filename}-turbo.pkl"
429
+ )
 
 
 
 
430
 
431
+ # 2) immediately move all tensors to CPU
432
+ real_cpu = real_latents.detach().cpu()
433
+ inversed_cpu= [x.detach().cpu() for x in inversed_latents]
434
 
435
+ # 3) repack a truly CPU-only state
436
+ cpu_meta_data = (caption, real_cpu, inversed_cpu, saved_weights)
 
437
 
438
+ # 4) your existing logic can then run on GPU, but it will
439
+ # consume only CPU tensors and return only CPU tensors
440
+ _, _, _, global_weights = cpu_meta_data
441
+ global weights; weights = global_weights
442
+ num_inference_steps = 10
443
+ if filename == "example1":
444
+ scale_value, new_prompt = 8, "a photo of a tree, summer, colourful"
445
+ elif filename == "example3":
446
+ scale_value, new_prompt = 6, "a realistic photo of a female warrior, ..."
447
+ else:
448
+ scale_value, new_prompt = 13, "a photo of plastic bottle on some sand, ..."
449
 
450
+ update_scale(scale_value)
451
+ img = apply_prompt(cpu_meta_data, new_prompt)
452
 
453
+ # 5) return only CPU objects (img is still a numpy array)
454
+ return filepath, img, cpu_meta_data, num_inference_steps, scale_value, scale_value
455
 
456
 
457
  def update_value(value, layer_type, resolution, depth):