ComfyUI icon indicating copy to clipboard operation
ComfyUI copied to clipboard

Implement Synchronous API Hooks

Open guill opened this issue 2 years ago • 1 comments

This PR allows ComfyUI to fulfill one of my last remaining uses for automatic1111's web UI -- easy integration with external tools.

The existing websocket-based API is ideal for interactive workflows, but is more complex than necessary for simple procedural uses (particularly those involving languages like C++ with sub-par support for websockets). This PR adds the necessary hooks for a custom node pack to add synchronous APIs.

Of note, this PR (along with the custom nodes at https://github.com/BadCafeCode/apitools-comfyui) allows ComfyUI to act as a drop-in replacement for the Automatic1111 API in tools like:

  1. oobabooga's Text WebUI (with the sd_api_pictures extension)
  2. Blender (with my fork of the dream-textures extension)
  3. GIMP (via a plugin like https://github.com/thndrbrrr/gimp-stable-boy)

New endpoints can also be easily prototyped and used with the apitools nodes.

I've tried to strike a balance here between adding full HTTP API support to the base ComfyUI (which I think goes beyond the intended scope) and making no changes to the base ComfyUI (which would require overriding a lot of core functionality from custom nodes and cause frequent bugs when the overwritten code is changed).

I'll include some of my specific reasoning in inline comments on this PR.

Note that although these requests look synchronous to the client, they do still use async functions behind the scenes and will properly queue up if multiple requests are received at once. Each request will return a result once the associated workflow has completed. Standard browsers will generally wait up to 5 minutes if the request is simply performed via CORS, but HTTP client libraries can just about always be configured to wait longer. (More info on long-polling available here.)

guill avatar Jul 09 '23 01:07 guill

Is where a documents like a1111?

inspire-boy avatar Jul 13 '23 12:07 inspire-boy

I tried it out and it works great! Thanks for this. Really makes ComfyUI a lot more easy to use for custom api workflows. No more injecting my arguments into a giant json document!

mikastamm avatar Jul 27 '23 09:07 mikastamm

+1 would like to see this ability in Comfy

sanguivore-easyco avatar Jul 28 '23 16:07 sanguivore-easyco

This will be awesome for app devs. Pair it with Runpod, and you have your self a fully customizable image generation api!

Robo0890 avatar Jul 29 '23 21:07 Robo0890

+1 for adding this in comfy!

Can you guys tell me how to test it? I understand I need to add both https://github.com/BadCafeCode/apitools-comfyui and https://github.com/BadCafeCode/masquerade-nodes-comfyui repos to the custom_nodes folder. Is that correct?

fidecastro avatar Aug 09 '23 23:08 fidecastro

its awesome!! I really hope it will be in the master

VolanDeVovan avatar Aug 15 '23 00:08 VolanDeVovan

This is fantastic and works great. Thanks! Would it be possible to return the promptId with the sync request to fetch the image when the first request ends? The request response is empty as of now.

lllleow avatar Aug 28 '23 18:08 lllleow

Closing this PR as I'm no longer maintaining it. The apitools-comfyui repository has been updated to work against my Node Expansion PR (https://github.com/comfyanonymous/ComfyUI/pull/931) instead. The lazy evaluation functionality provided by that PR is a better alternative to the hacks I had been doing within the apitools-comfyui repository. (I haven't updated the A1111 analog workflows within there yet though.)

guill avatar Sep 12 '23 03:09 guill