Stable Diffusion Inference

In this guide, we will look at how to run inferencing from Diffute and the Stable Diffusion models which it supports.

Basic requirements

Inferencing requires 4 things: an API key, an organization_id, a project_id within that organization and a webhook URL which Diffute uses to call back with results of inferencing.

Registering webhooks

Registering a webhook is quite simple. When sending an inferencing request, simply pass a developer-defined secret token along with a publicly accessible URL that supports HTTP POST with a JSON body. Diffute will callback with a JSON manifest containing matching identifiers based upon the name used for each prompt in the request.

The developer-defined secret token will be sent as a bearer token in an Authentication header in the HTTP POST callback so that you can verify that the authenticity of the callback.

Sending inferencing requests and parsing payloads received through a webhook

Diffute is optimized for sending many prompts at a time rather than one prompt per request. Your costs will be lower and, on average, faster if you batch your prompts together. In order to match responses from large batches of prompts, we allow you to name each prompt with string that makes sense for your application to match when receiving the payload. You may choose to use a unique identifier as the name that can then be associated with images generated for a given prompt.

Example request with x-api-key header

  curl --request PUT \
  --url https://api.diffute.com/v1/project/<organization_id>/<project_id>/media/inference \
  --header 'Content-Type: application/json' \
  --header 'x-api-key: long-api-key/default' \
  --data '{
	"prompts": [
  {
    "name": "Ansel_Adams",
    "prompt": "A series of black and white photographs capturing the beauty and grandeur of Yosemite National Park",
    "num_images": 3,
    "height": 1024,
    "width": 768,
    "style": "photography in the style of Ansel Adams"
  },
  {
    "name": "Henri_Rousseau",
    "prompt": "A painting of a lush jungle scene with a variety of exotic plants and animals",
    "num_images": 3,
    "height": 1024,
    "width": 768,
    "style": "painting in the style of Henri Rousseau"
  },
  {
    "name": "Rachel_Ruysch",
    "prompt": "Still life painting featuring a variety of flowers and plants found in a garden",
    "num_images": 3,
    "height": 1024,
    "width": 768,
    "style": "painting in the style of Rachel Ruysch"
  }
	],
  "webhook" : "https://example.com/customer/12345",
  "secret" : "4f986e48-0129-480e-996a-728ea57c020c",
  "pretrained_model_name" : "stabilityai/stable-diffusion-xl-base-1.0",
  "name" : "Houston"
}'

In the example above, multiple prompts were sent in a single request. Each individial prompt in prompts has a unique key called name while the overall request also has a name key used to identify the entire request. It may be convenient to use a customer id or some other identifier.

Using this approach allows you to receive payloads with unique identifiers that your application expects.

Responses to Inference Requests

The payload received by your application from inferencing will contain the name used in the request.

This is an abridged example of the payload received:

Example webhook payload

{
  "name": "Houston",
  "images":
  {
    "Ansel_Adams": ["https://url_to_media1", "https://url_to_media2", "https://url_to_media3"],
    "Henri_Rousseau": ["https://url_to_media4", "https://url_to_media5", "https://url_to_media6"],
    "Rachel_Ruysch": ["https://url_to_media7", "https://url_to_media8", "https://url_to_media9"]
  },
  "job_id": 1234567890
}

Inference

  • Name
    name
    Type
    Description

    A string representing the name of the overall request.

  • Name
    pretrained_model_name
    Type
    Description

    An optional string representing a pretrained model name.

  • Name
    prompts
    Type
    Description

    A list of prompts.

  • Name
    webhook
    Type
    Description

    An optional URL (strictly HTTPS) representing the webhook.

  • Name
    secret
    Type
    Description

    An optional string representing a secret.

Inference payload

{
  "name" : "Houston",
  "pretrained_model_name" : "stabilityai/stable-diffusion-xl-base-1.0",
  "prompts": [
  {
    "name": "Ansel_Adams",
    "prompt": "A black and white photograph capturing the beauty and grandeur of Yosemite National Park",
    "num_images": 3,
    "height": 1024,
    "width": 768,
    "style": "photography in the style of Ansel Adams"
  },
  {
    "name": "Henri_Rousseau",
    "prompt": "A painting of a lush jungle scene with a variety of exotic plants and animals",
    "num_images": 3,
    "height": 1024,
    "width": 768,
    "style": "painting in the style of Henri Rousseau"
  },
  {
    "name": "Rachel_Ruysch",
    "prompt": "Still life painting featuring a variety of flowers and plants found in a garden",
    "num_images": 3,
    "height": 1024,
    "width": 768,
    "style": "painting in the style of Rachel Ruysch"
  }
  ],
  "webhook" : "https://example.com/customer/12345",
  "secret" : "4f986e48-0129-480e-996a-728ea57c020c"
}