Nathyboy commited on
Commit
313946a
·
verified ·
1 Parent(s): 7c6f51d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -1
app.py CHANGED
@@ -20,4 +20,94 @@ DOWNLOADS = {
20
  "dest": "stable-diffusion-webui/extensions/ControlNet/models/control_v11f1e_sd15_tile.pth"
21
  },
22
  "temporalnet": {
23
- "url": "https://huggingface.co/CiaraRowles/TemporalNet/resolve/main/diff_control_sd15_temporal_
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  "dest": "stable-diffusion-webui/extensions/ControlNet/models/control_v11f1e_sd15_tile.pth"
21
  },
22
  "temporalnet": {
23
+ "url": "https://huggingface.co/CiaraRowles/TemporalNet/resolve/main/diff_control_sd15_temporalnet_fp16.safetensors",
24
+ "dest": "stable-diffusion-webui/extensions/ControlNet/models/diff_control_sd15_temporalnet_fp16.safetensors"
25
+ },
26
+ "civitai_model": {
27
+ "url": "https://civitai.com/api/download/models/143906?type=Model&format=SafeTensor&size=pruned&fp=fp16",
28
+ "dest": "stable-diffusion-webui/models/Stable-diffusion/civitai_model.safetensors"
29
+ }
30
+ }
31
+
32
+ WEBUI_ARGS = "--listen --xformers --enable-insecure-extension-access --medvram"
33
+ LAUNCH_PY = "launch.py" # relative to repo root
34
+
35
+ # -----------------------------
36
+ # Utilities
37
+ # -----------------------------
38
+ def ensure_persistent_folders():
39
+ """Ensure HF tree persistent folders exist."""
40
+ folders = [
41
+ "stable-diffusion-webui/deforum/input",
42
+ "stable-diffusion-webui/deforum/output_committed/frames",
43
+ "stable-diffusion-webui/deforum/output_committed/video",
44
+ "stable-diffusion-webui/models/Stable-diffusion",
45
+ "stable-diffusion-webui/models/Lora",
46
+ "stable-diffusion-webui/extensions/ControlNet/models",
47
+ ]
48
+ for f in folders:
49
+ os.makedirs(f, exist_ok=True)
50
+ print("✅ Persistent folders ensured.")
51
+
52
+ def download_file_with_retries(url, dest, max_retries=3, backoff=5):
53
+ """Download a file with retries, skip if exists."""
54
+ if os.path.exists(dest):
55
+ print(f"✅ Already exists: {dest}")
56
+ return True
57
+
58
+ os.makedirs(os.path.dirname(dest), exist_ok=True)
59
+ attempt = 1
60
+ while attempt <= max_retries:
61
+ try:
62
+ print(f"⬇️ Download attempt {attempt}: {url} -> {dest}")
63
+ with requests.get(url, stream=True, timeout=60) as r:
64
+ r.raise_for_status()
65
+ with open(dest + ".part", "wb") as f:
66
+ for chunk in r.iter_content(chunk_size=8192):
67
+ if chunk:
68
+ f.write(chunk)
69
+ os.replace(dest + ".part", dest)
70
+ print(f"✅ Downloaded: {dest}")
71
+ return True
72
+ except Exception as e:
73
+ print(f"⚠️ Download failed (attempt {attempt}) for {url}: {e}")
74
+ attempt += 1
75
+ time.sleep(backoff * attempt)
76
+ print(f"❌ Failed to download after {max_retries} attempts: {url}")
77
+ if os.path.exists(dest + ".part"):
78
+ os.remove(dest + ".part")
79
+ return False
80
+
81
+ def fetch_models():
82
+ """Download all configured models into HF tree persistent paths."""
83
+ print("🔽 Starting runtime model downloads (if missing)...")
84
+ success_all = True
85
+ for name, info in DOWNLOADS.items():
86
+ ok = download_file_with_retries(info["url"], info["dest"])
87
+ success_all = success_all and ok
88
+ if success_all:
89
+ print("✅ All runtime downloads done (or already present).")
90
+ else:
91
+ print("⚠️ Some runtime downloads failed. Check logs.")
92
+
93
+ # -----------------------------
94
+ # Start WebUI routine (Option B)
95
+ # -----------------------------
96
+ WEBUI_LINK = None
97
+
98
+ def start_webui():
99
+ """Launch WebUI in background and detect ready URL."""
100
+ global WEBUI_LINK
101
+ ensure_persistent_folders()
102
+ fetch_models()
103
+
104
+ args = ["python", LAUNCH_PY] + shlex.split(WEBUI_ARGS)
105
+ print("▶️ Launching WebUI with:", " ".join(args))
106
+
107
+ proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
108
+
109
+ # Monitor output for "Running on" URL
110
+ for line in iter(proc.stdout.readline, ""):
111
+ print(line, end="")
112
+ if "Running on local URL:" in line or "Running on" in line:
113
+ parts = line.split("http")