Skip to content

Instantly share code, notes, and snippets.

@nateraw
Created July 20, 2021 18:42
Show Gist options
  • Save nateraw/2a746ee168311eb4d3e64cedc2629f69 to your computer and use it in GitHub Desktop.
Save nateraw/2a746ee168311eb4d3e64cedc2629f69 to your computer and use it in GitHub Desktop.
Add Translation to Google Sheets using HuggingFace's API
function TRANSLATE(text, repo_id="Helsinki-NLP/opus-mt-en-es") {
endpoint = "https://api-inference.huggingface.co/pipeline/translation/" + repo_id;
const payload = JSON.stringify({
"inputs": text
});
// 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[0]['translation_text'];
}
@souldia1
Copy link

35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment