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

clarifai-nodejs-grpc

v10.10.1

Published

The official Clarifai Node.js gRPC client

Downloads

8,176

Readme

image

Clarifai Node.js gRPC Client

This is the official Clarifai gRPC Node.js client for interacting with our powerful recognition API. Clarifai provides a platform for data scientists, developers, researchers and enterprises to master the entire artificial intelligence lifecycle. Gather valuable business insights from images, video and text using computer vision and natural language processing.

  • Try the Clarifai demo at: https://clarifai.com/demo
  • Sign up for a free account at: https://portal.clarifai.com/signup
  • Read the documentation at: https://docs.clarifai.com/

npm Build

Installation

npm install clarifai-nodejs-grpc

Versioning

This library doesn't use semantic versioning. The first two version numbers (X.Y out of X.Y.Z) follow the API (backend) versioning, and whenever the API gets updated, this library follows it.

The third version number (Z out of X.Y.Z) is used by this library for any independent releases of library-specific improvements and bug fixes.

Getting started

There are two approaches to using this library: the dynamic and the static. The former has been around for a longer time, but latter provides type annotations via TypeScript declaration files which improves the IDE auto-completion experience to be more developer-friendly. Both approaches provide the exact same API capabilities.

The dynamic approach

Construct the Clarifai stub, which contains all the methods available in the Clarifai API, and the Metadata object that's used to authenticate:

const {ClarifaiStub, grpc} = require("clarifai-nodejs-grpc");

const stub = ClarifaiStub.grpc();

const metadata = new grpc.Metadata();
metadata.set("authorization", "Key YOUR_CLARIFAI_API_KEY");

Predict concepts in an image:

stub.PostModelOutputs(
    {
        // This is the model ID of a publicly available General model. You may use any other public or custom model ID.
        model_id: "aaa03c23b3724a16a56b629203edc62c",
        inputs: [{data: {image: {url: "https://samples.clarifai.com/dog2.jpeg"}}}]
    },
    metadata,
    (err, response) => {
        if (err) {
            console.log("Error: " + err);
            return;
        }

        if (response.status.code !== 10000) {
            console.log("Received failed status: " + response.status.description + "\n" + response.status.details);
            return;
        }

        console.log("Predicted concepts, with confidence values:")
        for (const c of response.outputs[0].data.concepts) {
            console.log(c.name + ": " + c.value);
        }
    }
);

See more in the Clarifai API Guide docs. Also see the integration tests.

Note: Do not require the grpc library directly via const grpc = require("@grpc/grpc-js");. This produces authentication issues (via grpc.Metadata) whenever any other co-installed libraries have the @grpc/grpc-js dependency (of a different version). Instead, require grpc as shown above.

The static approach

Create the V2Client object with which you access all the Clarifai API functionality, and the Metadata object that's used to authenticate:

const {grpc} = require("clarifai-nodejs-grpc");
const service = require("clarifai-nodejs-grpc/proto/clarifai/api/service_pb");
const resources = require("clarifai-nodejs-grpc/proto/clarifai/api/resources_pb");
const {StatusCode} = require("clarifai-nodejs-grpc/proto/clarifai/api/status/status_code_pb");
const {V2Client} = require("clarifai-nodejs-grpc/proto/clarifai/api/service_grpc_pb");

const clarifai = new V2Client("api.clarifai.com", grpc.ChannelCredentials.createSsl());

const metadata = new grpc.Metadata();
metadata.set("authorization", "Key YOUR_CLARIFAI_API_KEY");

Predict concepts in an image:

const request = new service.PostModelOutputsRequest();
// This is the model ID of a publicly available General model. You may use any other public or custom model ID.
request.setModelId("aaa03c23b3724a16a56b629203edc62c");
request.addInputs(
    new resources.Input()
        .setData(
            new resources.Data()
                .setImage(
                    new resources.Image()
                        .setUrl("https://samples.clarifai.com/dog2.jpeg")
                )
        )
)

clarifai.postModelOutputs(
    request,
    metadata,
    (error, response) => {
        if (error) {
            throw error;
        }

        if (response.getStatus().getCode() !== StatusCode.SUCCESS) {
            throw "Error: " + response.getStatus();
        }

        console.log("Predicted concepts, with confidence values:")
        for (const concept of response.getOutputsList()[0].getData().getConceptsList()) {
            console.log(concept.getName() + " " + concept.getValue());
        }
    }
)

See more in the Clarifai API Guide docs. Also see the integration tests.

Note: Currently, the NodeJS gRPC code examples in the Clarifai documentation show only the dynamic approach. These code examples can easily be translated to the static approach, since the structure is the same for both of them. The difference is only in the syntax.