Skip to content

Containerized self-hosted REST API for vision classification, utilizing Hugging Face transformers.

License

Notifications You must be signed in to change notification settings

doppeltilde/image_video_classification

Repository files navigation

Image & Video Classification for NSFW and SFW media.

Stack:

Installation

CPU Support: Use the latest tag.

services:
  image_video_classification:
    image: ghcr.io/doppeltilde/image_video_classification:latest
    ports:
      - "8000:8000"
    volumes:
      - models:/root/.cache/huggingface/hub:rw
    environment:
      - DEFAULT_MODEL_NAME
      - BATCH_SIZE
      - ACCESS_TOKEN
      - DEFAULT_SCORE
      - USE_API_KEYS
      - API_KEYS
    restart: unless-stopped

volumes:
  models:

NVIDIA GPU Support: Use the latest-cuda tag.

services:
  image_video_classification_cuda:
    image: ghcr.io/doppeltilde/image_video_classification:latest-cuda
    ports:
      - "8000:8000"
    volumes:
      - models:/root/.cache/huggingface/hub:rw
    environment:
      - DEFAULT_MODEL_NAME
      - BATCH_SIZE
      - ACCESS_TOKEN
      - DEFAULT_SCORE
      - USE_API_KEYS
      - API_KEYS
    restart: unless-stopped
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [ gpu ]

volumes:
  models:
  • Create a .env file and set the preferred values.
DEFAULT_MODEL_NAME=Falconsai/nsfw_image_detection
BATCH_SIZE=5
DEFAULT_SCORE=0.7
ACCESS_TOKEN=

# False == Public Access
# True == Access Only with API Key
USE_API_KEYS=False

# Comma seperated api keys
API_KEYS=abc,123,xyz

Models

Any model designed for image classification and compatible with huggingface transformers should work.

Examples

Usage

Note

Please be aware that the initial classification process may require some time, as the model is being downloaded.

Tip

Interactive API documentation can be found at: http://localhost:8000/docs

Simple Classification

Image Classification

POST request to the /api/image-classification endpoint.

curl -X 'POST' \
  'http://localhost:8000/api/image-classification?model_name=Falconsai/nsfw_image_detection' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F '[email protected]'

Multi Image Classification

POST request to the /api/multi-image-classification endpoint.

curl -X 'POST' \
  'http://localhost:8000/api/multi-image-classification?model_name=Falconsai/nsfw_image_detection' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F '[email protected];type=image/png' \
  -F '[email protected];type=image/jpeg' \
  -F '[email protected];type=image/gif'

Query Classification

You can utilize query parameters, if the standard classification isn't sufficient or you need a more nuanced response.

Optional parameters:

  • model_names List[str]

  • labels List[str]

  • score float

  • return_on_first_matching_label bool (default: false)

  • fast_mode bool (default: false)

  • skip_frames_percentage int (default: 5)

Single Image with Query Parameters

POST request to the /api/image-query-classification endpoint.

curl -X 'POST' \
  'http://localhost:8000/api/image-query-classification?model_names=Falconsai/nsfw_image_detection' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F '[email protected];type=image/jpeg'
Multi Image with Query Parameters

POST request to the /api/multi-image-query-classification endpoint.

curl -X 'POST' \
  'http://localhost:8000/api/multi-image-query-classification?model_names=Falconsai/nsfw_image_detection' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F '[email protected];type=image/png' \
  -F '[email protected];type=image/jpeg' \
  -F '[email protected];type=image/gif'

Video Classification

Video with Query Parameters

POST request to the /api/video-classification endpoint.

curl -X 'POST' \
  'http://localhost:8000/api/video-classification?model_names=Falconsai/nsfw_image_detection' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F '[email protected];type=video/mp4'

Tip

You can find code examples in the examples folder.


Notice: This project was initally created to be used in-house, as such the development is first and foremost aligned with the internal requirements.