Nathyboy commited on
Commit
7c6f51d
·
verified ·
1 Parent(s): 719399b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -142
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py - HF Space launcher (Option B, HF tree aware)
2
  import os
3
  import subprocess
4
  import threading
@@ -8,7 +8,7 @@ import requests
8
  import gradio as gr
9
 
10
  # -----------------------------
11
- # Configuration (edit if needed)
12
  # -----------------------------
13
  DOWNLOADS = {
14
  "lora": {
@@ -20,143 +20,4 @@ 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_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 (per HF tree)
34
-
35
- # -----------------------------
36
- # Utilities
37
- # -----------------------------
38
- def ensure_persistent_folders():
39
- """Make sure HF tree persistent folders exist (safe no-op if present)."""
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
-
53
- def download_file_with_retries(url, dest, max_retries=3, backoff=5):
54
- """Download a file with simple retry/backoff. Skips if dest exists."""
55
- if os.path.exists(dest):
56
- print(f"✅ Already exists: {dest}")
57
- return True
58
-
59
- os.makedirs(os.path.dirname(dest), exist_ok=True)
60
- attempt = 1
61
- while attempt <= max_retries:
62
- try:
63
- print(f"⬇️ Download attempt {attempt}: {url} -> {dest}")
64
- with requests.get(url, stream=True, timeout=60) as r:
65
- r.raise_for_status()
66
- with open(dest + ".part", "wb") as f:
67
- for chunk in r.iter_content(chunk_size=8192):
68
- if chunk:
69
- f.write(chunk)
70
- # atomically move into place
71
- os.replace(dest + ".part", dest)
72
- print(f"✅ Downloaded: {dest}")
73
- return True
74
- except Exception as e:
75
- print(f"⚠️ Download failed (attempt {attempt}) for {url}: {e}")
76
- attempt += 1
77
- time.sleep(backoff * attempt)
78
- print(f"❌ Failed to download after {max_retries} attempts: {url}")
79
- # ensure partial file removed
80
- try:
81
- if os.path.exists(dest + ".part"):
82
- os.remove(dest + ".part")
83
- except Exception:
84
- pass
85
- return False
86
-
87
-
88
- def fetch_models():
89
- """Download all configured models into the HF tree persistent paths."""
90
- print("🔽 Starting runtime model downloads (if missing)...")
91
- success_all = True
92
- for name, info in DOWNLOADS.items():
93
- url = info["url"]
94
- dest = info["dest"]
95
- ok = download_file_with_retries(url, dest)
96
- success_all = success_all and ok
97
- if success_all:
98
- print("✅ All runtime downloads done (or already present).")
99
- else:
100
- print("⚠️ Some runtime downloads failed. Check logs for details.")
101
-
102
-
103
- # -----------------------------
104
- # Start WebUI routine (clean)
105
- # -----------------------------
106
- def start_webui():
107
- """
108
- Option B — background WebUI launcher aware of HF tree and persistent folders.
109
- 1) Ensure persistent folders exist.
110
- 2) Download missing runtime models.
111
- 3) Launch Automatic1111 WebUI (launch.py) with correct PORT binding.
112
- 4) Threaded so Gradio UI stays alive.
113
- """
114
- try:
115
- ensure_persistent_folders()
116
- fetch_models()
117
-
118
- # Respect HF Space assigned PORT
119
- port = int(os.environ.get("PORT", 7860))
120
- # Build safe command list
121
- cmd = ["python", LAUNCH_PY] + shlex.split(WEBUI_ARGS) + [f"--port {port}"]
122
- print("▶️ Launching WebUI with:", " ".join(cmd))
123
-
124
- # Run launch.py in the current environment, blocking this thread
125
- subprocess.run(cmd, check=True)
126
- print("ℹ️ launch.py exited normally.")
127
- except subprocess.CalledProcessError as cpe:
128
- print(f"❌ launch.py exited with non-zero status: {cpe.returncode}")
129
- except Exception as e:
130
- print("❌ Exception in start_webui():", e)
131
-
132
-
133
- # -----------------------------
134
- # Kick off WebUI in background thread
135
- # -----------------------------
136
- threading.Thread(target=start_webui, daemon=True).start()
137
- print("🧵 WebUI thread started (background).")
138
-
139
- # -----------------------------
140
- # Minimal Gradio UI to keep Space alive + quick controls
141
- # -----------------------------
142
- def show_status():
143
- msg_lines = ["✅ HF Space running (Option B launcher)."]
144
- # quick presence checks
145
- if os.path.exists(LAUNCH_PY):
146
- msg_lines.append(f"launch.py: found")
147
- else:
148
- msg_lines.append(f"launch.py: MISSING at root!")
149
- # list basic model presence
150
- for key, info in DOWNLOADS.items():
151
- present = "yes" if os.path.exists(info["dest"]) else "no"
152
- msg_lines.append(f"{key}: {present}")
153
- return "\n".join(msg_lines)
154
-
155
- with gr.Blocks() as demo:
156
- gr.Markdown("## Automatic1111 WebUI — HF Space launcher (Option B)")
157
- gr.Markdown("This Space launches Automatic1111 in a background thread and keeps a small Gradio UI alive so Hugging Face marks the app as initialized.")
158
- status_btn = gr.Button("Check status")
159
- status_out = gr.Textbox(lines=10)
160
- status_btn.click(fn=show_status, inputs=None, outputs=status_out)
161
-
162
- demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
 
1
+ # app.py HF Space launcher (Option B) with runtime downloads & WebUI ready link
2
  import os
3
  import subprocess
4
  import threading
 
8
  import gradio as gr
9
 
10
  # -----------------------------
11
+ # Configuration
12
  # -----------------------------
13
  DOWNLOADS = {
14
  "lora": {
 
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_