Skip to main content

Stable Diffusion 2

Navigate through the Stochastic Platform in the Model API side tab and select Stable Diffusion 2 model card.

Model card

You can now choose to use the model either by our Playground or API. You can choose the method by selecting the corresponding tab.

Use the Playground

In this page you can find a text box in which you can insert any prompt you would like the model to generate a image from. On the right side panel you can personalize supported hyperparameter values based on your preference. Finally press the Generate button to trigger a request. The output image will be shown in the central panel.

  • Inference steps: Results are better the more steps you use, however the more steps, the longer the generation takes.
  • Guidance scale: Recommended between 7 - 8.5, forces the generation to better match the prompt potentially at the cost of image quality or diversity (classifier-free guidance)
  • Seed: Can be set for reproducible sampling

Std1 Playground

Stochastic-x model API usage

To use the model API in your application there are two main steps

info

To use the model API you have to have a Stochastic account. Sign up for a free account.

In this step we have to submit a inference request to the ApiUrl to get the responseUrl and the queuePosition. Specification of request and response are mentioned below.

  • Request

    • Method : POST

    • Header : In the request header add a property called apiKey . (Get the apiKey form the Stochastic Platform)

    Use tab

    • Body : The request body can contain the following properties:

      • prompt: Required, the prompt for text generation or can be an Array of Strings
      • params: Required, Params for generation

      Here is an example for the request body:

      {
      "prompt": "A step by step recipe to make bolognese pasta:",
      "params": {
      "max_length": 256,
      "min_length": 32,
      "top_k": 4,
      "penalty_alpha": 0.6
      }
      }
  • Response

    If the request is successful you will receive the responseUrl and the queuePosition

    {
    "success": true,
    "data": {
    "id": "6389ce23460c900d80fa2290",
    "responseUrl": "https://api.stochastic.ai/v1/modelApi/inference/6389ce23460c900d80fa2290",
    "queuePosition": "0"
    }
    }

Python example

Below you can find an example request with Python to get the generated images. Don't forget to add your API key in the example.

import requests
import time

response_step1 = requests.post(
url="https://api-dev.stochastic.ai/v1/modelApi/submit/stable-diffusion-2",
headers={
"apiKey": "your API key"
},
json={
"prompt": "A turtle – swimming underwater, – expressionist painting.",
"params": {
"img_height": 512,
"img_width": 512,
"num_inference_steps": 50,
"guidance_scale": 7.5,
"seed": None
}
}
)

response_step1.raise_for_status()
data_step1 = response_step1.json()["data"]

img_generated = False

while not img_generated:
response_step2 = requests.get(
data_step1["responseUrl"],
headers={
"apiKey": "your API key"
}
)
response_step2.raise_for_status()

data_step2 = response_step2.json()["data"]

generated_images = data_step2.get("generatedImages")

img_generated = generated_images is not None
time.sleep(5)

print(generated_images[0].get("presigned_url"))
Output
https://model-apis.s3.amazonaws.com/stable-diffusion/c99a276d-23b8-488d-aeaf-b732d50b056d.png?AWSAccessKeyId=ASIAQR6N5NB6CCFSLSPT&Signature=TWxdbfqvc%2Fj42izEHpQFRicnlzI%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEKj%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJHMEUCIQC0xUVVmfEK6OmcvsiPnWGzyJ2f3rsgDhLMQJEbSkkgxQIgJRWFmzl8eWox0Bv4UTJuTpsUNQ8zC2Uxi7tyF14FFRMqzQQIQRABGgwwMzg1NDk2NzgyMDQiDJsOm7qM%2FFGz13iqySqqBEja2ihYgqchNJnKYSwe%2FPPUdyAqOjHjlveXYbULwoUowCvRswxsencRz0avNKjyqZeuF6kCHkG%2BOrTbHvxmdtT3DVfchTVswh6gPIlhjd%2F4D6DpBQRF05LsWc0%2B%2BiuViAsTWnrL6Y4Ki8OakTeEtsmVnLfWo6KYcKKNh9JRSAoGifJ8EpxtgBUP1o3UtdeZwDDpBRC7HAy%2BNQM0ts5fwwVTb38Ai9RVNyCICuN3P%2FgFBjNGY%2BWC71ChHGJtAADoXVoEOdeMEECQcquwT%2BYOJdwS29TtivZMfgFkAf5QK1J%2BDxJ9HNKkd%2BfQHZLO7H0Uuakw%2BXRqc4cExdpZkVhZmjCWZNgffnwXdFRNm2YF%2FgceBqLlFWdR38tKKNlBncLWuMkdcu5B%2FXslSipqxsLIjZ9GJOHgYUZnAQeQHcfPGhLWDVicbGxpEC4izqRPdeQmLRlVmT2T8bIPBUcht60I4pALEpiQP3abxRd4yEZ3Os3%2FWZs%2Fe2UB8XPY%2BhbrGWzcKU3EAgMb8v%2FvSpW%2Fanb4Rd0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