42 def encode(self, prompt, negative_prompt, num_images_per_prompt):
43 API_KEY = get_api_key()
44 split_pos_text = prompt.split("|")
45 num_prompts = len(split_pos_text)
46 assert (
47 num_prompts * num_images_per_prompt <= 4
48 ), f"The num_prompts*num_images_per_prompt should be less than or equal 4, got {num_prompts}x{num_images_per_prompt}={num_prompts*num_images_per_prompt}, try to reduce the '|' or num_images_per_prompt"
49
50 payload = {
51 "positive_prompt": prompt,
222 "inputs": None,
223 "is_compress": True,
224 }
225 if latent is not None:
226 assert (
227 latent["samples"].shape[0] == kolors_embeds["prompt_embeds"].shape[0]
228 ), f"The batch size of latent samples should be the same as the prompt_embeds, got {latent['samples'].shape[0]} and {kolors_embeds['prompt_embeds'].shape[0]}"
229
230 # convert the tensors to numpy array
231 np_kolors_embeds = copy.deepcopy(kolors_embeds)
30 def super_resolution(self, image, scale="2x"):
31 API_KEY = get_api_key()
32 device = image.device
33 _, w, h, c = image.shape
34 assert (
35 w <= 512 and h <= 512
36 ), f"width and height must be less than 512, but got {w} and {h}"
37
38 # support RGB mode only now
39 image = image[:, :, :, :3]
76 def remove_background(self, image):
77 API_KEY = get_api_key()
78 device = image.device
79 _, w, h, _ = image.shape
80 assert (
81 w <= 1024 and h <= 1024
82 ), f"width and height must be less than 1024, but got {w} and {h}"
83
84 payload = {
85 "is_compress": True,
138 CATEGORY = "☁️BizyAir"
139
140 def generate_image(self, prompt, seed, width, height, cfg, batch_size):
141 API_KEY = get_api_key()
142 assert (
143 width <= 1024 and height <= 1024
144 ), f"width and height must be less than 1024, but got {width} and {height}"
145
146 payload = {
147 "batch_size": batch_size,
1 import base64 2 import json 3 import os 4 import pickle 5 import urllib.request 6 import urllib.parse 7 import zlib 8
23 """
24 try:
25 data = json.dumps(payload).encode("utf-8")
26 req = urllib.request.Request(api_url, data=data, headers=headers, method="POST")
27 with urllib.request.urlopen(req) as response:
28 response_data = response.read().decode("utf-8")
29 return response_data
30 except urllib.error.URLError as e:
90 print(
91 f"decode_and_deserialize: size of bytes is {format_bytes(len(tensor_bytes))}"
92 )
93
94 deserialized_object = pickle.loads(tensor_bytes)
95 return deserialized_object
96
97
110 return f"{num_bytes / (1024 * 1024):.2f} MB"
111
112
113 def validate_api_key(key: str):
114 assert isinstance(key, str), f"API_KEY must be a string, got {type(key)}"
115 assert key.startswith(
116 "sk"
117 ), f"invalid key, please refer to https://cloud.siliconflow.cn to get your API_KEY, got {key}"
111
112
113 def validate_api_key(key: str):
114 assert isinstance(key, str), f"API_KEY must be a string, got {type(key)}"
115 assert key.startswith(
116 "sk"
117 ), f"invalid key, please refer to https://cloud.siliconflow.cn to get your API_KEY, got {key}"
118
119
120 def get_api_key():