From 496924f98a8be67844a27fe046f0f6f31fc3d5b2 Mon Sep 17 00:00:00 2001 From: Kenny Ning Date: Tue, 14 May 2024 21:28:05 +0000 Subject: [PATCH] formatting --- 06_gpu_and_ml/comfyui/comfy_ui.py | 29 ++++++++++++++++----------- 06_gpu_and_ml/comfyui/infer.py | 1 + 06_gpu_and_ml/comfyui/workflow_api.py | 1 + 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/06_gpu_and_ml/comfyui/comfy_ui.py b/06_gpu_and_ml/comfyui/comfy_ui.py index 3e1e6dffd..cd6e6baea 100644 --- a/06_gpu_and_ml/comfyui/comfy_ui.py +++ b/06_gpu_and_ml/comfyui/comfy_ui.py @@ -4,32 +4,32 @@ # # # Run a ComfyUI workflow as an API # -# In this example, we show you how to -# +# In this example, we show you how to +# # 1) Run ComfyUI interactively -# +# # 2) Optimize performance with [@enter](/docs/guide/lifecycle-functions#enter) # # 3) Run a ComfyUI workflow JSON via API -# +# # The goal of this example is to give users an easy way to deploy an existing ComfyUI workflow on Modal and show some ways they can reduce inference latency. -# +# # An alternative approach is to port your ComfyUI workflow from JSON into Python, which you can check out [in this blog post](/blog/comfyui-prototype-to-production). # The Python approach reduces latency by skipping the server standup step entirely, but requires more effort to migrate to from JSON. -# +# # ## Quickstart # 1) Run `cd 06_gpu_and_ml` -# +# # 2) Run `modal serve comfyui.comfy_ui` to stand up the ComfyUI server. # This example serves the [ComfyUI inpainting example workflow](https://comfyanonymous.github.io/ComfyUI_examples/inpaint/) behind an API. # Inpainting is the process of filling in an image with another generated image. # # 3) Run inference with a text prompt: `python -m comfyui.infer --prompt "white heron"`. This creates the following image: # ![example comfyui image](./comfyui_gen_image.jpg) -# +# # Try running inference again with a different prompt e.g. `python -m comfyui.infer.py --prompt "white tiger"`. # Notice how successive inference calls are much faster. In our tests, inference calls drop from 30s to 3s due to our optimized performance design. -# +# # Now we'll dive into the step-by-step process of how to run ComfyUI both interactively and as an API, as well as how we're able to leverage Modal classes to run arbitrary workflows with minimal cold starts. # ## Run ComfyUI interactively # First, we define the ComfyUI image. @@ -69,11 +69,15 @@ # 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. -# +# # We move a lot of ComfyUI-specific code to `helpers.py`. # This includes functions like downloading checkpoints/plugins to the right directory on the ComfyUI server. with comfyui_image.imports(): - from .helpers import connect_to_local_server, download_to_comfyui, get_images + from .helpers import ( + connect_to_local_server, + download_to_comfyui, + get_images, + ) # Here we use Modal's class syntax to build the image (with our custom checkpoints/plugins). @app.cls( @@ -103,7 +107,7 @@ 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. # @@ -137,6 +141,7 @@ def infer(self, workflow_data: dict, params: dict): from typing import Dict + @app.function(image=web_image, container_idle_timeout=300) @modal.web_endpoint(method="POST") def backend(item: Dict): diff --git a/06_gpu_and_ml/comfyui/infer.py b/06_gpu_and_ml/comfyui/infer.py index e4c6d4595..8d4d5ca92 100644 --- a/06_gpu_and_ml/comfyui/infer.py +++ b/06_gpu_and_ml/comfyui/infer.py @@ -1,5 +1,6 @@ import argparse import pathlib + import requests parser = argparse.ArgumentParser() diff --git a/06_gpu_and_ml/comfyui/workflow_api.py b/06_gpu_and_ml/comfyui/workflow_api.py index 1c0be1731..9265dc1f1 100644 --- a/06_gpu_and_ml/comfyui/workflow_api.py +++ b/06_gpu_and_ml/comfyui/workflow_api.py @@ -15,6 +15,7 @@ from modal import App, Volume, web_endpoint from .comfy_ui import comfyui_image, comfyui_workflow_data_path + with comfyui_image.imports(): from .helpers import download_to_comfyui