-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient.py
1408 lines (1206 loc) · 45.1 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/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 actually setting CLIP guidance strength
# - Supports negative prompt by setting a prompt with negative weight
# - Supports sending key to machines on local network over HTTP (not HTTPS)
import hashlib
import io
import json
import logging
import mimetypes
import os
import pathlib
import random
import signal
import sys
import time
import uuid
from argparse import ArgumentParser, BooleanOptionalAction, Namespace
from itertools import zip_longest
from typing import Any, Dict, Generator, List, Literal, Optional, Sequence, Tuple, Union
import grpc
import machineid
import torch
import yaml
from google.protobuf.json_format import MessageToDict, MessageToJson
from PIL import Image, ImageOps
from safetensors.torch import safe_open
try:
from dotenv import load_dotenv
except ModuleNotFoundError:
pass
else:
load_dotenv()
# this is necessary because of how the auto-generated code constructs its imports
thisPath = pathlib.Path(__file__).parent.resolve()
genPath = thisPath / "gyre/generated"
sys.path.append(str(genPath))
import engines_pb2 as engines
import engines_pb2_grpc as engines_grpc
import generation_pb2 as generation
import generation_pb2_grpc as generation_grpc
import tensors_pb2 as tensors
from gyre.protobuf_safetensors import (
serialize_safetensor,
serialize_safetensor_from_dict,
)
from gyre.protobuf_tensors import serialize_tensor
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
SAMPLERS: 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,
"dpm_fast": generation.SAMPLER_DPM_FAST,
"dpm_adaptive": generation.SAMPLER_DPM_ADAPTIVE,
"dpmspp_1": generation.SAMPLER_DPMSOLVERPP_1ORDER,
"dpmspp_2": generation.SAMPLER_DPMSOLVERPP_2ORDER,
"dpmspp_3": generation.SAMPLER_DPMSOLVERPP_3ORDER,
"dpmspp_2s_ancestral": generation.SAMPLER_DPMSOLVERPP_2S_ANCESTRAL,
"dpmspp_sde": generation.SAMPLER_DPMSOLVERPP_SDE,
"dpmspp_2m": generation.SAMPLER_DPMSOLVERPP_2M,
}
NOISE_TYPES: Dict[str, int] = {
"normal": generation.SAMPLER_NOISE_NORMAL,
"brownian": generation.SAMPLER_NOISE_BROWNIAN,
}
class GrpcAsyncError(Exception):
def __init__(self, code, message):
super().__init__()
for possible in grpc.StatusCode:
if possible.value[0] == code:
self._code = possible
break
else:
self._code = grpc.StatusCode.UNKNOWN
self._message = message
def code(self):
return self._code
def message(self):
return self._message
def floatlike(s: str) -> bool:
try:
float(s)
return True
except (ValueError, TypeError):
return False
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 = SAMPLERS.get(algorithm_key, None)
if algorithm is None:
raise ValueError(f"unknown sampler {s}")
return algorithm
def get_noise_type_from_str(s: str) -> generation.SamplerNoiseType:
noise_key = s.lower().strip()
noise_type = NOISE_TYPES.get(noise_key, None)
if noise_type is None:
raise ValueError(f"unknown noise type {s}")
return noise_type
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]
def image_to_prompt(
im, init: bool = False, mask: bool = False, use_alpha=False
) -> generation.Prompt:
if init and mask:
raise ValueError("init and mask cannot both be True")
if use_alpha:
# Split into 3 channels
r, g, b, a = im.split()
# Recombine back to RGB image
im = Image.merge("RGB", (a, a, a))
im = ImageOps.invert(im)
buf = io.BytesIO()
im.save(buf, format="PNG")
buf.seek(0)
artifact_uuid = str(uuid.uuid4())
type = generation.ARTIFACT_IMAGE
if mask:
type = generation.ARTIFACT_MASK
prompt = generation.Prompt(
artifact=generation.Artifact(
type=type, uuid=artifact_uuid, binary=buf.getvalue()
),
parameters=generation.PromptParameters(init=init),
)
# bgremove = generation.ImageAdjustment(
# background_removal=generation.ImageAdjustment_BackgroundRemoval(
# mode=generation.BackgroundRemovalMode.BLUR
# )
# )
# prompt.artifact.adjustments.append(bgremove)
return prompt
def add_converter_to_hint_image_prompt(prompt, remove_bg, converter, args):
if converter is None or converter is False:
return
if remove_bg:
bgremove = generation.ImageAdjustment(
background_removal=generation.ImageAdjustment_BackgroundRemoval(
mode=generation.BackgroundRemovalMode.SOLID
)
)
prompt.artifact.adjustments.append(bgremove)
adjustment = None
hint_type = prompt.artifact.hint_image_type
if "depth" in hint_type:
adjustment = generation.ImageAdjustment(
depth=generation.ImageAdjustment_Depth()
)
elif "canny" in hint_type:
args = {"low_threshold": 100, "high_threshold": 200, **args}
adjustment = generation.ImageAdjustment(
canny_edge=generation.ImageAdjustment_CannyEdge(**args)
)
elif "hed" in hint_type or "softedge" in hint_type or "lineart" in hint_type:
adjustment = generation.ImageAdjustment(
edge_detection=generation.ImageAdjustment_EdgeDetection()
)
elif "sketch" in hint_type or "scribble" in hint_type:
adjustment = [
generation.ImageAdjustment(
edge_detection=generation.ImageAdjustment_EdgeDetection()
),
generation.ImageAdjustment(
blur=generation.ImageAdjustment_Gaussian(sigma=3)
),
generation.ImageAdjustment(
quantize=generation.ImageAdjustment_Quantize(threshold=[0.15])
),
]
elif "segment" in hint_type:
adjustment = generation.ImageAdjustment(
segmentation=generation.ImageAdjustment_Segmentation()
)
elif "keypose" in hint_type:
adjustment = generation.ImageAdjustment(
keypose=generation.ImageAdjustment_Keypose()
)
elif "openpose" in hint_type:
adjustment = generation.ImageAdjustment(
openpose=generation.ImageAdjustment_Openpose()
)
elif "normal" in hint_type:
args = {"preblur": 0, "postblur": 0, **args}
adjustment = generation.ImageAdjustment(
normal=generation.ImageAdjustment_Normal(**args)
)
elif "color" in hint_type:
args = {"colours": 8, **args}
adjustment = generation.ImageAdjustment(
palletize=generation.ImageAdjustment_Palletize(**args)
)
elif "shuffle" in hint_type:
adjustment = [
generation.ImageAdjustment(
autoscale=generation.ImageAdjustment_Autoscale(
mode=generation.RESCALE_COVER
)
),
generation.ImageAdjustment(shuffle=generation.ImageAdjustment_Shuffle()),
]
else:
raise ValueError(f"Gyre can't convert image to hint type {hint_type}")
if isinstance(adjustment, list):
prompt.artifact.adjustments.extend(adjustment)
else:
if isinstance(converter, str):
adjustment.engine_id = converter
prompt.artifact.adjustments.append(adjustment)
if remove_bg:
bgremove = generation.ImageAdjustment(
background_removal=generation.ImageAdjustment_BackgroundRemoval(
mode=generation.BackgroundRemovalMode.ALPHA, reapply=True
)
)
prompt.artifact.adjustments.append(bgremove)
return prompt
def hint_image_to_prompt(
image,
hint_type,
weight=1.0,
priority=generation.HINT_BALANCED,
remove_bg=False,
converter=None,
args={},
) -> generation.Prompt:
buf = io.BytesIO()
image.save(buf, format="PNG")
buf.seek(0)
artifact_uuid = str(uuid.uuid4())
prompt = generation.Prompt(
# echo_back=converter is not None,
artifact=generation.Artifact(
type=generation.ARTIFACT_HINT_IMAGE,
uuid=artifact_uuid,
binary=buf.getvalue(),
hint_image_type=hint_type,
),
parameters=generation.PromptParameters(weight=weight, hint_priority=priority),
)
add_converter_to_hint_image_prompt(prompt, remove_bg, converter, args)
return prompt
def ref_to_prompt(ref_uuid, type, stage=generation.ARTIFACT_AFTER_ADJUSTMENTS):
return generation.Prompt(
artifact=generation.Artifact(
type=type,
ref=generation.ArtifactReference(uuid=ref_uuid, stage=stage),
)
)
def sha256sum(filename):
h = hashlib.sha256()
b = bytearray(128 * 1024)
mv = memoryview(b)
with open(filename, "rb", buffering=0) as f:
while n := f.readinto(mv):
h.update(mv[:n])
return h.hexdigest()
def cache_id(path):
system_id = machineid.hashed_id("gyre-client")
file_hash = sha256sum(path)
return f"{system_id}-{file_hash}"
USE_DEPRECATED = False
def lora_to_prompt(path, weights, from_cache=False):
parameters = generation.PromptParameters()
if weights and len(weights) == 1:
parameters.weight = weights[0]
elif weights and len(weights) == 2:
parameters.named_weights.append(
generation.NamedWeight(name="unet", weight=weights[0])
)
parameters.named_weights.append(
generation.NamedWeight(name="text_encoder", weight=weights[1])
)
if ":" in path:
return generation.Prompt(
artifact=generation.Artifact(type=generation.ARTIFACT_LORA, url=path),
parameters=parameters,
)
elif from_cache:
return generation.Prompt(
artifact=generation.Artifact(
type=generation.ARTIFACT_LORA, cache_id=cache_id(path)
),
parameters=parameters,
)
else:
ext = os.path.splitext(path)[1]
if ext in {".bin", ".pt"}:
tensordict = torch.load(path, "cpu")
grpc_safetensors = serialize_safetensor_from_dict(tensordict)
else:
safetensors = safe_open(path, framework="pt", device="cpu")
grpc_safetensors = serialize_safetensor(safetensors)
return generation.Prompt(
artifact=generation.Artifact(
type=generation.ARTIFACT_LORA,
safetensors=grpc_safetensors,
cache_control=generation.CacheControl(
cache_id=cache_id(path), max_age=60 * 60 # Cache for an hour
),
),
parameters=parameters,
)
def ti_to_prompts(path, override_tokens, from_cache=False):
parameters = generation.PromptParameters()
for token in override_tokens:
parameters.token_overrides.append(generation.TokenOverride(token=token))
if ":" in path:
prompt = generation.Prompt(
artifact=generation.Artifact(
type=generation.ARTIFACT_TOKEN_EMBEDDING, url=path
),
parameters=parameters,
)
elif from_cache:
prompt = generation.Prompt(
artifact=generation.Artifact(
type=generation.ARTIFACT_TOKEN_EMBEDDING, cache_id=cache_id(path)
),
parameters=parameters,
)
else:
data = torch.load(path, "cpu")
if "string_to_param" in data:
data = data["string_to_param"]
prompt = generation.Prompt(
artifact=generation.Artifact(
type=generation.ARTIFACT_TOKEN_EMBEDDING,
safetensors=serialize_safetensor_from_dict(data),
cache_control=generation.CacheControl(
cache_id=cache_id(path), max_age=60 * 60 # Cache for an hour
),
),
parameters=parameters,
)
return [prompt]
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 in {
generation.ARTIFACT_IMAGE,
generation.ARTIFACT_MASK,
generation.ARTIFACT_HINT_IMAGE,
}:
if artifact.mime == "image/webp":
ext = ".webp"
else:
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
class StabilityInference:
def __init__(
self,
host: str = "grpc.stability.ai:443",
key: str = "",
proto: Literal["grpc", "grpc-web"] = "grpc",
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 = {}
if proto == "grpc":
self.grpc_args["wait_for_ready"] = wait_for_ready
if verbose:
logger.info(f"Opening channel to {host}")
maxMsgLength = 256 * 1024 * 1024 # 256 MB
channel_options = [
("grpc.max_message_length", maxMsgLength),
("grpc.max_send_message_length", maxMsgLength),
("grpc.max_receive_message_length", maxMsgLength),
]
call_credentials = []
if proto == "grpc-web":
from gyre.sonora import client as sonora_client
channel = sonora_client.insecure_web_channel(host)
elif 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
),
options=channel_options,
)
else:
channel = grpc.insecure_channel(host, options=channel_options)
if verbose:
logger.info(f"Channel opened to {host}")
self.stub = generation_grpc.GenerationServiceStub(channel)
self.engine_stub = engines_grpc.EnginesServiceStub(channel)
def list_engines(self, task_group=engines.GENERATE):
request = engines.ListEnginesRequest(task_group=task_group)
print(self.engine_stub.ListEngines(request))
def generate(
self,
prompt: Union[str, List[str], generation.Prompt, List[generation.Prompt]],
negative_prompt: str = None,
clip_layer: Optional[int] = None,
init_image: Optional[Image.Image] = None,
mask_image: Optional[Image.Image] = None,
mask_from_image_alpha: bool = False,
height: int | None = None,
width: int | None = None,
start_schedule: float = 1.0,
end_schedule: float = 0.01,
cfg_scale: float = 7.0,
eta: float = 0.0,
churn: float = None,
churn_tmin: float = None,
churn_tmax: float = None,
sigma_min: float = None,
sigma_max: float = None,
karras_rho: float = None,
noise_type: int = None,
sampler: generation.DiffusionSampler = generation.SAMPLER_K_LMS,
steps: int = 50,
seed: Union[Sequence[int], int] = 0,
samples: int = 1,
safety: bool = True,
classifiers: Optional[generation.ClassifierParameters] = None,
guidance_preset: generation.GuidancePreset = generation.GUIDANCE_PRESET_NONE,
guidance_cuts: int = 0,
guidance_strength: Optional[float] = None,
guidance_prompt: Union[str, generation.Prompt] = None,
guidance_models: List[str] = None,
hires_fix: bool | None = None,
hires_oos_fraction: float | None = None,
tiling: str = "no",
hint_images: list[dict[str, str | float]] | None = None,
lora: list[tuple[str, list[float]]] | None = None,
ti: list[tuple[str, list[str]]] | None = None,
as_async=False,
from_cache=True,
accept_webp=True,
) -> 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: DEPRECATED/UNUSED - Cannot be disabled.
:param classifiers: DEPRECATED/UNUSED - Has no effect on image generation.
:param guidance_preset: Guidance preset to use. See generation.GuidancePreset for supported values.
:param guidance_cuts: Number of cuts to use for guidance.
:param guidance_strength: Strength of the guidance. We recommend values in range [0.0,1.0]. A good default is 0.25
:param guidance_prompt: Prompt to use for guidance, defaults to `prompt` argument (above) if not specified.
:param guidance_models: Models to use for guidance.
:return: Generator of Answer objects.
"""
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"
)
if not seed:
seed = [random.randrange(0, 4294967295)]
elif isinstance(seed, int):
seed = [seed]
else:
seed = list(seed)
prompts: List[generation.Prompt] = []
if any(isinstance(prompt, t) for t in (str, generation.Prompt)):
prompt = [prompt]
for p in prompt:
if isinstance(p, str):
p = generation.Prompt(text=p)
if clip_layer:
p.parameters.clip_layer = clip_layer
elif not isinstance(p, generation.Prompt):
raise TypeError("prompt must be a string or generation.Prompt object")
prompts.append(p)
if negative_prompt:
prompts += [
generation.Prompt(
text=negative_prompt,
parameters=generation.PromptParameters(weight=-1),
)
]
sampler_parameters: dict[str, Any] = dict(cfg_scale=cfg_scale)
if eta:
sampler_parameters["eta"] = eta
if noise_type:
sampler_parameters["noise_type"] = noise_type
if churn:
churn_parameters = dict(churn=churn)
if churn_tmin:
churn_parameters["churn_tmin"] = churn_tmin
if churn_tmax:
churn_parameters["churn_tmax"] = churn_tmax
sampler_parameters["churn"] = generation.ChurnSettings(**churn_parameters)
sigma_parameters = {}
if sigma_min:
sigma_parameters["sigma_min"] = sigma_min
if sigma_max:
sigma_parameters["sigma_max"] = sigma_max
if karras_rho:
sigma_parameters["karras_rho"] = karras_rho
sampler_parameters["sigma"] = generation.SigmaParameters(**sigma_parameters)
step_parameters = dict(
scaled_step=0, sampler=generation.SamplerParameters(**sampler_parameters)
)
init_image_prompt = None
if init_image is not None:
# NB: Specifying schedule when there's no init image causes washed out results
step_parameters["schedule"] = generation.ScheduleParameters(
start=start_schedule,
end=end_schedule,
)
init_image_prompt = image_to_prompt(init_image, init=True)
prompts += [init_image_prompt]
if mask_image is not None:
prompts += [image_to_prompt(mask_image, mask=True)]
elif mask_from_image_alpha:
mask_prompt = ref_to_prompt(
init_image_prompt.artifact.uuid, type=generation.ARTIFACT_MASK
)
mask_prompt.artifact.adjustments.append(
generation.ImageAdjustment(
channels=generation.ImageAdjustment_Channels(
r=generation.CHANNEL_A,
g=generation.CHANNEL_A,
b=generation.CHANNEL_A,
a=generation.CHANNEL_DISCARD,
)
)
)
mask_prompt.artifact.adjustments.append(
generation.ImageAdjustment(
invert=generation.ImageAdjustment_Invert()
)
)
mask_prompt.artifact.adjustments.append(
generation.ImageAdjustment(
blur=generation.ImageAdjustment_Gaussian(
sigma=32, direction=generation.DIRECTION_UP
)
)
)
prompts += [mask_prompt]
if hint_images:
for hint in hint_images:
if "image" not in hint:
if init_image_prompt is None:
raise ValueError(
"Can't use hint_from_init without also passing init_image"
)
hint_prompt = ref_to_prompt(
init_image_prompt.artifact.uuid,
type=generation.ARTIFACT_HINT_IMAGE,
)
hint_prompt.echo_back = True
hint_prompt.artifact.hint_image_type = hint["hint_type"]
hint_prompt.parameters.weight = hint["weight"]
hint_prompt.parameters.priority = hint["prioriy"]
add_converter_to_hint_image_prompt(
hint_prompt, hint["remove_bg"], hint["converter"], hint["args"]
)
else:
hint_prompt = hint_image_to_prompt(**hint)
prompts += [hint_prompt]
if lora:
for path, weights in lora:
prompts += [lora_to_prompt(path, weights, from_cache=from_cache)]
if ti:
for path, overrides in ti:
prompts += ti_to_prompts(path, overrides, from_cache=from_cache)
if guidance_prompt:
if isinstance(guidance_prompt, str):
guidance_prompt = generation.Prompt(text=guidance_prompt)
elif not isinstance(guidance_prompt, generation.Prompt):
raise ValueError("guidance_prompt must be a string or Prompt object")
# if guidance_strength == 0.0:
# guidance_strength = None
# Build our CLIP parameters
if (
guidance_preset is not generation.GUIDANCE_PRESET_NONE
or guidance_strength is not None
):
# to do: make it so user can override this
# step_parameters['sampler']=None
if guidance_models:
guiders = [generation.Model(alias=model) for model in guidance_models]
else:
guiders = None
if guidance_cuts:
cutouts = generation.CutoutParameters(count=guidance_cuts)
else:
cutouts = None
step_parameters["guidance"] = generation.GuidanceParameters(
guidance_preset=guidance_preset,
instances=[
generation.GuidanceInstanceParameters(
guidance_strength=guidance_strength,
models=guiders,
cutouts=cutouts,
prompt=guidance_prompt,
)
],
)
if hires_fix is None and hires_oos_fraction is not None:
hires_fix = True
hires = None
if hires_fix is not None:
hires_params: dict[str, bool | float] = dict(enable=hires_fix)
if hires_oos_fraction is not None:
hires_params["oos_fraction"] = hires_oos_fraction
hires = generation.HiresFixParameters(**hires_params)
tiling_params = {}
if tiling == "xy" or tiling == "yes":
tiling_params["tiling"] = True
elif tiling == "x":
tiling_params["tiling_x"] = True
elif tiling == "y":
tiling_params["tiling_y"] = True
image_parameters = generation.ImageParameters(
transform=generation.TransformType(diffusion=sampler),
seed=seed,
steps=steps,
samples=samples,
parameters=[generation.StepParameter(**step_parameters)],
hires=hires,
**tiling_params,
)
if height is not None:
image_parameters.height = height
if width is not None:
image_parameters.width = width
if as_async:
return self.emit_async_request(
prompt=prompts,
image_parameters=image_parameters,
accept_webp=accept_webp,
)
else:
return self.emit_request(
prompt=prompts,
image_parameters=image_parameters,
accept_webp=accept_webp,
)
# The motivation here is to facilitate constructing requests by passing protobuf objects directly.
def emit_request(
self,
prompt: generation.Prompt,
image_parameters: generation.ImageParameters,
engine_id: str = None,
request_id: str = None,
accept_webp: bool = True,
):
if not request_id:
request_id = str(uuid.uuid4())
if not engine_id:
engine_id = self.engine
extra_kwargs = {}
if accept_webp:
extra_kwargs["accept"] = "image/webp, image/png"
rq = generation.Request(
engine_id=engine_id,
request_id=request_id,
prompt=prompt,
image=image_parameters,
**extra_kwargs,
)
# with open("request.json", "w") as f:
# json.dump(MessageToDict(rq), f, indent=2)
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()
# The motivation here is to facilitate constructing requests by passing protobuf objects directly.
def emit_async_request(
self,
prompt: generation.Prompt,
image_parameters: generation.ImageParameters,
engine_id: str = None,
request_id: str = None,
accept_webp: bool = True,
):
if not request_id:
request_id = str(uuid.uuid4())
if not engine_id:
engine_id = self.engine
extra_kwargs = {}
if accept_webp:
extra_kwargs["accept"] = "image/webp, image/png"
rq = generation.Request(
engine_id=engine_id,
request_id=request_id,
prompt=prompt,
image=image_parameters,
**extra_kwargs,
)
if self.verbose:
logger.info("Sending request.")
start = time.time()
handle = self.stub.AsyncGenerate(rq, **self.grpc_args)
print(handle)
def cancel_request(unused_signum, unused_frame):
print("Cancelling")
self.stub.AsyncCancel(handle)
sys.exit(0)
signal.signal(signal.SIGINT, cancel_request)
while True:
answers = self.stub.AsyncResult(handle)
for answer in answers.answer:
yield answer
if answers.complete:
if answers.status.code:
raise GrpcAsyncError(answers.status.code, answers.status.message)
print("Done")
break
time.sleep(1)
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"
)