DawnC commited on
Commit
6ec2fb5
·
verified ·
1 Parent(s): b375297

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -7
app.py CHANGED
@@ -6,6 +6,45 @@ warnings.filterwarnings("ignore")
6
 
7
  from ui_manager import UIManager
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def launch_final_blend_sceneweaver(share: bool = True, debug: bool = False):
10
  """Launch SceneWeaver Application"""
11
 
@@ -13,6 +52,9 @@ def launch_final_blend_sceneweaver(share: bool = True, debug: bool = False):
13
  print("✨ AI-Powered Image Background Generation")
14
 
15
  try:
 
 
 
16
  # Test imports first
17
  print("🔍 Testing imports...")
18
  try:
@@ -21,13 +63,11 @@ def launch_final_blend_sceneweaver(share: bool = True, debug: bool = False):
21
  ui = UIManager()
22
  print("✅ UIManager instance created successfully")
23
 
24
- # Note: On Hugging Face Spaces, models will be loaded on-demand
25
- # within @spaces.GPU decorated functions to comply with ZeroGPU requirements
26
  if os.getenv('SPACE_ID'):
27
  print("\n🔧 Detected Hugging Face Spaces environment")
28
- print("⚡ Models will load on-demand (ZeroGPU stateless requirement)")
29
- print(" First inpainting request: ~7 steps (280s)")
30
- print(" Subsequent requests: ~11 steps (245s)")
31
  print()
32
 
33
  # Launch UI
@@ -88,5 +128,4 @@ def main():
88
  raise
89
 
90
  if __name__ == "__main__":
91
- main()
92
-
 
6
 
7
  from ui_manager import UIManager
8
 
9
+ def preload_models_to_cache():
10
+ """
11
+ Pre-download models to HuggingFace cache before GPU allocation.
12
+ This runs on CPU and avoids downloading during @spaces.GPU execution.
13
+ """
14
+ if not os.getenv('SPACE_ID'):
15
+ return # Skip if not on Spaces
16
+
17
+ print("📦 Pre-downloading models to cache (CPU only, no GPU usage)...")
18
+
19
+ try:
20
+ from diffusers import ControlNetModel
21
+ import torch
22
+
23
+ # Pre-download ControlNet models to cache
24
+ models_to_cache = [
25
+ ("diffusers/controlnet-canny-sdxl-1.0", "Canny ControlNet"),
26
+ ("diffusers/controlnet-depth-sdxl-1.0", "Depth ControlNet"),
27
+ ]
28
+
29
+ for model_id, model_name in models_to_cache:
30
+ print(f" ⬇️ Downloading {model_name} ({model_id})...")
31
+ try:
32
+ _ = ControlNetModel.from_pretrained(
33
+ model_id,
34
+ torch_dtype=torch.float16,
35
+ use_safetensors=True,
36
+ local_files_only=False # Allow download
37
+ )
38
+ print(f" ✅ {model_name} cached")
39
+ except Exception as e:
40
+ print(f" ⚠️ {model_name} download failed (will retry on-demand): {e}")
41
+
42
+ print("✅ Model pre-caching complete")
43
+
44
+ except Exception as e:
45
+ print(f"⚠️ Model pre-caching failed: {e}")
46
+ print(" Models will be downloaded on first use instead.")
47
+
48
  def launch_final_blend_sceneweaver(share: bool = True, debug: bool = False):
49
  """Launch SceneWeaver Application"""
50
 
 
52
  print("✨ AI-Powered Image Background Generation")
53
 
54
  try:
55
+ # Pre-download models on Spaces to avoid downloading during GPU time
56
+ preload_models_to_cache()
57
+
58
  # Test imports first
59
  print("🔍 Testing imports...")
60
  try:
 
63
  ui = UIManager()
64
  print("✅ UIManager instance created successfully")
65
 
66
+ # Note: On Hugging Face Spaces, models are pre-cached at startup
 
67
  if os.getenv('SPACE_ID'):
68
  print("\n🔧 Detected Hugging Face Spaces environment")
69
+ print("⚡ Models pre-cached - ready for fast inference")
70
+ print(" Expected inference time: ~300-350s (with cached models)")
 
71
  print()
72
 
73
  # Launch UI
 
128
  raise
129
 
130
  if __name__ == "__main__":
131
+ main()