Skip to content

Commit

Permalink
Add endpoints for bizyair server address (#224)
Browse files Browse the repository at this point in the history
* init

* add front dlg

* add example

* use toast instead of dialog

* fix path

* refine

---------

Co-authored-by: Your Name <you@example.com>
  • Loading branch information
doombeaker and Your Name authored Nov 20, 2024
1 parent d9165dc commit b5997c4
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 8 deletions.
3 changes: 2 additions & 1 deletion bizyair_example_menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
},
"Remove the background from the image": "bizyair_showcase_remove_background.json",
"Recreate an existing image": "bizyair_flux_joycaption_img2img_workflow.json",
"Text Guided Segment Anything": "bizyair_text_guided_segment-anything.json"
"Text Guided Segment Anything": "bizyair_text_guided_segment-anything.json",
"Choose BizyAir Server Endpoint": "bizyair_switch_end_point.json"
}
42 changes: 42 additions & 0 deletions examples/bizyair_switch_end_point.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"last_node_id": 1,
"last_link_id": 0,
"nodes": [
{
"id": 1,
"type": "BizyAirToggleServerEndpoint",
"pos": {
"0": 714,
"1": 197
},
"size": {
"0": 403.1999816894531,
"1": 58
},
"flags": {},
"order": 0,
"mode": 0,
"inputs": [],
"outputs": [],
"properties": {
"Node name for S&R": "BizyAirToggleServerEndpoint"
},
"widgets_values": [
"https://bizyair-api-st.siliconflow.cn/x/v1"
]
}
],
"links": [],
"groups": [],
"config": {},
"extra": {
"ds": {
"scale": 1,
"offset": [
0,
0
]
}
},
"version": 0.4
}
17 changes: 17 additions & 0 deletions js/server_endpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { api } from "../../../scripts/api.js";
import { app } from "../../scripts/app.js";
import { toast } from './subassembly/toast.js';

app.registerExtension({
name: "bizyair.server.endpoint.switch.dlg",
async setup() {
function messageHandler(event) {
toast({
content: event.detail.message,
type: 'succeed',
center: true
})
}
api.addEventListener("bizyair.server.endpoint.switch", messageHandler);
},
})
5 changes: 1 addition & 4 deletions src/bizy_server/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import bizyair
import bizyair.common
from bizyair.common.env_var import BIZYAIR_SERVER_ADDRESS

from .errno import (
CHANGE_PUBLIC_ERR,
Expand All @@ -30,10 +31,6 @@
)
from .error_handler import ErrorHandler

BIZYAIR_SERVER_ADDRESS = os.getenv(
"BIZYAIR_SERVER_ADDRESS", "https://bizyair-api.siliconflow.cn/x/v1"
)


class APIClient:
def __init__(self):
Expand Down
1 change: 1 addition & 0 deletions src/bizyair/common/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def send_request(
) -> dict:
try:
headers = kwargs.pop("headers") if "headers" in kwargs else _headers()
headers["User-Agent"] = "BizyAir Client"

req = urllib.request.Request(
url, data=data, headers=headers, method=method, **kwargs
Expand Down
27 changes: 26 additions & 1 deletion src/bizyair/common/env_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@
from pathlib import Path


class ServerAddress:
_instance = None
_address = None

def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(ServerAddress, cls).__new__(cls)
return cls._instance

def __init__(self, address):
self._address = address

@property
def address(self):
return self._address

@address.setter
def address(self, new_address):
self._address = new_address

def __str__(self):
return self._address


def env(key, type_, default=None):
if key not in environ:
return default
Expand Down Expand Up @@ -60,9 +84,10 @@ def create_api_key_file(api_key):
# service_address: https://bizyair-api.siliconflow.cn/x/v1
# uat:
# service_address: https://uat-bizyair-api.siliconflow.cn/x/v1
BIZYAIR_SERVER_ADDRESS = os.getenv(
_BIZYAIR_SERVER_ADDRESS = os.getenv(
"BIZYAIR_SERVER_ADDRESS", "https://bizyair-api.siliconflow.cn/x/v1"
)
BIZYAIR_SERVER_ADDRESS = ServerAddress(_BIZYAIR_SERVER_ADDRESS)
BIZYAIR_API_KEY = env("BIZYAIR_API_KEY", str, load_api_key()[1])
# Development Settings
BIZYAIR_DEV_REQUEST_URL = env("BIZYAIR_DEV_REQUEST_URL", str, None)
Expand Down
4 changes: 2 additions & 2 deletions src/bizyair/path_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def load_yaml_config(file_path):

def get_service_route(service_config: Dict[str, str]) -> Union[str, None]:
if {"route"}.issubset(service_config):
return service_config.get(
"service_address", BIZYAIR_SERVER_ADDRESS
return str(
service_config.get("service_address", BIZYAIR_SERVER_ADDRESS)
) + service_config.get("route")
return None
36 changes: 36 additions & 0 deletions supernode.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,47 @@ def generate_image(self, prompt, seed, width, height, cfg, batch_size):
return (tensors,)


class ToggleServerEndpoint:
BIZYAIR_SERVER_ENDPOINTS = [
"https://bizyair-api-st.siliconflow.cn/x/v1",
"https://bizyair-api.siliconflow.cn/x/v1",
]

def __init__(self):
self.current_index = 0

@classmethod
def INPUT_TYPES(s):
return {
"required": {
"endpoint": (s.BIZYAIR_SERVER_ENDPOINTS,),
}
}

RETURN_TYPES = ()
FUNCTION = "toggle_endpoint"
OUTPUT_NODE = True

CATEGORY = "☁️BizyAir"

def toggle_endpoint(self, endpoint):
BIZYAIR_SERVER_ADDRESS.address = endpoint
from server import PromptServer

PromptServer.instance.send_sync(
"bizyair.server.endpoint.switch",
{"message": f"Switch server endpoint to {endpoint}"},
)
return ()


NODE_CLASS_MAPPINGS = {
"BizyAirRemoveBackground": RemoveBackground,
"BizyAirGenerateLightningImage": GenerateLightningImage,
"BizyAirToggleServerEndpoint": ToggleServerEndpoint,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"BizyAirRemoveBackground": "☁️BizyAir Remove Image Background",
"BizyAirGenerateLightningImage": "☁️BizyAir Generate Photorealistic Images",
"BizyAirToggleServerEndpoint": "☁️BizyAir Switch Server Endpoint",
}

0 comments on commit b5997c4

Please sign in to comment.