This commit is contained in:
nick 2024-09-17 14:32:52 -07:00
commit 06805e310d
3 changed files with 187 additions and 163 deletions

View File

@ -39,14 +39,34 @@ class ComfyUIDeployExternalImageBatch:
CATEGORY = "image" CATEGORY = "image"
def process_image(self, image):
image = ImageOps.exif_transpose(image)
image = image.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image_tensor = torch.from_numpy(image)[None,]
return image_tensor
def run(self, input_id, images=None, default_value=None, display_name=None, description=None): def run(self, input_id, images=None, default_value=None, display_name=None, description=None):
import requests
import zipfile
import io
processed_images = [] processed_images = []
try: try:
images_list = json.loads(images) # Assuming images is a JSON array string images_list = json.loads(images) # Assuming images is a JSON array string
print(images_list) print(images_list)
for img_input in images_list: for img_input in images_list:
if img_input.startswith('http'): if img_input.startswith('http') and img_input.endswith('.zip'):
import requests print("Fetching zip file from url: ", img_input)
response = requests.get(img_input)
zip_file = zipfile.ZipFile(io.BytesIO(response.content))
for file_name in zip_file.namelist():
if file_name.lower().endswith(('.png', '.jpg', '.jpeg')):
with zip_file.open(file_name) as file:
image = Image.open(file)
image = self.process_image(image)
processed_images.append(image)
elif img_input.startswith('http'):
from io import BytesIO from io import BytesIO
print("Fetching image from url: ", img_input) print("Fetching image from url: ", img_input)
response = requests.get(img_input) response = requests.get(img_input)

View File

@ -28,30 +28,28 @@ from aiohttp import web, ClientSession, ClientError, ClientTimeout
import atexit import atexit
# Global session # Global session
client_session = None # client_session = None
# def create_client_session(): # def create_client_session():
# global client_session # global client_session
# if client_session is None: # if client_session is None:
# client_session = aiohttp.ClientSession() # client_session = aiohttp.ClientSession()
# async def ensure_client_session():
# global client_session
# if client_session is None:
# client_session = aiohttp.ClientSession()
async def ensure_client_session(): # async def cleanup():
global client_session # global client_session
if client_session is None: # if client_session:
client_session = aiohttp.ClientSession() # await client_session.close()
async def cleanup():
global client_session
if client_session:
await client_session.close()
def exit_handler(): def exit_handler():
print("Exiting the application. Initiating cleanup...") print("Exiting the application. Initiating cleanup...")
loop = asyncio.get_event_loop() # loop = asyncio.get_event_loop()
loop.run_until_complete(cleanup()) # loop.run_until_complete(cleanup())
atexit.register(exit_handler) atexit.register(exit_handler)
@ -67,74 +65,77 @@ import time
async def async_request_with_retry( async def async_request_with_retry(
method, url, disable_timeout=False, token=None, **kwargs method, url, disable_timeout=False, token=None, **kwargs
): ):
global client_session # global client_session
await ensure_client_session() # await ensure_client_session()
retry_delay = 1 # Start with 1 second delay async with aiohttp.ClientSession() as client_session:
initial_timeout = 5 # 5 seconds timeout for the initial connection retry_delay = 1 # Start with 1 second delay
initial_timeout = 5 # 5 seconds timeout for the initial connection
start_time = time.time() start_time = time.time()
for attempt in range(max_retries): for attempt in range(max_retries):
try: try:
if not disable_timeout: if not disable_timeout:
timeout = ClientTimeout(total=None, connect=initial_timeout) timeout = ClientTimeout(total=None, connect=initial_timeout)
kwargs["timeout"] = timeout kwargs["timeout"] = timeout
if token is not None: if token is not None:
if "headers" not in kwargs: if "headers" not in kwargs:
kwargs["headers"] = {} kwargs["headers"] = {}
kwargs["headers"]["Authorization"] = f"Bearer {token}" kwargs["headers"]["Authorization"] = f"Bearer {token}"
request_start = time.time() request_start = time.time()
async with client_session.request(method, url, **kwargs) as response: async with client_session.request(method, url, **kwargs) as response:
request_end = time.time() request_end = time.time()
logger.info( logger.info(
f"Request attempt {attempt + 1} took {request_end - request_start:.2f} seconds" 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 response.status != 200:
if method.upper() == "GET": error_body = await response.text()
await response.read() logger.error(
f"Request failed with status {response.status} and body {error_body}"
)
# raise Exception(f"Request failed with status {response.status}")
total_time = time.time() - start_time response.raise_for_status()
logger.info( if method.upper() == "GET":
f"Request succeeded after {total_time:.2f} seconds (attempt {attempt + 1}/{max_retries})" 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})"
) )
return response except ClientError as e:
except asyncio.TimeoutError: end_time = time.time()
logger.warning( logger.error(
f"Request timed out after {initial_timeout} seconds (attempt {attempt + 1}/{max_retries})" f"Request failed (attempt {attempt + 1}/{max_retries}): {e}"
) )
except ClientError as e: logger.error(
end_time = time.time() f"Time taken for failed attempt: {end_time - request_start:.2f} seconds"
logger.error(f"Request failed (attempt {attempt + 1}/{max_retries}): {e}") )
logger.error( logger.error(f"Total time elapsed: {end_time - start_time:.2f} seconds")
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 # Log the response body for ClientError as well
if hasattr(e, "response") and e.response is not None: if hasattr(e, "response") and e.response is not None:
error_body = await e.response.text() error_body = await e.response.text()
logger.error(f"Error response body: {error_body}") logger.error(f"Error response body: {error_body}")
if attempt == max_retries - 1: if attempt == max_retries - 1:
logger.error(f"Request failed after {max_retries} attempts: {e}") logger.error(f"Request failed after {max_retries} attempts: {e}")
raise raise
await asyncio.sleep(retry_delay) await asyncio.sleep(retry_delay)
retry_delay *= retry_delay_multiplier retry_delay *= retry_delay_multiplier
total_time = time.time() - start_time total_time = time.time() - start_time
raise Exception( raise Exception(
f"Request failed after {max_retries} attempts and {total_time:.2f} seconds" f"Request failed after {max_retries} attempts and {total_time:.2f} seconds"
) )
from logging import basicConfig, getLogger from logging import basicConfig, getLogger
@ -1621,9 +1622,11 @@ async def update_run_with_output(
"node_meta": node_meta, "node_meta": node_meta,
"gpu_event_id": gpu_event_id, "gpu_event_id": gpu_event_id,
} }
have_upload_media = ( have_upload_media = False
"images" in data or "files" in data or "gifs" in data or "mesh" in data if data is not None:
) 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: if bypass_upload and have_upload_media:
print( print(
"CD_BYPASS_UPLOAD is enabled, skipping the upload of the output:", node_id "CD_BYPASS_UPLOAD is enabled, skipping the upload of the output:", node_id

View File

@ -192,11 +192,13 @@ const ext = {
registerCustomNodes() { registerCustomNodes() {
/** @type {LGraphNode}*/ /** @type {LGraphNode}*/
class ComfyDeploy { class ComfyDeploy extends LGraphNode {
color = LGraphCanvas.node_colors.yellow.color;
bgcolor = LGraphCanvas.node_colors.yellow.bgcolor;
groupcolor = LGraphCanvas.node_colors.yellow.groupcolor;
constructor() { constructor() {
super();
this.color = LGraphCanvas.node_colors.yellow.color;
this.bgcolor = LGraphCanvas.node_colors.yellow.bgcolor;
this.groupcolor = LGraphCanvas.node_colors.yellow.groupcolor;
if (!this.properties) { if (!this.properties) {
this.properties = {}; this.properties = {};
this.properties.workflow_name = ""; this.properties.workflow_name = "";
@ -204,65 +206,75 @@ const ext = {
this.properties.version = ""; this.properties.version = "";
} }
ComfyWidgets.STRING( this.addWidget(
this, "text",
"workflow_name", "workflow_name",
[ this.properties.workflow_name,
"", (v) => {
{ this.properties.workflow_name = v;
default: this.properties.workflow_name, },
multiline: false, { multiline: false }
},
],
app,
); );
ComfyWidgets.STRING( this.addWidget(
this, "text",
"workflow_id", "workflow_id",
[ this.properties.workflow_id,
"", (v) => {
{ this.properties.workflow_id = v;
default: this.properties.workflow_id, },
multiline: false, { multiline: false }
},
],
app,
); );
ComfyWidgets.STRING( this.addWidget(
this, "text",
"version", "version",
["", { default: this.properties.version, multiline: false }], this.properties.version,
app, (v) => {
this.properties.version = v;
},
{ multiline: false }
); );
// this.widgets.forEach((w) => {
// // w.computeSize = () => [200,10]
// w.computedHeight = 2;
// })
this.widgets_start_y = 10; this.widgets_start_y = 10;
this.setSize(this.computeSize());
// const config = { };
// console.log(this);
this.serialize_widgets = true; this.serialize_widgets = true;
this.isVirtualNode = true; this.isVirtualNode = true;
} }
onExecute() {
// This method is called when the node is executed
// You can add any necessary logic here
}
onSerialize(o) {
// This method is called when the node is being serialized
// Ensure all necessary data is saved
if (!o.properties) {
o.properties = {};
}
o.properties.workflow_name = this.properties.workflow_name;
o.properties.workflow_id = this.properties.workflow_id;
o.properties.version = this.properties.version;
}
onConfigure(o) {
// This method is called when the node is being configured (e.g., when loading a saved graph)
// Ensure all necessary data is restored
if (o.properties) {
this.properties = { ...this.properties, ...o.properties };
this.widgets[0].value = this.properties.workflow_name || "";
this.widgets[1].value = this.properties.workflow_id || "";
this.widgets[2].value = this.properties.version || "1";
}
}
} }
// Load default visibility // Register the node type
LiteGraph.registerNodeType("ComfyDeploy", Object.assign(ComfyDeploy, {
LiteGraph.registerNodeType( title: "Comfy Deploy",
"ComfyDeploy", title_mode: LiteGraph.NORMAL_TITLE,
Object.assign(ComfyDeploy, { collapsable: true,
title_mode: LiteGraph.NORMAL_TITLE, }));
title: "Comfy Deploy",
collapsable: true,
}),
);
ComfyDeploy.category = "deploy"; ComfyDeploy.category = "deploy";
}, },
@ -431,10 +443,10 @@ function createDynamicUIHtml(data) {
<h3 style="font-size: 14px; font-weight: semibold; margin-bottom: 8px;">Missing Nodes</h3> <h3 style="font-size: 14px; font-weight: semibold; margin-bottom: 8px;">Missing Nodes</h3>
<p style="font-size: 12px;">These nodes are not found with any matching custom_nodes in the ComfyUI Manager Database</p> <p style="font-size: 12px;">These nodes are not found with any matching custom_nodes in the ComfyUI Manager Database</p>
${data.missing_nodes ${data.missing_nodes
.map((node) => { .map((node) => {
return `<p style="font-size: 14px; color: #d69e2e;">${node}</p>`; return `<p style="font-size: 14px; color: #d69e2e;">${node}</p>`;
}) })
.join("")} .join("")}
</div> </div>
`; `;
} }
@ -442,17 +454,14 @@ function createDynamicUIHtml(data) {
Object.values(data.custom_nodes).forEach((node) => { Object.values(data.custom_nodes).forEach((node) => {
html += ` html += `
<div style="border-bottom: 1px solid #e2e8f0; padding-top: 16px;"> <div style="border-bottom: 1px solid #e2e8f0; padding-top: 16px;">
<a href="${ <a href="${node.url
node.url }" target="_blank" style="font-size: 18px; font-weight: semibold; color: white; text-decoration: none;">${node.name
}" target="_blank" style="font-size: 18px; font-weight: semibold; color: white; text-decoration: none;">${ }</a>
node.name
}</a>
<p style="font-size: 14px; color: #4b5563;">${node.hash}</p> <p style="font-size: 14px; color: #4b5563;">${node.hash}</p>
${ ${node.warning
node.warning ? `<p style="font-size: 14px; color: #d69e2e;">${node.warning}</p>`
? `<p style="font-size: 14px; color: #d69e2e;">${node.warning}</p>` : ""
: "" }
}
</div> </div>
`; `;
}); });
@ -466,9 +475,8 @@ function createDynamicUIHtml(data) {
Object.entries(data.models).forEach(([section, items]) => { Object.entries(data.models).forEach(([section, items]) => {
html += ` html += `
<div style="border-bottom: 1px solid #e2e8f0; padding-top: 8px; padding-bottom: 8px;"> <div style="border-bottom: 1px solid #e2e8f0; padding-top: 8px; padding-bottom: 8px;">
<h3 style="font-size: 18px; font-weight: semibold; margin-bottom: 8px;">${ <h3 style="font-size: 18px; font-weight: semibold; margin-bottom: 8px;">${section.charAt(0).toUpperCase() + section.slice(1)
section.charAt(0).toUpperCase() + section.slice(1) }</h3>`;
}</h3>`;
items.forEach((item) => { items.forEach((item) => {
html += `<p style="font-size: 14px; color: ${textColor};">${item.name}</p>`; html += `<p style="font-size: 14px; color: ${textColor};">${item.name}</p>`;
}); });
@ -484,9 +492,8 @@ function createDynamicUIHtml(data) {
Object.entries(data.files).forEach(([section, items]) => { Object.entries(data.files).forEach(([section, items]) => {
html += ` html += `
<div style="border-bottom: 1px solid #e2e8f0; padding-top: 8px; padding-bottom: 8px;"> <div style="border-bottom: 1px solid #e2e8f0; padding-top: 8px; padding-bottom: 8px;">
<h3 style="font-size: 18px; font-weight: semibold; margin-bottom: 8px;">${ <h3 style="font-size: 18px; font-weight: semibold; margin-bottom: 8px;">${section.charAt(0).toUpperCase() + section.slice(1)
section.charAt(0).toUpperCase() + section.slice(1) }</h3>`;
}</h3>`;
items.forEach((item) => { items.forEach((item) => {
html += `<p style="font-size: 14px; color: ${textColor};">${item.name}</p>`; html += `<p style="font-size: 14px; color: ${textColor};">${item.name}</p>`;
}); });
@ -1006,14 +1013,12 @@ export class LoadingDialog extends ComfyDialog {
showLoading(title, message) { showLoading(title, message) {
this.show(` this.show(`
<div style="width: 400px; display: flex; gap: 18px; flex-direction: column; overflow: unset"> <div style="width: 400px; display: flex; gap: 18px; flex-direction: column; overflow: unset">
<h3 style="margin: 0px; display: flex; align-items: center; justify-content: center; gap: 12px;">${title} ${ <h3 style="margin: 0px; display: flex; align-items: center; justify-content: center; gap: 12px;">${title} ${this.loadingIcon
this.loadingIcon }</h3>
}</h3> ${message
${ ? `<label style="max-width: 100%; white-space: pre-wrap; word-wrap: break-word;">${message}</label>`
message : ""
? `<label style="max-width: 100%; white-space: pre-wrap; word-wrap: break-word;">${message}</label>` }
: ""
}
</div> </div>
`); `);
} }
@ -1279,21 +1284,17 @@ export class ConfigDialog extends ComfyDialog {
</label> </label>
<label style="color: white; width: 100%;"> <label style="color: white; width: 100%;">
Endpoint: Endpoint:
<input id="endpoint" style="margin-top: 8px; width: 100%; height:40px; box-sizing: border-box; padding: 0px 6px;" type="text" value="${ <input id="endpoint" style="margin-top: 8px; width: 100%; height:40px; box-sizing: border-box; padding: 0px 6px;" type="text" value="${data.endpoint
data.endpoint }">
}">
</label> </label>
<div style="color: white;"> <div style="color: white;">
API Key: User / Org <button style="font-size: 18px;">${ API Key: User / Org <button style="font-size: 18px;">${data.displayName ?? ""
data.displayName ?? "" }</button>
}</button> <input id="apiKey" style="margin-top: 8px; width: 100%; height:40px; box-sizing: border-box; padding: 0px 6px;" type="password" value="${data.apiKey
<input id="apiKey" style="margin-top: 8px; width: 100%; height:40px; box-sizing: border-box; padding: 0px 6px;" type="password" value="${ }">
data.apiKey
}">
<button id="loginButton" style="margin-top: 8px; width: 100%; height:40px; box-sizing: border-box; padding: 0px 6px;"> <button id="loginButton" style="margin-top: 8px; width: 100%; height:40px; box-sizing: border-box; padding: 0px 6px;">
${ ${data.apiKey ? "Re-login with ComfyDeploy" : "Login with ComfyDeploy"
data.apiKey ? "Re-login with ComfyDeploy" : "Login with ComfyDeploy" }
}
</button> </button>
</div> </div>
</div> </div>