DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Cloud Backends for Frontend Developers
  • Build an AI Chatroom With ChatGPT and ZK by Asking It How!
  • Maximizing Efficiency With the Test Automation Pyramid: Leveraging API Tests for Optimal Results
  • The Embed Is the Product: Rethinking AI Distribution

Trending

  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • Migrate a Hardcoded LangGraph Agent to LaunchDarkly AI Configs in 20 Minutes
  • Why DDoS Protection Is an Architectural Decision for Developers
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Automatic 1111: Adding Custom APIs

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.

By 
Tharakarama Reddy Yernapalli Sreenivasulu user avatar
Tharakarama Reddy Yernapalli Sreenivasulu
·
Pavan Vemuri user avatar
Pavan Vemuri
DZone Core CORE ·
Prince Bose user avatar
Prince Bose
·
Jul. 31, 24 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
4.5K Views

Join the DZone community and get the full member experience.

Join For Free

In 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. 

GET type Options AP

The POST type Options API is used for overriding the default application configurations. Take a look at the example below.

POST type Options API

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.

JSON
 
{
  "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).
Python
 
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 the launch function, add the implementation of get_selected_model function.
Python
 
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 a selected_model attribute of type dictionary as the response attribute.
Python
 
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.

Get selected model

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):

Custom endpoint test result

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!

API UI application Attribute (computing) workflow

Opinions expressed by DZone contributors are their own.

Related

  • Cloud Backends for Frontend Developers
  • Build an AI Chatroom With ChatGPT and ZK by Asking It How!
  • Maximizing Efficiency With the Test Automation Pyramid: Leveraging API Tests for Optimal Results
  • The Embed Is the Product: Rethinking AI Distribution

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook