comfy-api-client


Namecomfy-api-client JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryNone
upload_time2024-11-08 08:41:39
maintainerNone
docs_urlNone
authorKristian Klemon
requires_python<4.0,>=3.10
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # comfy-api-client

A Python client for the ComfyUI API, providing:

:zap: Full API coverage  
:zap: Asynchronous execution  
:zap: WebSocket support  
:zap: Workflow templating  
:zap: WebSocket or HTTP-based polling

## Installation

Install the package using `pip`:

```bash
pip install comfy-api-client
```

## Usage

### Create a Client

Use the `create_client` context manager to create a ComfyUI client. This will set up the underlying HTTP client and a WebSocket or HTTP-based state tracker to poll results from the server:

```python
from comfy_api_client import create_client

# Protocol is omitted as the URL may be used for both HTTP and WebSocket requests
comfyui_server = "localhost:8188"

async with create_client(comfyui_server) as client:
    print(await client.get_system_stats())
```

### Submit Workflows

To submit a workflow, read the workflow configuration file and pass it to the client:

```python
from comfy_api_client import utils

workflow = utils.read_json("workflow.json")

async with create_client(comfyui_server) as client:
    prompt = await client.submit_workflow(workflow)

    result = await prompt.future
    image_items = result.output_images
    image = image_items[0].image
```

## Result polling

### HTTP-based polling

By default, the execution state of a prompt is tracked via a WebSocket connection. In cases where a WebSocket connection cannot be established, e.g. if the ComfyUI server is behind a proxy or firewall, HTTP-based polling can be used instead.

Simply provide `start_state_tracker="http"` to the `create_client` function:

```python
async with create_client(comfyui_server, start_state_tracker="http") as client:
    # Submit and await results as before
    prompt = await client.submit_workflow(workflow)
    result = await prompt.future
```

Note, that HTTP polling relies on frequent querying of the prompt status from the ComfyUI server and will thus create more traffic while being less responsive.

### Manual polling

The prompt status and results can also be queried manually. In this case, `start_state_tracker=None` can be passed to `create_client` and `return_future=False` to the `submit_workflow()` method.

```python
import time

async with create_client(comfyui_server, start_state_tracker=None) as client:
    # Submit and await result as before
    prompt = await client.submit_workflow(workflow, return_future=False)

    # Will be None; the status needs to be checked manually
    prompt.future

    # Wait for the prompt to finish
    time.sleep(20)

    result = await client.fetch_results(prompt)

    image_items = result.output_images
    image = image_items[0].image
```

## Tests

Run tests:

```bash
pytest tests
```

This will set up a local ComfyUI instance to test against.

## TODOs

- [ ] Add logging support
- [ ] Improve error handling and messages
- [ ] Implement a synchronous client
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "comfy-api-client",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Kristian Klemon",
    "author_email": "kristian.klemon@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/99/d1/56e737b837d88906e8d25c1ade8d86e8a63c684f0d62dc26e4bc030331eb/comfy_api_client-0.1.0.tar.gz",
    "platform": null,
    "description": "# comfy-api-client\n\nA Python client for the ComfyUI API, providing:\n\n:zap: Full API coverage  \n:zap: Asynchronous execution  \n:zap: WebSocket support  \n:zap: Workflow templating  \n:zap: WebSocket or HTTP-based polling\n\n## Installation\n\nInstall the package using `pip`:\n\n```bash\npip install comfy-api-client\n```\n\n## Usage\n\n### Create a Client\n\nUse the `create_client` context manager to create a ComfyUI client. This will set up the underlying HTTP client and a WebSocket or HTTP-based state tracker to poll results from the server:\n\n```python\nfrom comfy_api_client import create_client\n\n# Protocol is omitted as the URL may be used for both HTTP and WebSocket requests\ncomfyui_server = \"localhost:8188\"\n\nasync with create_client(comfyui_server) as client:\n    print(await client.get_system_stats())\n```\n\n### Submit Workflows\n\nTo submit a workflow, read the workflow configuration file and pass it to the client:\n\n```python\nfrom comfy_api_client import utils\n\nworkflow = utils.read_json(\"workflow.json\")\n\nasync with create_client(comfyui_server) as client:\n    prompt = await client.submit_workflow(workflow)\n\n    result = await prompt.future\n    image_items = result.output_images\n    image = image_items[0].image\n```\n\n## Result polling\n\n### HTTP-based polling\n\nBy default, the execution state of a prompt is tracked via a WebSocket connection. In cases where a WebSocket connection cannot be established, e.g. if the ComfyUI server is behind a proxy or firewall, HTTP-based polling can be used instead.\n\nSimply provide `start_state_tracker=\"http\"` to the `create_client` function:\n\n```python\nasync with create_client(comfyui_server, start_state_tracker=\"http\") as client:\n    # Submit and await results as before\n    prompt = await client.submit_workflow(workflow)\n    result = await prompt.future\n```\n\nNote, that HTTP polling relies on frequent querying of the prompt status from the ComfyUI server and will thus create more traffic while being less responsive.\n\n### Manual polling\n\nThe prompt status and results can also be queried manually. In this case, `start_state_tracker=None` can be passed to `create_client` and `return_future=False` to the `submit_workflow()` method.\n\n```python\nimport time\n\nasync with create_client(comfyui_server, start_state_tracker=None) as client:\n    # Submit and await result as before\n    prompt = await client.submit_workflow(workflow, return_future=False)\n\n    # Will be None; the status needs to be checked manually\n    prompt.future\n\n    # Wait for the prompt to finish\n    time.sleep(20)\n\n    result = await client.fetch_results(prompt)\n\n    image_items = result.output_images\n    image = image_items[0].image\n```\n\n## Tests\n\nRun tests:\n\n```bash\npytest tests\n```\n\nThis will set up a local ComfyUI instance to test against.\n\n## TODOs\n\n- [ ] Add logging support\n- [ ] Improve error handling and messages\n- [ ] Implement a synchronous client",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "0.1.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1ec5c5500819a8c28e610dce0a061fd1a5801f14b3a42eba9cce12e856ae9de8",
                "md5": "cfabf5cafb69209e42d3841a47d16f08",
                "sha256": "01be45a33c90c65930ee1f662b207c1827b0392b1d55b9554d840904727f526e"
            },
            "downloads": -1,
            "filename": "comfy_api_client-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cfabf5cafb69209e42d3841a47d16f08",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 9627,
            "upload_time": "2024-11-08T08:41:38",
            "upload_time_iso_8601": "2024-11-08T08:41:38.258885Z",
            "url": "https://files.pythonhosted.org/packages/1e/c5/c5500819a8c28e610dce0a061fd1a5801f14b3a42eba9cce12e856ae9de8/comfy_api_client-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "99d156e737b837d88906e8d25c1ade8d86e8a63c684f0d62dc26e4bc030331eb",
                "md5": "bdc0b9cc128f2d6402a936f1e2cb2e9c",
                "sha256": "52d2bcc43809c9774751ddb6cda91a662458b5f45c1eed51175c2c3752912d22"
            },
            "downloads": -1,
            "filename": "comfy_api_client-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "bdc0b9cc128f2d6402a936f1e2cb2e9c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 9569,
            "upload_time": "2024-11-08T08:41:39",
            "upload_time_iso_8601": "2024-11-08T08:41:39.407348Z",
            "url": "https://files.pythonhosted.org/packages/99/d1/56e737b837d88906e8d25c1ade8d86e8a63c684f0d62dc26e4bc030331eb/comfy_api_client-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-08 08:41:39",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "comfy-api-client"
}
        
Elapsed time: 0.95231s