-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_comfy.py
More file actions
164 lines (142 loc) · 5.27 KB
/
Copy pathbatch_comfy.py
File metadata and controls
164 lines (142 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
from typing import Dict, Any
import json
import sys
import asyncio
import argparse
import csv
from pathlib import Path
import logging
from comfy_client import ComfyClient
import comfy_utils
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
async def wait_for_all(futures: Dict[str, asyncio.Future]) -> Dict[str, Any]:
"""Wait for all futures to complete and return results.
Args:
futures: Dict mapping prefixes to their futures
Returns:
Dict mapping prefixes to their results (True for success, Exception for failure)
"""
results = {}
for prefix, future in futures.items():
try:
result = await future
results[prefix] = result
except Exception as e:
results[prefix] = e
return results
async def main():
# Set up argument parser
parser = argparse.ArgumentParser(description='Run ComfyUI workflows in batch mode')
parser.add_argument(
'--workflow',
type=Path,
default=Path('workflow/text2image.json'),
help='Path to the workflow JSON file'
)
parser.add_argument(
'--prompts',
type=Path,
default=Path('testdata/text2image.csv'),
help='Path to the CSV file containing prompts'
)
parser.add_argument(
'--log-file',
type=Path,
default=Path('comfy_batch_log.txt'),
help='Path to the log file for completed workflow hashes'
)
parser.add_argument(
'--port',
type=int,
help='Port number of the ComfyUI server (optional)'
)
args = parser.parse_args()
# Expand user paths
args.workflow = args.workflow.expanduser()
args.prompts = args.prompts.expanduser()
args.log_file = args.log_file.expanduser()
if not args.workflow.exists() or not args.prompts.exists():
print("Error: One or both input files do not exist")
print(f"Workflow file: {args.workflow}")
print(f"Prompts file: {args.prompts}")
sys.exit(1)
# Initialize client
client = await ComfyClient.create(port=args.port, log_file=args.log_file)
# Load workflow
try:
with open(args.workflow) as f:
workflow = json.load(f)
logger.info("Successfully loaded workflow file")
except json.JSONDecodeError as e:
print(f"Error: Invalid workflow JSON file: {e}")
sys.exit(1)
# Check if workflow is in UI format
if comfy_utils.is_ui_format_workflow(workflow):
print("Error: The workflow file appears to be in the UI format (saved using Workflow->Save As... or loaded from a .png file)."
" Please export the workflow in API format using Workflow->Export (API) in the ComfyUI interface instead.")
sys.exit(1)
# Read CSV file
try:
with open(args.prompts) as f:
reader = csv.DictReader(f)
rows = list(reader) # Convert to list for easier handling
logger.info(f"Successfully loaded {len(rows)} rows from CSV file")
except csv.Error as e:
print(f"Error reading CSV file: {e}")
sys.exit(1)
try:
# Queue all prompts from the rows
futures = {}
skipped_count = 0
for row in rows:
try:
updated_workflow = comfy_utils.update_workflow(workflow, row)
except (comfy_utils.UnusedColumnsError, comfy_utils.NoSuchInputError) as e:
logger.error(f"Error processing row {row}: {e}")
continue
workflow_hash = comfy_utils.get_workflow_hash(updated_workflow)
future = client.queue_prompt(updated_workflow)
# if future is already completed, skip it
if future.done() and future.result() == "skipped":
skipped_count += 1
continue
futures[workflow_hash] = future
queued_count = len(futures)
logger.info(f"Queued {queued_count} new workflows.")
if not futures:
logger.info("No new workflows to run.")
return
# Wait for all futures to complete
results = await wait_for_all(futures)
# Log results
success_count = 0
failure_count = 0
for id, result in results.items():
if isinstance(result, Exception):
logger.error(f"Failed to generate image with id {id[:8]}: {result}")
failure_count += 1
else:
logger.info(f"Successfully generated image with id: {id[:8]}")
success_count += 1
logger.info(f"Completed processing {len(results)} workflows.")
if failure_count > 0 or skipped_count > 0:
summary = []
if success_count > 0:
summary.append(f"{success_count} succeeded")
if failure_count > 0:
summary.append(f"{failure_count} failed")
if skipped_count > 0:
summary.append(f"{skipped_count} skipped")
logger.info(f"Summary: {', '.join(summary)}")
else:
logger.info(f"Summary: All {len(results)} rows completed successfully")
finally:
# Clean up client
await client.close()
if __name__ == "__main__":
asyncio.run(main())