This repo has initialized a fastapi server with multiple ComfyUI workflow tasks.
-
Navigate to your
ComfyUI/custom_nodes
directory -
Clone this repo
git clone https://github.com/Thatmakes11/ComfyUI-to-PyTorch-functions.git
-
Move
.py
files in examples to theComfyUI
directoryAfter cloning and moving, your
ComfyUI
directory should look like this:/comfy /comfy_extras /custom_nodes --/ComfyUI-to-Python-Extension /input /models /output /script_examples /web .gitignore LICENSE README.md comfyui_screenshot.png clothes_swap.py # file from examples cuda_mollac.py execution.py extra_model_paths.yaml.example fastapi_server.py # file from examples folder_paths.py latent_preview.py main.py nodes.py node_manager.py # file from examples path_manager.py # file from examples remove_background.py # file from examples requirements.txt server.py
-
Navigate to the
ComfyUI-to-Python-Extension
folder and install requirementspip install -r requirements.txt
-
Load up your favorite workflows in ComfyUI, then click the newly enabled
Save (API Format)
button under Queue Prompt -
Run the script with optional arguments:
python comfyui_to_pytorch_function.py --input_file "workflow_api.json" --output_file my_workflow.py
-
After running
comfyui_to_pytorch_function.py
, a new.py
file will be created in the current working directory. If you made no changes, look forworkflow_api.py
. -
Now you have a function that execute PyTorch process.
To start the FastAPI server, run:
uvicorn fastapi_server:app --host 0.0.0.0 --port 8080
or
python fastapi_server.py
Endpoint: /predict/image_generation
Method: POST
Request Body:
{ "task_type": "string", "task_id": "string", "params": "dict" }
Current supported tasks are ClothesSwap and Rmbg, if you wish to add your own tasks (Workflow to Pytorch), check for line 15 and line 52-58 in fastapi_server.py
.
Also, your customed torch functions are required to return a dict:
{ "status": "success or failed", "output_image": "Tensor or None", "message": "error or completed" }
Response:
{ "status": "success or failed", "output_path": "/path/to/generated/image_{timestamp}.png or None", "message": "error or completed" }
The result image will be saved in the "output_path".