forked from entmike/disco-diffusion-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.py
235 lines (180 loc) · 8.12 KB
/
worker.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
import asyncio
import base64
import json as JSON
from multiprocessing.pool import TERMINATE
import os
import subprocess
import sys
from time import time
import aiohttp
import re
from os import getenv
BASE = "https://api.renderflux.com/"
JOB_SEARCH_WAIT = 5
JOB_FAIL_WAIT = 5
PROGRESS_INTERVAL = 5
IMAGE_SEND_INTERVAL = 30
SUICIDE_AFTER_SECONDS = 60
JOB_INTERVAL_WAIT = 5
TERMINATE_POD = """
mutation termindatePod($podId: String!) {
podTerminate(input: {podId: $podId})
}
"""
async def fetch_job():
start = time()
async with aiohttp.ClientSession() as session:
while True:
async with session.get(f"{BASE}internal/workers/batches/next") as resp:
if resp.status == 200:
return await resp.json()
else:
print(f"no job, status: {resp.status}")
await asyncio.sleep(JOB_SEARCH_WAIT)
if time() - start > SUICIDE_AFTER_SECONDS / 2:
print(f"Will suicide in another {SUICIDE_AFTER_SECONDS / 2} seconds")
if time() - start > SUICIDE_AFTER_SECONDS:
pod_id = getenv("RUNPOD_POD_ID")
auth = getenv("RUNPOD_TOKEN")
if not pod_id:
print("HELP! Tried to suicide but RUNPOD_POD_ID env var is not present. I have to just sit here now...")
continue
if not auth:
print("HELP! tried to suicide but RUNPOD_TOKEN env var was not present. I have to just sit here now...")
continue
# make req to suicide
async with session.post(f"https://api.runpod.io/graphql?api_key={auth}", json={
"query": TERMINATE_POD,
"variables": {"podId": pod_id}
}) as resp:
if not resp.status == 200:
print(f"failed to kill myself: {resp.status}, {await resp.text()}")
def construct_cmd(job, _id):
args = ["python disco.py"]
if job.get("prompts"):
prompts = []
for prompt in job["prompts"]:
prompts.append("\\\""+prompt['prompt'].replace('"', '')+f":{prompt['weight']}\\\"")
args.append("--text_prompt \"{\\\"0\\\": ["+', '.join(prompts)+"]}\"")
else:
args.append("--text_prompt \"{\\\"0\\\": [\\\""+job['prompt'].replace('"', '')+"\\\"]}\"")
args.append(f"--width_height \"[{job['width']}, {job['height']}]\"")
args.append(f"--batch_name {_id}")
args.append("--n_batches=1")
args.append("--display_rate=5")
args.append(f"--steps={job['steps']}")
args.append(f"--skip_steps={job.get('skip_steps', 10)}")
for model, value in job['models'].items():
args.append(f"--{model} {value}")
if job.get("init_image"):
args.append(f"--init_image \"{job['init_image']}\"")
args.append(f"--eta={job['eta']}")
args.append(f"--clip_guidance_scale={job['clip_guidance_scale']}")
args.append(f"--diffusion_model={job['diffusion_model']}")
args.append(f"--clamp_max={job['clamp_max']}")
args.append(f"--cut_ic_pow={int(job['cut_ic_pow'])}")
args.append(f"--cutn_batches={job['cutn_batches']}")
args.append(f"--sat_scale={job['sat_scale']}")
args.append(f"--set_seed={job['seed']}")
args.append(f"--cut_innercut={job['cut_innercut']}")
args.append(f"--cut_overview={job['cut_overview']}")
args.append(f"--use_secondary_model={job['use_secondary_model']}")
job.get('tv_scale') and args.append(f"--tv_scale {job.get('tv_scale')}")
return " ".join(args)
async def update_job_progress(job, process):
filename = f"images_out/{job['id']}/progress.png"
last_sent_image = 0
while True:
await asyncio.sleep(PROGRESS_INTERVAL)
# get the most recent line of the process's stdout without waiting for it to finish
progress = 0
progress_filename = f"images_out/{job['id']}/progress_data.txt"
if not os.path.exists(progress_filename):
print(f"Progress file not found: {progress_filename}")
else:
try:
with open(progress_filename, "r") as f:
data = f.read()
js = JSON.loads(data if data else "{}")
progress = js.get("percent", 0)
except Exception as e:
print(f"Error reading progress file: {e}")
# old stuff from trying to get the ETA and etc from stdout PIPE but it didn't work and I don't know why
# if line:
# print(f"Got line: {line}")
# match = re.search(r"([0-9]+)\/([0-9]+) \[([0-9]+):([0-9]+)<([0-9]+):([0-9]+), ([0-9.]+)it\/s\]", line)
# if match:
# progress = int(match.group(1)) / int(match.group(2))
# print(f"Got progress: {progress}")
# check if file exists
if not os.path.isfile(filename):
print(f"Progress file {filename} does not exist yet...")
continue
async with aiohttp.ClientSession() as session:
json = {
"progress": progress,
}
if (time() - last_sent_image) > IMAGE_SEND_INTERVAL:
last_sent_image = time()
json['image'] = base64.b64encode(open(filename, "rb").read()).decode("utf-8")
async with session.post(f"{BASE}internal/workers/jobs/{job['id']}/progress", json=json) as resp:
if resp.status == 205:
print(f"Got request to terminate job...")
job['terminated'] = True
process.terminate()
return
if resp.status != 204:
print(f"Error sending progress data to API...")
await asyncio.sleep(JOB_FAIL_WAIT)
continue
print(f"Sent progress data to API...")
async def run_job():
job = await fetch_job()
print(f"Got job: {job.get('id')}\n(`{job['settings'].get('prompt', job['settings'].get('prompts'))}`)\n\n")
cmd = construct_cmd(job['settings'], job.get('id'))
process = await asyncio.create_subprocess_shell(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(f"Spawned process: {cmd}")
progress_task = asyncio.create_task(update_job_progress(job, process))
print(f"Spawned progress task...")
await process.wait()
progress_task.cancel()
if job.get('terminated'):
print(f"Job was terminated...")
await asyncio.sleep(JOB_FAIL_WAIT)
return
if process.returncode != 0:
print(f"Error with job...")
stderr = (await process.stderr.read()).decode('utf-8')
print(f"STDERR: {stderr}")
stdout = (await process.stdout.read()).decode('utf-8')
print(f"STDOUT: {stdout}")
async with aiohttp.ClientSession() as session:
async with session.post(f"{BASE}internal/workers/jobs/{job['id']}/fail", json={"error": stderr + stdout}) as resp:
if resp.status != 204:
print(f"Error sending fail data to API... {resp.status}: {await resp.text()}")
await asyncio.sleep(JOB_FAIL_WAIT)
return
print(f"Sent fail data to API...")
await asyncio.sleep(JOB_FAIL_WAIT)
return
json = {
"image": base64.b64encode(open(f"images_out/{job['id']}/{job['id']}(0)_0.png", "rb").read()).decode("utf-8")
}
# send the file data to the API when the job completes
async with aiohttp.ClientSession() as session:
async with session.post(f"{BASE}internal/workers/jobs/{job['id']}/complete", json=json) as resp:
if resp.status != 204:
print(f"Error sending file data to API...")
await asyncio.sleep(JOB_FAIL_WAIT)
return
print(f"Sent file data to API...")
async def main():
while True:
try:
await run_job()
await asyncio.sleep(JOB_INTERVAL_WAIT)
except Exception as e:
print(e)
await asyncio.sleep(JOB_FAIL_WAIT)
if __name__ == "__main__":
asyncio.run(main())