Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
linjm8780860 committed Nov 20, 2024
2 parents d4510d8 + b5997c4 commit 7a39e75
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/workflow-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ jobs:
python3 tests/test_examples.py
env:
BIZYAIR_KEY: ${{ secrets.BIZYAIR_KEY }}
BIZYAIR_API_KEY: ${{ secrets.BIZYAIR_KEY }}
PYTHONPATH: ${{ github.workspace }}/ComfyUI

- name: Run ComfyUI on Windows
Expand All @@ -112,4 +113,5 @@ jobs:
python3 tests/test_examples.py
env:
BIZYAIR_KEY: ${{ secrets.BIZYAIR_KEY }}
BIZYAIR_API_KEY: ${{ secrets.BIZYAIR_KEY }}
PYTHONPATH: ${{ github.workspace }}/ComfyUI
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"
}
25 changes: 14 additions & 11 deletions bizyair_extras/nodes_image_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import hashlib
import os
import re
import time
import urllib.request

import folder_paths
Expand Down Expand Up @@ -43,25 +43,28 @@ def apply(self, url: str):
if match:
# get image format
image_format = match.group(1)
# generate a file name with timestamp
timestamp = int(time.time() * 1000) # precise to milliseconds
filename = f"base64-{timestamp}.{image_format}"
image_data = url.split(",")[1]

# calculate hash of the base64 data
hash_object = hashlib.md5(image_data.encode())
hash_value = hash_object.hexdigest()

filename = f"base64-{hash_value}.{image_format}"
file_path = os.path.join(input_dir, filename)

# decode base64 data and save file
image_data = url.split(",")[1]
with open(file_path, "wb") as f:
f.write(base64.b64decode(image_data))
print(f"Base64 image saved as {filename}")
if not os.path.exists(file_path):
with open(file_path, "wb") as f:
f.write(base64.b64decode(image_data))
print(f"Base64 image saved as {filename}")
else:
print(f"Base64 image {filename} already exists, skipping save.")
else:
filename = os.path.basename(url)
file_path = os.path.join(input_dir, filename)

# Check if the file already exists
if os.path.exists(file_path):
print(f"File {filename} already exists, skipping download.")
else:
# Download the image
urllib.request.urlretrieve(url, file_path)
print(f"Image successfully downloaded and saved as {filename}.")

Expand Down
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
}
6 changes: 3 additions & 3 deletions js/biz_lib_frontend.js

Large diffs are not rendered by default.

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);
},
})
3 changes: 2 additions & 1 deletion src/bizy_server/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

from .errno import ErrorNo, errnos
from .error_handler import ErrorHandler
Expand All @@ -19,7 +20,7 @@ class APIClient:
def __init__(self):
self.error_handler = ErrorHandler()
self.session = None

async def get_session(self):
if self.session is None:
timeout = aiohttp.ClientTimeout(total=3)
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 7a39e75

Please sign in to comment.