multimodalart HF Staff commited on
Commit
657ac04
·
1 Parent(s): c13d23b

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +26 -30
index.html CHANGED
@@ -32,39 +32,35 @@
32
 
33
  <script type="module">
34
  import { createRepo, uploadFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm";
35
-
36
  const uploadButton = document.getElementById('uploadButton');
37
  uploadButton.addEventListener('click', upload);
38
-
39
- function isS3Url(url) {
40
- // Replace this with the actual condition to identify S3 URLs
41
- return url.startsWith('https://s3.amazonaws.com/');
42
- }
43
-
44
- function fetchWithProgress(url, init) {
45
- if (isS3Url(url)) {
46
  return new Promise((resolve, reject) => {
47
  const xhr = new XMLHttpRequest();
48
-
49
- xhr.open(init.method || 'GET', url);
50
-
51
  for (let header in init.headers) {
52
  xhr.setRequestHeader(header, init.headers[header]);
53
  }
54
-
55
  xhr.onload = () => {
56
  resolve({
57
  ok: xhr.status >= 200 && xhr.status < 300,
58
  status: xhr.status,
59
  statusText: xhr.statusText,
60
- text: () => Promise.resolve(xhr.responseText)
61
  });
62
  };
63
-
64
  xhr.onerror = () => reject(new TypeError('Network request failed'));
65
-
66
  xhr.ontimeout = () => reject(new TypeError('Network request failed due to timeout'));
67
-
68
  xhr.upload.onprogress = (event) => {
69
  if (event.lengthComputable) {
70
  const progress = event.loaded / event.total;
@@ -72,14 +68,14 @@
72
  progressBar.value = progress * 100;
73
  }
74
  };
75
-
76
  xhr.send(init.body);
77
  });
78
  } else {
79
- return fetch(url, init);
80
  }
81
  }
82
-
83
  async function upload() {
84
  const fileInput = document.getElementById('fileUpload');
85
  const files = Array.from(fileInput.files);
@@ -95,16 +91,16 @@
95
  messageDiv.textContent = '';
96
  errorDiv.textContent = '';
97
  processingMessage.textContent = '';
98
-
99
  if (files.length > 0) {
100
  let totalSize = 0;
101
  for (let file of files) {
102
  totalSize += file.size;
103
  }
104
  totalSize = totalSize / (1024 * 1024);
105
-
106
  const startTime = Date.now();
107
-
108
  try {
109
  await createRepo({
110
  repo: REPO_ID,
@@ -119,15 +115,14 @@
119
  return;
120
  }
121
  }
122
-
123
  try {
124
  await uploadFiles({
125
  repo: REPO_ID,
126
  credentials: { accessToken: HF_ACCESS_TOKEN },
127
- files: files.map(file => ({path: file.name, content: file})),
128
- fetch: fetchWithProgress
129
  });
130
-
131
  console.log(`All files uploaded successfully`);
132
  progressBar.value = 100;
133
  } catch (error) {
@@ -135,14 +130,14 @@
135
  errorDiv.textContent = 'Error uploading files';
136
  return;
137
  }
138
-
139
  const elapsedTime = (Date.now() - startTime) / 1000;
140
  let speed = totalSize / elapsedTime;
141
-
142
  let time1GB = (1024 / speed) / 60;
143
  let time5GB = (5 * 1024 / speed) / 60;
144
  let time10GB = (10 * 1024 / speed) / 60;
145
-
146
  messageDiv.innerHTML = `All files uploaded successfully in ${elapsedTime.toFixed(2)} seconds, for all ${totalSize.toFixed(2)} MB in the ${files.length} files, speed ${speed.toFixed(2)} MB/s.<br>To upload a 1GB model at this speed, it would take approximately ${time1GB.toFixed(2)} minutes.<br>To upload a 5GB model at this speed, it would take approximately ${time5GB.toFixed(2)} minutes.<br>To upload a 10GB model at this speed, it would take approximately ${time10GB.toFixed(2)} minutes.`;
147
  processingMessage.textContent = "All files processed";
148
  } else {
@@ -150,5 +145,6 @@
150
  }
151
  }
152
  </script>
 
153
  </body>
154
  </html>
 
32
 
33
  <script type="module">
34
  import { createRepo, uploadFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm";
35
+
36
  const uploadButton = document.getElementById('uploadButton');
37
  uploadButton.addEventListener('click', upload);
38
+
39
+ // Override global fetch
40
+ const originalFetch = window.fetch;
41
+
42
+ window.fetch = function(url, init) {
43
+ if (typeof url === 'string' && init && init.method === 'PUT') {
 
 
44
  return new Promise((resolve, reject) => {
45
  const xhr = new XMLHttpRequest();
46
+ xhr.open(init.method, url);
47
+
 
48
  for (let header in init.headers) {
49
  xhr.setRequestHeader(header, init.headers[header]);
50
  }
51
+
52
  xhr.onload = () => {
53
  resolve({
54
  ok: xhr.status >= 200 && xhr.status < 300,
55
  status: xhr.status,
56
  statusText: xhr.statusText,
57
+ text: () => Promise.resolve(xhr.responseText),
58
  });
59
  };
60
+
61
  xhr.onerror = () => reject(new TypeError('Network request failed'));
 
62
  xhr.ontimeout = () => reject(new TypeError('Network request failed due to timeout'));
63
+
64
  xhr.upload.onprogress = (event) => {
65
  if (event.lengthComputable) {
66
  const progress = event.loaded / event.total;
 
68
  progressBar.value = progress * 100;
69
  }
70
  };
71
+
72
  xhr.send(init.body);
73
  });
74
  } else {
75
+ return originalFetch(url, init);
76
  }
77
  }
78
+
79
  async function upload() {
80
  const fileInput = document.getElementById('fileUpload');
81
  const files = Array.from(fileInput.files);
 
91
  messageDiv.textContent = '';
92
  errorDiv.textContent = '';
93
  processingMessage.textContent = '';
94
+
95
  if (files.length > 0) {
96
  let totalSize = 0;
97
  for (let file of files) {
98
  totalSize += file.size;
99
  }
100
  totalSize = totalSize / (1024 * 1024);
101
+
102
  const startTime = Date.now();
103
+
104
  try {
105
  await createRepo({
106
  repo: REPO_ID,
 
115
  return;
116
  }
117
  }
118
+
119
  try {
120
  await uploadFiles({
121
  repo: REPO_ID,
122
  credentials: { accessToken: HF_ACCESS_TOKEN },
123
+ files: files.map(file => ({path: file.name, content: file}))
 
124
  });
125
+
126
  console.log(`All files uploaded successfully`);
127
  progressBar.value = 100;
128
  } catch (error) {
 
130
  errorDiv.textContent = 'Error uploading files';
131
  return;
132
  }
133
+
134
  const elapsedTime = (Date.now() - startTime) / 1000;
135
  let speed = totalSize / elapsedTime;
136
+
137
  let time1GB = (1024 / speed) / 60;
138
  let time5GB = (5 * 1024 / speed) / 60;
139
  let time10GB = (10 * 1024 / speed) / 60;
140
+
141
  messageDiv.innerHTML = `All files uploaded successfully in ${elapsedTime.toFixed(2)} seconds, for all ${totalSize.toFixed(2)} MB in the ${files.length} files, speed ${speed.toFixed(2)} MB/s.<br>To upload a 1GB model at this speed, it would take approximately ${time1GB.toFixed(2)} minutes.<br>To upload a 5GB model at this speed, it would take approximately ${time5GB.toFixed(2)} minutes.<br>To upload a 10GB model at this speed, it would take approximately ${time10GB.toFixed(2)} minutes.`;
142
  processingMessage.textContent = "All files processed";
143
  } else {
 
145
  }
146
  }
147
  </script>
148
+
149
  </body>
150
  </html>