Skip to content

Latest commit

Β 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ComfyUI Worker for Serverless

A synchronous ComfyUI wrapper designed for serverless environments, enabling automatic scaling and efficient resource management on platforms like Novita.ai.

🎯 Project Overview

This project transforms the traditional asynchronous ComfyUI workflow into a synchronous HTTP API service, making it compatible with serverless platforms that require request-response patterns for automatic scaling.

Key Features

  • πŸ”„ Synchronous API: Converts ComfyUI's async HTTP API to sync HTTP REST endpoints
  • πŸ“ˆ Auto-scaling Ready: Compatible with serverless platforms for automatic resource scaling
  • ⚑ High Performance: Optimized for fast startup and efficient processing
  • 🐳 Docker Ready: Pre-built containers for easy deployment
  • πŸ” Health Monitoring: Built-in health checks and monitoring endpoints
  • πŸ“Š Metrics & Logging: Comprehensive logging and execution time tracking

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client App    β”‚   HTTP/REST    β”‚        Serverless Platform      β”‚
β”‚                 β”‚ ──────────────►│         (Novita.ai)             β”‚
β”‚ β€’ Web Apps      β”‚                β”‚                                 β”‚
β”‚ β€’ Mobile Apps   β”‚                β”‚ β€’ Load Balancing                β”‚
β”‚ β€’ API Clients   β”‚                β”‚ β€’ Auto-scaling                  β”‚
β”‚                 β”‚                β”‚ β€’ Health Monitoring             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                β”‚ β€’ Request Routing               β”‚
                                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                 β”‚ Container
                                                 β”‚ Orchestration
                                                 β–Ό
                                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                   β”‚         ComfyUI Worker            β”‚   HTTP/REST    β”‚    ComfyUI      β”‚
                                   β”‚                                   β”‚ ──────────────►│    Server       β”‚
                                   β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚                β”‚                 β”‚
                                   β”‚  β”‚      FastAPI Handler        β”‚  β”‚                β”‚ β€’ Workflow      β”‚
                                   β”‚  β”‚   (server.py)               β”‚  β”‚                β”‚   Engine        β”‚
                                   β”‚  β”‚                             β”‚  β”‚                β”‚ β€’ Model         β”‚
                                   β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚  β”‚                β”‚   Runtime       β”‚
                                   β”‚  β”‚  β”‚   ComfyUI Client    β”‚    β”‚  β”‚                β”‚ β€’ Queue         β”‚
                                   β”‚  β”‚  β”‚   (client.py)       β”‚ ───┼──┼─────────────────   Manager       β”‚
                                   β”‚  β”‚  β”‚                     β”‚    β”‚  β”‚                β”‚                 β”‚
                                   β”‚  β”‚  β”‚ β€’ HTTP Polling      β”‚    β”‚  β”‚                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚  β”‚  β”‚ β€’ Task Monitoring   β”‚    β”‚  β”‚
                                   β”‚  β”‚  β”‚ β€’ Result Collection β”‚    β”‚  β”‚
                                   β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚  β”‚
                                   β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
                                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Python 3.8+
  • Docker (for containerized deployment)
  • ComfyUI server (or use our integrated setup)

Local Development

  1. Clone the repository
git clone https://github.com/garysng/worker-comfyui.git
cd worker-comfyui
  1. Install dependencies
pip install -r requirements.txt
  1. Start the handler server
python server.py
  1. Test the API
curl -X POST "https://0.0.0.0:18188/prompt_sync" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": {
      "3": {
        "class_type": "KSampler",
        "inputs": {
          "cfg": 8,
          "denoise": 1,
          "latent_image": ["5", 0],
          "model": ["4", 0],
          "negative": ["7", 0],
          "positive": ["6", 0],
          "sampler_name": "euler",
          "scheduler": "normal",
          "seed": 1047100913978686,
          "steps": 60
        }
      },
      "4": {
        "class_type": "CheckpointLoaderSimple",
        "inputs": {
          "ckpt_name": "flux1-dev-fp8.safetensors"
        }
      },
      "5": {
        "class_type": "EmptyLatentImage",
        "inputs": {
          "batch_size": 1,
          "height": 512,
          "width": 512
        }
      },
      "6": {
        "class_type": "CLIPTextEncode",
        "inputs": {
          "clip": ["4", 1],
          "text": "pretty girl"
        }
      },
      "7": {
        "class_type": "CLIPTextEncode",
        "inputs": {
          "clip": ["4", 1],
          "text": "text, watermark"
        }
      },
      "8": {
        "class_type": "VAEDecode",
        "inputs": {
          "samples": ["3", 0],
          "vae": ["4", 2]
        }
      },
      "9": {
        "class_type": "SaveImage",
        "inputs": {
          "filename_prefix": "ComfyUI",
          "images": ["8", 0]
        }
      }
    }
  }'

Docker Deployment

# Build the image
docker build -f deploy/Dockerfile -t comfyui-worker .

# Run the container
docker run -p 18188:18188 comfyui-worker

πŸ“‘ API Endpoints

Endpoint Method Description
/prompt_sync POST Submit prompt and wait for completion (synchronous)
/prompt POST Submit prompt asynchronously (ComfyUI compatible)
/queue GET Get current queue status
/history GET Get execution history
/health GET Health check endpoint
/interrupt POST Interrupt current execution

Response Format

{
  "prompt_id": "12345-abcde-67890",
  "status": "success", 
  "execution_time": 15.23,
  "outputs": {
    "9": {
      "images": [
        {
          "filename": "ComfyUI_00001_.png",
          "subfolder": "",
          "type": "output"
        }
      ]
    }
  },
  "node_errors": {}
}

πŸ”§ Configuration

Environment variables for customization:

# Server configuration
SERVER_HOST=0.0.0.0
SERVER_PORT=18188

# ComfyUI configuration  
COMFYUI_SERVER_ADDRESS=127.0.0.1:8188
DEFAULT_TIMEOUT=300

# Logging
LOG_LEVEL=INFO
LOG_OUTPUT=console  # console, file, both

πŸ“š Documentation

Core Documentationw

Tools & Examples

πŸ› οΈ Use Cases

Serverless Image Generation

Perfect for applications that need on-demand image generation with automatic scaling:

  • API Services: REST API for image generation
  • Web Applications: Direct integration with web frontends
  • Batch Processing: Scale up for bulk image generation
  • Cost Optimization: Pay only for actual usage

πŸ” Monitoring & Debugging

Health Checks

curl http://localhost:18188/health

Queue Monitoring

curl http://localhost:18188/queue

Execution History

curl http://localhost:18188/history

πŸ§ͺ Testing & Benchmarking

Performance Benchmarking

python benchmark.py --url "http://localhost:18188" --requests 10 --concurrent 2

πŸ› Troubleshooting

Common Issues

  1. Server not responding

    • Check if ComfyUI server is running
    • Verify port configuration
  2. Out of memory errors

    • Reduce concurrent requests
    • Use smaller models
    • Increase container memory limits
  3. Timeout errors

    • Increase DEFAULT_TIMEOUT value
    • Optimize prompt complexity
    • Check server resources

Debug Mode

Enable debug logging:

export LOG_LEVEL=DEBUG
python server.py

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • ComfyUI - The underlying image generation framework
  • Novita.ai - Serverless platform support

Ready to scale your ComfyUI workflows? Deploy on serverless today! πŸš€

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages