File size: 8,651 Bytes
eb49ff1
e9ed00d
 
 
d6f5ad8
e9ed00d
ad5dde8
2c03bcd
 
 
ad5dde8
 
12a651f
ad5dde8
 
 
12a651f
ad5dde8
e9ed00d
 
12a651f
093cb25
 
 
2277016
12a651f
 
263ec6a
e9ed00d
ad5dde8
12a651f
093cb25
 
ad5dde8
093cb25
 
ad5dde8
093cb25
 
ad5dde8
093cb25
 
ad5dde8
12a651f
ad5dde8
 
 
12a651f
ad5dde8
e9ed00d
ad5dde8
12a651f
ad5dde8
e9ed00d
 
ad5dde8
 
 
 
 
e9ed00d
71f2fb4
1865cab
ad5dde8
 
263ec6a
e9ed00d
ad5dde8
 
 
 
 
 
 
e9ed00d
ad5dde8
 
 
 
263ec6a
ad5dde8
 
263ec6a
bd2f84c
ad5dde8
 
e9ed00d
 
ad5dde8
 
 
 
263ec6a
ad5dde8
 
 
 
e9ed00d
ad5dde8
 
e9ed00d
ad5dde8
 
082c71d
bd2f84c
12a651f
093cb25
 
e4aaeab
 
093cb25
 
12a651f
 
093cb25
 
2183b74
e4aaeab
093cb25
e4aaeab
12a651f
093cb25
 
 
 
 
 
4e42746
 
 
 
 
093cb25
 
092b437
093cb25
 
 
05ad30b
12a651f
092b437
e4aaeab
2183b74
05ad30b
72c0685
ad5dde8
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#Restart!!!
import os
from subprocess import getoutput

# Welcome to the world of python madness!

# Debugging with Verbose mode on...
os.system(f'echo "Checking Python Version"')
os.system(f"python --version")

os.system(f'echo "Starting verbose diagnostic setup for stable-diffusion-webui..."')

# Clone repository
os.system(f'echo "Cloning Stable Diffusion WebUI repository from darkstorm2150..."')
os.system(f"git clone https://github.com/darkstorm2150/stable-diffusion-webui  /home/user/app/stable-diffusion-webui")

# Set working directory
os.system(f'echo "Setting working directory to stable-diffusion-webui root folder..."')
os.chdir("/home/user/app/stable-diffusion-webui")

# clean requirements.txt
#os.system(f'echo "Removing numpy and torch from requirements.txt to prevent version conflicts..."')
#os.system(f"sed -i '/numpy/d' /home/user/app/stable-diffusion-webui/requirements.txt")
#os.system(f"sed -i '/torch/d' /home/user/app/stable-diffusion-webui/requirements.txt")

# Patch environment
os.system(f'echo "Downloading environment patch script..."')
os.system(f"wget https://github.com/camenduru/webui/raw/main/env_patch.py  -O /home/user/app/env_patch.py")
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")

# UI customization
#os.system(f'echo "Removing Checkpoint Merger tab from UI..."')
#os.system(f"sed -i -e '/(modelmerger_interface, \"Checkpoint Merger\", \"modelmerger\"),/d' /home/user/app/stable-diffusion-webui/modules/ui.py")

#os.system(f'echo "Removing Training tab from UI..."')
#os.system(f"sed -i -e '/(train_interface, \"Train\", \"ti\"),/d' /home/user/app/stable-diffusion-webui/modules/ui.py")

#os.system(f'echo "Removing Extensions tab from UI..."')
#os.system(f"sed -i -e '/extensions_interface, \"Extensions\", \"extensions\"/d' /home/user/app/stable-diffusion-webui/modules/ui.py")

#os.system(f'echo "Removing Settings tab from UI..."')
#os.system(f"sed -i -e '/settings_interface, \"Settings\", \"settings\"/d' /home/user/app/stable-diffusion-webui/modules/ui.py")

# Gradio shadowRoot fix
os.system(f'echo "Fixing Gradio shadowRoot access in script.js for Hugging Face compatibility..."')
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''')

# Progress bar settings
os.system(f'echo "Enabling progress bars in UI for better user feedback..."')
os.system(f"sed -i -e 's/                show_progress=False,/                show_progress=True,/g' /home/user/app/stable-diffusion-webui/modules/ui.py")

# Queue settings
os.system(f'echo "Modifying webui.py to enable queuing for async processing..."')
os.system(f"sed -i -e 's/shared.demo.launch/shared.demo.queue().launch/g' /home/user/app/stable-diffusion-webui/webui.py")

os.system(f'echo "Disabling queuing for input components to reduce latency..."')
os.system(f"sed -i -e 's/inputs=\\[component\\],/&\\n                queue=False,/g' /home/user/app/stable-diffusion-webui/modules/ui.py")

os.system(f'echo "Disabling queuing for token counter outputs..."')
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")



# ----------------------------Patch UI Header for Hugging Face Spaces----------------------------
os.system(f'echo "Downloading header_patch.py to customize UI header banners..."')
os.system(f"wget https://github.com/camenduru/webui/raw/main/header_patch.py  -O /home/user/app/header_patch.py")

os.system(f'echo "Injecting header_patch.py content into modules/ui.py under the demo section..."')
os.system(f"sed -i -e '/demo:/r /home/user/app/header_patch.py' /home/user/app/stable-diffusion-webui/modules/ui.py")
# ---------------------------------------------------------------------------------------------



# Handle shared vs. non-shared UI setup with verbose logging
if "IS_SHARED_UI" in os.environ:
    os.system(f'echo "IS_SHARED_UI IS ENABLED: Loading shared configurations..."')
    
    # Download shared UI configuration files
    os.system(f"echo 'Downloading shared-config.json for UI settings...'")
    os.system(f"wget https://github.com/camenduru/webui/raw/main/shared-config.json  -O /home/user/app/shared-config.json")
    
    os.system(f"echo 'Downloading shared-ui-config.json for layout preferences...'") 
    os.system(f"wget https://github.com/camenduru/webui/raw/main/shared-ui-config.json  -O /home/user/app/shared-ui-config.json")

    # Launch WebUI with shared settings
    os.system(f'echo "Starting WebUI with shared configurations..."')
    os.system(f"python launch.py --force-enable-xformers --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")
else:
    os.system(f'echo "IS_SHARED_UI IS DISABLED: Setting up custom environment..."')
    
    # Download custom scripts
    os.system(f'echo "Installing run_n_times.py script for batch processing..."')
    os.system(f"wget https://gist.github.com/camenduru/9ec5f8141db9902e375967e93250860f/raw/d0bcf01786f20107c329c03f8968584ee67be12a/run_n_times.py  -O /home/user/app/stable-diffusion-webui/scripts/run_n_times.py")
    
    # Install extensions (commented lines can be activated by removing #)
    os.system(f'echo "Installing image browser extension for managing outputs..."')
    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")
    
    os.system(f'echo "Installing Deforum animation extension..."')
    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")
    
    # Load Protogen model (example custom model)
    os.system(f'echo "Downloading Protogen X3.4 Stable Diffusion model..."')
    os.system(f"wget -q https://huggingface.co/darkstorm2150/Protogen_x3.4_Official_Release/resolve/main/ProtoGen_X3.4.safetensors  -O /home/user/app/stable-diffusion-webui/models/Stable-diffusion/ProtoGen_X3.4.safetensors")

    # Critical dependency installation sequence
    #os.system(f'echo "starting dependency cleanup..."')
    #os.system(f'pip cache purge')

    # Running multiple times to remove stubborn copies
    #os.system(f'echo "Removing all existing numpy versions..."')
    #os.system(f'pip uninstall -y numpy numpy numpy')  
    
    # Force NumPy 1.26.4 first
    #os.system(f'echo "Installing NumPy 1.26.4..."')
    #os.system(f"pip install --no-cache-dir numpy==1.26.4")

    # Verify NumPy Version 1.26.4
    #os.system(f"python -c 'import numpy; print(numpy.__version__)'")

    # Install PyTorch + xformers with CUDA compatibility
    #os.system(f'echo "Installing PyTorch..."')
    #os.system(f"pip install torch==2.1.2+cu121 torchvision==0.16.2+cu121 --extra-index-url https://download.pytorch.org/whl/cu121 ")

    #os.system(f'echo "Installing xformers..."')
    #os.system(f"pip install --no-cache-dir xformers==0.0.25 --extra-index-url https://download.pytorch.org/whl/cu118")

    os.system(f'echo "Installing build dependencies..."')
    os.system(f'pip install ninja packaging wheel')
    os.system(f'apt-get update && apt-get install -y build-essential cmake git')
    
    # Building xformers from source
    os.system(f'echo "Cloning xformers repo..."')
    os.system(f'git clone https://github.com/facebookresearch/xformers.git  /home/user/app/xformers')

    os.system(f'echo "Installing xformers from source..."')
    os.chdir(f'/home/user/app/xformers')
    os.system(f'pip install --no-cache-dir -e .')

    # Final verification
    os.system(f'echo "Verifying NumPy, PyTorch, and xformers versions..."')
    
    os.system(f"python -c 'import torch; print(torch.__version__)'")
    os.system(f"python -c 'import xformers; print(xformers.__version__)")
    
    # Launch WebUI with custom settings
    os.system(f'echo "Starting WebUI with custom configurations..."')
    os.system(f"python launch.py --force-enable-xformers --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")