ComfyUI icon indicating copy to clipboard operation
ComfyUI copied to clipboard

API to convert workflow format to prompt format

Open jamesWalker55 opened this issue 1 year ago • 3 comments

When using the API, it uses a different format than workflow JSON files. This format can be retrieved from EXIF data in saved images, however it can be inconvenient to 1) build a workflow, then 2) generate an image, 3) load the EXIF data from the image, finally 4) use the new prompt in the API.

I would like to suggest an API endpoint that receives a workflow JSON object, converts it to a prompt and returns it. This way, the whole process can be easily automated with a Python script by making a request to that endpoint, then using the returned JSON to make API requests.

jamesWalker55 avatar Aug 04 '23 11:08 jamesWalker55

Click the settings wheel, then "enable dev mode options" in the pop up. This enables a save (API Format) button.

pydn avatar Aug 04 '23 19:08 pydn

This functionnality is required for the sake of simplicity in api consumer.

To use correctly the prompt api we have to send:

  1. client_id: this is not an issue
  2. prompt : the api json
  3. extra_data: { extra_pnginfo: { workflow json }

The third point is required to:

  1. Embed the workflow in the generated images
  2. Some nodes require this information to work

As of today, this mean you have to export the workflow.json and the workflow_api.json, then do the changes in BOTH files and send them to the api.

If a functionality to convert the workflow to workflow_api or even better an option to push the workflow.json to the prompt API exists, it will make the life of the API consumers a lot easier.

guinoise avatar Dec 19 '23 20:12 guinoise

I used this simple, hacky way to get the full prompt for the api (with "extra_data"). I agree there should be a better way to get this with the normal "Save (api_format)" operation.

In ./server.py, line 450+ at the time of this writing

        .
        .
        @routes.post("/prompt")
        async def post_prompt(request):
            print("got prompt")
            resp_code = 200
            out_string = ""
            json_data =  await request.json()
            json_data = self.trigger_on_prompt(json_data)
            with open("last_prompt.json", "w") as f:              # <- dump prompt to file
                json.dump(json_data, f, indent=4)                 #  in local directory

            if "number" in json_data:
            .
            . (etc.)

IntenseSunlight avatar Jan 01 '24 11:01 IntenseSunlight

looks like the graphToPrompt() function is probably the conversion code we're all looking for.

https://github.com/comfyanonymous/ComfyUI/blob/master/web/scripts/app.js#L1857-L1989

dmarx avatar Feb 21 '24 07:02 dmarx

we have a large external application and do a lot of ComfyUI graph generation there and we have to use the normal format. But now we need to execute the workflow of course which is impossible by that internal API format without doing that same conversion on our side. So I suggest a refactoring doing that conversion only on server-side in Python so both formats could be accepted there.

imperator-maximus avatar Mar 09 '24 11:03 imperator-maximus