mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2025-12-17 01:52:59 +08:00
convert StabilityAI to use new API client (#10582)
This commit is contained in:
parent
5f109fe6a0
commit
20182a393f
@ -20,13 +20,6 @@ from comfy_api_nodes.apis.stability_api import (
|
|||||||
StabilityAudioInpaintRequest,
|
StabilityAudioInpaintRequest,
|
||||||
StabilityAudioResponse,
|
StabilityAudioResponse,
|
||||||
)
|
)
|
||||||
from comfy_api_nodes.apis.client import (
|
|
||||||
ApiEndpoint,
|
|
||||||
HttpMethod,
|
|
||||||
SynchronousOperation,
|
|
||||||
PollingOperation,
|
|
||||||
EmptyRequest,
|
|
||||||
)
|
|
||||||
from comfy_api_nodes.util import (
|
from comfy_api_nodes.util import (
|
||||||
validate_audio_duration,
|
validate_audio_duration,
|
||||||
validate_string,
|
validate_string,
|
||||||
@ -34,6 +27,9 @@ from comfy_api_nodes.util import (
|
|||||||
bytesio_to_image_tensor,
|
bytesio_to_image_tensor,
|
||||||
tensor_to_bytesio,
|
tensor_to_bytesio,
|
||||||
audio_bytes_to_audio_input,
|
audio_bytes_to_audio_input,
|
||||||
|
sync_op,
|
||||||
|
poll_op,
|
||||||
|
ApiEndpoint,
|
||||||
)
|
)
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
@ -161,19 +157,11 @@ class StabilityStableImageUltraNode(IO.ComfyNode):
|
|||||||
"image": image_binary
|
"image": image_binary
|
||||||
}
|
}
|
||||||
|
|
||||||
auth = {
|
response_api = await sync_op(
|
||||||
"auth_token": cls.hidden.auth_token_comfy_org,
|
cls,
|
||||||
"comfy_api_key": cls.hidden.api_key_comfy_org,
|
ApiEndpoint(path="/proxy/stability/v2beta/stable-image/generate/ultra", method="POST"),
|
||||||
}
|
response_model=StabilityStableUltraResponse,
|
||||||
|
data=StabilityStableUltraRequest(
|
||||||
operation = SynchronousOperation(
|
|
||||||
endpoint=ApiEndpoint(
|
|
||||||
path="/proxy/stability/v2beta/stable-image/generate/ultra",
|
|
||||||
method=HttpMethod.POST,
|
|
||||||
request_model=StabilityStableUltraRequest,
|
|
||||||
response_model=StabilityStableUltraResponse,
|
|
||||||
),
|
|
||||||
request=StabilityStableUltraRequest(
|
|
||||||
prompt=prompt,
|
prompt=prompt,
|
||||||
negative_prompt=negative_prompt,
|
negative_prompt=negative_prompt,
|
||||||
aspect_ratio=aspect_ratio,
|
aspect_ratio=aspect_ratio,
|
||||||
@ -183,9 +171,7 @@ class StabilityStableImageUltraNode(IO.ComfyNode):
|
|||||||
),
|
),
|
||||||
files=files,
|
files=files,
|
||||||
content_type="multipart/form-data",
|
content_type="multipart/form-data",
|
||||||
auth_kwargs=auth,
|
|
||||||
)
|
)
|
||||||
response_api = await operation.execute()
|
|
||||||
|
|
||||||
if response_api.finish_reason != "SUCCESS":
|
if response_api.finish_reason != "SUCCESS":
|
||||||
raise Exception(f"Stable Image Ultra generation failed: {response_api.finish_reason}.")
|
raise Exception(f"Stable Image Ultra generation failed: {response_api.finish_reason}.")
|
||||||
@ -313,19 +299,11 @@ class StabilityStableImageSD_3_5Node(IO.ComfyNode):
|
|||||||
"image": image_binary
|
"image": image_binary
|
||||||
}
|
}
|
||||||
|
|
||||||
auth = {
|
response_api = await sync_op(
|
||||||
"auth_token": cls.hidden.auth_token_comfy_org,
|
cls,
|
||||||
"comfy_api_key": cls.hidden.api_key_comfy_org,
|
ApiEndpoint(path="/proxy/stability/v2beta/stable-image/generate/sd3", method="POST"),
|
||||||
}
|
response_model=StabilityStableUltraResponse,
|
||||||
|
data=StabilityStable3_5Request(
|
||||||
operation = SynchronousOperation(
|
|
||||||
endpoint=ApiEndpoint(
|
|
||||||
path="/proxy/stability/v2beta/stable-image/generate/sd3",
|
|
||||||
method=HttpMethod.POST,
|
|
||||||
request_model=StabilityStable3_5Request,
|
|
||||||
response_model=StabilityStableUltraResponse,
|
|
||||||
),
|
|
||||||
request=StabilityStable3_5Request(
|
|
||||||
prompt=prompt,
|
prompt=prompt,
|
||||||
negative_prompt=negative_prompt,
|
negative_prompt=negative_prompt,
|
||||||
aspect_ratio=aspect_ratio,
|
aspect_ratio=aspect_ratio,
|
||||||
@ -338,9 +316,7 @@ class StabilityStableImageSD_3_5Node(IO.ComfyNode):
|
|||||||
),
|
),
|
||||||
files=files,
|
files=files,
|
||||||
content_type="multipart/form-data",
|
content_type="multipart/form-data",
|
||||||
auth_kwargs=auth,
|
|
||||||
)
|
)
|
||||||
response_api = await operation.execute()
|
|
||||||
|
|
||||||
if response_api.finish_reason != "SUCCESS":
|
if response_api.finish_reason != "SUCCESS":
|
||||||
raise Exception(f"Stable Diffusion 3.5 Image generation failed: {response_api.finish_reason}.")
|
raise Exception(f"Stable Diffusion 3.5 Image generation failed: {response_api.finish_reason}.")
|
||||||
@ -427,19 +403,11 @@ class StabilityUpscaleConservativeNode(IO.ComfyNode):
|
|||||||
"image": image_binary
|
"image": image_binary
|
||||||
}
|
}
|
||||||
|
|
||||||
auth = {
|
response_api = await sync_op(
|
||||||
"auth_token": cls.hidden.auth_token_comfy_org,
|
cls,
|
||||||
"comfy_api_key": cls.hidden.api_key_comfy_org,
|
ApiEndpoint(path="/proxy/stability/v2beta/stable-image/upscale/conservative", method="POST"),
|
||||||
}
|
response_model=StabilityStableUltraResponse,
|
||||||
|
data=StabilityUpscaleConservativeRequest(
|
||||||
operation = SynchronousOperation(
|
|
||||||
endpoint=ApiEndpoint(
|
|
||||||
path="/proxy/stability/v2beta/stable-image/upscale/conservative",
|
|
||||||
method=HttpMethod.POST,
|
|
||||||
request_model=StabilityUpscaleConservativeRequest,
|
|
||||||
response_model=StabilityStableUltraResponse,
|
|
||||||
),
|
|
||||||
request=StabilityUpscaleConservativeRequest(
|
|
||||||
prompt=prompt,
|
prompt=prompt,
|
||||||
negative_prompt=negative_prompt,
|
negative_prompt=negative_prompt,
|
||||||
creativity=round(creativity,2),
|
creativity=round(creativity,2),
|
||||||
@ -447,9 +415,7 @@ class StabilityUpscaleConservativeNode(IO.ComfyNode):
|
|||||||
),
|
),
|
||||||
files=files,
|
files=files,
|
||||||
content_type="multipart/form-data",
|
content_type="multipart/form-data",
|
||||||
auth_kwargs=auth,
|
|
||||||
)
|
)
|
||||||
response_api = await operation.execute()
|
|
||||||
|
|
||||||
if response_api.finish_reason != "SUCCESS":
|
if response_api.finish_reason != "SUCCESS":
|
||||||
raise Exception(f"Stability Upscale Conservative generation failed: {response_api.finish_reason}.")
|
raise Exception(f"Stability Upscale Conservative generation failed: {response_api.finish_reason}.")
|
||||||
@ -544,19 +510,11 @@ class StabilityUpscaleCreativeNode(IO.ComfyNode):
|
|||||||
"image": image_binary
|
"image": image_binary
|
||||||
}
|
}
|
||||||
|
|
||||||
auth = {
|
response_api = await sync_op(
|
||||||
"auth_token": cls.hidden.auth_token_comfy_org,
|
cls,
|
||||||
"comfy_api_key": cls.hidden.api_key_comfy_org,
|
ApiEndpoint(path="/proxy/stability/v2beta/stable-image/upscale/creative", method="POST"),
|
||||||
}
|
response_model=StabilityAsyncResponse,
|
||||||
|
data=StabilityUpscaleCreativeRequest(
|
||||||
operation = SynchronousOperation(
|
|
||||||
endpoint=ApiEndpoint(
|
|
||||||
path="/proxy/stability/v2beta/stable-image/upscale/creative",
|
|
||||||
method=HttpMethod.POST,
|
|
||||||
request_model=StabilityUpscaleCreativeRequest,
|
|
||||||
response_model=StabilityAsyncResponse,
|
|
||||||
),
|
|
||||||
request=StabilityUpscaleCreativeRequest(
|
|
||||||
prompt=prompt,
|
prompt=prompt,
|
||||||
negative_prompt=negative_prompt,
|
negative_prompt=negative_prompt,
|
||||||
creativity=round(creativity,2),
|
creativity=round(creativity,2),
|
||||||
@ -565,25 +523,15 @@ class StabilityUpscaleCreativeNode(IO.ComfyNode):
|
|||||||
),
|
),
|
||||||
files=files,
|
files=files,
|
||||||
content_type="multipart/form-data",
|
content_type="multipart/form-data",
|
||||||
auth_kwargs=auth,
|
|
||||||
)
|
)
|
||||||
response_api = await operation.execute()
|
|
||||||
|
|
||||||
operation = PollingOperation(
|
response_poll = await poll_op(
|
||||||
poll_endpoint=ApiEndpoint(
|
cls,
|
||||||
path=f"/proxy/stability/v2beta/results/{response_api.id}",
|
ApiEndpoint(path=f"/proxy/stability/v2beta/results/{response_api.id}"),
|
||||||
method=HttpMethod.GET,
|
response_model=StabilityResultsGetResponse,
|
||||||
request_model=EmptyRequest,
|
|
||||||
response_model=StabilityResultsGetResponse,
|
|
||||||
),
|
|
||||||
poll_interval=3,
|
poll_interval=3,
|
||||||
completed_statuses=[StabilityPollStatus.finished],
|
|
||||||
failed_statuses=[StabilityPollStatus.failed],
|
|
||||||
status_extractor=lambda x: get_async_dummy_status(x),
|
status_extractor=lambda x: get_async_dummy_status(x),
|
||||||
auth_kwargs=auth,
|
|
||||||
node_id=cls.hidden.unique_id,
|
|
||||||
)
|
)
|
||||||
response_poll: StabilityResultsGetResponse = await operation.execute()
|
|
||||||
|
|
||||||
if response_poll.finish_reason != "SUCCESS":
|
if response_poll.finish_reason != "SUCCESS":
|
||||||
raise Exception(f"Stability Upscale Creative generation failed: {response_poll.finish_reason}.")
|
raise Exception(f"Stability Upscale Creative generation failed: {response_poll.finish_reason}.")
|
||||||
@ -628,24 +576,13 @@ class StabilityUpscaleFastNode(IO.ComfyNode):
|
|||||||
"image": image_binary
|
"image": image_binary
|
||||||
}
|
}
|
||||||
|
|
||||||
auth = {
|
response_api = await sync_op(
|
||||||
"auth_token": cls.hidden.auth_token_comfy_org,
|
cls,
|
||||||
"comfy_api_key": cls.hidden.api_key_comfy_org,
|
ApiEndpoint(path="/proxy/stability/v2beta/stable-image/upscale/fast", method="POST"),
|
||||||
}
|
response_model=StabilityStableUltraResponse,
|
||||||
|
|
||||||
operation = SynchronousOperation(
|
|
||||||
endpoint=ApiEndpoint(
|
|
||||||
path="/proxy/stability/v2beta/stable-image/upscale/fast",
|
|
||||||
method=HttpMethod.POST,
|
|
||||||
request_model=EmptyRequest,
|
|
||||||
response_model=StabilityStableUltraResponse,
|
|
||||||
),
|
|
||||||
request=EmptyRequest(),
|
|
||||||
files=files,
|
files=files,
|
||||||
content_type="multipart/form-data",
|
content_type="multipart/form-data",
|
||||||
auth_kwargs=auth,
|
|
||||||
)
|
)
|
||||||
response_api = await operation.execute()
|
|
||||||
|
|
||||||
if response_api.finish_reason != "SUCCESS":
|
if response_api.finish_reason != "SUCCESS":
|
||||||
raise Exception(f"Stability Upscale Fast failed: {response_api.finish_reason}.")
|
raise Exception(f"Stability Upscale Fast failed: {response_api.finish_reason}.")
|
||||||
@ -717,21 +654,13 @@ class StabilityTextToAudio(IO.ComfyNode):
|
|||||||
async def execute(cls, model: str, prompt: str, duration: int, seed: int, steps: int) -> IO.NodeOutput:
|
async def execute(cls, model: str, prompt: str, duration: int, seed: int, steps: int) -> IO.NodeOutput:
|
||||||
validate_string(prompt, max_length=10000)
|
validate_string(prompt, max_length=10000)
|
||||||
payload = StabilityTextToAudioRequest(prompt=prompt, model=model, duration=duration, seed=seed, steps=steps)
|
payload = StabilityTextToAudioRequest(prompt=prompt, model=model, duration=duration, seed=seed, steps=steps)
|
||||||
operation = SynchronousOperation(
|
response_api = await sync_op(
|
||||||
endpoint=ApiEndpoint(
|
cls,
|
||||||
path="/proxy/stability/v2beta/audio/stable-audio-2/text-to-audio",
|
ApiEndpoint(path="/proxy/stability/v2beta/audio/stable-audio-2/text-to-audio", method="POST"),
|
||||||
method=HttpMethod.POST,
|
response_model=StabilityAudioResponse,
|
||||||
request_model=StabilityTextToAudioRequest,
|
data=payload,
|
||||||
response_model=StabilityAudioResponse,
|
|
||||||
),
|
|
||||||
request=payload,
|
|
||||||
content_type="multipart/form-data",
|
content_type="multipart/form-data",
|
||||||
auth_kwargs= {
|
|
||||||
"auth_token": cls.hidden.auth_token_comfy_org,
|
|
||||||
"comfy_api_key": cls.hidden.api_key_comfy_org,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
response_api = await operation.execute()
|
|
||||||
if not response_api.audio:
|
if not response_api.audio:
|
||||||
raise ValueError("No audio file was received in response.")
|
raise ValueError("No audio file was received in response.")
|
||||||
return IO.NodeOutput(audio_bytes_to_audio_input(base64.b64decode(response_api.audio)))
|
return IO.NodeOutput(audio_bytes_to_audio_input(base64.b64decode(response_api.audio)))
|
||||||
@ -814,22 +743,14 @@ class StabilityAudioToAudio(IO.ComfyNode):
|
|||||||
payload = StabilityAudioToAudioRequest(
|
payload = StabilityAudioToAudioRequest(
|
||||||
prompt=prompt, model=model, duration=duration, seed=seed, steps=steps, strength=strength
|
prompt=prompt, model=model, duration=duration, seed=seed, steps=steps, strength=strength
|
||||||
)
|
)
|
||||||
operation = SynchronousOperation(
|
response_api = await sync_op(
|
||||||
endpoint=ApiEndpoint(
|
cls,
|
||||||
path="/proxy/stability/v2beta/audio/stable-audio-2/audio-to-audio",
|
ApiEndpoint(path="/proxy/stability/v2beta/audio/stable-audio-2/audio-to-audio", method="POST"),
|
||||||
method=HttpMethod.POST,
|
response_model=StabilityAudioResponse,
|
||||||
request_model=StabilityAudioToAudioRequest,
|
data=payload,
|
||||||
response_model=StabilityAudioResponse,
|
|
||||||
),
|
|
||||||
request=payload,
|
|
||||||
content_type="multipart/form-data",
|
content_type="multipart/form-data",
|
||||||
files={"audio": audio_input_to_mp3(audio)},
|
files={"audio": audio_input_to_mp3(audio)},
|
||||||
auth_kwargs= {
|
|
||||||
"auth_token": cls.hidden.auth_token_comfy_org,
|
|
||||||
"comfy_api_key": cls.hidden.api_key_comfy_org,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
response_api = await operation.execute()
|
|
||||||
if not response_api.audio:
|
if not response_api.audio:
|
||||||
raise ValueError("No audio file was received in response.")
|
raise ValueError("No audio file was received in response.")
|
||||||
return IO.NodeOutput(audio_bytes_to_audio_input(base64.b64decode(response_api.audio)))
|
return IO.NodeOutput(audio_bytes_to_audio_input(base64.b64decode(response_api.audio)))
|
||||||
@ -935,22 +856,14 @@ class StabilityAudioInpaint(IO.ComfyNode):
|
|||||||
mask_start=mask_start,
|
mask_start=mask_start,
|
||||||
mask_end=mask_end,
|
mask_end=mask_end,
|
||||||
)
|
)
|
||||||
operation = SynchronousOperation(
|
response_api = await sync_op(
|
||||||
endpoint=ApiEndpoint(
|
cls,
|
||||||
path="/proxy/stability/v2beta/audio/stable-audio-2/inpaint",
|
endpoint=ApiEndpoint(path="/proxy/stability/v2beta/audio/stable-audio-2/inpaint", method="POST"),
|
||||||
method=HttpMethod.POST,
|
response_model=StabilityAudioResponse,
|
||||||
request_model=StabilityAudioInpaintRequest,
|
data=payload,
|
||||||
response_model=StabilityAudioResponse,
|
|
||||||
),
|
|
||||||
request=payload,
|
|
||||||
content_type="multipart/form-data",
|
content_type="multipart/form-data",
|
||||||
files={"audio": audio_input_to_mp3(audio)},
|
files={"audio": audio_input_to_mp3(audio)},
|
||||||
auth_kwargs={
|
|
||||||
"auth_token": cls.hidden.auth_token_comfy_org,
|
|
||||||
"comfy_api_key": cls.hidden.api_key_comfy_org,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
response_api = await operation.execute()
|
|
||||||
if not response_api.audio:
|
if not response_api.audio:
|
||||||
raise ValueError("No audio file was received in response.")
|
raise ValueError("No audio file was received in response.")
|
||||||
return IO.NodeOutput(audio_bytes_to_audio_input(base64.b64decode(response_api.audio)))
|
return IO.NodeOutput(audio_bytes_to_audio_input(base64.b64decode(response_api.audio)))
|
||||||
|
|||||||
@ -77,7 +77,7 @@ class _PollUIState:
|
|||||||
|
|
||||||
|
|
||||||
_RETRY_STATUS = {408, 429, 500, 502, 503, 504}
|
_RETRY_STATUS = {408, 429, 500, 502, 503, 504}
|
||||||
COMPLETED_STATUSES = ["succeeded", "succeed", "success", "completed"]
|
COMPLETED_STATUSES = ["succeeded", "succeed", "success", "completed", "finished"]
|
||||||
FAILED_STATUSES = ["cancelled", "canceled", "fail", "failed", "error"]
|
FAILED_STATUSES = ["cancelled", "canceled", "fail", "failed", "error"]
|
||||||
QUEUED_STATUSES = ["created", "queued", "queueing", "submitted"]
|
QUEUED_STATUSES = ["created", "queued", "queueing", "submitted"]
|
||||||
|
|
||||||
@ -589,7 +589,7 @@ async def _request_base(cfg: _RequestConfig, expect_binary: bool):
|
|||||||
operation_id = _generate_operation_id(method, cfg.endpoint.path, attempt)
|
operation_id = _generate_operation_id(method, cfg.endpoint.path, attempt)
|
||||||
logging.debug("[DEBUG] HTTP %s %s (attempt %d)", method, url, attempt)
|
logging.debug("[DEBUG] HTTP %s %s (attempt %d)", method, url, attempt)
|
||||||
|
|
||||||
payload_headers = {"Accept": "*/*"}
|
payload_headers = {"Accept": "*/*"} if expect_binary else {"Accept": "application/json"}
|
||||||
if not parsed_url.scheme and not parsed_url.netloc: # is URL relative?
|
if not parsed_url.scheme and not parsed_url.netloc: # is URL relative?
|
||||||
payload_headers.update(get_auth_header(cfg.node_cls))
|
payload_headers.update(get_auth_header(cfg.node_cls))
|
||||||
if cfg.endpoint.headers:
|
if cfg.endpoint.headers:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user