Skip to content

Commit

Permalink
feat(transpile): rename bootstrap option to runtime (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaoses-Ib committed Jun 13, 2024
1 parent 26bd039 commit cc8868a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
41 changes: 38 additions & 3 deletions docs/Transpiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,47 @@ Usage: python -m comfy_script.transpile [OPTIONS] WORKFLOW
Transpile workflow to ComfyScript.

Options:
--api TEXT [default: http://127.0.0.1:8188/]
--bootstrap Wrap the script with bootstrap imports and workflow context.
--help Show this message and exit.
--api TEXT [default: http://127.0.0.1:8188/]
--runtime Wrap the script with runtime imports and workflow context.
--help Show this message and exit.
```

Example:
```powershell
python -m comfy_script.transpile "D:\workflow.json"
```
Output:
```python
model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt')
conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip)
conditioning2 = CLIPTextEncode('text, watermark', clip)
latent = EmptyLatentImage(512, 512, 1)
latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1)
image = VAEDecode(latent, vae)
SaveImage(image, 'ComfyUI')
```

Wrap the script with runtime imports and workflow context:
```powershell
python -m comfy_script.transpile "tests\transpile\default.json" --runtime
```
Output:
```python
from comfy_script.runtime import *
load()
from comfy_script.runtime.nodes import *

with Workflow():
model, clip, vae = CheckpointLoaderSimple('v1-5-pruned-emaonly.ckpt')
conditioning = CLIPTextEncode('beautiful scenery nature glass bottle landscape, , purple galaxy bottle,', clip)
conditioning2 = CLIPTextEncode('text, watermark', clip)
latent = EmptyLatentImage(512, 512, 1)
latent = KSampler(model, 156680208700286, 20, 8, 'euler', 'normal', conditioning, conditioning2, latent, 1)
image = VAEDecode(latent, vae)
SaveImage(image, 'ComfyUI')
```

Save the code to `script.py`:
```powershell
python -m comfy_script.transpile "tests\transpile\default.json" --runtime > script.py
```
5 changes: 3 additions & 2 deletions src/comfy_script/transpile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,10 @@ def visit(node):
for v in end_nodes:
yield from visit(v)

def to_script(self, end_nodes: list[int | str] | None = None, *, bootstrap: bool = False) -> str:
def to_script(self, end_nodes: list[int | str] | None = None, *, runtime: bool = False) -> str:
'''
- `end_nodes`: The id can be of a different type than the type used by the workflow.
- `runtime`: Whether to wrap the script with runtime imports and workflow context.
'''
# From leaves to roots or roots to leaves?
# ComfyUI now executes workflows from leaves to roots, but there is a PR to change this to from roots to leaves with topological sort: https://github.com/comfyanonymous/ComfyUI/pull/931
Expand All @@ -342,7 +343,7 @@ def to_script(self, end_nodes: list[int | str] | None = None, *, bootstrap: bool
# TODO: Add line breaks if a node has multiple inputs
c += self._node_to_assign_st(self.G.nodes[node])

if bootstrap:
if runtime:
import textwrap

c = textwrap.indent(c, ' ')
Expand Down
6 changes: 3 additions & 3 deletions src/comfy_script/transpile/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
@click.command(help='Transpile workflow to ComfyScript.')
@click.argument('workflow', type=click.File('r'))
@click.option('--api', type=click.STRING, default='http://127.0.0.1:8188/', show_default=True)
@click.option('--bootstrap', is_flag=True, default=False, show_default=True, help='Wrap the script with bootstrap imports and workflow context.')
def cli(workflow, api: str, bootstrap: bool):
@click.option('--runtime', is_flag=True, default=False, show_default=True, help='Wrap the script with runtime imports and workflow context.')
def cli(workflow, api: str, runtime: bool):
workflow = workflow.read()
script = WorkflowToScriptTranspiler(workflow, api).to_script(bootstrap=bootstrap)
script = WorkflowToScriptTranspiler(workflow, api).to_script(runtime=runtime)
print(script)

cli()

0 comments on commit cc8868a

Please sign in to comment.