# infinitered/nsfwjs architecture

> Architecture diagram of the GitHub repository infinitered/nsfwjs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/infinitered/nsfwjs
- Repository: https://github.com/infinitered/nsfwjs

## Overview

NSFWJS is a JavaScript image-classification library: callers load a bundled or hosted TensorFlow.js model, submit an image for classification, and receive ranked probabilities across five content classes. The graph separates the public entry point, model registry and definitions, and model data. The README documents hosted models, caching, and multiple backends; source excerpts show the load, inference, and classification implementation. Some internal loading details are omitted from the excerpts, so only documented or directly evidenced relationships are drawn.

Main entry points: [`src/default_models.ts`](https://github.com/infinitered/nsfwjs/blob/master/src/default_models.ts) (Default registry), [`src/core.ts`](https://github.com/infinitered/nsfwjs/blob/master/src/core.ts) (Load and classify), [`src/index.ts`](https://github.com/infinitered/nsfwjs/blob/master/src/index.ts) (Public load API), [`models`](https://github.com/infinitered/nsfwjs/tree/master/models) (Bundled model files)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Library API"]
  node_entry["Public load API<br/>[index.ts]"]
end

subgraph group_models["Model Catalog"]
  node_registry["Default registry<br/>[default_models.ts]"]
  node_mobiledef["MobileNetV2 definition<br/>[mobilenet_v2.ts]"]
  node_middef["MobileNetV2Mid definition"]
  node_inceptiondef["InceptionV3 definition<br/>[inception_v3.ts]"]
  node_mobileimports["MobileNetV2 loaders<br/>[mobilenet_v2.ts]"]
  node_midimports["MobileNetV2Mid loaders"]
  node_inceptionimports["InceptionV3 loaders<br/>[inception_v3.ts]"]
  node_modeldata["Bundled model files"]
end

subgraph group_runtime["Inference Runtime"]
  node_core["Load and classify<br/>[core.ts]"]
  node_classes["Class labels<br/>[nsfw_classes.ts]"]
end

node_caller(("Application"))
node_tfjs["TensorFlow.js"]
node_hostedmodel["Hosted model files"]

node_caller -->|"calls load"| node_entry
node_entry -->|"selects defaults"| node_registry
node_entry -->|"delegates load"| node_core
node_registry -->|"registers"| node_mobiledef
node_registry -->|"registers"| node_middef
node_registry -->|"registers"| node_inceptiondef
node_mobiledef -->|"uses loaders"| node_mobileimports
node_middef -->|"uses loaders"| node_midimports
node_inceptiondef -->|"uses loaders"| node_inceptionimports
node_mobileimports -->|"loads shards"| node_modeldata
node_midimports -->|"loads shards"| node_modeldata
node_inceptionimports -->|"loads shards"| node_modeldata
node_core -->|"loads and predicts"| node_tfjs
node_core -->|"uses labels"| node_classes
node_caller -->|"classifies image"| node_core
node_caller -->|"provides model URL"| node_hostedmodel

click node_entry "https://github.com/infinitered/nsfwjs/blob/master/src/index.ts"
click node_core "https://github.com/infinitered/nsfwjs/blob/master/src/core.ts"
click node_classes "https://github.com/infinitered/nsfwjs/blob/master/src/nsfw_classes.ts"
click node_registry "https://github.com/infinitered/nsfwjs/blob/master/src/default_models.ts"
click node_mobiledef "https://github.com/infinitered/nsfwjs/blob/master/src/models/mobilenet_v2.ts"
click node_middef "https://github.com/infinitered/nsfwjs/blob/master/src/models/mobilenet_v2_mid.ts"
click node_inceptiondef "https://github.com/infinitered/nsfwjs/blob/master/src/models/inception_v3.ts"
click node_mobileimports "https://github.com/infinitered/nsfwjs/blob/master/src/model_imports/mobilenet_v2.ts"
click node_midimports "https://github.com/infinitered/nsfwjs/blob/master/src/model_imports/mobilenet_v2_mid.ts"
click node_inceptionimports "https://github.com/infinitered/nsfwjs/blob/master/src/model_imports/inception_v3.ts"
click node_modeldata "https://github.com/infinitered/nsfwjs/tree/master/models"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry toneBlue
class node_registry,node_mobiledef,node_middef,node_inceptiondef,node_mobileimports,node_midimports,node_inceptionimports,node_modeldata toneAmber
class node_core,node_classes toneMint
class node_caller,node_tfjs,node_hostedmodel toneIndigo
```

## Components

### Library API

- [Public load API](https://github.com/infinitered/nsfwjs/blob/master/src/index.ts): `src/index.ts`

### Model Catalog

- [Default registry](https://github.com/infinitered/nsfwjs/blob/master/src/default_models.ts): `src/default_models.ts`
- [MobileNetV2 definition](https://github.com/infinitered/nsfwjs/blob/master/src/models/mobilenet_v2.ts): `src/models/mobilenet_v2.ts`
- [MobileNetV2Mid definition](https://github.com/infinitered/nsfwjs/blob/master/src/models/mobilenet_v2_mid.ts): `src/models/mobilenet_v2_mid.ts`
- [InceptionV3 definition](https://github.com/infinitered/nsfwjs/blob/master/src/models/inception_v3.ts): `src/models/inception_v3.ts`
- [MobileNetV2 loaders](https://github.com/infinitered/nsfwjs/blob/master/src/model_imports/mobilenet_v2.ts): `src/model_imports/mobilenet_v2.ts`
- [MobileNetV2Mid loaders](https://github.com/infinitered/nsfwjs/blob/master/src/model_imports/mobilenet_v2_mid.ts): `src/model_imports/mobilenet_v2_mid.ts`
- [InceptionV3 loaders](https://github.com/infinitered/nsfwjs/blob/master/src/model_imports/inception_v3.ts): `src/model_imports/inception_v3.ts`
- [Bundled model files](https://github.com/infinitered/nsfwjs/tree/master/models): `models`

### Inference Runtime

- [Load and classify](https://github.com/infinitered/nsfwjs/blob/master/src/core.ts): `src/core.ts`
- [Class labels](https://github.com/infinitered/nsfwjs/blob/master/src/nsfw_classes.ts): `src/nsfw_classes.ts`

### Other components

- **Application**
- **TensorFlow.js**
- **Hosted model files**

## Connections

- Application → Public load API: calls load (evidence: `README.md`)
- Public load API → Default registry: selects defaults (evidence: `src/index.ts`)
- Public load API → Load and classify: delegates load (evidence: `src/index.ts`)
- Default registry → MobileNetV2 definition: registers (evidence: `src/default_models.ts`)
- Default registry → MobileNetV2Mid definition: registers (evidence: `src/default_models.ts`)
- Default registry → InceptionV3 definition: registers (evidence: `src/default_models.ts`)
- MobileNetV2 definition → MobileNetV2 loaders: uses loaders (evidence: `src/models/mobilenet_v2.ts`)
- MobileNetV2Mid definition → MobileNetV2Mid loaders: uses loaders (evidence: `src/models/mobilenet_v2_mid.ts`)
- InceptionV3 definition → InceptionV3 loaders: uses loaders (evidence: `src/models/inception_v3.ts`)
- MobileNetV2 loaders → Bundled model files: loads shards (evidence: `src/model_imports/mobilenet_v2.ts`)
- MobileNetV2Mid loaders → Bundled model files: loads shards (evidence: `src/model_imports/mobilenet_v2_mid.ts`)
- InceptionV3 loaders → Bundled model files: loads shards (evidence: `src/model_imports/inception_v3.ts`)
- Load and classify → TensorFlow.js: loads and predicts (evidence: `README.md`)
- Load and classify → Class labels: uses labels (evidence: `src/core.ts`)
- Application → Load and classify: classifies image (evidence: `README.md`)
- Application → Hosted model files: provides model URL (evidence: `README.md`)
