Spaces:
Running
Running
Commit
·
d823e4a
1
Parent(s):
c2a8871
Update index.html
Browse files- index.html +43 -33
index.html
CHANGED
@@ -30,7 +30,7 @@
|
|
30 |
<div id="error"></div>
|
31 |
|
32 |
<script type="module">
|
33 |
-
import { createRepo, commit
|
34 |
|
35 |
const uploadButton = document.getElementById('uploadButton');
|
36 |
uploadButton.addEventListener('click', uploadFiles);
|
@@ -50,45 +50,55 @@
|
|
50 |
errorDiv.textContent = ''; // clear previous errors
|
51 |
|
52 |
if (files.length > 0) {
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
for (let i = 0; i < files.length; i++) {
|
61 |
const file = files[i];
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
const reader = new FileReader();
|
65 |
-
reader.readAsArrayBuffer(file);
|
66 |
-
reader.onload = async () => {
|
67 |
-
const arrayBuffer = reader.result;
|
68 |
-
const uint8Array = new Uint8Array(arrayBuffer);
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
|
82 |
-
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
};
|
92 |
}
|
93 |
|
94 |
messageDiv.textContent = 'All files uploaded successfully';
|
|
|
30 |
<div id="error"></div>
|
31 |
|
32 |
<script type="module">
|
33 |
+
import { createRepo, commit } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm";
|
34 |
|
35 |
const uploadButton = document.getElementById('uploadButton');
|
36 |
uploadButton.addEventListener('click', uploadFiles);
|
|
|
50 |
errorDiv.textContent = ''; // clear previous errors
|
51 |
|
52 |
if (files.length > 0) {
|
53 |
+
try {
|
54 |
+
// Attempt to create the repo
|
55 |
+
await createRepo({
|
56 |
+
repo: REPO_ID,
|
57 |
+
credentials: { accessToken: HF_ACCESS_TOKEN },
|
58 |
+
});
|
59 |
+
} catch (error) {
|
60 |
+
// If the repo already exists (a 409 conflict error), we simply log and continue
|
61 |
+
if (error.message.includes('409')) {
|
62 |
+
console.log('Repository already exists, proceeding to upload files');
|
63 |
+
} else {
|
64 |
+
console.error('Error creating repository', error);
|
65 |
+
errorDiv.textContent = 'Error creating repository';
|
66 |
+
return; // stop if other errors occur during repository creation
|
67 |
+
}
|
68 |
+
}
|
69 |
|
70 |
for (let i = 0; i < files.length; i++) {
|
71 |
const file = files[i];
|
72 |
+
const arrayBuffer = await new Promise((resolve, reject) => {
|
73 |
+
const reader = new FileReader();
|
74 |
+
reader.readAsArrayBuffer(file);
|
75 |
+
reader.onload = () => resolve(reader.result);
|
76 |
+
reader.onerror = reject;
|
77 |
+
});
|
78 |
|
79 |
+
const uint8Array = new Uint8Array(arrayBuffer);
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
+
try {
|
82 |
+
// upload file
|
83 |
+
await commit({
|
84 |
+
repo: REPO_ID,
|
85 |
+
credentials: { accessToken: HF_ACCESS_TOKEN },
|
86 |
+
message: `committing file: ${file.name}`,
|
87 |
+
files: [{
|
88 |
+
path: file.name,
|
89 |
+
content: uint8Array,
|
90 |
+
}],
|
91 |
+
});
|
92 |
|
93 |
+
console.log(`File ${file.name} uploaded successfully`);
|
94 |
|
95 |
+
// update progress bar
|
96 |
+
progressBar.value = ((i + 1) / files.length) * 100;
|
97 |
+
} catch (error) {
|
98 |
+
console.error('Error uploading file', error);
|
99 |
+
errorDiv.textContent = 'Error uploading file';
|
100 |
+
return; // stop uploading further files on error
|
101 |
+
}
|
|
|
102 |
}
|
103 |
|
104 |
messageDiv.textContent = 'All files uploaded successfully';
|