Files
siliconflow-BizyAir/supernode.py
T
5be8fc9dd5 UI Redesign & Add Model Upload Capability (#111)
* update: test

* update: test

* test

* update: test

* update: test

* test

* update: test

* update: test

* update: test

* update: test

* update: test

* test

* test

* test

* test

* test

* test

* test

* test

* test

* fix

* test

* test

* frontend

* update: test

* init

* update: list-files

* init

* fix

* update: list-files

* update: list-files

* fix

* update: list-files

* fix

* Dev modelhost yaochi (#109)

* remove florence2 caption node (#98)

* fix bug of category name (#100)

* Add ultimate-sd-upscale (#102)

* Add ultimate-sd-upscale

* refine

* refine

* ultimate_sd_upscale

* refine

* remove model that cannot be used in commercial

* Update README.md

* Update README.md

---------

Co-authored-by: Yao Chi <later@usopp.net>

* add sr deprecated message (#103)

* formatting code (#106)

* formatting code

* refine

* Refactor Bizyair and Code Formatting Improvements (#104)

* Refactor Bizyair and Code Formatting Improvements

* refine

* Delete redundant code

* Delete redundant code

* fix UNIQUE_ID

* refine

* refine

* refine

* skip Super Resolution test

* Add lora test server (#105)

* refine

* Delete src/bizyair/commands/servers/server_base.py

---------

Co-authored-by: Yao Chi <later@usopp.net>

* reformat

* news to new url

---------

Co-authored-by: FengWen <109639975+ccssu@users.noreply.github.com>

* add Single file upload

* fix: add get_api_key

* fix: dependencies check and invalid api_key errno

* fix: dependencies check and invalid api_key errno

* fix: print message

* refine the text content

* reformat

* fix

* refine content

* update: reduce allow types

* update: reduce allow types

* update: reduce allow types

* update: merge requirements_adv.txt to requirements.txt

* fix

* fix

* fix

* fix

* reformat

* update: change all server_address to bizyair-api.siliconflow.cn

* update ci ComfyUI version

* reformat and updage

* fix

* fix

* update: upload limit

* update: upload limit

* update: upload limit

* update: upload limit

* fix

* update: upload limit

* change to wait for bizyair logo

* fix

* update: adjust structure

* fix

* refine the queue btn CI

* use comfy queue btn class

* use js to wait queue finished

* refine

* write ini file and reformat

* fix: oss progressbar

* fix

* update: clear upload cache

* fix

* fix

* fix

* Fix Model Find (#112)

Co-authored-by: Yao Chi <later@usopp.net>

* remformat

* fix

* fix: model repeat delete

* Dev modelhost async upload file (#113)

* refine

* refine get loop

* chagne to async upload

* fix: path_manager.py

* fix: remove useless dependency

* fix: service_address

* fix

* flux dev fp8 (#107)

* flux dev fp8

* refine

---------

Co-authored-by: Yao Chi <later@usopp.net>

* add docs for lora model host (#115)

* refine

* fix the url route

* update readme

* add flux lora demo

* Add lora modelhost docs (#118)

* refine

* fix the url route

* update readme

* update news

---------

Co-authored-by: dantegarden <761043617@qq.com>
Co-authored-by: root <root@vscode-1631487780716577-vscode-master-0.vscode-1631487780716577-vscode.org-1599978210328609.svc.cluster.local>
Co-authored-by: Yao Chi <later@usopp.net>
Co-authored-by: FengWen <109639975+ccssu@users.noreply.github.com>
2024-09-06 13:00:34 +08:00

262 lines
7.3 KiB
Python

import json
import os
import uuid
import torch
from bizyair.image_utils import decode_data, encode_data
from .utils import (
decode_and_deserialize,
get_api_key,
send_post_request,
serialize_and_encode,
)
BIZYAIR_SERVER_ADDRESS = os.getenv(
"BIZYAIR_SERVER_ADDRESS", "https://bizyair-api.siliconflow.cn/x/v1"
)
class SuperResolution:
API_URL = f"{BIZYAIR_SERVER_ADDRESS}/supernode/superresolution"
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"scale": (["2x", "4x"],),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "super_resolution"
CATEGORY = "☁️BizyAir/Super Resolution"
def super_resolution(self, image, scale="2x"):
API_KEY = get_api_key()
device = image.device
_, w, h, c = image.shape
assert (
w <= 512 and h <= 512
), f"width and height must be less than 512, but got {w} and {h}"
# support RGB mode only now
image = image[:, :, :, :3]
payload = {
"scale": scale,
"is_compress": True,
"image": None,
}
auth = f"Bearer {API_KEY}"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": auth,
}
input_image, compress = serialize_and_encode(image, compress=True)
payload["image"] = input_image
payload["is_compress"] = compress
response: str = send_post_request(
self.API_URL, payload=payload, headers=headers
)
image = decode_and_deserialize(response)
image = image.to(device)
return (image,)
class RemoveBackground:
API_URL = f"{BIZYAIR_SERVER_ADDRESS}/supernode/removebg"
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
}
}
RETURN_TYPES = ("IMAGE", "MASK")
FUNCTION = "remove_background"
CATEGORY = "☁️BizyAir"
def remove_background(self, image):
API_KEY = get_api_key()
device = image.device
_, h, w, _ = image.shape
assert (
w <= 1536 and h <= 1536
), f"width and height must be less than 1536, but got {w} and {h}"
payload = {
"is_compress": True,
"image": None,
}
auth = f"Bearer {API_KEY}"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": auth,
}
input_image, compress = serialize_and_encode(image, compress=True)
payload["image"] = input_image
payload["is_compress"] = compress
response: str = send_post_request(
self.API_URL, payload=payload, headers=headers
)
tensors = decode_and_deserialize(response)
t_images = tensors["images"].to(device)
t_mask = tensors["mask"].to(device)
return (t_images, t_mask)
class GenerateLightningImage:
API_URL = f"{BIZYAIR_SERVER_ADDRESS}/supernode/realvis4lightning"
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"prompt": (
"STRING",
{"multiline": True, "dynamicPrompts": True, "default": "a dog"},
),
"seed": ("INT", {"default": 1, "min": 0, "max": 0xFFFFFFFFFFFFFFFF}),
"width": ("INT", {"default": 1024, "min": 16, "max": 1024, "step": 8}),
"height": ("INT", {"default": 1024, "min": 16, "max": 1024, "step": 8}),
"cfg": (
"FLOAT",
{
"default": 1.5,
"min": 0.0,
"max": 10.0,
"step": 0.1,
"round": 0.01,
},
),
"batch_size": ("INT", {"default": 1, "min": 1, "max": 4}),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "generate_image"
CATEGORY = "☁️BizyAir"
def generate_image(self, prompt, seed, width, height, cfg, batch_size):
API_KEY = get_api_key()
assert (
width <= 1024 and height <= 1024
), f"width and height must be less than 1024, but got {width} and {height}"
payload = {
"batch_size": batch_size,
"width": width,
"height": height,
"prompt": prompt,
"cfg": cfg,
"seed": seed,
}
auth = f"Bearer {API_KEY}"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": auth,
}
response: str = send_post_request(
self.API_URL, payload=payload, headers=headers
)
tensors_np = decode_and_deserialize(response)
tensors = torch.from_numpy(tensors_np)
return (tensors,)
class AuraSR:
API_URL = f"{BIZYAIR_SERVER_ADDRESS}/supernode/aurasr"
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "super_resolution"
CATEGORY = "☁️BizyAir/Super Resolution"
def super_resolution(self, image):
API_KEY = get_api_key()
SIZE_LIMIT = 1536
device = image.device
_, w, h, c = image.shape
assert (
w <= SIZE_LIMIT and h <= SIZE_LIMIT
), f"width and height must be less than {SIZE_LIMIT}x{SIZE_LIMIT}, but got {w} and {h}"
# support RGB mode only now
image = image[:, :, :, :3]
payload = {
"is_compress": True,
"image": None,
}
auth = f"Bearer {API_KEY}"
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": auth,
}
input_image = encode_data(image, disable_image_marker=True)
payload["image"] = input_image
payload["is_compress"] = True
ret: str = send_post_request(self.API_URL, payload=payload, headers=headers)
ret = json.loads(ret)
try:
if "result" in ret:
ret = json.loads(ret["result"])
except Exception as e:
raise Exception(f"Unexpected response: {ret} {e=}")
if ret["status"] == "error":
raise Exception(ret["message"])
msg = ret["data"]
if msg["type"] not in (
"comfyair",
"bizyair",
):
raise Exception(f"Unexpected response type: {msg}")
image_b64 = msg["data"]["payload"]
image = decode_data(image_b64)
image = image.to(device)
return (image,)
NODE_CLASS_MAPPINGS = {
"BizyAirSuperResolution": SuperResolution,
"BizyAirRemoveBackground": RemoveBackground,
"BizyAirGenerateLightningImage": GenerateLightningImage,
"BizyAirAuraSR": AuraSR,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"BizyAirAuraSR": "☁️BizyAir Photorealistic Image Super Resolution",
"BizyAirSuperResolution": "☁️BizyAir Anime Image Super Resolution",
"BizyAirRemoveBackground": "☁️BizyAir Remove Image Background",
"BizyAirGenerateLightningImage": "☁️BizyAir Generate Photorealistic Images",
}