alphabagibagi commited on
Commit
8f579e1
Β·
verified Β·
1 Parent(s): 2a8c4fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -5
app.py CHANGED
@@ -145,11 +145,27 @@ setup_environment()
145
 
146
  # --- ComfyUI Imports ---
147
  # Configure Execution Arguments for ComfyUI
148
- if not torch.cuda.is_available():
149
- print("⚠️ CUDA not available, forcing CPU mode for ComfyUI...")
150
- # Inject --cpu argument so ComfyUI optimizes for CPU instead of crashing on CUDA init
151
- if "--cpu" not in sys.argv:
152
- sys.argv.append("--cpu")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
 
154
  # These must happen AFTER setup because ComfyUI folder might not exist before
155
  try:
 
145
 
146
  # --- ComfyUI Imports ---
147
  # Configure Execution Arguments for ComfyUI
148
+ # Aggressively force CPU if CUDA is not available or if we want to ensure no crashes on CPU Spaces
149
+ try:
150
+ if not torch.cuda.is_available():
151
+ print("⚠️ CUDA not available, forcing CPU mode for ComfyUI...")
152
+
153
+ # 1. Force environment variable
154
+ os.environ["CUDA_VISIBLE_DEVICES"] = ""
155
+
156
+ # 2. Inject --cpu argument
157
+ if "--cpu" not in sys.argv:
158
+ sys.argv.append("--cpu")
159
+
160
+ # 3. Monkeypatch torch.cuda to ensure ComfyUI doesn't try to initialize CUDA
161
+ # This is necessary because some ComfyUI versions checks might be aggressive
162
+ torch.cuda.is_available = lambda: False
163
+ torch.cuda.device_count = lambda: 0
164
+ torch.cuda.current_device = lambda: None
165
+
166
+ print("βœ… Applied CPU enforcement patches.")
167
+ except Exception as e:
168
+ print(f"⚠️ Error applying CPU patches: {e}")
169
 
170
  # These must happen AFTER setup because ComfyUI folder might not exist before
171
  try: