Enhance Image Safety Checks with the Stable Diffusion Safety Checker Actions

24 Apr 2025
Enhance Image Safety Checks with the Stable Diffusion Safety Checker Actions

In today's digital landscape, ensuring content safety is paramount, especially when dealing with user-generated images. The Stable Diffusion Safety Checker actions provide developers with powerful tools to identify potentially inappropriate content in images quickly and accurately. Leveraging the capabilities of the Stable Diffusion Safety Checker model, these pre-built actions simplify the process of safeguarding your applications against NSFW (Not Safe For Work) content.

Prerequisites

Before diving into the integration of the Cognitive Actions, ensure you have the following:

  • An API key for the Cognitive Actions platform. This key will authenticate your requests to the service.
  • A basic understanding of making HTTP requests in your preferred programming language.

Authentication typically involves passing your API key in the request headers to securely access the Cognitive Actions.

Cognitive Actions Overview

Identify NSFW Images

The Identify NSFW Images action leverages the Stable Diffusion Safety Checker model to detect NSFW content in images. This action is categorized under image-analysis and is essential for applications that need to maintain a safe user experience by filtering inappropriate images.

Input

The input for this action requires a single field:

  • image (string): A URI pointing to the input image. This field is mandatory and must be a valid URL.

Example Input:

{
  "image": "https://replicate.delivery/pbxt/JoWCSlm2UHp4ZniPXIoXFDF2ew8xijFJ3uG6hr6YoPzd4pJq/out%200.png"
}

Output

Upon successful execution, the action returns a status indicating whether the image is safe or not. The typical output for this action is:

  • Output: "safe" or "unsafe" (the latter would indicate the presence of NSFW content).

Conceptual Usage Example (Python)

Here’s a conceptual Python code snippet demonstrating how you might call the Identify NSFW Images action using a hypothetical Cognitive Actions API endpoint:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"  # Hypothetical endpoint

action_id = "1a533023-8a36-4da6-9324-228d44ce1299"  # Action ID for Identify NSFW Images

# Construct the input payload based on the action's requirements
payload = {
    "image": "https://replicate.delivery/pbxt/JoWCSlm2UHp4ZniPXIoXFDF2ew8xijFJ3uG6hr6YoPzd4pJq/out%200.png"
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload}  # Hypothetical structure
    )
    response.raise_for_status()  # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body: {e.response.text}")

In this code snippet, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key, and the endpoint URL is illustrative. The action_id is set to the ID for the Identify NSFW Images action, and the input payload is constructed based on the required schema.

Conclusion

Integrating the Stable Diffusion Safety Checker actions into your applications can significantly enhance content safety and user experience. By utilizing the Identify NSFW Images action, developers can quickly and effectively filter out inappropriate content, safeguarding their platforms. As you explore these capabilities, consider other potential use cases and how they can be tailored to fit your application's needs. Happy coding!