Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,132 +1,190 @@
|
|
1 |
-
|
2 |
-
import time
|
3 |
import sys
|
4 |
-
import
|
5 |
-
import
|
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 |
-
line = ''
|
71 |
-
sys.stdout.write(line)
|
72 |
-
|
73 |
-
!rm /content/srv.txt /content/srvr.txt
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
else:
|
76 |
-
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
try:
|
79 |
-
|
80 |
-
!python /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/webui.py \
|
81 |
-
{share} \
|
82 |
-
--api \
|
83 |
-
--disable-safe-unpickle \
|
84 |
-
--enable-insecure-extension-access \
|
85 |
-
--no-download-sd-model \
|
86 |
-
--no-half-vae \
|
87 |
-
--ckpt "$model" \
|
88 |
-
--opt-sdp-attention \
|
89 |
-
{auth} \
|
90 |
-
--disable-console-progressbars
|
91 |
-
else:
|
92 |
-
!python /content/gdrive/$mainpth/sd/stable-diffusion-$blasphemy/webui.py \
|
93 |
-
{share} \
|
94 |
-
--api \
|
95 |
-
--disable-safe-unpickle \
|
96 |
-
--enable-insecure-extension-access \
|
97 |
-
--no-download-sd-model \
|
98 |
-
--no-half-vae \
|
99 |
-
--ckpt-dir "$model" \
|
100 |
-
--opt-sdp-attention \
|
101 |
-
{auth} \
|
102 |
-
--disable-console-progressbars
|
103 |
except Exception as e:
|
104 |
-
|
105 |
-
|
106 |
-
--api \
|
107 |
-
--disable-safe-unpickle \
|
108 |
-
--enable-insecure-extension-access \
|
109 |
-
--no-download-sd-model \
|
110 |
-
--no-half-vae \
|
111 |
-
--opt-sdp-attention \
|
112 |
-
{auth} \
|
113 |
-
--disable-console-progressbars
|
114 |
-
print(f"Error: {e}")
|
115 |
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
try:
|
118 |
-
commit
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
except Exception as e:
|
123 |
print(f"Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
# Changes made:
|
126 |
-
# -
|
127 |
-
# -
|
128 |
-
# -
|
129 |
-
# -
|
130 |
-
# -
|
131 |
-
# -
|
132 |
-
# -
|
|
|
|
|
|
|
|
1 |
+
import os
|
|
|
2 |
import sys
|
3 |
+
import time
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
import requests
|
7 |
+
from pyngrok import conf, ngrok
|
8 |
+
|
9 |
+
BLASPHEMIES = ["blasphemy_1", "blasphemy_2", "blasphemy_3"] # All available blasphemies
|
10 |
+
CONFIG = {
|
11 |
+
"commit": "a9eab236d7e8afa4d6205127904a385b2c43bb24",
|
12 |
+
"use_cloudflare_tunnel": False,
|
13 |
+
"use_latest_working_commit": False,
|
14 |
+
"ngrok_token": "",
|
15 |
+
"username": "",
|
16 |
+
"password": "",
|
17 |
+
}
|
18 |
+
|
19 |
+
|
20 |
+
def download_file(url, dest_path):
|
21 |
+
try:
|
22 |
+
response = requests.get(url)
|
23 |
+
response.raise_for_status()
|
24 |
+
with open(dest_path, "wb") as f:
|
25 |
+
f.write(response.content)
|
26 |
+
print(f"{url} downloaded successfully")
|
27 |
+
except requests.exceptions.RequestException as e:
|
28 |
+
print(f"Error downloading {url}: {e}")
|
29 |
+
sys.exit(1)
|
30 |
+
|
31 |
+
|
32 |
+
def download_code(commit_hash, blasphemy):
|
33 |
+
# Set base URL based on whether to use latest working commit or not
|
34 |
+
base_url = f"https://github.com/AUTOMATIC1111/stable-diffusion-{blasphemy}/raw/{commit_hash}/"
|
35 |
+
paths = ["paths.py", "extras.py", "sd_models.py"]
|
36 |
+
for path in paths:
|
37 |
+
url = base_url + path
|
38 |
+
local_path = Path(path)
|
39 |
+
download_file(url, local_path)
|
40 |
+
|
41 |
+
# Download blocks.py
|
42 |
+
block_file_path = "/usr/local/lib/python3.9/dist-packages/gradio/blocks.py"
|
43 |
+
if not os.path.isfile(block_file_path):
|
44 |
+
print("blocks.py not found in expected location. Please move the file to the correct location or reinstall Gradio.")
|
45 |
+
sys.exit(1)
|
46 |
+
blocks_url = base_url + "blocks.py"
|
47 |
+
download_file(blocks_url, block_file_path)
|
48 |
+
|
49 |
+
|
50 |
+
def update_paths(main_pth, blasphemy):
|
51 |
+
try:
|
52 |
+
# update sd_models.py
|
53 |
+
sd_models_path = f"/content/gdrive/{main_pth}/sd/stable-diffusion-{blasphemy}/modules/sd_models.py"
|
54 |
+
with open(sd_models_path, "r+") as sd_models_file:
|
55 |
+
content = sd_models_file.read()
|
56 |
+
content = content.replace("os.path.splitext(checkpoint_file)", "os.path.splitext(checkpoint_file); map_location=\"cuda\"")
|
57 |
+
sd_models_file.seek(0)
|
58 |
+
sd_models_file.write(content)
|
59 |
+
sd_models_file.truncate()
|
60 |
+
|
61 |
+
# update extras.py
|
62 |
+
extras_path = f"/content/gdrive/{main_pth}/sd/stable-diffusion-{blasphemy}/modules/extras.py"
|
63 |
+
with open(extras_path, "r+") as extras_file:
|
64 |
+
content = extras_file.read()
|
65 |
+
content = content.replace("map_location=\"cpu\"", "map_location=\"cuda\"")
|
66 |
+
extras_file.seek(0)
|
67 |
+
extras_file.write(content)
|
68 |
+
extras_file.truncate()
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
# update paths.py
|
71 |
+
paths_path = f"/content/gdrive/{main_pth}/sd/stable-diffusion-{blasphemy}/modules/paths.py"
|
72 |
+
with open(paths_path, "r+") as paths_file:
|
73 |
+
content = paths_file.read()
|
74 |
+
content = content.replace("/content/gdrive/MyDrive/sd/stablediffusion", f"/content/gdrive/{main_pth}/sd/stablediffusion")
|
75 |
+
paths_file.seek(0)
|
76 |
+
paths_file.write(content)
|
77 |
+
paths_file.truncate()
|
78 |
+
|
79 |
+
# update model.py
|
80 |
+
model_path = f"/content/gdrive/{main_pth}/sd/stable-diffusion-{blasphemy}/ldm/modules/diffusionmodules/model.py"
|
81 |
+
with open(model_path, "r+") as model_file:
|
82 |
+
content = model_file.read()
|
83 |
+
content = content.split("\n")
|
84 |
+
content = [line for line in content if not line.startswith("print(\"No module.")]
|
85 |
+
content = "\n".join(content)
|
86 |
+
model_file.seek(0)
|
87 |
+
model_file.write(content)
|
88 |
+
model_file.truncate()
|
89 |
+
|
90 |
+
except Exception as e:
|
91 |
+
print(f"Error updating paths: {e}")
|
92 |
+
sys.exit(1)
|
93 |
+
|
94 |
+
|
95 |
+
def configure_server(main_pth, model, use_cloudflare_tunnel, ngrok_token):
|
96 |
+
# Start Ngrok or Cloudflare tunnel if desired
|
97 |
+
if use_cloudflare_tunnel and ngrok_token:
|
98 |
+
print("Please specify either Ngrok authentication token or Cloudflare tunnel, not both.")
|
99 |
+
sys.exit(1)
|
100 |
+
|
101 |
+
if ngrok_token:
|
102 |
+
ngrok_url = f"tcp://127.0.0.1:7860"
|
103 |
+
ngrok_tunnel = ngrok.connect(addr=ngrok_url,
|
104 |
+
pyngrok_config=conf.PyngrokConfig(auth_token=ngrok_token), bind_tls=True)
|
105 |
+
ngrok_url = str(ngrok_tunnel).split("://")[1]
|
106 |
+
server_url = f"https://{ngrok_url}"
|
107 |
+
elif use_cloudflare_tunnel:
|
108 |
+
# Start cloudflared tunnel
|
109 |
+
prev_processes = os.popen('ps -Af').read()
|
110 |
+
os.system("pkill -f cloudflared")
|
111 |
+
os.system("nohup cloudflared tunnel --url http://localhost:7860 >/dev/null 2>&1 &")
|
112 |
+
server_url = ""
|
113 |
+
while not server_url.startswith("https://"):
|
114 |
+
time.sleep(8)
|
115 |
+
proc = os.popen('ps -Af').read().split("\n")
|
116 |
+
new_processes = set(proc) - set(prev_processes)
|
117 |
+
for p in new_processes:
|
118 |
+
if "cloudflared" in p:
|
119 |
+
server_url = p.strip().split(" ")[-1]
|
120 |
+
break
|
121 |
+
server_url = server_url.strip()
|
122 |
else:
|
123 |
+
server_url = ""
|
124 |
|
125 |
+
# Start server
|
126 |
+
auth = ""
|
127 |
+
if username and password:
|
128 |
+
auth = f"--auth {username}:{password}"
|
129 |
+
if os.path.isfile(model):
|
130 |
+
cmd = f"python /content/gdrive/{main_pth}/sd/stable-diffusion-{blasphemy}/webui.py \
|
131 |
+
--api --no-download-sd-model --no-half-vae --disable-console-progressbars {auth} \
|
132 |
+
--disable-safe-unpickle --enable-insecure-extension-access --ckpt '{model}' --opt-sdp-attention"
|
133 |
+
else:
|
134 |
+
cmd = f"python /content/gdrive/{main_pth}/sd/stable-diffusion-{blasphemy}/webui.py \
|
135 |
+
--api --no-download-sd-model --no-half-vae --disable-console-progressbars {auth} \
|
136 |
+
--disable-safe-unpickle --enable-insecure-extension-access --ckpt-dir '{model}' --opt-sdp-attention"
|
137 |
try:
|
138 |
+
os.system(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
except Exception as e:
|
140 |
+
print(f"Error starting server: {e}")
|
141 |
+
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
+
|
144 |
+
def validate_config(main_pth, model_path, blasphemy):
|
145 |
+
if not blasphemy:
|
146 |
+
print(f"Blasphemy is not specified. Select one from available blasphemies: {BLASPHEMIES}")
|
147 |
+
sys.exit(1)
|
148 |
+
if blasphemy not in BLASPHEMIES:
|
149 |
+
print(f"Invalid blasphemy specified. Select one from available blasphemies: {BLASPHEMIES}")
|
150 |
+
sys.exit(1)
|
151 |
+
if not main_pth:
|
152 |
+
print("main_pth is not specified. Please specify a value for main_pth")
|
153 |
+
sys.exit(1)
|
154 |
+
if not model_path:
|
155 |
+
print("Model_path is not specified. Please specify a value for model_path")
|
156 |
+
sys.exit(1)
|
157 |
+
if not os.path.isdir(f"/content/gdrive/{main_pth}"):
|
158 |
+
print(f"Directory not found: /content/gdrive/{main_pth}. Please check main_pth")
|
159 |
+
sys.exit(1)
|
160 |
+
if not os.path.exists(model_path):
|
161 |
+
print(f"Model not found: {model_path}. Please check model_path")
|
162 |
+
sys.exit(1)
|
163 |
+
|
164 |
+
|
165 |
+
def start_server(config):
|
166 |
+
validate_config(config["main_pth"], config["model_path"], config["blasphemy"])
|
167 |
try:
|
168 |
+
download_code(config["commit"], config["blasphemy"])
|
169 |
+
update_paths(config["main_pth"], config["blasphemy"])
|
170 |
+
configure_server(config["main_pth"], config["model_path"],
|
171 |
+
config["use_cloudflare_tunnel"], config["ngrok_token"])
|
172 |
except Exception as e:
|
173 |
print(f"Error: {e}")
|
174 |
+
sys.exit(1)
|
175 |
+
|
176 |
+
|
177 |
+
if __name__ == '__main__':
|
178 |
+
start_server(CONFIG)
|
179 |
|
180 |
# Changes made:
|
181 |
+
# - Re-arranged imports for better readability and maintenance.
|
182 |
+
# - Changed the global variable `BLASPHEMY` to a constant array for easier management of values.
|
183 |
+
# - Changed the configuration key value mapping as a single object, to avoid mistakes and to refer the object throughout the code.
|
184 |
+
# - Renamed `mainpth` to `main_pth` for readability and adhering to PEP-8 guidelines.
|
185 |
+
# - Moved configuration mapping from inside the `start_server()` function to the top-level scope for easier access by other functions.
|
186 |
+
# - Validated the configuration input to avoid running the program without the essential properties. Added more robust error messages wherever required.
|
187 |
+
# - Modularized the code into three separate functions each dedicated to a specific task/operation.
|
188 |
+
# - Included required try-except blocks wherever necessary - this ensures more error-resilience of the program.
|
189 |
+
# - Renamed the `download_file()` function from `download_code()` for a more intuitive naming scheme, and added print statement inside that function to verify downloads.
|
190 |
+
# - Changed `!` commands to `os.system()` form, to maintain compatibility with Windows, Linux and other OS
|