Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f5940ac899 | ||
|
|
67703abb8a | ||
|
|
dfee31f0ed |
@@ -12,11 +12,11 @@ class ComfyUIDeployExternalBoolean:
|
||||
"optional": {
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,11 +25,11 @@ class ComfyUIDeployExternalCheckpoint:
|
||||
"default_value": (folder_paths.get_filename_list("checkpoints"), ),
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@ class ComfyUIDeployExternalImage:
|
||||
"default_value": ("IMAGE",),
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@ class ComfyUIDeployExternalImageAlpha:
|
||||
"default_value": ("IMAGE",),
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,11 @@ class ComfyUIDeployExternalImageBatch:
|
||||
"default_value": ("IMAGE",),
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,21 +25,17 @@ class ComfyUIDeployExternalLora:
|
||||
},
|
||||
"optional": {
|
||||
"default_lora_name": (folder_paths.get_filename_list("loras"),),
|
||||
"lora_save_name": ( # if `default_lora_name` is a link to download a file, we will attempt to save it with this name
|
||||
"lora_save_name": ( # if `default_lora_name` is a link to download a file, we will attempt to save it with this name
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
),
|
||||
),
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
),
|
||||
"lora_url": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
},
|
||||
}
|
||||
@@ -51,20 +47,12 @@ class ComfyUIDeployExternalLora:
|
||||
|
||||
CATEGORY = "deploy"
|
||||
|
||||
def run(
|
||||
self,
|
||||
input_id,
|
||||
default_lora_name=None,
|
||||
lora_save_name=None,
|
||||
display_name=None,
|
||||
description=None,
|
||||
lora_url=None,
|
||||
):
|
||||
def run(self, input_id, default_lora_name=None, lora_save_name=None, display_name=None, description=None):
|
||||
import requests
|
||||
import os
|
||||
import uuid
|
||||
|
||||
if lora_url and lora_url.startswith("http"):
|
||||
if default_lora_name.startswith("http"):
|
||||
if lora_save_name:
|
||||
existing_loras = folder_paths.get_filename_list("loras")
|
||||
# Check if lora_save_name exists in the list
|
||||
@@ -79,9 +67,9 @@ class ComfyUIDeployExternalLora:
|
||||
folder_paths.folder_names_and_paths["loras"][0][0], lora_save_name
|
||||
)
|
||||
print(destination_path)
|
||||
print("Downloading external lora - " + lora_url + " to " + destination_path)
|
||||
print("Downloading external lora - " + input_id + " to " + destination_path)
|
||||
response = requests.get(
|
||||
lora_url,
|
||||
input_id,
|
||||
headers={"User-Agent": "Mozilla/5.0"},
|
||||
allow_redirects=True,
|
||||
)
|
||||
|
||||
@@ -20,11 +20,11 @@ class ComfyUIDeployExternalNumber:
|
||||
),
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,11 +20,11 @@ class ComfyUIDeployExternalNumberInt:
|
||||
),
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,11 @@ class ComfyUIDeployExternalNumberSlider:
|
||||
),
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,11 +20,11 @@ class ComfyUIDeployExternalText:
|
||||
),
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,11 +21,11 @@ class ComfyUIDeployExternalTextList:
|
||||
"optional": {
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -764,14 +764,14 @@ class ComfyUIDeployExternalVideo:
|
||||
"optional": {
|
||||
"meta_batch": ("VHS_BatchManager",),
|
||||
"vae": ("VAE",),
|
||||
"default_video": (sorted(files),),
|
||||
"default_value": (sorted(files),),
|
||||
"display_name": (
|
||||
"STRING",
|
||||
{"multiline": False, "default": ""},
|
||||
{"multiline": False, "default": "Name of the node (optional)"},
|
||||
),
|
||||
"description": (
|
||||
"STRING",
|
||||
{"multiline": True, "default": ""},
|
||||
{"multiline": True, "default": "Description of the node (optional)"},
|
||||
),
|
||||
},
|
||||
"hidden": {
|
||||
@@ -834,7 +834,7 @@ class ComfyUIDeployExternalVideo:
|
||||
):
|
||||
out_file.write(chunk)
|
||||
else:
|
||||
video = kwargs.get("default_video", None)
|
||||
video = kwargs.get("default_value", "")
|
||||
if video is None:
|
||||
raise "No default video given and no external video provided"
|
||||
video_path = folder_paths.get_annotated_filepath(video.strip('"'))
|
||||
|
||||
+55
-134
@@ -1,5 +1,4 @@
|
||||
from io import BytesIO
|
||||
from pprint import pprint
|
||||
from aiohttp import web
|
||||
import os
|
||||
import requests
|
||||
@@ -34,7 +33,7 @@ client_session = None
|
||||
# global client_session
|
||||
# if client_session is None:
|
||||
# client_session = aiohttp.ClientSession()
|
||||
|
||||
|
||||
async def ensure_client_session():
|
||||
global client_session
|
||||
if client_session is None:
|
||||
@@ -44,7 +43,7 @@ async def cleanup():
|
||||
global client_session
|
||||
if client_session:
|
||||
await client_session.close()
|
||||
|
||||
|
||||
def exit_handler():
|
||||
print("Exiting the application. Initiating cleanup...")
|
||||
loop = asyncio.get_event_loop()
|
||||
@@ -57,65 +56,39 @@ retry_delay_multiplier = float(os.environ.get('RETRY_DELAY_MULTIPLIER', '2'))
|
||||
|
||||
print(f"max_retries: {max_retries}, retry_delay_multiplier: {retry_delay_multiplier}")
|
||||
|
||||
import time
|
||||
|
||||
async def async_request_with_retry(method, url, disable_timeout=False, token=None, **kwargs):
|
||||
async def async_request_with_retry(method, url, disable_timeout=False, **kwargs):
|
||||
global client_session
|
||||
await ensure_client_session()
|
||||
# async with aiohttp.ClientSession() as client_session:
|
||||
retry_delay = 1 # Start with 1 second delay
|
||||
initial_timeout = 5 # 5 seconds timeout for the initial connection
|
||||
|
||||
start_time = time.time()
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
# Set a timeout for the initial connection
|
||||
if not disable_timeout:
|
||||
timeout = ClientTimeout(total=None, connect=initial_timeout)
|
||||
kwargs['timeout'] = timeout
|
||||
|
||||
if token is not None:
|
||||
if 'headers' not in kwargs:
|
||||
kwargs['headers'] = {}
|
||||
kwargs['headers']['Authorization'] = f"Bearer {token}"
|
||||
|
||||
request_start = time.time()
|
||||
async with client_session.request(method, url, **kwargs) as response:
|
||||
request_end = time.time()
|
||||
logger.info(f"Request attempt {attempt + 1} took {request_end - request_start:.2f} seconds")
|
||||
|
||||
if response.status != 200:
|
||||
error_body = await response.text()
|
||||
logger.error(f"Request failed with status {response.status} and body {error_body}")
|
||||
# raise Exception(f"Request failed with status {response.status}")
|
||||
|
||||
response.raise_for_status()
|
||||
if method.upper() == 'GET':
|
||||
await response.read()
|
||||
|
||||
total_time = time.time() - start_time
|
||||
logger.info(f"Request succeeded after {total_time:.2f} seconds (attempt {attempt + 1}/{max_retries})")
|
||||
return response
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(f"Request timed out after {initial_timeout} seconds (attempt {attempt + 1}/{max_retries})")
|
||||
except ClientError as e:
|
||||
end_time = time.time()
|
||||
logger.error(f"Request failed (attempt {attempt + 1}/{max_retries}): {e}")
|
||||
logger.error(f"Time taken for failed attempt: {end_time - request_start:.2f} seconds")
|
||||
logger.error(f"Total time elapsed: {end_time - start_time:.2f} seconds")
|
||||
|
||||
# Log the response body for ClientError as well
|
||||
if hasattr(e, 'response') and e.response is not None:
|
||||
error_body = await e.response.text()
|
||||
logger.error(f"Error response body: {error_body}")
|
||||
|
||||
if attempt == max_retries - 1:
|
||||
logger.error(f"Request failed after {max_retries} attempts: {e}")
|
||||
raise
|
||||
|
||||
await asyncio.sleep(retry_delay)
|
||||
retry_delay *= retry_delay_multiplier
|
||||
# raise
|
||||
logger.warning(f"Request failed (attempt {attempt + 1}/{max_retries}): {e}")
|
||||
|
||||
total_time = time.time() - start_time
|
||||
raise Exception(f"Request failed after {max_retries} attempts and {total_time:.2f} seconds")
|
||||
# Wait before retrying
|
||||
await asyncio.sleep(retry_delay)
|
||||
retry_delay *= retry_delay_multiplier # Exponential backoff
|
||||
|
||||
# If all retries fail, raise an exception
|
||||
raise Exception(f"Request failed after {max_retries} attempts")
|
||||
|
||||
from logging import basicConfig, getLogger
|
||||
|
||||
@@ -143,7 +116,7 @@ def log(level, message, **kwargs):
|
||||
getattr(logger, level)(message, **kwargs)
|
||||
else:
|
||||
getattr(logger, level)(f"{message} {kwargs}")
|
||||
|
||||
|
||||
# For a span, you might need to create a context manager
|
||||
from contextlib import contextmanager
|
||||
|
||||
@@ -286,7 +259,7 @@ def apply_random_seed_to_workflow(workflow_api):
|
||||
logger.info(f"Applied random noise_seed {workflow_api[key]['inputs']['noise_seed']} to SamplerCustom")
|
||||
continue
|
||||
|
||||
def apply_inputs_to_workflow(workflow_api: Any, inputs: Any, sid: str = None):
|
||||
def apply_inputs_to_workflow(workflow_api: Any, inputs: Any, sid: str | None = None):
|
||||
# Loop through each of the inputs and replace them
|
||||
for key, value in workflow_api.items():
|
||||
if 'inputs' in value:
|
||||
@@ -319,7 +292,7 @@ def apply_inputs_to_workflow(workflow_api: Any, inputs: Any, sid: str = None):
|
||||
value['inputs']["images"] = new_value
|
||||
|
||||
if value["class_type"] == "ComfyUIDeployExternalLora":
|
||||
value["inputs"]["lora_url"] = new_value
|
||||
value["inputs"]["default_lora_name"] = new_value
|
||||
|
||||
if value["class_type"] == "ComfyUIDeployExternalSlider":
|
||||
value["inputs"]["default_value"] = new_value
|
||||
@@ -366,14 +339,6 @@ def send_prompt(sid: str, inputs: StreamingPrompt):
|
||||
|
||||
@server.PromptServer.instance.routes.post("/comfyui-deploy/run")
|
||||
async def comfy_deploy_run(request):
|
||||
# Extract the bearer token from the Authorization header
|
||||
auth_header = request.headers.get('Authorization')
|
||||
token = None
|
||||
if auth_header:
|
||||
parts = auth_header.split()
|
||||
if len(parts) == 2 and parts[0].lower() == 'bearer':
|
||||
token = parts[1]
|
||||
|
||||
data = await request.json()
|
||||
|
||||
# In older version, we use workflow_api, but this has inputs already swapped in nextjs frontend, which is tricky
|
||||
@@ -389,14 +354,13 @@ async def comfy_deploy_run(request):
|
||||
prompt = {
|
||||
"prompt": workflow_api,
|
||||
"client_id": "comfy_deploy_instance", #api.client_id
|
||||
"prompt_id": prompt_id,
|
||||
"prompt_id": prompt_id
|
||||
}
|
||||
|
||||
prompt_metadata[prompt_id] = SimplePrompt(
|
||||
status_endpoint=data.get('status_endpoint'),
|
||||
file_upload_endpoint=data.get('file_upload_endpoint'),
|
||||
workflow_api=workflow_api,
|
||||
token=token
|
||||
workflow_api=workflow_api
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -434,7 +398,7 @@ async def comfy_deploy_run(request):
|
||||
|
||||
return web.json_response(res, status=status)
|
||||
|
||||
async def stream_prompt(data, token):
|
||||
async def stream_prompt(data):
|
||||
# In older version, we use workflow_api, but this has inputs already swapped in nextjs frontend, which is tricky
|
||||
workflow_api = data.get("workflow_api_raw")
|
||||
# The prompt id generated from comfy deploy, can be None
|
||||
@@ -454,8 +418,7 @@ async def stream_prompt(data, token):
|
||||
prompt_metadata[prompt_id] = SimplePrompt(
|
||||
status_endpoint=data.get('status_endpoint'),
|
||||
file_upload_endpoint=data.get('file_upload_endpoint'),
|
||||
workflow_api=workflow_api,
|
||||
token=token
|
||||
workflow_api=workflow_api
|
||||
)
|
||||
|
||||
# log('info', "Begin prompt", prompt=prompt)
|
||||
@@ -504,14 +467,6 @@ comfy_message_queues: Dict[str, asyncio.Queue] = {}
|
||||
async def stream_response(request):
|
||||
response = web.StreamResponse(status=200, reason='OK', headers={'Content-Type': 'text/event-stream'})
|
||||
await response.prepare(request)
|
||||
|
||||
# Extract the bearer token from the Authorization header
|
||||
auth_header = request.headers.get('Authorization')
|
||||
token = None
|
||||
if auth_header:
|
||||
parts = auth_header.split()
|
||||
if len(parts) == 2 and parts[0].lower() == 'bearer':
|
||||
token = parts[1]
|
||||
|
||||
pending = True
|
||||
data = await request.json()
|
||||
@@ -523,7 +478,7 @@ async def stream_response(request):
|
||||
log('info', 'Streaming prompt')
|
||||
|
||||
try:
|
||||
result = await stream_prompt(data=data, token=token)
|
||||
result = await stream_prompt(data=data)
|
||||
await response.write(f"event: event_update\ndata: {json.dumps(result)}\n\n".encode('utf-8'))
|
||||
# await response.write(.encode('utf-8'))
|
||||
await response.drain() # Ensure the buffer is flushed
|
||||
@@ -652,10 +607,9 @@ async def upload_file_endpoint(request):
|
||||
with open(file_path, 'rb') as f:
|
||||
headers = {
|
||||
"Content-Type": file_type,
|
||||
# "Content-Length": str(file_size)
|
||||
# "x-amz-acl": "public-read",
|
||||
"Content-Length": str(file_size)
|
||||
}
|
||||
if content.get('include_acl') is True:
|
||||
headers["x-amz-acl"] = "public-read"
|
||||
upload_response = await async_request_with_retry('PUT', upload_url, data=f, headers=headers)
|
||||
if upload_response.status == 200:
|
||||
return web.json_response({
|
||||
@@ -901,19 +855,19 @@ async def send(event, data, sid=None):
|
||||
except Exception as e:
|
||||
logger.info(f"Exception: {e}")
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
@server.PromptServer.instance.routes.get('/comfydeploy/{tail:.*}')
|
||||
@server.PromptServer.instance.routes.post('/comfydeploy/{tail:.*}')
|
||||
async def proxy_to_comfydeploy(request):
|
||||
# Get the base URL
|
||||
base_url = f'https://www.comfydeploy.com/{request.match_info["tail"]}'
|
||||
|
||||
|
||||
# Get all query parameters
|
||||
query_params = request.query_string
|
||||
|
||||
|
||||
# Construct the full target URL with query parameters
|
||||
target_url = f"{base_url}?{query_params}" if query_params else base_url
|
||||
|
||||
|
||||
# print(f"Proxying request to: {target_url}")
|
||||
|
||||
try:
|
||||
@@ -1038,22 +992,17 @@ async def send_json_override(self, event, data, sid=None):
|
||||
# await update_run_with_output(prompt_id, data)
|
||||
|
||||
if event == 'executed' and 'node' in data and 'output' in data:
|
||||
node_meta = None
|
||||
if prompt_id in prompt_metadata:
|
||||
node = data.get('node')
|
||||
class_type = prompt_metadata[prompt_id].workflow_api[node]['class_type']
|
||||
logger.info(f"Executed {class_type} {data}")
|
||||
node_meta = {
|
||||
"node_id": node,
|
||||
"node_class": class_type,
|
||||
}
|
||||
if class_type == "PreviewImage":
|
||||
logger.info("Skipping preview image")
|
||||
return
|
||||
else:
|
||||
logger.info(f"Executed {data}")
|
||||
|
||||
await update_run_with_output(prompt_id, data.get('output'), node_id=data.get('node'), node_meta=node_meta)
|
||||
|
||||
await update_run_with_output(prompt_id, data.get('output'), node_id=data.get('node'))
|
||||
# await update_run_with_output(prompt_id, data.get('output'), node_id=data.get('node'))
|
||||
# update_run_with_output(prompt_id, data.get('output'))
|
||||
|
||||
@@ -1068,7 +1017,6 @@ async def update_run_live_status(prompt_id, live_status, calculated_progress: fl
|
||||
return
|
||||
|
||||
status_endpoint = prompt_metadata[prompt_id].status_endpoint
|
||||
token = prompt_metadata[prompt_id].token
|
||||
|
||||
if (status_endpoint is None):
|
||||
return
|
||||
@@ -1092,7 +1040,7 @@ async def update_run_live_status(prompt_id, live_status, calculated_progress: fl
|
||||
})
|
||||
|
||||
# requests.post(status_endpoint, json=body)
|
||||
await async_request_with_retry('POST', status_endpoint, token=token, json=body)
|
||||
await async_request_with_retry('POST', status_endpoint, json=body)
|
||||
|
||||
|
||||
async def update_run(prompt_id: str, status: Status):
|
||||
@@ -1123,8 +1071,7 @@ async def update_run(prompt_id: str, status: Status):
|
||||
try:
|
||||
# requests.post(status_endpoint, json=body)
|
||||
if (status_endpoint is not None):
|
||||
token = prompt_metadata[prompt_id].token
|
||||
await async_request_with_retry('POST', status_endpoint, token=token, json=body)
|
||||
await async_request_with_retry('POST', status_endpoint, json=body)
|
||||
|
||||
if (status_endpoint is not None) and cd_enable_run_log and (status == Status.SUCCESS or status == Status.FAILED):
|
||||
try:
|
||||
@@ -1154,7 +1101,7 @@ async def update_run(prompt_id: str, status: Status):
|
||||
]
|
||||
}
|
||||
|
||||
await async_request_with_retry('POST', status_endpoint, token=token, json=body)
|
||||
await async_request_with_retry('POST', status_endpoint, json=body)
|
||||
# requests.post(status_endpoint, json=body)
|
||||
except Exception as log_error:
|
||||
logger.info(f"Error reading log file: {log_error}")
|
||||
@@ -1175,7 +1122,7 @@ async def update_run(prompt_id: str, status: Status):
|
||||
})
|
||||
|
||||
|
||||
async def upload_file(prompt_id, filename, subfolder=None, content_type="image/png", type="output", item=None):
|
||||
async def upload_file(prompt_id, filename, subfolder=None, content_type="image/png", type="output"):
|
||||
"""
|
||||
Uploads file to S3 bucket using S3 client object
|
||||
:return: None
|
||||
@@ -1205,47 +1152,33 @@ async def upload_file(prompt_id, filename, subfolder=None, content_type="image/p
|
||||
logger.info(f"Uploading file {file}")
|
||||
|
||||
file_upload_endpoint = prompt_metadata[prompt_id].file_upload_endpoint
|
||||
token = prompt_metadata[prompt_id].token
|
||||
|
||||
filename = quote(filename)
|
||||
prompt_id = quote(prompt_id)
|
||||
content_type = quote(content_type)
|
||||
|
||||
target_url = f"{file_upload_endpoint}?file_name={filename}&run_id={prompt_id}&type={content_type}&version=v2"
|
||||
|
||||
start_time = time.time() # Start timing here
|
||||
result = await async_request_with_retry("GET", target_url, disable_timeout=True)
|
||||
end_time = time.time() # End timing after the request is complete
|
||||
logger.info("Time taken for getting file upload endpoint: {:.2f} seconds".format(end_time - start_time))
|
||||
ok = await result.json()
|
||||
|
||||
start_time = time.time() # Start timing here
|
||||
|
||||
async with aiofiles.open(file, 'rb') as f:
|
||||
data = await f.read()
|
||||
size = str(len(data))
|
||||
target_url = f"{file_upload_endpoint}?file_name={filename}&run_id={prompt_id}&type={content_type}&version=v2"
|
||||
|
||||
start_time = time.time() # Start timing here
|
||||
logger.info(f"Target URL: {target_url}")
|
||||
result = await async_request_with_retry("GET", target_url, disable_timeout=True, token=token)
|
||||
end_time = time.time() # End timing after the request is complete
|
||||
logger.info("Time taken for getting file upload endpoint: {:.2f} seconds".format(end_time - start_time))
|
||||
ok = await result.json()
|
||||
|
||||
logger.info(f"Result: {ok}")
|
||||
|
||||
start_time = time.time() # Start timing here
|
||||
headers = {
|
||||
# "x-amz-acl": "public-read",
|
||||
"Content-Type": content_type,
|
||||
# "Content-Length": size,
|
||||
"Content-Length": str(len(data)),
|
||||
}
|
||||
|
||||
if ok.get('include_acl') is True:
|
||||
headers["x-amz-acl"] = "public-read"
|
||||
|
||||
# response = requests.put(ok.get("url"), headers=headers, data=data)
|
||||
response = await async_request_with_retry('PUT', ok.get("url"), headers=headers, data=data)
|
||||
logger.info(f"Upload file response status: {response.status}, status text: {response.reason}")
|
||||
end_time = time.time() # End timing after the request is complete
|
||||
logger.info("Upload time: {:.2f} seconds".format(end_time - start_time))
|
||||
|
||||
if item is not None:
|
||||
file_download_url = ok.get("download_url")
|
||||
if file_download_url is not None:
|
||||
item["url"] = file_download_url
|
||||
item["upload_duration"] = end_time - start_time
|
||||
if ok.get("is_public") is not None:
|
||||
item["is_public"] = ok.get("is_public")
|
||||
|
||||
def have_pending_upload(prompt_id):
|
||||
if prompt_id in prompt_metadata and len(prompt_metadata[prompt_id].uploading_nodes) > 0:
|
||||
@@ -1359,15 +1292,14 @@ async def handle_upload(prompt_id: str, data, key: str, content_type_key: str, d
|
||||
item.get("filename"),
|
||||
subfolder=item.get("subfolder"),
|
||||
type=item.get("type"),
|
||||
content_type=file_type,
|
||||
item=item
|
||||
content_type=file_type
|
||||
))
|
||||
|
||||
# Execute all upload tasks concurrently
|
||||
await asyncio.gather(*upload_tasks)
|
||||
|
||||
# Upload files in the background
|
||||
async def upload_in_background(prompt_id: str, data, node_id=None, have_upload=True, node_meta=None):
|
||||
async def upload_in_background(prompt_id: str, data, node_id=None, have_upload=True):
|
||||
try:
|
||||
upload_tasks = [
|
||||
handle_upload(prompt_id, data, 'images', "content_type", "image/png"),
|
||||
@@ -1378,22 +1310,12 @@ async def upload_in_background(prompt_id: str, data, node_id=None, have_upload=T
|
||||
|
||||
await asyncio.gather(*upload_tasks)
|
||||
|
||||
status_endpoint = prompt_metadata[prompt_id].status_endpoint
|
||||
token = prompt_metadata[prompt_id].token
|
||||
if have_upload:
|
||||
if status_endpoint is not None:
|
||||
body = {
|
||||
"run_id": prompt_id,
|
||||
"output_data": data,
|
||||
"node_meta": node_meta,
|
||||
}
|
||||
# pprint(body)
|
||||
await async_request_with_retry('POST', status_endpoint, token=token, json=body)
|
||||
await update_file_status(prompt_id, data, False, node_id=node_id)
|
||||
except Exception as e:
|
||||
await handle_error(prompt_id, data, e)
|
||||
|
||||
async def update_run_with_output(prompt_id, data, node_id=None, node_meta=None):
|
||||
async def update_run_with_output(prompt_id, data, node_id=None):
|
||||
if prompt_id not in prompt_metadata:
|
||||
return
|
||||
|
||||
@@ -1404,8 +1326,7 @@ async def update_run_with_output(prompt_id, data, node_id=None, node_meta=None):
|
||||
|
||||
body = {
|
||||
"run_id": prompt_id,
|
||||
"output_data": data,
|
||||
"node_meta": node_meta,
|
||||
"output_data": data
|
||||
}
|
||||
have_upload_media = 'images' in data or 'files' in data or 'gifs' in data or 'mesh' in data
|
||||
if bypass_upload and have_upload_media:
|
||||
@@ -1419,15 +1340,15 @@ async def update_run_with_output(prompt_id, data, node_id=None, node_meta=None):
|
||||
if have_upload_media:
|
||||
await update_file_status(prompt_id, data, True, node_id=node_id)
|
||||
|
||||
asyncio.create_task(upload_in_background(prompt_id, data, node_id=node_id, have_upload=have_upload_media, node_meta=node_meta))
|
||||
asyncio.create_task(upload_in_background(prompt_id, data, node_id=node_id, have_upload=have_upload_media))
|
||||
# await upload_in_background(prompt_id, data, node_id=node_id, have_upload=have_upload)
|
||||
|
||||
except Exception as e:
|
||||
await handle_error(prompt_id, data, e)
|
||||
|
||||
# requests.post(status_endpoint, json=body)
|
||||
elif status_endpoint is not None:
|
||||
token = prompt_metadata[prompt_id].token
|
||||
await async_request_with_retry('POST', status_endpoint, token=token, json=body)
|
||||
if status_endpoint is not None:
|
||||
await async_request_with_retry('POST', status_endpoint, json=body)
|
||||
|
||||
await send('outputs_uploaded', {
|
||||
"prompt_id": prompt_id
|
||||
@@ -1486,4 +1407,4 @@ if cd_enable_log:
|
||||
@server.PromptServer.instance.routes.get("/comfyui-deploy/filename_list_cache")
|
||||
async def get_filename_list_cache(_):
|
||||
from folder_paths import filename_list_cache
|
||||
return web.json_response({'filename_list': filename_list_cache})
|
||||
return web.json_response({'filename_list': filename_list_cache})
|
||||
|
||||
@@ -29,8 +29,6 @@ class SimplePrompt(BaseModel):
|
||||
status_endpoint: Optional[str]
|
||||
file_upload_endpoint: Optional[str]
|
||||
|
||||
token: Optional[str]
|
||||
|
||||
workflow_api: dict
|
||||
status: Status = Status.NOT_STARTED
|
||||
progress: set = set()
|
||||
|
||||
Reference in New Issue
Block a user