Spaces:
Running
Running
Commit
•
03e3790
1
Parent(s):
2137dcd
Update index.html
Browse files- index.html +82 -140
index.html
CHANGED
@@ -33,158 +33,100 @@
|
|
33 |
<script type="module">
|
34 |
import { createRepo, uploadFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/hub@0.8.3/+esm";
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
// Create a variable to track the total uploaded size
|
50 |
-
let totalUploaded = 0;
|
51 |
-
|
52 |
-
// Replace the global fetch function
|
53 |
-
let progressMap = new Map();
|
54 |
-
|
55 |
-
fetch = (url, init) => {
|
56 |
-
console.log(url, init)
|
57 |
-
if (init.method === 'PUT') {
|
58 |
-
return new Promise((resolve, reject) => {
|
59 |
-
const xhr = new XMLHttpRequest();
|
60 |
-
|
61 |
-
xhr.open(init.method, url);
|
62 |
-
|
63 |
-
for (let header in init.headers) {
|
64 |
-
xhr.setRequestHeader(header, init.headers[header]);
|
65 |
-
}
|
66 |
-
|
67 |
-
xhr.onload = () => {
|
68 |
-
resolve({
|
69 |
-
ok: xhr.status >= 200 && xhr.status < 300,
|
70 |
-
status: xhr.status,
|
71 |
-
statusText: xhr.statusText,
|
72 |
-
text: () => Promise.resolve(xhr.responseText),
|
73 |
-
json: () => Promise.resolve(JSON.parse(xhr.responseText)),
|
74 |
-
headers: {
|
75 |
-
get: (header) => xhr.getResponseHeader(header)
|
76 |
-
}
|
77 |
-
});
|
78 |
-
};
|
79 |
-
|
80 |
-
|
81 |
-
xhr.onerror = () => reject(new TypeError('Network request failed'));
|
82 |
-
xhr.ontimeout = () => reject(new TypeError('Network request failed due to timeout'));
|
83 |
-
|
84 |
-
xhr.upload.onprogress = (event) => {
|
85 |
-
if (event.lengthComputable) {
|
86 |
-
progressMap.set(url, event.loaded);
|
87 |
-
totalUploaded = Array.from(progressMap.values()).reduce((a, b) => a + b, 0);
|
88 |
-
|
89 |
-
// Calculate speed
|
90 |
-
const elapsedSeconds = (Date.now() - startTime) / 1000;
|
91 |
-
const speedInMBps = totalUploaded / elapsedSeconds / 1024 / 1024;
|
92 |
-
|
93 |
-
// Update progress bar and speed display
|
94 |
-
const progressBar = document.getElementById('progressBar');
|
95 |
-
progressBar.value = totalUploaded;
|
96 |
-
|
97 |
-
// Update processing message
|
98 |
-
const processingMessage = document.getElementById('processingMessage');
|
99 |
-
if (totalUploaded === 0) {
|
100 |
-
processingMessage.textContent = 'Preparing your upload';
|
101 |
-
} else if (totalUploaded === progressBar.max) {
|
102 |
-
processingMessage.textContent = 'Processing your upload';
|
103 |
-
}
|
104 |
-
}
|
105 |
-
};
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
xhr.send(init.body);
|
110 |
-
});
|
111 |
-
} else {
|
112 |
-
// Use the original fetch function for non-S3 requests
|
113 |
-
return originalFetch(url, init);
|
114 |
}
|
115 |
-
};
|
116 |
-
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
138 |
}
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
const
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
156 |
}
|
|
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
try {
|
160 |
-
await
|
161 |
-
repo: REPO_ID,
|
162 |
-
credentials: { accessToken: HF_ACCESS_TOKEN },
|
163 |
-
files: files.map(file => ({path: file.name, content: file}))
|
164 |
-
});
|
165 |
-
|
166 |
-
console.log(`All files uploaded successfully`);
|
167 |
-
progressBar.value = totalSize;
|
168 |
} catch (error) {
|
169 |
-
|
170 |
-
errorDiv.textContent = 'Error uploading files';
|
171 |
-
return;
|
172 |
}
|
|
|
173 |
|
174 |
-
|
175 |
-
let totalSizeMB = totalSize / (1024 * 1024)
|
176 |
-
let speed = totalSizeMB / elapsedTime;
|
177 |
|
178 |
-
|
179 |
-
|
180 |
-
let
|
181 |
-
|
182 |
-
messageDiv.innerHTML = `All files uploaded successfully in ${elapsedTime.toFixed(2)} seconds, for all ${totalSizeMB.toFixed(2)} MB in the ${files.length} files, speed ${speed.toFixed(2)} MB/s.`;
|
183 |
-
processingMessage.textContent = "All files processed";
|
184 |
-
}
|
185 |
-
|
186 |
-
|
|
|
|
|
|
|
187 |
}
|
|
|
|
|
188 |
</script>
|
189 |
</body>
|
190 |
</html>
|
|
|
33 |
<script type="module">
|
34 |
import { createRepo, uploadFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/hub@0.8.3/+esm";
|
35 |
|
36 |
+
class FileUploader {
|
37 |
+
constructor() {
|
38 |
+
this.fileInput = document.getElementById('fileUpload');
|
39 |
+
this.uploadButton = document.getElementById('uploadButton');
|
40 |
+
this.tokenInput = document.getElementById('tokenInput');
|
41 |
+
this.repoInput = document.getElementById('repoInput');
|
42 |
+
this.progressBar = document.getElementById('progressBar');
|
43 |
+
this.messageDiv = document.getElementById('message');
|
44 |
+
this.errorDiv = document.getElementById('error');
|
45 |
+
this.processingMessage = document.getElementById('processingMessage');
|
46 |
+
|
47 |
+
this.hijackFetch();
|
48 |
+
this.uploadButton.addEventListener('click', this.upload);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
}
|
|
|
|
|
50 |
|
51 |
+
hijackFetch = () => {
|
52 |
+
const originalFetch = fetch;
|
53 |
+
this.totalUploaded = 0;
|
54 |
+
this.progressMap = new Map();
|
55 |
+
|
56 |
+
fetch = (url, init) => init.method !== 'PUT' ? originalFetch(url, init) : this.handleFetch(url, init);
|
57 |
+
};
|
58 |
+
|
59 |
+
handleFetch = (url, init) => new Promise((resolve, reject) => {
|
60 |
+
const xhr = new XMLHttpRequest();
|
61 |
+
xhr.open(init.method, url);
|
62 |
+
for (let header in init.headers) xhr.setRequestHeader(header, init.headers[header]);
|
63 |
+
xhr.onload = () => resolve({ok: xhr.status >= 200 && xhr.status < 300, status: xhr.status, statusText: xhr.statusText, text: () => Promise.resolve(xhr.responseText), json: () => Promise.resolve(JSON.parse(xhr.responseText)), headers: {get: (header) => xhr.getResponseHeader(header)}});
|
64 |
+
xhr.onerror = () => reject(new TypeError('Network request failed'));
|
65 |
+
xhr.ontimeout = () => reject(new TypeError('Network request failed due to timeout'));
|
66 |
+
xhr.upload.onprogress = (event) => this.updateUploadProgress(event, url);
|
67 |
+
xhr.send(init.body);
|
68 |
+
});
|
69 |
+
|
70 |
+
updateUploadProgress = (event, url) => {
|
71 |
+
if (event.lengthComputable) {
|
72 |
+
this.progressMap.set(url, event.loaded);
|
73 |
+
this.totalUploaded = Array.from(this.progressMap.values()).reduce((a, b) => a + b, 0);
|
74 |
+
this.progressBar.value = this.totalUploaded;
|
75 |
+
this.processingMessage.textContent = this.totalUploaded === 0 ? 'Preparing your upload' : this.totalUploaded === this.progressBar.max ? 'Processing your upload' : '';
|
76 |
}
|
77 |
+
};
|
78 |
+
|
79 |
+
upload = async () => {
|
80 |
+
const files = Array.from(this.fileInput.files);
|
81 |
+
const HF_ACCESS_TOKEN = this.tokenInput.value;
|
82 |
+
const REPO_ID = this.repoInput.value;
|
83 |
+
this.progressBar.value = 0;
|
84 |
+
this.messageDiv.textContent = '';
|
85 |
+
this.errorDiv.textContent = '';
|
86 |
+
this.processingMessage.textContent = '';
|
87 |
+
|
88 |
+
if (files.length > 0) {
|
89 |
+
this.progressBar.max = files.reduce((total, file) => total + file.size, 0);
|
90 |
+
this.totalUploaded = 0;
|
91 |
+
this.startTime = Date.now();
|
92 |
+
|
93 |
+
try {
|
94 |
+
await this.createRepository(REPO_ID, HF_ACCESS_TOKEN);
|
95 |
+
await this.uploadFilesToRepo(REPO_ID, HF_ACCESS_TOKEN, files);
|
96 |
+
this.handleUploadSuccess(files);
|
97 |
+
} catch (error) {
|
98 |
+
this.handleErrorDuringUpload(error);
|
99 |
}
|
100 |
+
} else {
|
101 |
+
this.messageDiv.textContent = 'Please select files to upload';
|
102 |
}
|
103 |
+
};
|
104 |
|
105 |
+
createRepository = async (REPO_ID, HF_ACCESS_TOKEN) => {
|
106 |
try {
|
107 |
+
await createRepo({repo: REPO_ID, credentials: { accessToken: HF_ACCESS_TOKEN }});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
} catch (error) {
|
109 |
+
if (error.message !== 'You already created this model repo') throw error;
|
|
|
|
|
110 |
}
|
111 |
+
};
|
112 |
|
113 |
+
uploadFilesToRepo = (REPO_ID, HF_ACCESS_TOKEN, files) => uploadFiles({repo: REPO_ID, credentials: { accessToken: HF_ACCESS_TOKEN }, files: files.map(file => ({path: file.name, content: file}))});
|
|
|
|
|
114 |
|
115 |
+
handleUploadSuccess = (files) => {
|
116 |
+
const elapsedTime = (Date.now() - this.startTime) / 1000;
|
117 |
+
let totalSizeMB = this.progressBar.max / (1024 * 1024)
|
118 |
+
let speed = totalSizeMB / elapsedTime;
|
119 |
+
this.messageDiv.innerHTML = `All files uploaded successfully in ${elapsedTime.toFixed(2)} seconds, for all ${totalSizeMB.toFixed(2)} MB in the ${files.length} files, speed ${speed.toFixed(2)} MB/s.`;
|
120 |
+
this.processingMessage.textContent = "All files processed";
|
121 |
+
};
|
122 |
+
|
123 |
+
handleErrorDuringUpload = (error) => {
|
124 |
+
console.error('Error during upload', error);
|
125 |
+
this.errorDiv.textContent = 'Error during upload: ' + error.message;
|
126 |
+
};
|
127 |
}
|
128 |
+
|
129 |
+
new FileUploader();
|
130 |
</script>
|
131 |
</body>
|
132 |
</html>
|