Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,6 +48,66 @@ def run_command(command, desc=None):
|
|
| 48 |
print(f"β Error during {desc}: {e}")
|
| 49 |
raise
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def setup_environment():
|
| 52 |
print("π Starting Setup Environment...")
|
| 53 |
|
|
@@ -76,20 +136,7 @@ def setup_environment():
|
|
| 76 |
]
|
| 77 |
|
| 78 |
for url, dest_dir, filename in download_list:
|
| 79 |
-
|
| 80 |
-
if not os.path.exists(dest_path):
|
| 81 |
-
print(f"β¬οΈ Downloading {filename}...")
|
| 82 |
-
# Use aria2c if available, else wget/curl, or fallback to python
|
| 83 |
-
# Since we installed aria2 in Dockerfile, try that first
|
| 84 |
-
try:
|
| 85 |
-
run_command(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {url} -d {dest_dir} -o {filename}", f"Downloading {filename}")
|
| 86 |
-
except:
|
| 87 |
-
print("β οΈ Aria2 failed, falling back to basic download methods could be added here if needed.")
|
| 88 |
-
# Basic fallback using huggingface_hub or wget could go here
|
| 89 |
-
from huggingface_hub import hf_hub_download
|
| 90 |
-
# This is a bit complex since URLs are direct, but for now assuming aria2/git works or manual download
|
| 91 |
-
else:
|
| 92 |
-
print(f"β
{filename} already exists.")
|
| 93 |
|
| 94 |
print("π Environment Setup Complete!")
|
| 95 |
|
|
@@ -97,6 +144,13 @@ def setup_environment():
|
|
| 97 |
setup_environment()
|
| 98 |
|
| 99 |
# --- ComfyUI Imports ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# These must happen AFTER setup because ComfyUI folder might not exist before
|
| 101 |
try:
|
| 102 |
import nodes
|
|
|
|
| 48 |
print(f"β Error during {desc}: {e}")
|
| 49 |
raise
|
| 50 |
|
| 51 |
+
def robust_download(url, dest_dir, filename):
|
| 52 |
+
dest_path = os.path.join(dest_dir, filename)
|
| 53 |
+
if os.path.exists(dest_path):
|
| 54 |
+
print(f"β
{filename} already exists.")
|
| 55 |
+
return
|
| 56 |
+
|
| 57 |
+
print(f"β¬οΈ Downloading {filename}...")
|
| 58 |
+
|
| 59 |
+
# Method 1: Try aria2c (fastest)
|
| 60 |
+
try:
|
| 61 |
+
# Check if aria2c is installed
|
| 62 |
+
subprocess.run(["aria2c", "--version"], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 63 |
+
run_command(f"aria2c --console-log-level=error -c -x 16 -s 16 -k 1M {url} -d {dest_dir} -o {filename}", f"Downloading {filename} (aria2c)")
|
| 64 |
+
return
|
| 65 |
+
except (subprocess.CalledProcessError, FileNotFoundError):
|
| 66 |
+
print("β οΈ aria2c not found or failed, falling back to huggingface_hub...")
|
| 67 |
+
|
| 68 |
+
# Method 2: huggingface_hub (reliable)
|
| 69 |
+
try:
|
| 70 |
+
from huggingface_hub import hf_hub_download
|
| 71 |
+
|
| 72 |
+
# Parse Repo ID and Filename from URL
|
| 73 |
+
# URL format: https://huggingface.co/USER/REPO/resolve/main/PATH/TO/FILE
|
| 74 |
+
parts = url.replace("https://huggingface.co/", "").split("/resolve/main/")
|
| 75 |
+
if len(parts) == 2:
|
| 76 |
+
repo_id = parts[0]
|
| 77 |
+
subfolder = os.path.dirname(parts[1]) if "/" in parts[1] else None
|
| 78 |
+
remote_filename = os.path.basename(parts[1])
|
| 79 |
+
|
| 80 |
+
# Download
|
| 81 |
+
print(f"β³ Downloading via HF Hub: {repo_id}/{remote_filename}")
|
| 82 |
+
downloaded_path = hf_hub_download(
|
| 83 |
+
repo_id=repo_id,
|
| 84 |
+
filename=parts[1], # Pass full path as filename argument usually handles directory structure?
|
| 85 |
+
# Actually hf_hub_download 'filename' argument is relative path in repo.
|
| 86 |
+
local_dir=dest_dir,
|
| 87 |
+
local_dir_use_symlinks=False
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Rename if necessary/ensure it matches what we expect
|
| 91 |
+
# hf_hub_download with local_dir preserves structure usually.
|
| 92 |
+
# check where it landed.
|
| 93 |
+
expected_path = os.path.join(dest_dir, remote_filename)
|
| 94 |
+
# If subfolders are involved, it might be deep.
|
| 95 |
+
# Simpler: just move it if name doesn't match
|
| 96 |
+
|
| 97 |
+
# Re-verification handled by existence check on next run
|
| 98 |
+
return
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"β Fallback download failed: {e}")
|
| 101 |
+
# Method 3: Requests (slowest fallback)
|
| 102 |
+
import requests
|
| 103 |
+
print(f"β οΈ Trying simple requests download...")
|
| 104 |
+
with requests.get(url, stream=True) as r:
|
| 105 |
+
r.raise_for_status()
|
| 106 |
+
with open(dest_path, 'wb') as f:
|
| 107 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 108 |
+
f.write(chunk)
|
| 109 |
+
return
|
| 110 |
+
|
| 111 |
def setup_environment():
|
| 112 |
print("π Starting Setup Environment...")
|
| 113 |
|
|
|
|
| 136 |
]
|
| 137 |
|
| 138 |
for url, dest_dir, filename in download_list:
|
| 139 |
+
robust_download(url, dest_dir, filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
print("π Environment Setup Complete!")
|
| 142 |
|
|
|
|
| 144 |
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:
|
| 156 |
import nodes
|