Enabling API for stable-diffusion-webui

Hello,

I’m trying to develop an application that uses the API of stable-diffusion (API · AUTOMATIC1111/stable-diffusion-webui Wiki · GitHub) to create images.

My problem is that I need to start the stable-diffusion jetson container with the parameter “–api” in order to enable that API. So far I’ve been unable to change the parameters. In the next image it’s shown the steps I’ve tried to do:

1-Select the docker file in jetson-containers/packages/diffusion/stable-diffusion-webui/Dockerfile
2-Edit the CMD to include the “–api”
3-Start the container “jetson-containers run $(autotag stable-diffusion-webui)”

What do I need to modify in order to start the stable-diffusion-webui container with the “–api” in the args? Any help would be greatly appreciated.

Hi @esteban.gallardo, try starting the container like this:

jetson-containers run \
  --workdir /opt/stable-diffusion-webui \
  $(autotag stable-diffusion-webui) \
    python3 launch.py \
     --data=/data/models/stable-diffusion \
     --enable-insecure-extension-access \
     --xformers \
     --listen \
     --port=7860 \
     --api

Editing the Dockerfile is unnecessary to change the default CMD, you can override it by providing your own when you run the container. Also you would have needed to rebuild the container to pick up any changes in the Dockerfile.

1 Like

Thanks a lot. It worked.

For future programmers who stumble with stable-diffusion api implementation here is the working endpoint:

from flask import Flask, request, jsonify
import base64
import requests

app = Flask(__name__)

@app.route("/ai/image", methods=["POST"])
def image_generation() -> bytes:
        args = request.args
        prompt = request.json
        description = prompt["description"]
        exclude = prompt["exclude"]
        steps = int(prompt["steps"])
        width = int(prompt["width"])
        height = int(prompt["height"])

        # Define the URL and the payload to send.
        url = "http://0.0.0.0:7860"

        payload = {
            "prompt": description,
            "negative_prompt": exclude,
            "steps": steps,
            "width": width,
            "height": height
        }

        # Send said payload to said URL through the API.
        response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
        r = response.json()
        return base64.b64decode(r['images'][0])

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, threaded=False)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.