Automatic 1111: Adding Custom APIs
Following up on the last article that covered the fundamentals of Automatic 1111, let's take a look at the Options API and adding custom APIs.
Join the DZone community and get the full member experience.
Join For FreeIn our previous article, we thoroughly covered the fundamental aspects of an Automatic 1111. This included the setup process, enabling APIs, and other crucial considerations for optimal API usage. In this article, we will be discussing the Options API in depth and adding our own custom APIs in Automatic 1111 (1.9.3 version).
Options API
Options API is one of the most commonly used APIs for fetching and updating Automatic 1111 application configurations.
The GET type Options API /sdapi/v1/options
is mainly used for getting all the application configs as a list.
The POST type Options API is used for overriding the default application configurations. Take a look at the example below.
In this example, we are trying to override the default model selection using the attribute sd_model_checkpoint
. This will unload the previously loaded model and load the one that we are passing into the payload. If the operation is successful, we will receive a null
response. If we try to load a model that does not exist in the model directory, then we'll see the RuntimeError
.
{
"error": "RuntimeError",
"detail": "",
"body": "",
"errors": "model 'sd_xl_base_1.0.safetensors [31e35c80fc]' not found"
}
Adding Custom APIs
As mentioned in the previous article, the Stable Diffusion Web UI enables you to create custom APIs to support your unique workflows. Adding custom APIs is straightforward and easy. Follow the example below to see how it's done:
Example
In this example, we will be adding a new /selected_model
endpoint to check the default model selected for image generation.
Navigate to webui --> modules --> api folder, edit api.py, and make the following changes:
- Add this line under all the API routes in the
api
class,__init__
function, approximately line #247 (might differ for other versions of Automatic 1111).
self.add_api_route("/sdapi/v1/selected-model", self.get_selected_model, methods=["GET"], response_model=models.SelectedModelResponse)
- At the end of the
api
class and just above thelaunch
function, add the implementation ofget_selected_model
function.
def get_selected_model(self):
# Call the options endpoint to get all the app configs
res = requests.get("{URL}/sdapi/v1/options")
if res is None or res.status_code != 200:
return None
model = {res.json()['sd_model_checkpoint']}
res_dict = {
'sd_model_checkpoint': model,
}
return models.SelectedModelResponse(selected_model=res_dict)
This function internally calls the /sdapi/v1/options
endpoint to retrieve the default configs of the application. From this list, we filter the response to extract only the "sd_model_checkpoint" attribute, which we then return as our custom API response. Save the updated api.py file and proceed to the models.py file in the api folder. Make the following changes:
- At the end of the models.py file, add the
SelectedModelResponse
class with aselected_model
attribute of type dictionary as the response attribute.
class SelectedModelResponse(BaseModel):
selected_model: dict = Field(default=None, title="Model Selected", description="Name of the the selected model")
That is all: just restart the application and you should be able to see the new API listed in Swagger docs.
Test the custom endpoint within the Swagger docs and you should be able to see the response something similar to this (model name might differ):
Conclusion
In conclusion, adding custom APIs to the Stable Diffusion Web UI (Automatic 1111) significantly enhances its flexibility and utility. By leveraging the Options API, users can efficiently manage and override default application configurations to suit their specific needs.
We also provided a step-by-step guide for creating a custom API endpoint, illustrating how users can conveniently enhance the functionality of their Stable Diffusion setup. As we continue to explore and innovate with Automatic 1111, these customizations empower users to tailor their workflows precisely, optimizing performance and expanding the capabilities of the application.
Hope you found something useful in this article. See you soon in our next article. Happy learning!
Opinions expressed by DZone contributors are their own.
Comments