JavaScript Example for inference API

Hi,

Is there a JavaScript example for using inference API - 🤗 Accelerated Inference API — Api inference documentation

Hi @hgarg ,

Currently we don’t provide documentation for JS (or TS).

You could probably use simple fetch:

const HF_API_TOKEN = "api_xxxx";
const model = "XXX"

const data = {inputs:"Something here"};

const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {headers: {"Authorization": `Bearer ${HF_API_TOKEN}`}, method: "POST", data:JSON.stringify(data)});
const data = await response.json()

should work.
Cheers,
Nicolas

1 Like

thanks @Narsil

I am facing these weird problems while calling the API using JavaScript. Example…
Model - EleutherAI/gpt-neo-2.7B
Input - { inputs: 'cat jumps over the mouse ’ }
Output -
[
{
generated_text: “import React from ‘react’;\n” +
“import { get } from ‘lodash’;\n” +
“import { getText, getLanguage } from ‘…/…/…/…/…/utils/utils’;\n” +
“import { Component } from '…/…/…/components”
}
]

Model - facebook/bart-large-cnn
input -
{
inputs: ‘The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.’
}
output -
[
{
summary_text: ‘CNN.com will feature iReporter.com in a iReporters gallery next week. Please send photos of your pets to iReport.com. i-Reporters. i-Photography.com is a team of journalists at the U.S. Embassy in Washington, DC.’
}
]

This happens every time, no matter the input. The same thing works fine when calling the API from python.

1 Like

Same here! I get the “CNN.com will feature…” response for any input. Same outcome using sshleifer/distilbart-cnn-12-6 too. Only JS, not Py.

1 Like

Hi, we added NodeJS to the documentation so you can check out working examples:

Does that work better ? I think my example had an error.

2 Likes

Yes, this returns the correct summary! Thanks for your help. Is there any way to specify the min_length of the summary with the API? Detailed parameters — Api inference documentation appears there’s just three unrelated options.

You can use it.

It’s an unofficial parameter because it’s defined in tokens which are hard to use properly when you manipulate string. But before we have string oriented parameters you can use this one !

Cheers,
Nicolas

1 Like