Automated 1111: Including Customized APIs – DZone – Uplaza

In our earlier article, we totally coated the elemental points of an Automated 1111. This included the setup course of, enabling APIs, and different essential concerns for optimum API utilization. On this article, we will likely be discussing the Choices API in depth and including our personal customized APIs in Automated 1111 (1.9.3 model).

Choices API

Choices API is without doubt one of the mostly used APIs for fetching and updating Automated 1111 utility configurations.

The GET sort Choices API /sdapi/v1/choices is principally used for getting all the appliance configs as an inventory. 

The POST sort Choices API is used for overriding the default utility configurations. Check out the instance beneath.

On this instance, we try to override the default mannequin choice utilizing the attribute sd_model_checkpoint. This can unload the beforehand loaded mannequin and cargo the one which we’re passing into the payload. If the operation is profitable, we’ll obtain a null response. If we attempt to load a mannequin that doesn’t exist within the mannequin listing, then we’ll see the RuntimeError.

{
  "error": "RuntimeError",
  "detail": "",
  "body": "",
  "errors": "model 'sd_xl_base_1.0.safetensors [31e35c80fc]' not found"
}

Including Customized APIs

As talked about within the earlier article, the Secure Diffusion Net UI lets you create customized APIs to help your distinctive workflows. Including customized APIs is easy and simple. Comply with the instance beneath to see the way it’s performed:

Instance

On this instance, we will likely be including a brand new /selected_model endpoint to verify the default mannequin chosen for picture technology.

Navigate to webui –> modules –> api folder, edit api.py, and make the next adjustments:

  • Add this line underneath all of the API routes within the api class, __init__ perform, roughly line #247 (may differ for different variations of Automated 1111).
self.add_api_route("/sdapi/v1/selected-model", self.get_selected_model, strategies=["GET"], response_model=fashions.SelectedModelResponse)

 

  • On the finish of the api class and simply above the launch perform, add the implementation of get_selected_model perform.
def get_selected_model(self):
    # Name the choices endpoint to get all of the app configs
    res = requests.get("{URL}/sdapi/v1/options")
    if res is None or res.status_code != 200:
    	return None
    mannequin = {res.json()['sd_model_checkpoint']}
    res_dict = { 
      	'sd_model_checkpoint': mannequin,
    }
    return fashions.SelectedModelResponse(selected_model=res_dict)

This perform internally calls the /sdapi/v1/choices endpoint to retrieve the default configs of the appliance. From this checklist, we filter the response to extract solely the “sd_model_checkpoint” attribute, which we then return as our customized API response. Save the up to date api.py file and proceed to the fashions.py file within the api folder. Make the next adjustments:

  • On the finish of the fashions.py file, add the SelectedModelResponse class with a selected_model attribute of sort dictionary because the response attribute.
class SelectedModelResponse(BaseModel):
    selected_model: dict = Discipline(default=None, title="Model Selected", description="Name of the the selected model")

That’s all: simply restart the appliance and you need to be capable to see the brand new API listed in Swagger docs.

Take a look at the customized endpoint inside the Swagger docs and you need to be capable to see the response one thing just like this (mannequin title may differ):

Conclusion

In conclusion, including customized APIs to the Secure Diffusion Net UI (Automated 1111) considerably enhances its flexibility and utility. By leveraging the Choices API, customers can effectively handle and override default utility configurations to swimsuit their particular wants. 

We additionally offered a step-by-step information for making a customized API endpoint, illustrating how customers can conveniently improve the performance of their Secure Diffusion setup. As we proceed to discover and innovate with Automated 1111, these customizations empower customers to tailor their workflows exactly, optimizing efficiency and increasing the capabilities of the appliance. 

Hope you discovered one thing helpful on this article. See you quickly in our subsequent article. Completely happy studying!

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version