Skip to content

Instantly share code, notes, and snippets.

@feconroses
Created July 26, 2021 17:32
Show Gist options
  • Save feconroses/302474ddd3f3c466dc069ecf16bb09d7 to your computer and use it in GitHub Desktop.
Save feconroses/302474ddd3f3c466dc069ecf16bb09d7 to your computer and use it in GitHub Desktop.
Add Zero-shot classification to Google Sheets using HuggingFace's API
function ANALYZE(input, labels, repo_id="facebook/bart-large-mnli") {
endpoint = "https://api-inference.huggingface.co/models/" + repo_id;
const payload = JSON.stringify({
"inputs": input,
"parameters": {"candidate_labels": labels}});
// Add your token from https://huggingface.co/settings/token
const options = {
"headers": {"Authorization": "Bearer <YOUR HUGGINGFACE API KEY>"},
"wait_for_model": true,
"use_gpu": false,
"method" : "POST",
"contentType" : "application/json",
"payload" : payload,
};
const response = UrlFetchApp.fetch(endpoint, options);
const data = JSON.parse(response.getContentText());
return data['labels'][0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment