### MinIO (Local S3-Compatible) Setup Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/configuration.md Configuration and Docker command to start a local MinIO instance for S3-compatible storage. ```bash export API_CACHE=memory export S3_ENDPOINT_URL=http://minio:9000 export S3_ACCESS_KEY_ID=minioadmin export S3_SECRET_ACCESS_KEY=minioadmin export S3_BUCKET_NAME=comfyui # Start MinIO docker run -d \ -p 9000:9000 \ -e MINIO_ROOT_USER=minioadmin \ -e MINIO_ROOT_PASSWORD=minioadmin \ minio/minio server /data ``` -------------------------------- ### Start ComfyUI Service Source: https://github.com/ai-dock/comfyui/blob/main/build/COPY_ROOT_1/usr/local/share/ai-dock/comfyui.ipynb Use this command to start the ComfyUI service. This script is used to initiate the service. ```python # Start the ComfyUI service !jupyter-start-service.sh comfyui ``` -------------------------------- ### Example Queue Info Response Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md An example of the JSON response format for the `/queue-info` endpoint, showing a list of UUIDs representing queued requests. ```json [ "550e8400-e29b-41d4-a716-446655440000", "660e8400-e29b-41d4-a716-446655440001" ] ``` -------------------------------- ### Generate OpenAPI Examples Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/request_models.md Loads OpenAPI example payloads from JSON files in the ./payloads/ directory. This method is used by FastAPI endpoints to populate Swagger/OpenAPI documentation. ```python examples = Payload.get_openapi_examples() print(examples.keys()) # dict_keys(['Imgsave', ...]) ``` -------------------------------- ### Instantiate WebHook with Extra Parameters Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/request_models.md Example of creating a WebHook instance with a specific URL and additional parameters. These parameters will be included in the POST body. ```python webhook = WebHook( url="https://example.com/callback", extra_params={"user_id": "123", "project_id": "abc"} ) # POST body will include these extra params ``` -------------------------------- ### Iterate Through Output Files Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/response_models.md Example of how to access and print local paths and download URLs for generated output files after a successful request. ```python # After status is 'success' for output in result['output']: print(f"Local file: {output['local_path']}") if 'url' in output: print(f"Download URL: {output['url']}") # URL expires in 7 days ``` -------------------------------- ### Startup Event Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md Describes the asynchronous function that runs once when the FastAPI server starts, responsible for initiating background tasks. ```APIDOC ## Startup Event ```python @app.on_event("startup") async def startup_event(): asyncio.create_task(main()) ``` Async function that runs once when FastAPI server starts. **Behavior:** - Creates asyncio task for `main()` function (does not wait for completion) - Allows FastAPI to remain responsive to requests while workers run **Timing:** - Runs before first request is served - Runs in background; FastAPI server starts immediately after task creation ``` -------------------------------- ### get_openapi_examples Method Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/request_models.md This static method loads OpenAPI example payloads from JSON files in the `./payloads/` directory. It processes these files to create a dictionary suitable for FastAPI's `openapi_examples` parameter. ```APIDOC ## Method: get_openapi_examples ```python @staticmethod def get_openapi_examples() ``` Load OpenAPI example payloads from JSON files in the `./payloads/` directory. **Returns:** dict — OpenAPI examples map with format: ```python { "Example Name": {"value": {...payload dict...}}, "Another Example": {"value": {...payload dict...}} } ``` **Process:** 1. Scans `./payloads/` directory for `.json` files 2. Loads each JSON file into dict 3. Converts filename to natural language (e.g., `text2image.json` → "Text2image") 4. Returns dict suitable for FastAPI's `openapi_examples` parameter **Files Loaded:** - `./payloads/imgsave.json` — Image save workflow example **Used By:** FastAPI endpoint to populate Swagger/OpenAPI documentation **Example:** ```python examples = Payload.get_openapi_examples() print(examples.keys()) # dict_keys(['Imgsave', ...]) ``` ``` -------------------------------- ### Main Pipeline Integration Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/workers.md Sets up and runs multiple worker instances for preprocessing, generation, and postprocessing. It demonstrates how to configure worker queues and stores, and then starts the asynchronous tasks for each worker type. ```python async def main(): worker_config = { "preprocess_queue": preprocess_queue, "generation_queue": generation_queue, "postprocess_queue": postprocess_queue, "request_store": request_store, "response_store": response_store, } # 3 preprocess workers preprocess_workers = [PreprocessWorker(i, worker_config) for i in range(1, 4)] preprocess_tasks = [asyncio.create_task(worker.work()) for worker in preprocess_workers] # 1 generation worker generation_workers = [GenerationWorker(i, worker_config) for i in range(1, 2)] generation_tasks = [asyncio.create_task(worker.work()) for worker in generation_workers] # 3 postprocess workers postprocess_workers = [PostprocessWorker(i, worker_config) for i in range(1, 4)] postprocess_tasks = [asyncio.create_task(worker.work()) for worker in postprocess_workers] await asyncio.gather(*preprocess_tasks, *generation_tasks, *postprocess_tasks) ``` -------------------------------- ### Get Secure UI Links Source: https://github.com/ai-dock/comfyui/blob/main/build/COPY_ROOT_1/usr/local/share/ai-dock/comfyui.ipynb Generates secure links for accessing the UI. The '-p' flag specifies the port. ```python # Get secure UI links !cfqt-url.sh -p 1111 ``` -------------------------------- ### BaseModifier Usage Example Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/basemodifier.md Demonstrates creating a BaseModifier instance with custom parameters, loading a workflow, and retrieving the modified workflow. Note that BaseModifier requires a workflow to be explicitly loaded. ```python import asyncio from modifiers.basemodifier import BaseModifier async def example(): # Create a modifier with custom parameters mods = {"seed": 42, "steps": 30} modifier = BaseModifier(mods) # Load a workflow (must provide one since BaseModifier has no WORKFLOW_JSON) workflow = { "3": { "inputs": {"seed": 0, "steps": 20}, "class_type": "KSampler" }, "10": { "inputs": { "image": "https://example.com/input.png" }, "class_type": "LoadImage" } } await modifier.load_workflow(workflow) # Get the modified workflow (replaces URLs with local files) modified = await modifier.get_modified_workflow() print(modified) # Output: {"3": {...}, "10": {"inputs": {"image": "abc123def456.png"}}} asyncio.run(example()) ``` -------------------------------- ### Example of WebHook URL Validation Call Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/request_models.md Demonstrates how to instantiate a WebHook with a URL. Calling `has_valid_url()` on this instance would currently raise a NameError due to an unresolved dependency. ```python webhook = WebHook(url="https://example.com/callback") # webhook.has_valid_url() # Would raise NameError currently ``` -------------------------------- ### Run PreprocessWorker Loop Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/workers.md Start the main worker loop to continuously process requests from the preprocess queue. Signal the worker to exit by placing None in the queue. ```python # Start worker in background task = asyncio.create_task(worker.work()) # Add requests to queue await preprocess_queue.put("request-id-123") # Later, stop the worker await preprocess_queue.put(None) # Signals worker to exit ``` -------------------------------- ### Local Development API Call Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/configuration.md Example of making an API call for local development without S3 uploads. Results are stored locally. ```bash # No S3 uploads export API_CACHE=memory # No S3 config needed - results stored locally curl -X POST http://localhost:8188/ai-dock/api/payload \ -H "Content-Type: application/json" \ -d '{ "input": { "modifier": "Text2Image", "modifications": {"seed": 42} } }' ``` -------------------------------- ### Startup Event Handler Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md An asynchronous function that runs once when the FastAPI server starts. It creates an asyncio task for the main() function to run in the background, allowing the server to remain responsive. ```python @app.on_event("startup") async def startup_event(): asyncio.create_task(main()) ``` -------------------------------- ### Run PostprocessWorker Loop Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/workers.md Starts the main loop for the PostprocessWorker. This method runs indefinitely, processing tasks from the queue, organizing output files, and uploading them to S3 if configured. It handles task completion and error states. ```python async def work() ``` ```python worker = PostprocessWorker(1, worker_config) task = asyncio.create_task(worker.work()) ``` -------------------------------- ### Example JSON for Result Model ID Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/response_models.md Illustrates the expected JSON structure for the `id` field within a Result model response. ```json { "id": "550e8400-e29b-41d4-a716-446655440000", ... } ``` -------------------------------- ### Accessing Generation Duration (Future) Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/response_models.md Example of how to access timing information, specifically the generation duration, once the timings field is populated. This code will only work when timing data is available. ```python if result['timings']: print(f"Generation took {result['timings']['generation_duration']}s") ``` -------------------------------- ### Get File Extension Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/basemodifier.md Detects the MIME type of a file and returns its appropriate extension. Falls back to '.jpg' if detection fails. ```python async def get_file_extension(self, filepath) ``` ```python ext = await modifier.get_file_extension("/opt/ComfyUI/input/a1b2c3d4e5f6") print(ext) # ".png" or ".jpg" depending on file content ``` -------------------------------- ### Upload File and Get URL Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/workers.md Uploads a single file to S3 and generates a presigned download URL. Requires an aiobotocore S3 client and specifies the bucket name and local file path. Returns a URL valid for 7 days or None on error. ```python async def upload_file_and_get_url(self, requst_id, s3_client, bucket_name, local_path) ``` ```python url = await worker.upload_file_and_get_url( "request-123", s3_client, "comfyui", "/opt/ComfyUI/output/request-123/output.png" ) print(url) # https://s3.example.com/request-123/output.png?X-Amz-Signature=... ``` -------------------------------- ### ComfyUI Response Structure Example Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/response_models.md Illustrates the expected JSON structure of the 'comfyui_response' field after a generation is complete. It shows how outputs from different ComfyUI nodes are organized. ```json { "outputs": { "9": { "images": [ { "filename": "ComfyUI_00001_.png", "subfolder": "", "type": "output" } ] }, "14": { "text": ["positive prompt"] } } } ``` -------------------------------- ### Submit Custom Workflow with Python Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/README.md This example shows how to submit a request for a custom workflow without a predefined modifier. It requires the 'requests' library and allows for direct specification of the ComfyUI workflow JSON. ```python requests.post("http://localhost:8188/ai-dock/api/payload", json={ "input": { "modifier": "", # Use BaseModifier "workflow_json": { "3": { "inputs": {"seed": 42, "steps": 20}, "class_type": "KSampler" } # ... full workflow } } }) ``` -------------------------------- ### Apply Text2Image Modifications Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/text2image_modifier.md Apply modifications to the text-to-image workflow, customizing generation parameters. This example demonstrates setting various parameters including prompts, model, and input image. ```python import asyncio from modifiers.text2image import Text2Image async def example(): mods = { "seed": 42, "steps": 50, "sampler_name": "euler_ancestral", "scheduler": "karras", "denoise": 0.95, "include_text": "a cat wearing sunglasses", "exclude_text": "blurry, low quality", "ckpt_name": "sd15.ckpt", "input_image": "https://example.com/photo.jpg" } modifier = Text2Image(mods) await modifier.load_workflow() await modifier.apply_modifications() # Now modifier.workflow contains all customizations print(f"Seed: {modifier.workflow['3']['inputs']['seed']}") print(f"Steps: {modifier.workflow['3']['inputs']['steps']}") asyncio.run(example()) ``` -------------------------------- ### Example JSON for Result Model Message Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/response_models.md Demonstrates the `message` field in a Result model response, showing a specific error message when the status is 'failed'. ```json { "id": "request-123", "status": "failed", "message": "Workflow modifier failed: input_image required but not set", ... } ``` -------------------------------- ### Text2Image Modifier API Request Example Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/text2image_modifier.md Submit a request to the /payload endpoint to use the Text2Image modifier. This example demonstrates setting various modification parameters, including text prompts, image inputs, and S3 configurations. ```python import requests url = "http://localhost:8188/ai-dock/api/payload" payload = { "input": { "modifier": "Text2Image", "modifications": { "seed": 42, "steps": 50, "sampler_name": "euler", "scheduler": "normal", "denoise": 0.87, "include_text": "a beautiful landscape painting", "exclude_text": "watermark, text, low quality", "input_image": "https://example.com/landscape.jpg", "ckpt_name": "sd15.ckpt" }, "s3": { "access_key_id": "key", "secret_access_key": "secret", "endpoint_url": "https://s3.example.com", "bucket_name": "outputs" } } } response = requests.post(url, json=payload) result = response.json() print(f"Request ID: {result['id']}") ``` -------------------------------- ### Initialize PreprocessWorker Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/workers.md Instantiate a PreprocessWorker, providing a unique ID and a configuration dictionary with queue and cache references. ```python import asyncio from workers.preprocess_worker import PreprocessWorker worker_config = { "preprocess_queue": preprocess_queue, "generation_queue": generation_queue, "postprocess_queue": postprocess_queue, "request_store": request_store, "response_store": response_store, } worker = PreprocessWorker(1, worker_config) ``` -------------------------------- ### Instantiate Payload Model Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/request_models.md Demonstrates the basic instantiation of the Payload model, requiring an Input object. ```python payload = Payload(input=Input(...)) ``` -------------------------------- ### Get URL Hash Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/basemodifier.md Generates an MD5 hash of a URL, suitable for use as a filename prefix. ```python def get_url_hash(self, url) ``` ```python hash_val = modifier.get_url_hash("https://example.com/image.png") print(hash_val) # "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4" ``` -------------------------------- ### Override WORKFLOW_JSON in Subclass Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/basemodifier.md Example of how a subclass, such as Text2Image, can override the WORKFLOW_JSON attribute to specify its workflow file. ```python class Text2Image(BaseModifier): WORKFLOW_JSON = "workflows/text2image.json" ``` -------------------------------- ### Initialize FastAPI Application Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md Creates a FastAPI instance with a specified root path. All API routes will be prefixed with this path. Auto-generated OpenAPI documentation is available at /docs and ReDoc at /redoc. ```python from fastapi import FastAPI app = FastAPI(root_path="/ai-dock/api") ``` -------------------------------- ### Get Modified Workflow Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/text2image_modifier.md Retrieves the final modified workflow after all customizations have been applied. This workflow can then be sent to ComfyUI. ```python modifier = Text2Image(mods) await modifier.load_workflow() await modifier.apply_modifications() modified = await modifier.get_modified_workflow() # Send to ComfyUI import json print(json.dumps(modified, indent=2)) ``` -------------------------------- ### Get Queue Info Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/INDEX.md Retrieves information about the current status of the processing queue. Note: This endpoint has a known bug. ```APIDOC ## GET /queue-info ### Description Retrieves information about the current status of the processing queue. (Known bug documented). ### Method GET ### Endpoint /queue-info ``` -------------------------------- ### Load Default or Custom Workflow Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/text2image_modifier.md Loads the default text-to-image workflow from a file or uses a provided custom workflow dictionary. Use this to set up the workflow before applying modifications. ```python modifier = Text2Image({"seed": 42}) # Load default workflow from file await modifier.load_workflow() # Or provide custom workflow custom = { "3": {"inputs": {"seed": 0}, "class_type": "KSampler"}, "4": {"inputs": {"ckpt_name": "model.ckpt"}, "class_type": "CheckpointLoaderSimple"} } await modifier.load_workflow(custom) ``` -------------------------------- ### Get Result Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/INDEX.md Retrieves the result of a previously submitted request. Requires the request ID obtained from the initial payload submission. ```APIDOC ## GET /result/{request_id} ### Description Retrieves the result of a previously submitted request. ### Method GET ### Endpoint /result/{request_id} ### Parameters #### Path Parameters - **request_id** (string) - Required - The unique identifier for the request. ``` -------------------------------- ### Get Modified Workflow Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/basemodifier.md Retrieve the complete workflow after all modifications have been applied. This method first ensures all customizations are executed by calling `apply_modifications()`. ```python async def get_modified_workflow() modified = await modifier.get_modified_workflow() # Send modified to ComfyUI API ``` -------------------------------- ### Queue Configuration Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md Explains the global queue configurations for preprocessing, generation, and postprocessing, outlining their purpose and characteristics. ```APIDOC ### Queue Configuration ```python preprocess_queue = asyncio.Queue() generation_queue = asyncio.Queue() postprocess_queue = asyncio.Queue() ``` **Variables:** - `preprocess_queue` — Queue of request IDs waiting for modification - `generation_queue` — Queue of request IDs waiting for ComfyUI submission - `postprocess_queue` — Queue of request IDs waiting for S3 upload **Characteristics:** - Unbounded FIFO queues (no size limit) - Thread-safe asyncio.Queue - Shared across FastAPI handlers and worker tasks ``` -------------------------------- ### Pydantic JSON Encoding Example Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/response_models.md Demonstrates how FastAPI automatically serializes a Pydantic model to JSON. This is useful for understanding the structure of API responses. ```python result = Result( id="123", message="Test", status="pending", comfyui_response={"node": "data"}, output=[{"local_path": "/path"}], timings={} ) json_str = result.model_dump_json() # {"id":"123","message":"Test",...} ``` -------------------------------- ### Get Queue Info Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md Retrieves the current list of request IDs waiting in the preprocessing queue. This endpoint is intended for monitoring the queue depth. ```APIDOC ## GET /ai-dock/api/queue-info ### Description Retrieves a list of request IDs currently in the preprocessing queue. ### Method GET ### Endpoint /ai-dock/api/queue-info ### Response #### Success Response (200) - **queue_info** (List[str]) - A list of strings, where each string is a request ID. ### Response Example ```json [ "550e8400-e29b-41d4-a716-446655440000", "660e8400-e29b-41d4-a716-446655440001" ] ``` ``` -------------------------------- ### Get Result by Request ID Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md Retrieves the processing status and results for a given request ID. Handles cases where the request ID is not found. ```APIDOC ## GET /ai-dock/api/result/{request_id} ### Description Retrieves the processing status and results associated with a unique request identifier. If the request ID does not exist, it returns a 404 Not Found status with an appropriate error message. ### Method GET ### Endpoint /ai-dock/api/result/{request_id} ### Parameters #### Path Parameters - **request_id** (str) - Required - Unique request identifier ### Response #### Success Response (200 OK) - **id** (str) - The request ID. - **status** (str) - The current status of the request (e.g., "pending", "success", "failed"). - **message** (str) - A message indicating the status or any errors. - **comfyui_response** (object) - Response details from ComfyUI, may be empty. - **output** (array) - Output data, may be empty. - **timings** (object) - Timing information for the process. #### Not Found Response (404 Not Found) - **id** (str) - The requested request ID. - **message** (str) - "Request ID not found". - **status** (str) - "failed". - **comfyui_response** (object) - Empty object. - **output** (array) - Empty array. - **timings** (object) - Empty object. ### Response Example (200 OK - Success) { "id": "example-request-id", "status": "success", "message": "Process complete.", "comfyui_response": {}, "output": [], "timings": {} } ### Response Example (404 Not Found) { "id": "nonexistent-id", "message": "Request ID not found", "status": "failed", "comfyui_response": {}, "output": [], "timings": {} } ``` -------------------------------- ### Initialize PostprocessWorker Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/workers.md Instantiate a PostprocessWorker. Requires a unique worker ID and a configuration dictionary containing queue and cache references. ```python def __init__(self, worker_id, kwargs) ``` -------------------------------- ### Backblaze B2 Configuration Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/configuration.md Environment variables for configuring the API to use Backblaze B2 storage. ```bash export API_CACHE=redis export S3_ENDPOINT_URL=https://s3.us-west-000.backblazeb2.com export S3_ACCESS_KEY_ID=your-app-key-id export S3_SECRET_ACCESS_KEY=your-app-key export S3_BUCKET_NAME=your-bucket ``` -------------------------------- ### Invalid Request Schema Example Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md Illustrates the JSON payload sent by a client for an invalid request and the corresponding 422 Unprocessable Entity response from FastAPI. ```json { "input": null } ``` ```json { "detail": [ { "loc": ["body", "input"], "msg": "field required", "type": "value_error.missing" } ] } ``` -------------------------------- ### Initialize Queues Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md Sets up asyncio Queues for different stages of processing: preprocessing, generation, and postprocessing. These queues are unbounded FIFO queues and are shared across FastAPI handlers and worker tasks. ```python preprocess_queue = asyncio.Queue() generation_queue = asyncio.Queue() postprocess_queue = asyncio.Queue() ``` -------------------------------- ### Get Default WebHook Values Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/request_models.md Retrieves the default configuration values for a WebHook. This method is used internally to set default values for the Input field. ```python @staticmethod def get_defaults() ``` ```python { "url": "", "extra_params": {} } ``` -------------------------------- ### load_workflow Method Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/basemodifier.md Loads a ComfyUI workflow, either from a default file path or a provided dictionary, and prepares it for modification. ```APIDOC ## load_workflow ### Description Load a ComfyUI workflow from the default JSON file or from a provided dictionary. ### Parameters #### Path Parameters - **workflow** (dict) - Optional - A ComfyUI workflow dictionary. If provided and no `WORKFLOW_JSON` is defined, this becomes `self.workflow`. Otherwise, loads from the file specified in `WORKFLOW_JSON` class attribute. ### Request Example ```python # Load from default class file await modifier.load_workflow() # Or provide a workflow dict directly custom_workflow = { "3": { "inputs": {"seed": 123, "steps": 20}, "class_type": "KSampler" } } await modifier.load_workflow(custom_workflow) ``` ``` -------------------------------- ### Viewing Container Logs Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/configuration.md Commands to view container logs in real-time or redirect them to a file. ```bash # View container logs docker logs -f # Or redirect to file docker run ... > /var/log/comfyui-api.log 2>&1 ``` -------------------------------- ### ComfyUI API Result Response Example Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/endpoints.md This JSON structure represents a successful response from the /ai-dock/api/result/{request_id} endpoint, showing processing completion and output details. ```json { "id": "550e8400-e29b-41d4-a716-446655440000", "message": "Process complete.", "status": "success", "comfyui_response": { "outputs": { "9": { "images": [ { "filename": "ComfyUI_00001_.png", "subfolder": "", "type": "output" } ] } } }, "output": [ { "local_path": "/opt/ComfyUI/output/550e8400-e29b-41d4-a716-446655440000/ComfyUI_00001_.png", "url": "https://s3.example.com/550e8400-e29b-41d4-a716-446655440000/ComfyUI_00001_.png" } ], "timings": {} } ``` -------------------------------- ### View Live Logs Source: https://github.com/ai-dock/comfyui/blob/main/build/COPY_ROOT_1/usr/local/share/ai-dock/comfyui.ipynb This command displays live logs from the service. It's useful for monitoring activity and debugging. ```python # View the live logs !logtail.sh ``` -------------------------------- ### Get Default S3Config Values Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/request_models.md Retrieves the default values for all S3Config fields. The returned dictionary contains default strings for keys and integers for timeouts and attempts. ```python @staticmethod def get_defaults() ``` ```python defaults = S3Config.get_defaults() assert defaults["connect_timeout"] == "5" # Note: string, not int ``` -------------------------------- ### FastAPI Application Instance Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md Details the FastAPI application instance and its configured properties, including the root path and access to auto-generated OpenAPI documentation. ```APIDOC ## Application: app ```python from fastapi import FastAPI app = FastAPI(root_path="/ai-dock/api") ``` FastAPI instance serving all endpoints under `/ai-dock/api` root path. **Configured Properties:** - `root_path` = "/ai-dock/api" — Base path for all routes - Auto-generated OpenAPI docs available at `/ai-dock/api/docs` (Swagger UI) - Auto-generated ReDoc docs available at `/ai-dock/api/redoc` ``` -------------------------------- ### Retrieve Workflow Value with Default Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/basemodifier.md Use this method to get a specific parameter from the modifications dictionary. Provide a default value to avoid errors if the key is not found. ```python async def modify_workflow_value(self, key, default=None) # With default value seed = await modifier.modify_workflow_value("seed", 12345) # Without default (raises if key missing) steps = await modifier.modify_workflow_value("steps") ``` -------------------------------- ### Initialize GenerationWorker Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/workers.md Instantiate a GenerationWorker with a unique ID and configuration. The configuration dictionary should include references to queue and cache objects. ```python worker = GenerationWorker(1, worker_config) task = asyncio.create_task(worker.work()) # Worker will process requests from generation_queue ``` -------------------------------- ### Get Resolved S3Config Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/request_models.md Obtains the resolved S3 configuration, applying fallbacks to environment variables if field values are empty. This method is used by the PostprocessWorker before uploading to S3. ```python def get_config(self) ``` ```python { "access_key_id": resolved_value, "secret_access_key": resolved_value, "endpoint_url": resolved_value, "bucket_name": resolved_value, "connect_timeout": "5", "connect_attempts": "1" } ``` ```python # Request has access_key, env has endpoint_url config_obj = S3Config( access_key_id="request-key", # endpoint_url empty, will use S3_ENDPOINT_URL env var ) resolved = config_obj.get_config() # resolved["access_key_id"] = "request-key" # resolved["endpoint_url"] = value of S3_ENDPOINT_URL env var or "" ``` -------------------------------- ### Download URL Content with Caching Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/basemodifier.md Downloads content from a given URL to the input directory and returns the local filename. It caches downloaded files using an MD5 hash of the URL as a filename prefix, returning the existing file if already downloaded. ```python async def get_url_content(self, url) filename = await modifier.get_url_content("https://example.com/model.safetensors") print(filename) # e.g., "a1b2c3d4e5f6.safetensors" ``` -------------------------------- ### API Cache Configuration Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md Specifies the cache backend to be used, either 'memory' or 'redis', via an environment variable. ```bash API_CACHE=memory # or "redis" ``` -------------------------------- ### Initialize and Run Worker Pools Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md This async function initializes and runs preprocess, generation, and postprocess worker pools. It uses asyncio to manage concurrent tasks and waits for all workers to complete. ```python async def main(): worker_config = { "preprocess_queue": preprocess_queue, "generation_queue": generation_queue, "postprocess_queue": postprocess_queue, "request_store": request_store, "response_store": response_store, } preprocess_workers = [PreprocessWorker(i, worker_config) for i in range(1, 4)] preprocess_tasks = [asyncio.create_task(worker.work()) for worker in preprocess_workers] generation_workers = [GenerationWorker(i, worker_config) for i in range(1, 2)] generation_tasks = [asyncio.create_task(worker.work()) for worker in generation_workers] postprocess_workers = [PostprocessWorker(i, worker_config) for i in range(1, 4)] postprocess_tasks = [asyncio.create_task(worker.work()) for worker in postprocess_workers] await asyncio.gather(*preprocess_tasks, *generation_tasks, *postprocess_tasks) ``` -------------------------------- ### Worker Pool Size Configuration Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md Demonstrates how to adjust the number of preprocess workers by modifying the range in the worker initialization. Requires rebuilding the container image. ```python # For 5 preprocess workers instead of 3 preprocess_workers = [PreprocessWorker(i, worker_config) for i in range(1, 6)] ``` -------------------------------- ### load_workflow Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/text2image_modifier.md Loads a default text-to-image workflow or a custom one. The default workflow is loaded from `workflows/image2image.json` unless a custom workflow dictionary is provided. ```APIDOC ## Method: load_workflow ### Description Loads the default text-to-image workflow from `workflows/image2image.json` or uses a provided custom workflow dictionary. ### Method `async def load_workflow(self, workflow={})` ### Parameters #### Request Body - **workflow** (dict) - Optional - `{}` - Custom workflow dict. If provided and non-empty, uses this instead of loading from file. ### Example ```python modifier = Text2Image({"seed": 42}) # Load default workflow from file await modifier.load_workflow() # Or provide custom workflow custom = { "3": {"inputs": {"seed": 0}, "class_type": "KSampler"}, "4": {"inputs": {"ckpt_name": "model.ckpt"}, "class_type": "CheckpointLoaderSimple"} } await modifier.load_workflow(custom) ``` ``` -------------------------------- ### Get Queue Info API Request Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/endpoints.md Use this Python script to retrieve and display the current request queue status from the AI Dock API. It prints the number of requests and their IDs. ```python import requests url = "http://localhost:8188/ai-dock/api/queue-info" response = requests.get(url) queue = response.json() print(f"Requests in queue: {len(queue)}") for request_id in queue: print(f" - {request_id}") ``` -------------------------------- ### Load ComfyUI Workflow Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/basemodifier.md Load a ComfyUI workflow either from a default JSON file specified by the WORKFLOW_JSON class attribute or directly from a provided dictionary. If no file is specified, the provided dictionary is used. ```python # Load from default class file await modifier.load_workflow() # Or provide a workflow dict directly custom_workflow = { "3": { "inputs": {"seed": 123, "steps": 20}, "class_type": "KSampler" } } await modifier.load_workflow(custom_workflow) ``` -------------------------------- ### Request Persistence with Request Store Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/architecture.md Demonstrates immediate storage of incoming requests and pending results using asynchronous operations. This is crucial for ensuring request durability, especially when using a Redis cache for persistence. ```python await request_store.set(request_id, payload) await response_store.set(request_id, result_pending) ``` -------------------------------- ### Get Queue Info Endpoint (Fixed) Source: https://github.com/ai-dock/comfyui/blob/main/_autodocs/api-reference/main_fastapi.md This is the corrected version of the `queue_info` endpoint. It correctly accesses the internal queue of `preprocess_queue` to return a list of request IDs waiting for preprocessing, allowing for queue depth monitoring. ```python async def queue_info(): return list(preprocess_queue._queue) # Access internal queue ```