|
|
const HF_TOKEN = "hf_";
|
|
|
const REPO_ID = "you/space";
|
|
|
const FILENAME = "data.json.gz"
|
|
|
|
|
|
function onOpen() {
|
|
|
const ui = SpreadsheetApp.getUi();
|
|
|
ui.createMenu('HF')
|
|
|
.addItem('Deploy', 'pushToHuggingFace')
|
|
|
.addToUi();
|
|
|
}
|
|
|
|
|
|
function pushToHuggingFace() {
|
|
|
const sheet = SpreadsheetApp.getActiveSheet();
|
|
|
const jsonString = JSON.stringify(sheet.getDataRange().getValues());
|
|
|
|
|
|
|
|
|
const blob = Utilities.newBlob(jsonString, 'application/json', 'data.json');
|
|
|
const gzippedBlob = Utilities.gzip(blob, FILENAME);
|
|
|
const base64Content = Utilities.base64Encode(gzippedBlob.getBytes());
|
|
|
|
|
|
|
|
|
|
|
|
const url = `https://huggingface.co/api/spaces/${REPO_ID}/commit/main`;
|
|
|
|
|
|
const payload = {
|
|
|
"summary": `Update ${FILENAME} from Google Sheets`,
|
|
|
"files": [
|
|
|
{
|
|
|
"path": FILENAME,
|
|
|
"content": base64Content,
|
|
|
"encoding": "base64"
|
|
|
}
|
|
|
]
|
|
|
};
|
|
|
|
|
|
const options = {
|
|
|
"method": "POST",
|
|
|
"headers": {
|
|
|
"Authorization": `Bearer ${HF_TOKEN}`,
|
|
|
"Content-Type": "application/json"
|
|
|
},
|
|
|
"payload": JSON.stringify(payload),
|
|
|
"muteHttpExceptions": true
|
|
|
};
|
|
|
|
|
|
|
|
|
try {
|
|
|
const response = UrlFetchApp.fetch(url, options);
|
|
|
const code = response.getResponseCode();
|
|
|
const result = response.getContentText();
|
|
|
|
|
|
if (code === 200 || code === 201) {
|
|
|
Logger.log("Successfully committed: " + result);
|
|
|
SpreadsheetApp.getUi().alert(`β
${REPO_ID} updated!`);
|
|
|
} else {
|
|
|
Logger.log("Failed with code " + code + ": " + result);
|
|
|
SpreadsheetApp.getUi().alert("β Error: " + result);
|
|
|
}
|
|
|
} catch (e) {
|
|
|
Logger.log("Script Error: " + e.toString());
|
|
|
SpreadsheetApp.getUi().alert("β Script Exception: " + e.message);
|
|
|
}
|
|
|
} |