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, includingHF_HOMEand potentially any other related variables. UsingAIWORKER_CACHE_HOMEis 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 setHF_HOMEinstead.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53c78bab9bb87b94ac0e7f2b973449db7680fef8b43e3f493884d95d032d757b
|
|
| MD5 |
58cecaaade405868972e7676e7a2fb5c
|
|
| BLAKE2b-256 |
6d1d09554ad7196abf2a99f6cd1831c7966da8239af212a9b88b4dc363da7847
|
Provenance
The following attestation bundles were made for horde_safety-0.3.0.tar.gz:
Publisher:
release.yml on Haidra-Org/horde-safety
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
horde_safety-0.3.0.tar.gz -
Subject digest:
53c78bab9bb87b94ac0e7f2b973449db7680fef8b43e3f493884d95d032d757b - Sigstore transparency entry: 302000300
- Sigstore integration time:
-
Permalink:
Haidra-Org/horde-safety@76e76ab81d6acd70253214387a3934c924e8005b -
Branch / Tag:
refs/heads/releases - Owner: https://github.com/Haidra-Org
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@76e76ab81d6acd70253214387a3934c924e8005b -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5254e144dcf7e469e96e7ef6f18fc060328f6348b8b8139c763187e1032ba711
|
|
| MD5 |
28359847ada8409d0e1d75d4ed30bafa
|
|
| BLAKE2b-256 |
ee50cf687085a07768e542bbf410744784ab5539e8d560d301e2f80611514723
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
horde_safety-0.3.0-py3-none-any.whl -
Subject digest:
5254e144dcf7e469e96e7ef6f18fc060328f6348b8b8139c763187e1032ba711 - Sigstore transparency entry: 302000312
- Sigstore integration time:
-
Permalink:
Haidra-Org/horde-safety@76e76ab81d6acd70253214387a3934c924e8005b -
Branch / Tag:
refs/heads/releases - Owner: https://github.com/Haidra-Org
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@76e76ab81d6acd70253214387a3934c924e8005b -
Trigger Event:
push
-
Statement type: