Skip to main content

Provides some of the tools the AI Horde uses for safety.

Project description

horde-safety

Provides safety features used by the horde, especially to do with image generation.

Environment Variables

This library is made with the default AI Horde worker in mind, and relies on the environment variable AIWORKER_CACHE_HOME to establish isolation of the clip models on disk. If you do not want to rely on a horde specific folder structure, define HF_HOME to define where you'd prefer the models to be. If neither are defined, the default huggingface folder location for the system used, typically ~/.cache, depending on other environnement variables, see the official huggingface docs for more info.

Warning: If you use AIWORKER_CACHE_HOME, other environment variables can and will be overridden, including HF_HOME and potentially any other related variables. Using AIWORKER_CACHE_HOME is explictly opting into the horde isolation scheme, which may not be suitable for other contexts and may lead to duplicate (and very large) models on disk. If you want to use this library outside of the horde, it is recommended to set HF_HOME instead.

Installing

Make sure pytorch is installed, preferably with CUDA/ROCM or other GPU support.

pip install horde_safety

Use

This library currently relies on clip_interrogator. The check_for_csam function requires an instance of clip_interrogator.Interrogator to be passed. You can pass in on yourself, or use the helper function get_interrogator_no_blip (note that calling this function immediately loads the CLIP model). Use the device parameter to choose the device to load to and use for interrogation. If you want to only use the CPU but have CUDA pytorch installed, use get_interrogator_no_blip(device="cpu").

import PIL.Image

from horde_safety.deep_danbooru_model import get_deep_danbooru_model
from horde_safety.interrogate import get_interrogator_no_blip
from horde_safety.nsfw_checker_class import NSFWChecker, NSFWResult

interrogator = get_interrogator_no_blip() # Will trigger a download if not on disk (~1.2 gb)
deep_danbooru_model = get_deep_danbooru_model() # Will trigger a download if not on disk (~614 mb)

nsfw_checker = NSFWChecker(
    interrogator,
    deep_danbooru_model,  # Optional, significantly improves results for anime images
)

image: PIL.Image.Image = PIL.Image.open("image.jpg")
prompt: str | None = None  # if this is an image generation, you can provide the prompt here
model_info: dict | None = None  # if this is an image generation, you can provide the model info here

nsfw_result: NSFWResult | None = nsfw_checker.check_for_nsfw(image, prompt=prompt, model_info=model_info)

if nsfw_result is None:
    print("No NSFW result (Did it fail to load the image?)")
    exit(1)


if nsfw_result.is_anime:
    print("Anime detected!")

if nsfw_result.is_nsfw:
    print("NSFW detected!")

if nsfw_result.is_csam:
    print("CSAM detected!")
    exit(1)

If you reject a job as a horde worker for CSAM, you should report 'state': 'csam' in the generate submit payload.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

horde_safety-0.3.0.tar.gz (72.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

horde_safety-0.3.0-py3-none-any.whl (51.7 kB view details)

Uploaded Python 3

File details

Details for the file horde_safety-0.3.0.tar.gz.

File metadata

  • Download URL: horde_safety-0.3.0.tar.gz
  • Upload date:
  • Size: 72.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for horde_safety-0.3.0.tar.gz
Algorithm Hash digest
SHA256 53c78bab9bb87b94ac0e7f2b973449db7680fef8b43e3f493884d95d032d757b
MD5 58cecaaade405868972e7676e7a2fb5c
BLAKE2b-256 6d1d09554ad7196abf2a99f6cd1831c7966da8239af212a9b88b4dc363da7847

See more details on using hashes here.

Provenance

The following attestation bundles were made for horde_safety-0.3.0.tar.gz:

Publisher: release.yml on Haidra-Org/horde-safety

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file horde_safety-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: horde_safety-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 51.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for horde_safety-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5254e144dcf7e469e96e7ef6f18fc060328f6348b8b8139c763187e1032ba711
MD5 28359847ada8409d0e1d75d4ed30bafa
BLAKE2b-256 ee50cf687085a07768e542bbf410744784ab5539e8d560d301e2f80611514723

See more details on using hashes here.

Provenance

The following attestation bundles were made for horde_safety-0.3.0-py3-none-any.whl:

Publisher: release.yml on Haidra-Org/horde-safety

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page