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
2022-01-01T12:02:17Z
I would also be interested in this. All help highly appreciated
2022-01-01T17:44:59Z
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 atthe API documentation.Hereyou can find a Gist showing how to use atable-question-answeringmodel. It’s a good self contained example. Here would be a minimal version forbert. 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);
2022-01-01T21:34:43Z
Awesome, thanks so much for the replyIf it is okay, I have a simple follow up question:In some of the models likeobject-detection-task, the sample code uses NodeJs specific syntax and functions, more specificallyimport fs from "fs";andconst data = fs.readFileSync(filename);There does not seem to be an equivalent offs.readFileSyncin clean/vanilla JavaScript, so how might I use a mode likeobject-detection-taskwithouth using NodeJs?Hope my question makes sense?
2022-01-02T05:52:50Z
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)
2022-01-03T12:37:28Z
Okay, thanks@osansevieroand@merveI 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