File size: 2,008 Bytes
f7f8cfe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
const HF_TOKEN = "hf_"; // Get this from hf.co/settings/tokens (Write access)
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());
// 2. Gzip the JSON and Base64 encode it
const blob = Utilities.newBlob(jsonString, 'application/json', 'data.json');
const gzippedBlob = Utilities.gzip(blob, FILENAME);
const base64Content = Utilities.base64Encode(gzippedBlob.getBytes());
// 3. Prepare the Commit Payload
// Use /api/spaces/REPO_ID/commit/main
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 // Allows us to see the error message if it fails
};
// 4. Send the Commit
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);
}
} |