Last active
July 7, 2021 15:06
-
-
Save osanseviero/b98dd51d0f7ee593ed114182101381cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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
Thanks ๐ ๐