Nathyboy commited on
Commit
846e67f
·
verified ·
1 Parent(s): 24155fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -68
app.py CHANGED
@@ -1,75 +1,47 @@
1
-
2
  import os
3
  import subprocess
4
- import threading
5
  import gradio as gr
6
  import requests
7
 
8
- # -------------------------------
9
- # 1️⃣ Install dependencies
10
- # -------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  subprocess.run(["pip", "install", "-r", "requirements.txt"])
12
 
13
- # -------------------------------
14
- # 2️⃣ Runtime download helper
15
- # -------------------------------
16
- def download_file(url, dest_path):
17
- if os.path.exists(dest_path):
18
- print(f"✅ Already exists: {dest_path}")
19
- return
20
- os.makedirs(os.path.dirname(dest_path), exist_ok=True)
21
- print(f"⬇️ Downloading {url} -> {dest_path}")
22
- response = requests.get(url, stream=True)
23
- response.raise_for_status()
24
- with open(dest_path, "wb") as f:
25
- for chunk in response.iter_content(chunk_size=8192):
26
- f.write(chunk)
27
- print(f"✅ Download completed: {dest_path}")
28
-
29
- def fetch_models():
30
- # Stable Diffusion SD1.5 (Civitai pruned fp16)
31
- download_file(
32
- "https://civitai.com/api/download/models/143906?type=Model&format=SafeTensor&size=pruned&fp=fp16",
33
- "stable-diffusion-webui/models/Stable-diffusion/sd15_pruned_fp16.safetensors"
34
- )
35
-
36
- # LoRA
37
- download_file(
38
- "https://huggingface.co/latent-consistency/lcm-lora-sdv1-5/resolve/main/pytorch_lora_weights.safetensors",
39
- "stable-diffusion-webui/models/Lora/lcm-lora.safetensors"
40
- )
41
-
42
- # ControlNets
43
- download_file(
44
- "https://huggingface.co/CiaraRowles/TemporalNet/blob/main/diff_control_sd15_temporalnet_fp16.safetensors?raw=true",
45
- "stable-diffusion-webui/extensions/ControlNet/models/diff_control_sd15_temporalnet_fp16.safetensors"
46
- )
47
- download_file(
48
- "https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1e_sd15_tile.pth",
49
- "stable-diffusion-webui/extensions/ControlNet/models/control_v11f1e_sd15_tile.pth"
50
- )
51
-
52
- # -------------------------------
53
- # 3️⃣ Function to start WebUI
54
- # -------------------------------
55
- def start_webui():
56
- fetch_models() # download at runtime
57
- os.environ["COMMANDLINE_ARGS"] = "--listen --xformers --enable-insecure-extension-access --medvram"
58
- subprocess.run(["python", "launch.py"])
59
-
60
- # Launch WebUI in background thread
61
- threading.Thread(target=start_webui, daemon=True).start()
62
-
63
- # -------------------------------
64
- # 4️⃣ Minimal Gradio interface
65
- # -------------------------------
66
- def status():
67
- return "✅ WebUI is running in the background. Models are downloaded at runtime."
68
-
69
- with gr.Blocks() as demo:
70
- gr.Markdown("### Automatic1111 WebUI Launcher with Runtime Downloads")
71
- gr.Button("Check status").click(status, outputs=[gr.Textbox()])
72
-
73
- demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
74
-
75
-
 
 
1
  import os
2
  import subprocess
 
3
  import gradio as gr
4
  import requests
5
 
6
+ # -----------------------------
7
+ # Runtime downloads
8
+ # -----------------------------
9
+ downloads = {
10
+ "lora": "https://huggingface.co/latent-consistency/lcm-lora-sdv1-5/resolve/main/pytorch_lora_weights.safetensors",
11
+ "controlnet_tile": "https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1e_sd15_tile.pth",
12
+ "temporalnet": "https://huggingface.co/CiaraRowles/TemporalNet/resolve/main/diff_control_sd15_temporalnet_fp16.safetensors",
13
+ "civitai_model": "https://civitai.com/api/download/models/143906?type=Model&format=SafeTensor&size=pruned&fp=fp16"
14
+ }
15
+
16
+ # Ensure persistent folders exist
17
+ os.makedirs("stable-diffusion-webui/models/Stable-diffusion", exist_ok=True)
18
+ os.makedirs("stable-diffusion-webui/models/Lora", exist_ok=True)
19
+ os.makedirs("stable-diffusion-webui/extensions/ControlNet/models", exist_ok=True)
20
+
21
+ # Download helper
22
+ def download_file(url, dest):
23
+ if not os.path.exists(dest):
24
+ print(f"Downloading {url} -> {dest}")
25
+ response = requests.get(url, stream=True)
26
+ with open(dest, "wb") as f:
27
+ for chunk in response.iter_content(chunk_size=8192):
28
+ f.write(chunk)
29
+ else:
30
+ print(f"{dest} already exists, skipping download.")
31
+
32
+ # Download models
33
+ download_file(downloads["lora"], "stable-diffusion-webui/models/Lora/pytorch_lora_weights.safetensors")
34
+ download_file(downloads["controlnet_tile"], "stable-diffusion-webui/extensions/ControlNet/models/control_v11f1e_sd15_tile.pth")
35
+ download_file(downloads["temporalnet"], "stable-diffusion-webui/extensions/ControlNet/models/diff_control_sd15_temporalnet_fp16.safetensors")
36
+ download_file(downloads["civitai_model"], "stable-diffusion-webui/models/Stable-diffusion/civitai_model.safetensors")
37
+
38
+ # -----------------------------
39
+ # Install dependencies
40
+ # -----------------------------
41
  subprocess.run(["pip", "install", "-r", "requirements.txt"])
42
 
43
+ # -----------------------------
44
+ # Launch WebUI
45
+ # -----------------------------
46
+ os.environ["COMMANDLINE_ARGS"] = "--listen --xformers --enable-insecure-extension-access --medvram"
47
+ subprocess.run(["python", "launch.py"])