> For the complete documentation index, see [llms.txt](https://incubiq.gitbook.io/opensourceais/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://incubiq.gitbook.io/opensourceais/apis/ais/txt2img-and-img2img/ai_diffusion.md).

# ai\_diffusion

An authenticated client can post a request to Stable Diffusion v1.4

Stable Diffusion comes with two flavors to produce images:

* TXT2IMG: a text to image generation AI. Input text and a few additional parameters.
* IMG2IMG: an image to image generation AI. Input a guiding image (full URI of the image), additional text, and a few additional parameters

{% embed url="<https://en.wikipedia.org/wiki/Stable_Diffusion>" %}
Check more details about Stable Diffusion and its parameters on wikipedia
{% endembed %}

## Get the JSON config file for this AI

<mark style="color:blue;">`GET`</mark> `https://opensourceais.com/api/v1/public/config/diffusion`

You should not need to call this endpoint. This is just a FYI to understand all input parameters sent to the AI.

{% tabs %}
{% tab title="200: OK The full JSON config file" %}

```json
{
    "data": {
        "engine": "diffusion",
        "version": "1.4.0",
        "name": "Stable Diffusion v1.4",
        "description": "Text to Image",
        "isFree": false,
        "requiresGPU": true,
        "aParam": [
            {
                "in": "word",
                "out": "--prompt",
                "isMandatory": true,
                "type": "string",
                "default": null,
                "ui": {
                    "title": "Input (text and/or image)",
                    "placeholder": "eg: gold coin from rome 100 BC, full size",
                    "widget": "uiEdit",
                    "column": 1
                }
            },
            {
                "in": "url_upload",
                "out": "-filename",
                "isMandatory": false,
                "type": "string",
                "default": null,
                "ui": {
                    "widget": "uiSelectPicture",
                    "column": 1
                }
            },
            {
                "in": "steps",
                "out": "--ddim_steps",
                "isMandatory": true,
                "type": "int",
                "default": 20,
                "freeAccess": {
                    "$max": 100
                },
                "ui": {
                    "title": "Step Count",
                    "tooltip": "",
                    "min": 1,
                    "max": 200,
                    "unit": "",
                    "widget": "uiRangeSlider",
                    "column": 1
                }
            },
            {
                "in": "guidance",
                "out": "--scale",
                "isMandatory": true,
                "type": "float",
                "default": 7.5,
                "ui": {
                    "title": "Guidance scale",
                    "tooltip": "",
                    "displayAsPercent": true,
                    "unit": "",
                    "min": 1,
                    "max": 12,
                    "widget": "uiRangeSlider",
                    "column": 1,
                    "conditional": {
                        "$null": "url_upload"
                    }
                }
            },
            {
                "in": "strength",
                "out": "--strength",
                "isMandatory": true,
                "type": "float",
                "default": 0.5,
                "ui": {
                    "title": "Strength",
                    "tooltip": "",
                    "displayAsPercent": true,
                    "unit": "",
                    "min": 0,
                    "max": 1,
                    "widget": "uiRangeSlider",
                    "column": 1,
                    "conditional": {
                        "$exist": "url_upload"
                    }
                }
            },
            {
                "in": "width",
                "out": "--W",
                "isMandatory": true,
                "type": "int",
                "default": 512,
                "accept": {
                    "$in": [
                        320,
                        384,
                        448,
                        512,
                        640,
                        768
                    ],
                    "display": [
                        "320x320",
                        "384x384",
                        "448x448",
                        "512x512",
                        "640x640",
                        "768x768"
                    ]
                },
                "freeAccess": {
                    "$max": 512
                },
                "ui": {
                    "title": "Output resolution",
                    "unit": "px",
                    "widget": "uiMultiToggle",
                    "autofill": [
                        {
                            "in": "height",
                            "out": "--H"
                        }
                    ],
                    "column": 2
                }
            },
            {
                "in": "height",
                "out": "--H",
                "isMandatory": true,
                "type": "int",
                "default": 512,
                "accept": {
                    "$in": [
                        320,
                        384,
                        448,
                        512,
                        640,
                        768
                    ]
                },
                "freeAccess": {
                    "$max": 512
                },
                "ui": {
                    "unit": "px"
                }
            },
            {
                "in": "cimg",
                "out": "--n_samples",
                "isMandatory": true,
                "type": "int",
                "default": 1,
                "accept": {
                    "$in": [
                        1,
                        2,
                        3,
                        4
                    ],
                    "display": [
                        "1",
                        "2",
                        "3",
                        "4"
                    ]
                },
                "ui": {
                    "title": "Number of images to generate",
                    "widget": "uiMultiToggle",
                    "column": 2
                }
            },
            {
                "in": "saveinterval",
                "out": "--se",
                "isMandatory": true,
                "type": "int",
                "default": 2,
                "freeAccess": {
                    "$max": 20
                },
                "ui": {
                    "title": "Save every",
                    "tooltip": "",
                    "min": 1,
                    "max": 20,
                    "unit": "",
                    "widget": "uiRangeSlider",
                    "column": 2
                }
            },
            {
                "in": "seed",
                "out": "--seed",
                "isMandatory": true,
                "type": "function",
                "default": "$random"
            },
            {
                "in": "model",
                "out": "--ckpt",
                "isMandatory": true,
                "type": "string",
                "default": "sd-v1-4.ckpt"
            },
            {
                "in": "output",
                "out": "--output",
                "isMandatory": true,
                "type": "function",
                "default": "$output_uid.ext"
            }
        ]
    }
}
```

{% endtab %}
{% endtabs %}

## Post a request to Stable Diffusion via OpenSourceAIs

<mark style="color:green;">`POST`</mark> `https://opensourceais.com/api/v1/private/client/ai/diffusion`

#### Request Body

| Name                                   | Type   | Description                                                                                                                                                                                                  |
| -------------------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| width                                  | Number | The width of the output image. Defaults to 320                                                                                                                                                               |
| height                                 | Number | The height of the output image. Defaults to 320                                                                                                                                                              |
| steps                                  | Number | The number of steps requested for generating the output image. High number of steps cost more GPU compute. Defaults to 20. Should not exceed 200.                                                            |
| seed                                   | Number | A random number between 1 and 1,000,000 used by the TXT2IMG generation. Same seed and exact same params delivers same output. Defaults to 1. Should send random values for better experience.                |
| word<mark style="color:red;">\*</mark> | String | A text input to tell the AI what to generate.                                                                                                                                                                |
| url\_upload                            | String | A well formed URI starting with https\://... and accessing a PNG or JPG image. Note that if the image is not of the width and height size passed in parameters, it will be automatically cropped or resized. |
| strength                               | Float  | A float between 0 and 1, corresponding to a % of strength applied for the IMG2IMG generation. Defaults to 0.75.                                                                                              |
| guidance                               | Float  | A float between 0.1 and 10, corresponding to the guidance scale applied for the TXT2IMG generation. Defaults to 7.5.                                                                                         |
| cimg                                   | Number | The number of images to produce. Defaults to 1. Note that more images to produce will cost proportionally more in GPU compute.                                                                               |

{% tabs %}
{% tab title="201: Created Confirmation that the request was processed" %}

```json
{
    "data": {
        "uid": 1678361974000,
        "created_at": "2023-03-09T11:39:44.000Z"
    }
}
```

{% endtab %}

{% tab title="400: Bad Request  This error should not happen unless authentication is tampered with" %}

```json
{
     data: null,
     status: 400,
     statusText: "A client token is required"
}
```

{% endtab %}

{% tab title="409: Conflict Could not add request to backlog" %}

```json
{
     data: null,
     status: 409,
     statusText: "Could not add to backlog"
}

```

{% endtab %}
{% endtabs %}
