Skip to main content

clip-interrogator-sfw

This is the SFW Version of the CLIP Interrogator by pharmapsychotic. I removed the NSFW words from flavors.txt by creating clip embeddings for obvious NSFW words and removing the top 20% similar words from flavors.txt resulting flavors-sfw.txt. I also removed some other words that I thought were not appropriate for a general audience. The first part of the interrogator is defined by the BLIB Model, so anything coming from the BLIB model is not modified.

Original README follows...

Want to figure out what a good prompt might be to create new images like an existing one? The CLIP Interrogator is here to get you answers!

Run it!

🆕 Now available as a Stable Diffusion Web UI Extension! 🆕


Run Version 2 on Colab, HuggingFace, and Replicate!

Open In Colab Generic badge Replicate Lambda


Version 1 still available in Colab for comparing different CLIP models

Open In Colab

About

The CLIP Interrogator is a prompt engineering tool that combines OpenAI's CLIP and Salesforce's BLIP to optimize text prompts to match a given image. Use the resulting prompts with text-to-image models like Stable Diffusion on DreamStudio to create cool art!

Using as a library

Create and activate a Python virtual environment

python3 -m venv ci_env
(for linux  ) source ci_env/bin/activate
(for windows) .\ci_env\Scripts\activate

Install with PIP

# install torch with GPU support for example:
pip3 install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu117

# install clip-interrogator
pip install clip-interrogator==0.5.4

# or for very latest WIP with BLIP2 support
#pip install clip-interrogator==0.6.0

You can then use it in your script

from PIL import Image
from clip_interrogator import Config, Interrogator
image = Image.open(image_path).convert('RGB')
ci = Interrogator(Config(clip_model_name="ViT-L-14/openai"))
print(ci.interrogate(image))

CLIP Interrogator uses OpenCLIP which supports many different pretrained CLIP models. For the best prompts for Stable Diffusion 1.X use ViT-L-14/openai for clip_model_name. For Stable Diffusion 2.0 use ViT-H-14/laion2b_s32b_b79k

Configuration

The Config object lets you configure CLIP Interrogator's processing.

  • clip_model_name: which of the OpenCLIP pretrained CLIP models to use
  • cache_path: path where to save precomputed text embeddings
  • download_cache: when True will download the precomputed embeddings from huggingface
  • chunk_size: batch size for CLIP, use smaller for lower VRAM
  • quiet: when True no progress bars or text output will be displayed

On systems with low VRAM you can call config.apply_low_vram_defaults() to reduce the amount of VRAM needed (at the cost of some speed and quality). The default settings use about 6.3GB of VRAM and the low VRAM settings use about 2.7GB.

See the run_cli.py and run_gradio.py for more examples on using Config and Interrogator classes.

Ranking against your own list of terms (requires version 0.6.0)

from clip_interrogator import Config, Interrogator, LabelTable, load_list
from PIL import Image

ci = Interrogator(Config(blip_model_type=None))
image = Image.open(image_path).convert('RGB')
table = LabelTable(load_list('terms.txt'), 'terms', ci)
best_match = table.rank(ci.image_to_features(image), top_count=1)[0]
print(best_match)

Release files for clip-interrogator-sfw 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clip-interrogator-sfw 0.6.1
File Size Uploaded
clip_interrogator_sfw-0.6.1.tar.gz 781.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for clip-interrogator-sfw 0.6.1
File Interpreter ABI Platform
clip_interrogator_sfw-0.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 1.6 MB

Release files / clip_interrogator_sfw-0.6.1.tar.gz

Download URL clip_interrogator_sfw-0.6.1.tar.gz
Size 781.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b11241a09a59b39b8927f219d1e23ccdd515038e3a953f4d2c43c30fd9657c68
BLAKE2b-256 checksum
How to use checksums
7980d56b13487d17c2c51fa1862f38d5a7823687ace4cab428c8ca758d1d59ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.7

Release files / clip_interrogator_sfw-0.6.1-py3-none-any.whl

Download URL clip_interrogator_sfw-0.6.1-py3-none-any.whl
Size 780.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e98254cf0920b37a1b04911002d03f00803588348058b09bb00d17a4e9670412
BLAKE2b-256 checksum
How to use checksums
20fad491e840d8317d960a89de0784494efdc63ec2fb25e1890e552ddb9e66e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.6.1 This release

2 release files

0.6.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page