npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nsfai

v2.0.0

Published

A simplified wrapper around Clarifai's NSFW detection.

Downloads

26

Readme

nsfai Codacy Badge Build Status npm Discord

nsfai banner logo

A simplified wrapper around Clarifai's NSFW detection.

Features

  • Simplified result, so you don't need to deal with complex objects and arrays.
  • Automatic data recognition, whether it's Base64, a Data URL, or a URL.

Example

var NSFAI = require('nsfai');

var nsfai = new NSFAI("YOUR_CLARIFAI_API_KEY_HERE");

function handleResult(result) {
    if (result.sfw) {
        console.log(`This image is A-OK with a confidence of ${result.confidence}.`);
    } else {
        console.log(`This image is NSFW with a confidence of ${result.confidence}.`);
    }
}

function handleError(error) {
    console.error(error);
}

nsfai.predict("https://example.com/example.png").then(handleResult).catch(handleError); // URL
// or //
nsfai.predict("data:image/png;base64,dGhpc2lzbm90YW5pbWFnZQ==").then(handleResult).catch(handleError); // Data URL
// or //
nsfai.predict("dGhpc2lzbm90YW5pbWFnZQ==", handleResult).then(handleResult).catch(handleError); // Base64

Setup

Setting up your Clarifai app

After you've created and logged in your account, hover on your name in the top bar, and click on Applications.

clarifai applications

Click on Create New Application, give your app a name, and set the base workflow to NSFW (optional). The default language doesn't matter.

clarifai create app

After clicking on Create App, head over to the API Keys page in the sidebar.

clarifai api keys

You can already see an API key, and you could use that, but you should create a new API key with limited permissions, so if your key gets leaked, you can just revoke it, and less damage could be done.

Click on Create new API Key, and select your app in the Apps dropdown. Give your key a name, and select the Predict on Public and Custom Models scope. That's all you need.

clarifai create api key

Click on Save Changes, and copy your new and shiny API key.

clarifai api keys new

Setting up your project

First, you'll need to install NSFAI via NPM. Use the -s flag, so it gets saved into your package.json file.

npm install -s nsfai

First, you'll need to require and initialize NSFAI in your code. Use require('nsfai'), and then, you'll need to create a new NSFAI instance. Here's where your API key comes in.

For security reasons, you shouldn't hardcode your API key into your code, because if you upload it to GitHub, or someone gets the code, they can just read out the key. What we recommend, is storing it in your environment variables. You know, that place where PATH, and stuff like that is.

Go ahead and save your API key in an environment variable. Now, when you push the code to GitHub, or upload is somewhere, people can't see it.

After you've securely saved your API key into an environment variable, we can create the NSFAI instance. Use new NSFAI(process.env.YOUR_ENVIRONMENT_VARIABLE_NAME_HERE) for that.

var NSFAI = require('nsfai');

var nsfai = new NSFAI(process.env.MYAPP_CLARIFAI_KEY);

Congratulations! Now you can predict the NSFWness of images. Let's see an example of how to do that!

var NSFAI = require('nsfai');

var nsfai = new NSFAI(process.env.MYAPP_CLARIFAI_KEY);

nsfai.predict("https://example.com/image.png").then(function(result) {
    if (result.sfw) { // If the result is safe for work:
        console.log(`This image is safe for work, with a confidence of ${result.confidence}!`);
    } else { // If the result is not safe for work:
        console.log(`This image is not safe for work, with a confidence of ${result.confidence}!`);
    }
}).catch(function(error) {
    console.error(error); // Print the error to the console.
});