|
6 | 6 | import random
|
7 | 7 | import sys
|
8 | 8 | import re
|
9 |
| -from typing import Dict, List, Any, Callable, Tuple |
| 9 | +from typing import Dict, List, Callable, Tuple |
| 10 | +from argparse import ArgumentParser |
10 | 11 |
|
11 | 12 | import black
|
12 | 13 |
|
|
23 | 24 | from nodes import NODE_CLASS_MAPPINGS
|
24 | 25 |
|
25 | 26 |
|
| 27 | +DEFAULT_INPUT_FILE = "workflow_api.json" |
| 28 | +DEFAULT_OUTPUT_FILE = "workflow_api.py" |
| 29 | +DEFAULT_QUEUE_SIZE = 1 |
| 30 | + |
| 31 | + |
26 | 32 | class FileHandler:
|
27 | 33 | """Handles reading and writing files.
|
28 | 34 |
|
@@ -224,7 +230,6 @@ def generate_workflow(
|
224 | 230 | custom_nodes = False
|
225 | 231 | # Loop over each dictionary in the load order list
|
226 | 232 | for idx, data, is_special_function in load_order:
|
227 |
| - |
228 | 233 | # Generate class definition and inputs from the data
|
229 | 234 | inputs, class_type = data["inputs"], data["class_type"]
|
230 | 235 | class_def = self.node_class_mappings[class_type]()
|
@@ -552,13 +557,57 @@ def execute(self):
|
552 | 557 | print(f"Code successfully generated and written to {self.output_file}")
|
553 | 558 |
|
554 | 559 |
|
555 |
| -if __name__ == "__main__": |
556 |
| - # Update class parameters here |
557 |
| - input_file = "workflow_api.json" |
558 |
| - output_file = "workflow_api.py" |
559 |
| - queue_size = 10 |
560 |
| - |
561 |
| - # Convert ComfyUI workflow to Python |
562 |
| - ComfyUItoPython( |
563 |
| - input_file=input_file, output_file=output_file, queue_size=queue_size |
| 560 | +def run( |
| 561 | + input_file: str = DEFAULT_INPUT_FILE, |
| 562 | + output_file: str = DEFAULT_OUTPUT_FILE, |
| 563 | + queue_size: int = DEFAULT_QUEUE_SIZE, |
| 564 | +) -> None: |
| 565 | + """Generate Python code from a ComfyUI workflow_api.json file. |
| 566 | +
|
| 567 | + Args: |
| 568 | + input_file (str): Path to the input JSON file. Defaults to "workflow_api.json". |
| 569 | + output_file (str): Path to the output Python file. |
| 570 | + Defaults to "workflow_api.py". |
| 571 | + queue_size (int): The number of times a workflow will be executed by the script. |
| 572 | + Defaults to 1. |
| 573 | +
|
| 574 | + Returns: |
| 575 | + None |
| 576 | + """ |
| 577 | + ComfyUItoPython(input_file, output_file, queue_size) |
| 578 | + |
| 579 | + |
| 580 | +def main() -> None: |
| 581 | + """Main function to generate Python code from a ComfyUI workflow_api.json file.""" |
| 582 | + parser = ArgumentParser( |
| 583 | + description="Generate Python code from a ComfyUI workflow_api.json file." |
| 584 | + ) |
| 585 | + parser.add_argument( |
| 586 | + "-f", |
| 587 | + "--input_file", |
| 588 | + type=str, |
| 589 | + help="path to the input JSON file", |
| 590 | + default=DEFAULT_INPUT_FILE, |
| 591 | + ) |
| 592 | + parser.add_argument( |
| 593 | + "-o", |
| 594 | + "--output_file", |
| 595 | + type=str, |
| 596 | + help="path to the output Python file", |
| 597 | + default=DEFAULT_OUTPUT_FILE, |
564 | 598 | )
|
| 599 | + parser.add_argument( |
| 600 | + "-q", |
| 601 | + "--queue_size", |
| 602 | + type=int, |
| 603 | + help="number of times the workflow will be executed by default", |
| 604 | + default=DEFAULT_QUEUE_SIZE, |
| 605 | + ) |
| 606 | + pargs = parser.parse_args() |
| 607 | + ComfyUItoPython(**vars(pargs)) |
| 608 | + print("Done.") |
| 609 | + |
| 610 | + |
| 611 | +if __name__ == "__main__": |
| 612 | + """Run the main function.""" |
| 613 | + main() |
0 commit comments