Skip to content

Commit 22c9d9c

Browse files
committed
modify the code
1 parent c4063aa commit 22c9d9c

3 files changed

Lines changed: 112 additions & 228 deletions

File tree

llm.py

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import json
3-
43
import aiohttp
54
from aiohttp import web
65
from server import PromptServer
@@ -282,6 +281,7 @@ def __init__(self):
282281

283282
# refer to: https://huggingface.co/spaces/fancyfeast/joy-caption-pre-alpha
284283
API_URL = f"{BIZYAIR_SERVER_ADDRESS}/supernode/joycaption2"
284+
# API_URL = "https://maas.platform.oneflow.cloud/supernode/crossing-joycaption2/supernode/crossing-joycaption2"
285285

286286
@classmethod
287287
def INPUT_TYPES(s):
@@ -353,12 +353,28 @@ def INPUT_TYPES(s):
353353
}
354354

355355
RETURN_TYPES = ("STRING",)
356-
FUNCTION = "joycaption2"
356+
FUNCTION = "my_joycaption"
357357

358358
CATEGORY = "☁️BizyAir/AI Assistants"
359359

360-
def joycaption2(
360+
async def send_post_request_async(
361+
self, session, url, payload, headers, max_retries=3, retry_delay=2, timeout=100
362+
):
363+
try:
364+
async with session.post(
365+
url, json=payload, headers=headers, timeout=timeout
366+
) as response:
367+
response.raise_for_status()
368+
ret = await response.json()
369+
return ret
370+
except Exception as e:
371+
372+
print(f"Request failed. Error: {e}")
373+
return {"data": {"type": "bizyair", "data": ""}}
374+
375+
async def joycaption2(
361376
self,
377+
session,
362378
image,
363379
do_sample,
364380
temperature,
@@ -371,6 +387,8 @@ def joycaption2(
371387
):
372388
API_KEY = get_api_key()
373389
SIZE_LIMIT = 1536
390+
max_retries = 3
391+
retry_delay = 2
374392
_, w, h, c = image.shape
375393
assert (
376394
w <= SIZE_LIMIT and h <= SIZE_LIMIT
@@ -396,17 +414,15 @@ def joycaption2(
396414
input_image = encode_data(image, disable_image_marker=True)
397415
payload["image"] = input_image
398416

399-
ret: str = send_post_request(self.API_URL, payload=payload, headers=headers)
400-
ret = json.loads(ret)
401-
402-
try:
403-
if "result" in ret:
404-
ret = json.loads(ret["result"])
405-
if ret["type"] == "error":
406-
raise Exception(ret["message"])
407-
except Exception as e:
408-
raise Exception(f"Unexpected response: {ret} {e=}")
409-
417+
ret = await self.send_post_request_async(
418+
session,
419+
self.API_URL,
420+
payload=payload,
421+
headers=headers,
422+
max_retries=max_retries,
423+
retry_delay=retry_delay,
424+
)
425+
ret = json.loads(ret["result"])
410426
msg = ret["data"]
411427
if msg["type"] not in (
412428
"comfyair",
@@ -417,6 +433,25 @@ def joycaption2(
417433
caption = msg["data"]
418434
return (caption,)
419435

436+
async def my_joycaption_async(self, image, **kwargs):
437+
captions = []
438+
tasks = []
439+
async with aiohttp.ClientSession() as session:
440+
for i in range(image.size(0)):
441+
image_file = image[i].unsqueeze(0)
442+
tasks.append(self.joycaption2(session, image_file, **kwargs))
443+
444+
results = await asyncio.gather(*tasks)
445+
446+
for result in results:
447+
captions.append(result[0])
448+
449+
combined_caption = " | ".join(captions)
450+
return {"ui": {"text": (combined_caption,)}, "result": (combined_caption,)}
451+
452+
def my_joycaption(self, image, **kwargs):
453+
return asyncio.run(self.my_joycaption_async(image, **kwargs))
454+
420455

421456
NODE_CLASS_MAPPINGS = {
422457
"BizyAirSiliconCloudLLMAPI": SiliconCloudLLMAPI,

0 commit comments

Comments
 (0)