DawnC commited on
Commit
2974931
·
verified ·
1 Parent(s): 3164a7a

Update inpainting_module.py

Browse files
Files changed (1) hide show
  1. inpainting_module.py +49 -14
inpainting_module.py CHANGED
@@ -1,5 +1,6 @@
1
  import gc
2
  import logging
 
3
  import time
4
  import traceback
5
  from dataclasses import dataclass, field
@@ -899,21 +900,37 @@ class InpaintingModule:
899
  self._last_seed = seed
900
  generator = torch.Generator(device=self.device).manual_seed(seed)
901
 
 
 
 
902
  # Stage 1: Preview generation
903
- if progress_callback:
904
- progress_callback("Generating preview...", 30)
905
 
906
- preview_result = self._generate_inpaint(
907
- image=image,
908
- mask=processed_mask,
909
- control_image=control_image,
910
- prompt=enhanced_prompt,
911
- negative_prompt=negative_prompt,
912
- num_inference_steps=self.config.preview_steps,
913
- guidance_scale=self.config.preview_guidance_scale,
914
- controlnet_conditioning_scale=conditioning_scale,
915
- generator=generator
916
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
917
 
918
  if preview_only:
919
  generation_time = time.time() - start_time
@@ -942,6 +959,24 @@ class InpaintingModule:
942
  num_steps = kwargs.get('num_inference_steps', self.config.num_inference_steps)
943
  guidance = kwargs.get('guidance_scale', self.config.guidance_scale)
944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
945
  full_result = self._generate_inpaint(
946
  image=image,
947
  mask=processed_mask,
@@ -1308,4 +1343,4 @@ class InpaintingModule:
1308
 
1309
  status["memory"] = self._check_memory_status()
1310
 
1311
- return status
 
1
  import gc
2
  import logging
3
+ import os
4
  import time
5
  import traceback
6
  from dataclasses import dataclass, field
 
900
  self._last_seed = seed
901
  generator = torch.Generator(device=self.device).manual_seed(seed)
902
 
903
+ # Check if running on Hugging Face Spaces
904
+ is_spaces = os.getenv('SPACE_ID') is not None
905
+
906
  # Stage 1: Preview generation
907
+ # On Spaces, skip preview to save time (300s hard limit)
908
+ preview_result = None
909
 
910
+ if preview_only or not is_spaces:
911
+ if progress_callback:
912
+ progress_callback("Generating preview...", 30)
913
+
914
+ # Optimize preview steps for Hugging Face Spaces
915
+ preview_steps = self.config.preview_steps
916
+ if is_spaces:
917
+ # On Spaces, use minimal preview steps
918
+ preview_steps = min(preview_steps, 8)
919
+ logger.info(f"Spaces environment - using {preview_steps} preview steps")
920
+
921
+ preview_result = self._generate_inpaint(
922
+ image=image,
923
+ mask=processed_mask,
924
+ control_image=control_image,
925
+ prompt=enhanced_prompt,
926
+ negative_prompt=negative_prompt,
927
+ num_inference_steps=preview_steps,
928
+ guidance_scale=self.config.preview_guidance_scale,
929
+ controlnet_conditioning_scale=conditioning_scale,
930
+ generator=generator
931
+ )
932
+ else:
933
+ logger.info("Spaces environment - skipping preview to fit 300s limit")
934
 
935
  if preview_only:
936
  generation_time = time.time() - start_time
 
959
  num_steps = kwargs.get('num_inference_steps', self.config.num_inference_steps)
960
  guidance = kwargs.get('guidance_scale', self.config.guidance_scale)
961
 
962
+ # Optimize for Hugging Face Spaces ZeroGPU (stateless, 300s hard limit)
963
+ if is_spaces:
964
+ # ZeroGPU timing breakdown (stateless architecture):
965
+ # - Model loading: ~90-120s (SDXL + ControlNet transfer to GPU)
966
+ # - Inference: ~15s/step (slower on shared H200 vs dedicated L4)
967
+ # - Platform limit: 300s hard limit (Pro tier)
968
+ #
969
+ # Strategy: Skip preview stage, use all time for single-stage generation
970
+ # Time budget: 300s - 120s (loading) - 15s (overhead) = 165s available
971
+ # Safe steps: 165s / 15s = 11 steps maximum
972
+ spaces_max_steps = 11 # Balanced quality vs. time constraint
973
+
974
+ if num_steps > spaces_max_steps:
975
+ original_steps = num_steps
976
+ num_steps = spaces_max_steps
977
+ logger.info(f"Spaces ZeroGPU optimization - reduced steps: {original_steps} → {num_steps}")
978
+ logger.info(f"Estimated total time: ~{120 + num_steps * 15}s (within 300s limit)")
979
+
980
  full_result = self._generate_inpaint(
981
  image=image,
982
  mask=processed_mask,
 
1343
 
1344
  status["memory"] = self._check_memory_status()
1345
 
1346
+ return status