Search code examples
javascriptreactjsclarifai

How could i implement Demographic Clarify.ai model on project built on react.js?


I've tried to build a simple React app using basic Clarifai.FACE_DETECT_MODEL, but now I wanna change it to more advanced "Demographic", maybe somebody knows how to di it? I know that I have to change clarify model but idk exactly how to do it

 onButtonClick = () =>{
    this.setState({imageUrl: this.state.input});
    app.modelsw
      .predict(
        Clarifai.FACE_DETECT_MODEL,
        this.state.input)
      .then(response =>this.displayFaceBox(this.calculateFaceLocation(response)))
      .catch(err => console.log("OOOOOOPS fix me!!!!"));}````



Solution

  • the demographics now only support requests from the backend. This is the nodejs request.

    const {ClarifaiStub} = require("clarifai-nodejs-grpc");
    const grpc = require("@grpc/grpc-js");
    
    const metadata = new grpc.Metadata();
    metadata.set("authorization", "{My key}");
    const stub = ClarifaiStub.json()
    
    stub.PostWorkflowResults(
        {
            workflow_id: "Demographics",
            inputs: [
                {data: {image: {url: "https://static.independent.co.uk/s3fs-public/thumbnails/image/2015/06/06/15/Chris-Pratt.jpg"}}}
            ]
        },
        metadata,
        (err, response) => {
            if(response){
                console.log(response.results[0].outputs[2].data.regions[0].data.concepts)
            } else {
                console.log(err)
            }
        }
    )