Skip to content

Instantly share code, notes, and snippets.

@bmhaskar
Forked from osanseviero/tapas_demo
Created July 5, 2021 16:13
Show Gist options
  • Save bmhaskar/f4483cf39a2b4247a18bea9a1630fa5c to your computer and use it in GitHub Desktop.
Save bmhaskar/f4483cf39a2b4247a18bea9a1630fa5c to your computer and use it in GitHub Desktop.
/*
* Converts table from [[Header0, Header1, Header2], [Column0Val0, Column1Val0, Column2Val0], ...]
* to {Header0: [ColumnVal0, ...], Header1: [Column1Val0, ...], Header2: [Column2Val0, ...]}
*/
function convertTableToData(table) {
transposed = table[0].map((_, colIndex) => table.map(row => row[colIndex]));
result = {}
for(var i = 0; i < transposed.length; i++) {
header = transposed[i][0]
result[header] = transposed[i].slice(1, 4).map(String);
}
return result;
}
function ANSWER_QUESTION(query, table, repo_id="google/tapas-base-finetuned-wtq") {
const endpoint = "https://api-inference.huggingface.co/pipeline/";
const pipeline = "table-question-answering/"
const payload = JSON.stringify({
"query": query,
"table": convertTableToData(table),
});
// Add your token from https://huggingface.co/settings/token
const options = {
"headers" = {"Authorization": "Bearer YOUR_TOKEN"};
"wait_for_model": true,
"use_gpu": false,
"method" : "POST",
"contentType" : "application/json",
"payload" : payload
};
path = endpoint + pipeline + repo_id
const response = UrlFetchApp.fetch(path, options);
const data = JSON.parse(response.getContentText());
// There is a lot of other useful data here that is useful for
// more complex questions.
return data["cells"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment