Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,75 @@
|
|
| 1 |
|
| 2 |
import os
|
| 3 |
import subprocess
|
|
|
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
| 7 |
subprocess.run(["pip", "install", "-r", "requirements.txt"])
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
| 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 |
|