Skip to main content

nonebot-plugin-nsfw

NoneBot Plugin NSFW

此插件是一个集成于 NoneBot 的、基于深度神经网络的 群聊 NSFW 图片检测插件,带有 撤回、警告、禁言 等功能。可选择使用 Safety CheckerNSFW Model 模型。

注意: 目前插件仅在 matcha 完成测试,只能保证 OneBot V11 兼容。

模型对比

Safety Checker NSFW Model(默认) nsfw_image_detection
训练时间 2022 2020 2023 年 10 月
适用情景 R15+ R18+ R15+
框架 PyTorch Tensorflow PyTorch
模型大小 600MB (fp16) ~10MB (fp16) 300MB
内存占用 1.2GB 极小
执行时间 (CPU Ryzen 7 7840H) 0.5s (10) 0.05s (1) -
执行时间 (GPU 4060 Laptop) 0.15s (4060 laptop) 暂无 -

快速开始

插件默认使用 NSFW Model 轻量模型,默认设备为 CPU。

安装 NoneBot:

pip install nonebot2[fastapi]

安装插件:

pip install nonebot-plugin-nsfw[nsfw-model]

将插件直接导入即可使用。

默认行为:检测群聊消息中的图片并自动撤回 nsfw 图片,发送警告消息,警告次数累计 3 次执行禁言。

机器人是非管理员身份时,则不会进行任何消息检测。

使用 Safety Checker

Safety Checker 由我本人编写,只有 hook diffusers 的版本,暂时没有独立的实现,不过正常使用没问题。也许将来能实现检测强度配置。可参考 https://github.com/iyume/safety-checker/issues/1

Safety Checker 是基于最新最热 CLIP 的 NSFW 图像概念实现。

使用下面命令安装:

pip install nonebot-plugin-nsfw[safety-checker]

此过程会自动下载 torch GPU 版本,大约 2GB。

如果你需要仅 CPU 的 torch,需要提前使用这条命令安装 torch:pip install torch --index-url https://download.pytorch.org/whl/cpu

第一次加载插件时会从 huggingface 下载一个 600 MB 的模型文件,请确保网络连接通畅。(模型缓存由 huggingface_lab 管理,位置在 ~/.cache/huggingface/hub

载入 Safety Checker 需要 至少 1.2 GB 内存/显存。 CPU (Ryzen 7 7840H) 每次调用大约耗时 0.5s,图像大小不影响调用耗时。

使用 NSFW Model(默认)

NSFW Model 是基于 Inception V3 和 MobileNet V2 的传统视觉模型。

nsfwjs 是它的 js 实现,可以进行在线测试。

官方提供了 多种模型下载地址,但是强烈建议使用默认选项也就是 v1.2.0 模型,因为 v1.1.0 的模型对 r18 图像可能存在误判,上面那个在线测试网页用的大概就是 v1.1.0 的模型。

注意: 不同于 pytorch cuda 的一键安装,tensorflow 的 cuda 环境依赖于系统,使用前需要你熟悉 cuda、cuDNN 等概念并完成环境安装。如果你仅用 CPU 那么这是个不错的选择。

使用下面命令安装:

pip install nonebot-plugin-nsfw[nsfw-model]

有可能把 NSFW Model 迁移到 PyTorch / NumPy 吗?
由于两个平台各种函数实现的略微差异,同一个权重文件表现可能不一致。 同理,尝试使用 numpy 去实现也是困难的。 目前似乎没看到可以直接完美实现前向计算的轻量库,如果你找到了可以告诉我。

使用 nsfw_image_detection

这是 2023 年 10 月份出的基于 ViT 的模型,识别率和内存占用上应该都比 safety-checker 要好点。

使用下面命令安装:

pip install nonebot-plugin-nsfw[nsfw_image_detection]

插件配置项

默认值 可选值 说明
nsfw__model "nsfw-model" "safety-checker" / "nsfw-model" / "nsfw_image_detection"
nsfw__device "cpu" "cpu", "cuda", etc.
nsfw__withdraw True True, False 撤回检测到 NSFW 图片的消息
nsfw__nsfw_model_path cwd() / nsfw_mobilenet2_v1.2.0.h5 .h5 or SavedModel path nsfw-model 模型路径,没配置则自动下载
nsfw__warning_capacity 3 非负整数 一天内警告 N 次后禁言,0 不警告,ban=True 直接禁言
nsfw__ban True True, False 是否启用禁言
nsfw__ban_time 1800 正整数 禁言时长,单位为秒数
nsfw__save_image True True, False 是否保存涩涩图片 (Thanks to https://github.com/iyume/nonebot-plugin-nsfw/pull/6)
nsfw__image_save_path cwd() / images 字符串 涩涩图片保存路径

更多配置正在开发中...

欢迎 issue 提出问题和想法。

Download files

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

Source Distribution

nonebot_plugin_nsfw-0.13.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

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

nonebot_plugin_nsfw-0.13-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file nonebot_plugin_nsfw-0.13.tar.gz.

File metadata

  • Download URL: nonebot_plugin_nsfw-0.13.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.20.1 CPython/3.11.9 Windows/10

File hashes

Hashes for nonebot_plugin_nsfw-0.13.tar.gz
Algorithm Hash digest
SHA256 432c127c14168074b5c112e07b7bc2108ad94f96fa2af6bc5c1795d544f03bc7
MD5 ab1a687b3e86b51b8bb33a2d14866f53
BLAKE2b-256 1879940ca924244dc1dedce58c4f58940848b952cd334014e6d9b713003ef172

See more details on using hashes here.

File details

Details for the file nonebot_plugin_nsfw-0.13-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_nsfw-0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 b3cd8153b709c852b0964e2d521a65780e6b5437d873349a5db5d8406cfcd0d2
MD5 9f0195cde67df33161e2b2f702235587
BLAKE2b-256 333a1cf7affee09f1a77ed91723dc15ba28476eebbd6b1fb9f6a49451e9951a2

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.13 This release

2 files

0.12

2 files

0.11

2 files

0.10

2 files

0.9

2 files

0.8

2 files

0.7

2 files

0.6

2 files

0.5

2 files

0.4

2 files

0.3

2 files

0.2

2 files

0.1.0

2 files

Supported by

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