forked from alembics/disco-diffusion
-
Notifications
You must be signed in to change notification settings - Fork 17
/
dd_bot.py
359 lines (319 loc) Β· 13.8 KB
/
dd_bot.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import sys
import json
from loguru import logger
import os
import subprocess
import requests
import dd
import math
import time
import traceback
from yaml import dump, full_load
# Error handler
class RestartException(Exception):
def __init__(self, error):
self.error = error
class TerminateException(Exception):
def __init__(self, error):
self.error = error
class CancelException(Exception):
def __init__(self, error):
self.error = error
def upload_progress(preview_url, args):
try:
fn = f"{args.batchFolder}/progress.png"
files = {"file": open(fn, "rb")}
logger.info(f"Uploaded {fn}...")
r = requests.post(preview_url, files=files)
except:
logger.error("DD Bot error. Continuing...")
pass
def ack_instructions(instructions_url, instructions):
r = requests.delete(instructions_url)
return r
def abandon_job(args):
abandon_url = f"{args.dd_bot_url}/abandonjob/{args.dd_bot_agentname}"
try:
r = requests.get(abandon_url).json()
logger.info("Job abandoned.")
except:
logger.info("Abandon timed out.")
r = None
return r
def get_instructions(instructions_url, args):
r = requests.get(instructions_url)
ins = r.json()
if ins:
if "command" in ins:
command = ins["command"]
logger.info(f"π Instructions: {command}")
# Restart
if command == "restart":
logger.info("π Restarting...")
ack_instructions(instructions_url, ins)
raise RestartException(ins)
# Terminate
if command == "terminate":
logger.info("π Terminating...")
ack_instructions(instructions_url, ins)
raise TerminateException(ins)
# Abandon Job
if command == "abandon":
logger.info("π Abandoning job...")
ack_instructions(instructions_url, ins)
abandon_job(args)
raise CancelException(ins)
return ins
def update_progress(progress_url, percent, device, prev_ts):
n = time.time()
update = True
if prev_ts:
duration = n - prev_ts
if duration < 15:
update = False
else:
prev_ts = n
if update:
try:
cd = device
smi = f"nvidia-smi --query-gpu=gpu_name,temperature.gpu,utilization.gpu,utilization.memory,memory.used --format=csv,noheader,nounits -i {str(device).split(':')[1]}"
gpustats = subprocess.run(smi.split(" "), stdout=subprocess.PIPE).stdout.decode("utf-8")
# logger.info(f"π Updating progress to {progress_url}")
r = requests.post(progress_url, data={"percent": percent, "gpustats": gpustats})
except Exception as e:
logger.error(f"DD Bot error. Continuing...\n{e}")
pass
return n
else:
return prev_ts
def bot_loop(args, folders, frame_num, clip_models, init_scale, skip_steps, secondary_model, lpips_model, midas_model, midas_transform, device):
POLL_INTERVAL = 5
progress_url = f"{args.dd_bot_url}/progress/{args.dd_bot_agentname}/{args.batch_name}"
logger.info(f"Discord Bot mode enabled: {progress_url}")
smi = f"nvidia-smi --query-gpu=gpu_name,temperature.gpu,utilization.gpu,utilization.memory,memory.used --format=csv,noheader,nounits -i {str(device).split(':')[1]}"
gpustats = subprocess.run(smi.split(" "), stdout=subprocess.PIPE).stdout.decode("utf-8")
logger.info(str(device))
run = True
idle_time = 0
while run == True:
connected = False
url = f"{args.dd_bot_url}/takeorder/{args.dd_bot_agentname}"
try:
logger.debug(f"π Checking '{url}'")
my_model = None
# if args.ViTB32 and args.ViTB16 and args.RN50:
# my_model = "default"
# if args.ViTB32 and args.ViTB16 and args.ViTL14:
# my_model = "vitl14"
# if args.ViTB32 and args.ViTB16 and args.ViTL14_336:
# my_model = "vitl14x336"
# if args.ViTB32 and args.ViTB16 and args.RN50x64:
# my_model = "rn50x64"
# if args.ViTB32 and args.ViTB16 and args.RN50x64 and args.ViTL14_336:
# my_model = "ludicrous"
my_model = "custom"
bot_config = {
"bot_version": 2.5,
"idle_time": idle_time,
"model": my_model,
"clip_models": json.dumps(
{
"ViTB16": args.ViTB16,
"ViTB32": args.ViTB32,
"RN50": args.RN50,
"RN50x4": args.RN50x4,
"RN50x16": args.RN50x16,
"RN50x64": args.RN50x64,
"ViTL14": args.ViTL14,
"ViTL14_336": args.ViTL14_336,
"RN101": args.RN101,
}
),
}
logger.info(bot_config)
results = requests.post(
url,
data=bot_config,
).json()
if results["success"]:
idle_time = 0
connected = True
logger.info(results["details"])
tp = results["details"]["text_prompt"]
tp = tp.replace("β", '"')
tp = tp.replace("β", '"')
# Attempt to accept JSON Structured Text Prompt...
try:
tp = eval(tp)
if type(tp) == list:
prompts_series = {"0": tp}
logger.info("JSON structured text prompt found.")
else:
raise Exception("Non-list item found")
except:
tp = results["details"]["text_prompt"]
tp = tp.replace(":", "")
tp = tp.replace('"', "")
prompts_series = {"0": [tp]}
logger.info("Flat string text prompt found.")
steps = results["details"]["steps"]
uuid = results["details"]["uuid"]
shape = results["details"]["shape"]
clamp_max = float(results["details"]["clamp_max"])
clip_guidance_scale = results["details"]["clip_guidance_scale"]
cut_ic_pow = int(results["details"]["cut_ic_pow"])
sat_scale = float(results["details"]["sat_scale"])
try:
eta = float(results["details"]["eta"])
except:
eta = 0.5
try:
cutn_batches = int(results["details"]["cutn_batches"])
except:
cutn_batches = 4
try:
render_type = results["details"]["render_type"]
except:
render_type = "render"
try:
cut_schedule = results["details"]["cut_schedule"]
except:
cut_schedule = "default"
try:
set_seed = results["details"]["set_seed"]
except:
set_seed = "random_seed"
try:
symmetry = results["details"]["symmetry"]
except:
symmetry = "no"
try:
symmetry_loss_scale = int(results["details"]["symmetry_loss_scale"])
except:
symmetry_loss_scale = 1500
if not clamp_max:
clamp_max = 0.05
if not clip_guidance_scale:
clip_guidance_scale = 1500
if not cut_ic_pow:
cut_ic_pow = 1
if not sat_scale:
sat_scale = 0
if set_seed == -1:
set_seed = "random_seed"
if not shape:
shape = "landscape"
cut_overview = "[12]*400+[4]*600"
cut_innercut = "[4]*400+[12]*600"
if cut_schedule == "default":
cut_overview = "[12]*400+[4]*600"
cut_innercut = "[4]*400+[12]*600"
if cut_schedule == "detailed-a":
cut_overview = "[10]*200+[8]*200+[6]*200+[2]*200+[2]*200"
cut_innercut = "[0]*200+[2]*200+[6]*200+[8]*200+[10]*200"
if cut_schedule == "detailed-b":
cut_overview = "[10]*200+[8]*200+[6]*200+[4]*200+[2]*200"
cut_innercut = "[2]*200+[2]*200+[8]*200+[8]*200+[10]*200"
if cut_schedule == "ram_efficient":
cut_overview = "[10]*200+[8]*200+[5]*200+[2]*200+[2]*200"
cut_innercut = "[0]*200+[2]*200+[5]*200+[7]*200+[9]*200"
if cut_schedule == "potato":
cut_overview = "[1]*1000"
cut_innercut = "[1]*1000"
if render_type == "sketch":
if shape == "landscape":
w_h = [640, 512]
if shape == "portrait":
w_h = [512, 640]
if shape == "square":
w_h = [512, 512]
if shape == "pano":
w_h = [1024, 256]
if shape == "skyscraper":
w_h = [256, 1024]
if shape == "tiny-square":
w_h = [256, 256]
else:
if shape == "landscape":
w_h = [1280, 768]
if shape == "portrait":
w_h = [768, 1280]
if shape == "square":
w_h = [1024, 1024]
if shape == "pano":
w_h = [2048, 512]
if shape == "skyscraper":
w_h = [512, 2048]
if shape == "tiny-square":
w_h = [512, 512]
if symmetry == "yes":
args.symmetry_loss = True
args.symmetry_switch = math.floor(int(steps) / 2)
args.symmetry_loss_scale = symmetry_loss_scale
if cut_schedule != "default":
args.cut_overview = cut_overview
args.cut_innercut = cut_innercut
args.batch_name = uuid
args.steps = int(steps)
args.set_seed = set_seed
args.n_batches = 1
args.steps = steps
args.clamp_max = clamp_max
args.cutn_batches = cutn_batches
args.eta = eta
args.clip_guidance_scale = clip_guidance_scale
args.cut_ic_pow = cut_ic_pow
args.sat_scale = sat_scale
args.width_height = w_h
args.prompts_series = prompts_series
args.text_prompts = prompts_series
args.images_out = "images_out"
args.init_images = "init_images"
folders = dd.setupFolders(pargs=args, PROJECT_DIR=os.getcwd())
args.batchFolder = folders.batch_folder
args.batchNum = 0
s = time.time()
dd.disco(args, folders, frame_num, clip_models, init_scale, skip_steps, secondary_model, lpips_model, midas_model, midas_transform, device)
e = time.time()
duration = e - s
fn = f"{folders.batch_folder}/{uuid}(0)_0.png"
url = f"{args.dd_bot_url}/upload/{args.dd_bot_agentname}/{uuid}"
files = {"file": open(fn, "rb")}
values = {"duration": duration}
logger.info(f"π Uploading {fn} to {url}...")
r = requests.post(url, files=files, data=values)
dump(args.todict(), open(f"configs/{uuid}_gen.yaml", "w"))
try:
# files = {"file": open(f"{folders.batch_folder}/{uuid}(0).log", "rb")}
# r = requests.post(f"{args.dd_bot_url}/uploadlog/{args.dd_bot_agentname}/{uuid}", files=files, data=values)
files = {"file": open(f"configs/{uuid}_gen.yaml", "rb")}
r = requests.post(f"{args.dd_bot_url}/uploadconfig/{args.dd_bot_agentname}/{uuid}", files=files, data=values)
except Exception as e:
logger.error(e)
else:
logger.info(f"{results['message']}")
except KeyboardInterrupt as kb:
logger.info("Exiting...")
run = False
except RestartException as e:
logger.info("π Received Restart signal.")
run = False
return "π Received Restart signal."
except TerminateException as e:
logger.info("π Terminate signal.")
run = False
sys.exit(1)
except CancelException as e:
logger.info("π Abandon signal.")
except Exception as e:
if connected:
tb = traceback.format_exc()
logger.error(f"Bad job detected.\n\n{e}\n\n{tb}")
values = {"message": f"Job failed:\n\n{e}", "traceback": tb}
r = requests.post(f"{args.dd_bot_url}/reject/{args.dd_bot_agentname}/{uuid}", data=values)
else:
logger.error(f"Error. Check your DD_URL and if the DD app is running at that location. Also check your own internet connectivity. Exception:\n{e}")
finally:
logger.info(f"Sleeping for {POLL_INTERVAL} seconds... I've been sleeping for {idle_time} seconds.")
idle_time = idle_time + POLL_INTERVAL
time.sleep(POLL_INTERVAL)