Spaces:
Build error
Build error
Upload huggingFace.js
Browse files- huggingFace.js +102 -0
huggingFace.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import {uploadFile} from "https://cdn.jsdelivr.net/npm/@huggingface/hub@1.0.2/+esm";
|
| 2 |
+
|
| 3 |
+
document.getElementById('submit-upload-hf').addEventListener('click', async () => {
|
| 4 |
+
const repoId = document.getElementById('repo-id-hf').value.trim();
|
| 5 |
+
const accessToken = document.getElementById('access-token-hf').value.trim();
|
| 6 |
+
const submitButton = document.getElementById('submit-upload-hf');
|
| 7 |
+
const loadingSpinner = document.getElementById('loading-spinner-hf'); // Reference to loading spinner
|
| 8 |
+
|
| 9 |
+
if (!repoId || !accessToken) {
|
| 10 |
+
alert("Please enter both a repository name and an access token.");
|
| 11 |
+
return;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
if ((document.querySelectorAll('#survey-area .question')).length == 0) {
|
| 15 |
+
alert("No Content added to Canvas. Please add Components before uploading to Hugging Face");
|
| 16 |
+
return;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// Show loading spinner and disable the submit button
|
| 20 |
+
submitButton.disabled = true;
|
| 21 |
+
loadingSpinner.style.display = "inline-block";
|
| 22 |
+
|
| 23 |
+
try {
|
| 24 |
+
|
| 25 |
+
const surveyPythonCode = buildSurveyPythonCode(); // Get survey Python code
|
| 26 |
+
const requirements = `web3==7.8.0\nrequests==2.31.0\nstreamlit==1.28.2`;
|
| 27 |
+
|
| 28 |
+
const [username, repoName] = repoId.split("/");
|
| 29 |
+
if (!username || !repoName) {
|
| 30 |
+
alert("Invalid repository format. Use 'username/repository'.");
|
| 31 |
+
} else {
|
| 32 |
+
await uploadToHF(repoId, accessToken, surveyPythonCode);
|
| 33 |
+
await uploadToHFReq(repoId, accessToken, requirements);
|
| 34 |
+
await uploadToHFReadMe(repoId, accessToken, requirements);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
// Close the GitHub modal after successful upload
|
| 38 |
+
const hfModal = bootstrap.Modal.getInstance(document.getElementById('hfModal'));
|
| 39 |
+
hfModal.hide();
|
| 40 |
+
} catch (error) {
|
| 41 |
+
console.error("Upload failed:", error);
|
| 42 |
+
alert("Failed to upload. Please check console for details.");
|
| 43 |
+
} finally {
|
| 44 |
+
// Hide loading spinner and re-enable the button after upload completes
|
| 45 |
+
loadingSpinner.style.display = "none";
|
| 46 |
+
submitButton.disabled = false;
|
| 47 |
+
}
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
async function uploadToHF(repoId, token, content) {
|
| 53 |
+
const blob = new Blob([content], { type: 'text/plain' });
|
| 54 |
+
try {
|
| 55 |
+
await uploadFile({
|
| 56 |
+
repo: "spaces/" + repoId,
|
| 57 |
+
accessToken: token,
|
| 58 |
+
// Can work with native File in browsers
|
| 59 |
+
file: {
|
| 60 |
+
path: "app.py",
|
| 61 |
+
content: blob
|
| 62 |
+
}
|
| 63 |
+
});
|
| 64 |
+
alert(`Survey uploaded successfully!`);
|
| 65 |
+
savestatus = true;
|
| 66 |
+
} catch (error) {
|
| 67 |
+
alert("Failed to upload. Make sure your repository exists and your access token is correct");
|
| 68 |
+
console.error("Upload failed:", error);
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
async function uploadToHFReq(repoId, token, content) {
|
| 73 |
+
const blob = new Blob(["web3==7.8.0\nrequests==2.31.0\nstreamlit==1.28.2"], { type: 'text/plain' });
|
| 74 |
+
await uploadFile({
|
| 75 |
+
repo: "spaces/" + repoId,
|
| 76 |
+
accessToken: token,
|
| 77 |
+
// Can work with native File in browsers
|
| 78 |
+
file: {
|
| 79 |
+
path: "requirements.txt",
|
| 80 |
+
content: blob
|
| 81 |
+
}
|
| 82 |
+
});
|
| 83 |
+
savestatus = true;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
async function uploadToHFReadMe(repoId, token, content) {
|
| 87 |
+
const response = await fetch('./images/README.md');
|
| 88 |
+
if (!response.ok) {
|
| 89 |
+
throw new Error('File not found or not accessible.');
|
| 90 |
+
}
|
| 91 |
+
const blob = await response.blob();
|
| 92 |
+
await uploadFile({
|
| 93 |
+
repo: "spaces/" + repoId,
|
| 94 |
+
accessToken: token,
|
| 95 |
+
// Can work with native File in browsers
|
| 96 |
+
file: {
|
| 97 |
+
path: "README.md",
|
| 98 |
+
content: blob
|
| 99 |
+
}
|
| 100 |
+
});
|
| 101 |
+
savestatus = true;
|
| 102 |
+
}
|