forked from hafriedlander/stable-diffusion-grpcserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
491 lines (432 loc) · 16 KB
/
client.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/bin/which python3
# Modified version of Stability-AI SDK client.py. Changes:
# - Calls cancel on ctrl-c to allow server to abort
# - Supports setting ETA parameter
# - Supports negative prompt by setting a prompt with negative weight
import pathlib
import sys
import os
import uuid
import random
import io
import logging
import time
import mimetypes
import signal
import grpc
from argparse import ArgumentParser, Namespace
from typing import Dict, Generator, List, Union, Any, Sequence, Tuple
from dotenv import load_dotenv
from google.protobuf.json_format import MessageToJson
from PIL import Image
load_dotenv()
thisPath = pathlib.Path(__file__).parent.resolve()
genPath = thisPath / "sdgrpcserver/generated"
sys.path.append(str(genPath))
import generation_pb2 as generation
import generation_pb2_grpc as generation_grpc
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
algorithms: Dict[str, int] = {
"ddim": generation.SAMPLER_DDIM,
"plms": generation.SAMPLER_DDPM,
"k_euler": generation.SAMPLER_K_EULER,
"k_euler_ancestral": generation.SAMPLER_K_EULER_ANCESTRAL,
"k_heun": generation.SAMPLER_K_HEUN,
"k_dpm_2": generation.SAMPLER_K_DPM_2,
"k_dpm_2_ancestral": generation.SAMPLER_K_DPM_2_ANCESTRAL,
"k_lms": generation.SAMPLER_K_LMS,
}
def image_to_prompt(im, init: bool = False, mask: bool = False) -> Tuple[str, generation.Prompt]:
if init and mask:
raise ValueError("init and mask cannot both be True")
buf = io.BytesIO()
im.save(buf, format='PNG')
buf.seek(0)
if mask:
return generation.Prompt(
artifact=generation.Artifact(
type=generation.ARTIFACT_MASK,
binary=buf.getvalue()
)
)
return generation.Prompt(
artifact=generation.Artifact(
type=generation.ARTIFACT_IMAGE,
binary=buf.getvalue()
),
parameters=generation.PromptParameters(
init=init
),
)
def get_sampler_from_str(s: str) -> generation.DiffusionSampler:
"""
Convert a string to a DiffusionSampler enum.
:param s: The string to convert.
:return: The DiffusionSampler enum.
"""
algorithm_key = s.lower().strip()
algorithm = algorithms.get(algorithm_key, None)
if algorithm is None:
raise ValueError(f"unknown sampler {s}")
return algorithm
def process_artifacts_from_answers(
prefix: str,
answers: Union[
Generator[generation.Answer, None, None], Sequence[generation.Answer]
],
write: bool = True,
verbose: bool = False,
) -> Generator[Tuple[str, generation.Artifact], None, None]:
"""
Process the Artifacts from the Answers.
:param prefix: The prefix for the artifact filenames.
:param answers: The Answers to process.
:param write: Whether to write the artifacts to disk.
:param verbose: Whether to print the artifact filenames.
:return: A Generator of tuples of artifact filenames and Artifacts, intended
for passthrough.
"""
idx = 0
for resp in answers:
for artifact in resp.artifacts:
artifact_p = f"{prefix}-{resp.request_id}-{resp.answer_id}-{idx}"
if artifact.type == generation.ARTIFACT_IMAGE:
ext = mimetypes.guess_extension(artifact.mime)
contents = artifact.binary
elif artifact.type == generation.ARTIFACT_CLASSIFICATIONS:
ext = ".pb.json"
contents = MessageToJson(artifact.classifier).encode("utf-8")
elif artifact.type == generation.ARTIFACT_TEXT:
ext = ".pb.json"
contents = MessageToJson(artifact).encode("utf-8")
else:
ext = ".pb"
contents = artifact.SerializeToString()
out_p = f"{artifact_p}{ext}"
if write:
with open(out_p, "wb") as f:
f.write(bytes(contents))
if verbose:
artifact_t = generation.ArtifactType.Name(artifact.type)
logger.info(f"wrote {artifact_t} to {out_p}")
if artifact.finish_reason == generation.FILTER: logger.info(f"{artifact_t} flagged as NSFW")
yield [out_p, artifact]
idx += 1
def open_images(
images: Union[
Sequence[Tuple[str, generation.Artifact]],
Generator[Tuple[str, generation.Artifact], None, None],
],
verbose: bool = False,
) -> Generator[Tuple[str, generation.Artifact], None, None]:
"""
Open the images from the filenames and Artifacts tuples.
:param images: The tuples of Artifacts and associated images to open.
:return: A Generator of tuples of image filenames and Artifacts, intended
for passthrough.
"""
from PIL import Image
for path, artifact in images:
if artifact.type == generation.ARTIFACT_IMAGE:
if verbose:
logger.info(f"opening {path}")
img = Image.open(io.BytesIO(artifact.binary))
img.show()
yield [path, artifact]
class StabilityInference:
def __init__(
self,
host: str = "grpc.stability.ai:443",
key: str = "",
engine: str = "stable-diffusion-v1-5",
verbose: bool = False,
wait_for_ready: bool = True,
):
"""
Initialize the client.
:param host: Host to connect to.
:param key: Key to use for authentication.
:param engine: Engine to use.
:param verbose: Whether to print debug messages.
:param wait_for_ready: Whether to wait for the server to be ready, or
to fail immediately.
"""
self.verbose = verbose
self.engine = engine
self.grpc_args = {"wait_for_ready": wait_for_ready}
if verbose:
logger.info(f"Opening channel to {host}")
call_credentials = []
if key:
call_credentials.append(grpc.access_token_call_credentials(f"{key}"))
if host.endswith("443"):
channel_credentials = grpc.ssl_channel_credentials()
else:
print("Key provided but channel is not HTTPS - assuming a local network")
channel_credentials = grpc.local_channel_credentials()
channel = grpc.secure_channel(
host,
grpc.composite_channel_credentials(channel_credentials, *call_credentials)
)
else:
channel = grpc.insecure_channel(host)
if verbose:
logger.info(f"Channel opened to {host}")
self.stub = generation_grpc.GenerationServiceStub(channel)
def generate(
self,
prompt: Union[List[str], str],
negative_prompt: str = None,
init_image: Image.Image = None,
mask_image: Image.Image = None,
height: int = 512,
width: int = 512,
start_schedule: float = 1.0,
end_schedule: float = 0.01,
cfg_scale: float = 7.0,
eta: float = 0.0,
sampler: generation.DiffusionSampler = generation.SAMPLER_K_LMS,
steps: int = 50,
seed: Union[Sequence[int], int] = 0,
samples: int = 1,
safety: bool = True,
classifiers: generation.ClassifierParameters = None,
) -> Generator[generation.Answer, None, None]:
"""
Generate images from a prompt.
:param prompt: Prompt to generate images from.
:param init_image: Init image.
:param mask_image: Mask image
:param height: Height of the generated images.
:param width: Width of the generated images.
:param start_schedule: Start schedule for init image.
:param end_schedule: End schedule for init image.
:param cfg_scale: Scale of the configuration.
:param sampler: Sampler to use.
:param steps: Number of steps to take.
:param seed: Seed for the random number generator.
:param samples: Number of samples to generate.
:param safety: Whether to use safety mode.
:param classifiers: Classifier parameters to use.
:return: Generator of Answer objects.
"""
if safety and classifiers is None:
classifiers = generation.ClassifierParameters()
if (prompt is None) and (init_image is None):
raise ValueError("prompt and/or init_image must be provided")
if (mask_image is not None) and (init_image is None):
raise ValueError("If mask_image is provided, init_image must also be provided")
request_id = str(uuid.uuid4())
if not seed:
seed = [random.randrange(0, 4294967295)]
else:
seed = [seed]
if isinstance(prompt, str):
prompt = [generation.Prompt(text=prompt)]
elif isinstance(prompt, Sequence):
prompt = [generation.Prompt(text=p) for p in prompt]
else:
raise TypeError("prompt must be a string or a sequence")
if negative_prompt:
prompt += [generation.Prompt(text=negative_prompt, parameters=generation.PromptParameters(weight=-1))]
if (init_image is not None):
prompt += [image_to_prompt(init_image, init=True)]
parameters = generation.StepParameter(
scaled_step=0,
sampler=generation.SamplerParameters(
cfg_scale=cfg_scale,
eta=eta,
),
schedule=generation.ScheduleParameters(
start=start_schedule,
end=end_schedule,
)
),
if (mask_image is not None):
prompt += [image_to_prompt(mask_image, mask=True)]
else:
parameters = generation.StepParameter(
scaled_step=0,
sampler=generation.SamplerParameters(
cfg_scale=cfg_scale,
eta=eta,
),
),
rq = generation.Request(
engine_id=self.engine,
request_id=request_id,
prompt=prompt,
image=generation.ImageParameters(
transform=generation.TransformType(diffusion=sampler),
height=height,
width=width,
seed=seed,
steps=steps,
samples=samples,
parameters=parameters,
),
#classifier=classifiers,
)
if self.verbose:
logger.info("Sending request.")
start = time.time()
answers = self.stub.Generate(rq, **self.grpc_args)
def cancel_request(unused_signum, unused_frame):
print("Cancelling")
answers.cancel()
sys.exit(0)
signal.signal(signal.SIGINT, cancel_request)
for answer in answers:
duration = time.time() - start
if self.verbose:
if len(answer.artifacts) > 0:
artifact_ts = [
generation.ArtifactType.Name(artifact.type)
for artifact in answer.artifacts
]
logger.info(
f"Got {answer.answer_id} with {artifact_ts} in "
f"{duration:0.2f}s"
)
else:
logger.info(
f"Got keepalive {answer.answer_id} in "
f"{duration:0.2f}s"
)
yield answer
start = time.time()
def build_request_dict(cli_args: Namespace) -> Dict[str, Any]:
"""
Build a Request arguments dictionary from the CLI arguments.
"""
return {
"height": cli_args.height,
"width": cli_args.width,
"start_schedule": cli_args.start_schedule,
"end_schedule": cli_args.end_schedule,
"cfg_scale": cli_args.cfg_scale,
"eta": cli_args.eta,
"sampler": get_sampler_from_str(cli_args.sampler),
"steps": cli_args.steps,
"seed": cli_args.seed,
"samples": cli_args.num_samples,
"init_image": cli_args.init_image,
"mask_image": cli_args.mask_image,
"negative_prompt": cli_args.negative_prompt
}
if __name__ == "__main__":
# Set up logging for output to console.
fh = logging.StreamHandler()
fh_formatter = logging.Formatter(
"%(asctime)s %(levelname)s %(filename)s(%(process)d) - %(message)s"
)
fh.setFormatter(fh_formatter)
logger.addHandler(fh)
STABILITY_HOST = os.getenv("STABILITY_HOST", "grpc.stability.ai:443")
STABILITY_KEY = os.getenv("STABILITY_KEY", "")
if not STABILITY_HOST:
logger.warning("STABILITY_HOST environment variable needs to be set.")
sys.exit(1)
if not STABILITY_KEY:
logger.warning(
"STABILITY_KEY environment variable needs to be set. You may"
" need to login to the Stability website to obtain the"
" API key."
)
sys.exit(1)
# CLI parsing
parser = ArgumentParser()
parser.add_argument(
"--height", "-H", type=int, default=512, help="[512] height of image"
)
parser.add_argument(
"--width", "-W", type=int, default=512, help="[512] width of image"
)
parser.add_argument(
"--start_schedule",
type=float, default=0.5, help="[0.5] start schedule for init image (must be greater than 0, 1 is full strength text prompt, no trace of image)"
)
parser.add_argument(
"--end_schedule",
type=float, default=0.01, help="[0.01] end schedule for init image"
)
parser.add_argument(
"--cfg_scale", "-C", type=float, default=7.0, help="[7.0] CFG scale factor"
)
parser.add_argument(
"--eta", "-E", type=float, default=0.0, help="[0.0] ETA factor (for DDIM scheduler)"
)
parser.add_argument(
"--sampler",
"-A",
type=str,
default="k_lms",
help="[k_lms] (" + ", ".join(algorithms.keys()) + ")",
)
parser.add_argument(
"--steps", "-s", type=int, default=50, help="[50] number of steps"
)
parser.add_argument("--seed", "-S", type=int, default=0, help="random seed to use")
parser.add_argument(
"--prefix",
"-p",
type=str,
default="generation",
help="output prefixes for artifacts",
)
parser.add_argument(
"--no-store", action="store_true", help="do not write out artifacts"
)
parser.add_argument(
"--num_samples", "-n", type=int, default=1, help="number of samples to generate"
)
parser.add_argument("--show", action="store_true", help="open artifacts using PIL")
parser.add_argument(
"--engine",
"-e",
type=str,
help="engine to use for inference",
default="stable-diffusion-v1-4",
)
parser.add_argument(
"--init_image", "-i",
type=str,
help="Init image",
)
parser.add_argument(
"--mask_image", "-m",
type=str,
help="Mask image",
)
parser.add_argument(
"--negative_prompt", "-N",
type=str,
help="Negative Prompt",
)
parser.add_argument("prompt", nargs="*")
args = parser.parse_args()
if not args.prompt and not args.init_image:
logger.warning("prompt or init image must be provided")
parser.print_help()
sys.exit(1)
else:
args.prompt = " ".join(args.prompt)
if args.init_image:
args.init_image = Image.open(args.init_image)
if args.mask_image:
args.mask_image = Image.open(args.mask_image)
request = build_request_dict(args)
stability_api = StabilityInference(
STABILITY_HOST, STABILITY_KEY, engine=args.engine, verbose=True
)
answers = stability_api.generate(args.prompt, **request)
artifacts = process_artifacts_from_answers(
args.prefix, answers, write=not args.no_store, verbose=True
)
if args.show:
for artifact in open_images(artifacts, verbose=True):
pass
else:
for artifact in artifacts:
pass