Skip to content

Commit

Permalink
ruff check
Browse files Browse the repository at this point in the history
  • Loading branch information
kning committed May 14, 2024
1 parent 496924f commit 9d7bac5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
31 changes: 18 additions & 13 deletions 06_gpu_and_ml/comfyui/comfy_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
# You can define custom checkpoints, plugins, and more in the `workflow-examples/base-model.json` in this directory.

comfyui_workflow_data_path = pathlib.Path(__file__).parent / "workflow-examples"
base_models = json.loads((pathlib.Path(comfyui_workflow_data_path) / "base-model.json").read_text())
base_models = json.loads(
(pathlib.Path(comfyui_workflow_data_path) / "base-model.json").read_text()
)

# Specific workflows (like our inpainting example) have their own folder containing the workflow JSON as well as that workflow's corresponding `model.json` which specifies the custom checkpoints/plugins used in the workflow.
# These get loaded once at container start time and not build time; we'll go into more detail on how that works in the next section.
Expand All @@ -79,6 +81,7 @@
get_images,
)


# Here we use Modal's class syntax to build the image (with our custom checkpoints/plugins).
@app.cls(
allow_concurrent_inputs=100,
Expand Down Expand Up @@ -106,22 +109,21 @@ def _run_comfyui_server(self):
def ui(self):
self._run_comfyui_server()

# When you run `modal serve comfyui.comfy_ui`, you'll see a `ComfyUI.ui` link to interactively develop your ComfyUI workflow that has the custom checkpoints/plugins loaded in.
#
# Notice the `__init__` constructor.
# This allows us to leverage a special Modal pattern called [parameterized functions](/docs/guide/lifecycle-functions#parametrized-functions) that will allow us to support arbitrary workflows and custom checkpoints/plugins in an optimized way.
#
# ## Optimize performance with `@enter`
#
# By setting a `models` argument for the class, we can dynamically download arbitrary models at runtime on top of the base objects that were downloaded at `@build` time.
# We can use the `@enter` function to optimize inference time by downloading custom models and standing up the ComfyUI server exactly once at container startup time.
# When you run `modal serve comfyui.comfy_ui`, you'll see a `ComfyUI.ui` link to interactively develop your ComfyUI workflow that has the custom checkpoints/plugins loaded in.
#
# Notice the `__init__` constructor.
# This allows us to leverage a special Modal pattern called [parameterized functions](/docs/guide/lifecycle-functions#parametrized-functions) that will allow us to support arbitrary workflows and custom checkpoints/plugins in an optimized way.
#
# ## Optimize performance with `@enter`
#
# By setting a `models` argument for the class, we can dynamically download arbitrary models at runtime on top of the base objects that were downloaded at `@build` time.
# We can use the `@enter` function to optimize inference time by downloading custom models and standing up the ComfyUI server exactly once at container startup time.
@modal.enter()
def prepare_comfyui(self):
self._run_comfyui_server()


# Lastly, we write the inference method that takes in any workflow JSON and additional arguments you may want to use to parameterize your workflow JSON (e.g. handle user-defined text prompts, input images).
# It then runs the workflow programmatically against the running ComfyUI server and returns the images.
# Lastly, we write the inference method that takes in any workflow JSON and additional arguments you may want to use to parameterize your workflow JSON (e.g. handle user-defined text prompts, input images).
# It then runs the workflow programmatically against the running ComfyUI server and returns the images.
@modal.method()
def infer(self, workflow_data: dict, params: dict):
# input images need to be downloaded to the container at this step
Expand Down Expand Up @@ -156,13 +158,15 @@ def backend(item: Dict):
images = ComfyUI(models).infer.remote(workflow, params)
return Response(content=images[0], media_type="image/jpeg")


# To deploy this API, run `modal deploy comfyui.comfy_ui`

# ## Further optimization
# After deploying, you can also apply [keep warm](/docs/reference/modal.Function#keep_warm) to a particular `model.json` combination of checkpoints/plugins.
# This will stand up a dedicated container pool for any workflows that have the same `model.json` config.
# This can help you further minimize a harsh cold start when a workflow is run for the first time.


@app.local_entrypoint()
def apply_config():
DeployedComfyUI = modal.Cls.lookup("example-comfyui", "ComfyUI")
Expand All @@ -173,6 +177,7 @@ def apply_config():
)
DeployedComfyUI(models).infer.keep_warm(1)


# Some other things to try:
#
# * Cache downloaded checkpoints/plugins to a [Volume](https://modal.com/docs/guide/volumes) in the `@enter` step and load from there in successive cold starts.
Expand Down
6 changes: 4 additions & 2 deletions 06_gpu_and_ml/comfyui/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

parser = argparse.ArgumentParser()

parser.add_argument('--prompt', type=str, required=True, help='object to draw into the image')
parser.add_argument(
"--prompt", type=str, required=True, help="object to draw into the image"
)
args = parser.parse_args()

comfyui_workflow_data_path = (
Expand All @@ -27,7 +29,7 @@
res = requests.post(url, json=data)
if res.status_code == 200:
print("Image finished generating!")
filename = 'comfyui_gen_image.png'
filename = "comfyui_gen_image.png"
(pathlib.Path.home() / filename).write_bytes(res.content)
print(f"saved '{filename}'")
else:
Expand Down
6 changes: 4 additions & 2 deletions 06_gpu_and_ml/comfyui/workflow_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ def run_python_workflow(item: Dict):
)

download_image(item["image"])
models = json.loads((comfyui_workflow_data_path / "inpainting" / "model.json").read_text())
models = json.loads(
(comfyui_workflow_data_path / "inpainting" / "model.json").read_text()
)
for m in models:
download_to_comfyui(m['url'], m['path'])
download_to_comfyui(m["url"], m["path"])

with torch.inference_mode():
loadimage = LoadImage()
Expand Down

0 comments on commit 9d7bac5

Please sign in to comment.