Inference API in JavaScript

Dear Community,

Good day. How can I use Inference API in pure JavaScript. My goal is to test the ML model on the web by sending API request and getting a response. Is it possible to do it? Can someone show me an example?

Akbar

I would also be interested in this. All help highly appreciated :slightly_smiling_face:

Yes, the Inference API is just using usual HTTP requests, so you can use Python, JavaScript or even direct curl requests to use it. You can find more about the structure of the requests at the API documentation.

Here you can find a Gist showing how to use a table-question-answering model. It’s a good self contained example. Here would be a minimal version for bert. There are multiple mechanisms to make HTTP requests in JavaScript and I’m not an expert in the area, but you get the idea.

const api_url = "https://api-inference.huggingface.co/models/bert-base-uncased";
const pipeline = "table-question-answering/"
const payload = JSON.stringify({
  "query": {"inputs": "The answer to the universe is [MASK]."},
});

// Add your token from https://huggingface.co/settings/token
const body= {
    "headers" = {"Authorization": "Bearer YOUR_TOKEN"},
    "wait_for_model": true,
    "use_gpu": false,
    "method" : "POST",
    "contentType" : "application/json",
    "payload" : payload
  };

var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", api_url, false);
xmlHttp.send(body);
console.log(xmlHttp.responseText);
 
1 Like

Awesome, thanks so much for the reply :slightly_smiling_face:

If it is okay, I have a simple follow up question:
In some of the models like object-detection-task, the sample code uses NodeJs specific syntax and functions, more specifically
import fs from "fs"; and
const data = fs.readFileSync(filename);
There does not seem to be an equivalent of fs.readFileSync in clean/vanilla JavaScript, so how might I use a mode like object-detection-task withouth using NodeJs?
Hope my question makes sense?

I’m not a JS expert but you just need to take a look at what inference API takes as input for that specific task and put it in the request itself (the data part) I don’t know equivalent for vanilla JS of that function, but it’s anything that reads an image for object detection, I guess. (it’s only text in NLP anyway if you’re working on NLP)

1 Like

Okay, thanks @osanseviero and @merve I will give it a try!
And if anyone has a tip for how to send an image as input in vanilla JS I am all ears :ear: