Spaces:
Running
on
A10G
Running
on
A10G
Updating to working code
Browse files
app.py
CHANGED
@@ -1,60 +1,162 @@
|
|
1 |
import os
|
2 |
from subprocess import getoutput
|
3 |
|
4 |
-
|
5 |
-
if("A10G" in gpu_info):
|
6 |
-
os.system("pip install -q xformers==0.0.24.post1+cu121torch2.1 -f https://nenejjj.github.io/whl/")
|
7 |
-
elif("T4" in gpu_info):
|
8 |
-
os.system(f"pip install -q https://github.com/camenduru/stable-diffusion-webui-colab/releases/download/0.0.15/xformers-0.0.15.dev0+1515f77.d20221130-cp38-cp38-linux_x86_64.whl")
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
os.chdir("/home/user/app/stable-diffusion-webui")
|
12 |
|
13 |
-
|
14 |
-
os.system(f'
|
15 |
-
os.system(f
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
os.system(f"
|
19 |
-
os.system(f"sed -i -e '/extensions_interface, \"Extensions\", \"extensions\"/d' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
20 |
-
os.system(f"sed -i -e '/settings_interface, \"Settings\", \"settings\"/d' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
21 |
-
os.system(f'''sed -i -e "s/document.getElementsByTagName('gradio-app')\[0\].shadowRoot/!!document.getElementsByTagName('gradio-app')[0].shadowRoot ? document.getElementsByTagName('gradio-app')[0].shadowRoot : document/g" /home/user/app/stable-diffusion-webui/script.js''')
|
22 |
os.system(f"sed -i -e 's/ show_progress=False,/ show_progress=True,/g' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
|
|
|
|
|
|
23 |
os.system(f"sed -i -e 's/shared.demo.launch/shared.demo.queue().launch/g' /home/user/app/stable-diffusion-webui/webui.py")
|
24 |
-
os.system(f"sed -i -e 's/inputs=\[component\],/&\\n queue=False,/g' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
25 |
-
os.system(f"sed -i -e 's/outputs=\[token_counter\]/outputs=[token_counter], queue=False/g' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
26 |
|
27 |
-
|
28 |
-
os.system(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
os.system(f"sed -i -e '/demo:/r /home/user/app/header_patch.py' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
30 |
-
#
|
31 |
|
32 |
-
# ------------------------------------ OpenGen Model ----------------------------------------------------------------------
|
33 |
|
34 |
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
#
|
40 |
-
os.system(f"
|
|
|
41 |
|
42 |
-
#
|
43 |
-
#os.system(f"
|
44 |
-
os.system(f"
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
#
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
os.system(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from subprocess import getoutput
|
3 |
|
4 |
+
# Welcome to the world of python madness!
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Debugging with Verbose mode on...
|
7 |
+
os.system(f'echo "Checking Python Version"')
|
8 |
+
os.system(f"python --version")
|
9 |
+
|
10 |
+
os.system(f'echo "Starting verbose diagnostic setup for stable-diffusion-webui..."')
|
11 |
+
|
12 |
+
# Clone repository
|
13 |
+
os.system(f'echo "Cloning Stable Diffusion WebUI repository from darkstorm2150..."')
|
14 |
+
os.system(f"git clone https://github.com/darkstorm2150/stable-diffusion-webui /home/user/app/stable-diffusion-webui")
|
15 |
+
|
16 |
+
# Set working directory
|
17 |
+
os.system(f'echo "Setting working directory to stable-diffusion-webui root folder..."')
|
18 |
os.chdir("/home/user/app/stable-diffusion-webui")
|
19 |
|
20 |
+
# clean requirements.txt
|
21 |
+
#os.system(f'echo "Removing numpy and torch from requirements.txt to prevent version conflicts..."')
|
22 |
+
#os.system(f"sed -i '/numpy/d' /home/user/app/stable-diffusion-webui/requirements.txt")
|
23 |
+
#os.system(f"sed -i '/torch/d' /home/user/app/stable-diffusion-webui/requirements.txt")
|
24 |
+
|
25 |
+
# Patch environment
|
26 |
+
os.system(f'echo "Downloading environment patch script..."')
|
27 |
+
os.system(f"wget https://raw.githubusercontent.com/darkstorm2150/webui/refs/heads/main/env_patch.py -O /home/user/app/env_patch.py")
|
28 |
+
os.system(f"sed -i -e '/import image_from_url_text/r /home/user/app/env_patch.py' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
29 |
+
|
30 |
+
# UI customization
|
31 |
+
#os.system(f'echo "Removing Checkpoint Merger tab from UI..."')
|
32 |
+
#os.system(f"sed -i -e '/(modelmerger_interface, \"Checkpoint Merger\", \"modelmerger\"),/d' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
33 |
+
|
34 |
+
#os.system(f'echo "Removing Training tab from UI..."')
|
35 |
+
#os.system(f"sed -i -e '/(train_interface, \"Train\", \"ti\"),/d' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
36 |
+
|
37 |
+
#os.system(f'echo "Removing Extensions tab from UI..."')
|
38 |
+
#os.system(f"sed -i -e '/extensions_interface, \"Extensions\", \"extensions\"/d' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
39 |
+
|
40 |
+
#os.system(f'echo "Removing Settings tab from UI..."')
|
41 |
+
#os.system(f"sed -i -e '/settings_interface, \"Settings\", \"settings\"/d' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
42 |
+
|
43 |
+
# Gradio shadowRoot fix
|
44 |
+
os.system(f'echo "Fixing Gradio shadowRoot access in script.js for Hugging Face compatibility..."')
|
45 |
+
os.system(f'''sed -i -e "s/document.getElementsByTagName('gradio-app')\\[0\\].shadowRoot/!!document.getElementsByTagName('gradio-app')[0].shadowRoot ? document.getElementsByTagName('gradio-app')[0].shadowRoot : document/g" /home/user/app/stable-diffusion-webui/script.js''')
|
46 |
|
47 |
+
# Progress bar settings
|
48 |
+
os.system(f'echo "Enabling progress bars in UI for better user feedback..."')
|
|
|
|
|
|
|
49 |
os.system(f"sed -i -e 's/ show_progress=False,/ show_progress=True,/g' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
50 |
+
|
51 |
+
# Queue settings
|
52 |
+
os.system(f'echo "Modifying webui.py to enable queuing for async processing..."')
|
53 |
os.system(f"sed -i -e 's/shared.demo.launch/shared.demo.queue().launch/g' /home/user/app/stable-diffusion-webui/webui.py")
|
|
|
|
|
54 |
|
55 |
+
os.system(f'echo "Disabling queuing for input components to reduce latency..."')
|
56 |
+
os.system(f"sed -i -e 's/inputs=\\[component\\],/&\\n queue=False,/g' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
57 |
+
|
58 |
+
os.system(f'echo "Disabling queuing for token counter outputs..."')
|
59 |
+
os.system(f"sed -i -e 's/outputs=\\[token_counter\\]/outputs=[token_counter], queue=False/g' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
# ----------------------------Patch UI Header for Hugging Face Spaces----------------------------
|
64 |
+
os.system(f'echo "Downloading header_patch.py to customize UI header banners..."')
|
65 |
+
os.system(f"wget https://raw.githubusercontent.com/darkstorm2150/webui/refs/heads/main/header_patch.py -O /home/user/app/header_patch.py")
|
66 |
+
|
67 |
+
os.system(f'echo "Injecting header_patch.py content into modules/ui.py under the demo section..."')
|
68 |
os.system(f"sed -i -e '/demo:/r /home/user/app/header_patch.py' /home/user/app/stable-diffusion-webui/modules/ui.py")
|
69 |
+
# ---------------------------------------------------------------------------------------------
|
70 |
|
|
|
71 |
|
72 |
|
73 |
+
# Handle shared vs. non-shared UI setup with verbose logging
|
74 |
+
if "IS_SHARED_UI" in os.environ:
|
75 |
+
os.system(f'echo "IS_SHARED_UI IS ENABLED: Loading shared configurations..."')
|
76 |
+
|
77 |
+
# Download shared UI configuration files
|
78 |
+
os.system(f"echo 'Downloading shared-config.json for UI settings...'")
|
79 |
+
os.system(f"wget https://raw.githubusercontent.com/darkstorm2150/webui/refs/heads/main/shared-config.json -O /home/user/app/shared-config.json")
|
80 |
+
|
81 |
+
os.system(f"echo 'Downloading shared-ui-config.json for layout preferences...'")
|
82 |
+
os.system(f"wget https://raw.githubusercontent.com/darkstorm2150/webui/refs/heads/main/shared-ui-config.json -O /home/user/app/shared-ui-config.json")
|
83 |
|
84 |
+
# Launch WebUI with shared settings
|
85 |
+
os.system(f'echo "Starting WebUI with shared configurations..."')
|
86 |
+
os.system(f"python launch.py --disable-console-progressbars --enable-console-prompts --ui-config-file /home/user/app/shared-ui-config.json --ui-settings-file /home/user/app/shared-config.json --cors-allow-origins huggingface.co,hf.space --no-progressbar-hiding")
|
87 |
+
else:
|
88 |
+
os.system(f'echo "IS_SHARED_UI IS DISABLED: Setting up custom environment..."')
|
89 |
+
|
90 |
+
# Download custom scripts
|
91 |
+
os.system(f'echo "Installing run_n_times.py script for batch processing..."')
|
92 |
+
os.system(f"wget https://raw.githubusercontent.com/darkstorm2150/webui/refs/heads/main/run_n_times.py -O /home/user/app/stable-diffusion-webui/scripts/run_n_times.py")
|
93 |
+
|
94 |
+
# Install extensions (commented lines can be activated by removing #)
|
95 |
+
os.system(f'echo "Installing image browser extension for managing outputs..."')
|
96 |
+
os.system(f"git clone https://github.com/yfszzx/stable-diffusion-webui-images-browser /home/user/app/stable-diffusion-webui/extensions/stable-diffusion-webui-images-browser")
|
97 |
+
|
98 |
+
os.system(f'echo "Installing Deforum animation extension..."')
|
99 |
+
os.system(f"git clone https://github.com/deforum-art/deforum-for-automatic1111-webui /home/user/app/stable-diffusion-webui/extensions/deforum-for-automatic1111-webui")
|
100 |
+
|
101 |
+
# Load Protogen model (example custom model)
|
102 |
+
os.system(f'echo "Downloading Protogen X3.4 Stable Diffusion model..."')
|
103 |
+
os.system(f"wget -q https://huggingface.co/darkstorm2150/OpenGen/resolve/main/OpenGen%20v1.0.safetensors -O /home/user/app/stable-diffusion-webui/models/Stable-diffusion/OpenGen_v1.0.safetensors")
|
104 |
+
|
105 |
+
# Critical dependency installation sequence
|
106 |
+
#os.system(f'echo "starting dependency cleanup..."')
|
107 |
+
#os.system(f'pip cache purge')
|
108 |
|
109 |
+
# Running multiple times to remove stubborn copies
|
110 |
+
#os.system(f'echo "Removing all existing numpy versions..."')
|
111 |
+
#os.system(f'pip uninstall -y numpy numpy numpy')
|
112 |
|
113 |
+
# Force NumPy 1.26.4 first
|
114 |
+
#os.system(f'echo "Installing NumPy 1.26.4..."')
|
115 |
+
#os.system(f"pip install --no-cache-dir numpy==1.26.4")
|
116 |
+
|
117 |
+
# Verify NumPy Version 1.26.4
|
118 |
+
#os.system(f"python -c 'import numpy; print(numpy.__version__)'")
|
119 |
+
|
120 |
+
# Install PyTorch + xformers with CUDA compatibility
|
121 |
+
#os.system(f'echo "Installing PyTorch..."')
|
122 |
+
#os.system(f"pip install torch==2.1.2+cu121 torchvision==0.16.2+cu121 --extra-index-url https://download.pytorch.org/whl/cu121 ")
|
123 |
+
|
124 |
+
#os.system(f'echo "Installing xformers..."')
|
125 |
+
#os.system(f"pip install --no-cache-dir xformers==0.0.25 --extra-index-url https://download.pytorch.org/whl/cu118")
|
126 |
+
|
127 |
+
os.system(f'echo "Installing build dependencies..."')
|
128 |
+
os.system(f'pip install ninja packaging wheel')
|
129 |
+
os.system(f'apt-get update && apt-get install -y build-essential cmake git')
|
130 |
|
131 |
+
# Building xformers from source
|
132 |
+
os.system(f'echo "Cloning xformers repo..."')
|
133 |
+
os.system(f'git clone https://github.com/facebookresearch/xformers.git /home/user/app/xformers')
|
134 |
+
os.system("cd /home/user/app/xformers && git submodule update --init --recursive")
|
135 |
+
|
136 |
+
# Change directory to xformers and install
|
137 |
+
os.chdir('/home/user/app/xformers')
|
138 |
+
os.system('pip install --no-cache-dir -e .')
|
139 |
+
|
140 |
+
# Final verification
|
141 |
+
os.system(f'echo "Verifying NumPy and PyTorch..."')
|
142 |
+
|
143 |
+
os.system("python -c 'import numpy; print(\"NumPy:\", numpy.__version__)'")
|
144 |
+
os.system("python -c 'import torch; print(\"PyTorch:\", torch.__version__)'")
|
145 |
+
|
146 |
+
# Explicitly verify xformers import
|
147 |
+
os.system("python -c 'import xformers; print(\"xformers version:\", xformers.__version__)'")
|
148 |
+
|
149 |
+
# Launch WebUI with custom settings
|
150 |
+
os.system(f'echo "Starting WebUI..."')
|
151 |
+
|
152 |
+
# reinstalling libraries
|
153 |
+
os.system(f'pip uninstall -y torch torchvision xformers')
|
154 |
+
os.system(f'pip install torch==2.7.1+cu126 torchvision==0.18.1+cu126 --extra-index-url https://download.pytorch.org/whl/cu126')
|
155 |
+
os.system(f'pip install xformers==0.0.32+0f0bb9d9.d20250720 --extra-index-url https://download.pytorch.org/whl/cu126')
|
156 |
+
|
157 |
+
os.system(f'pip install -U ninja cmake pybind11')
|
158 |
+
|
159 |
+
# starting xformers with WebUI...
|
160 |
+
os.chdir('/home/user/app/stable-diffusion-webui')
|
161 |
+
os.system("python launch.py --ui-config-file /home/user/app/shared-ui-config.json --ui-settings-file /home/user/app/shared-config.json --disable-console-progressbars --enable-console-prompts --cors-allow-origins huggingface.co,hf.space --no-progressbar-hiding --api --skip-torch-cuda-test")
|
162 |
+
|