Spaces:
Running
Running
| <html> | |
| <head> | |
| <style> | |
| body { | |
| font-family: Arial, sans-serif; | |
| } | |
| input, button, progress { | |
| display: block; | |
| margin-top: 10px; | |
| } | |
| #message { | |
| margin-top: 20px; | |
| color: blue; | |
| } | |
| #error { | |
| color: red; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <label for="tokenInput">Hugging Face Token:</label> | |
| <input type="password" id="tokenInput" name="tokenInput"> | |
| <label for="repoInput">Repository ID:</label> | |
| <input type="text" id="repoInput" name="repoInput" placeholder="my-user/nlp-model"> | |
| <input type="file" id="fileUpload" webkitdirectory> | |
| <button id="uploadButton">Upload Files</button> | |
| <div id="processingMessage"></div> | |
| <progress id="progressBar" value="0" max="100"></progress> | |
| <div id="message"></div> | |
| <div id="error"></div> | |
| <script type="module"> | |
| import { createRepo, uploadFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm"; | |
| const uploadButton = document.getElementById('uploadButton'); | |
| uploadButton.addEventListener('click', upload); | |
| function isS3Url(url) { | |
| // The condition to identify S3 URLs and other URLs to track | |
| return url.startsWith('https://s3.us-east-1.amazonaws/') || | |
| url.includes('preupload/main') || | |
| url.includes('commit/main'); | |
| } | |
| // Save the original fetch function | |
| const originalFetch = fetch; | |
| // Create a variable to track the total uploaded size | |
| let totalUploaded = 0; | |
| // Replace the global fetch function | |
| let progressMap = new Map(); | |
| fetch = (url, init) => { | |
| console.log(url, init) | |
| if (init.method === 'PUT') { | |
| return new Promise((resolve, reject) => { | |
| const xhr = new XMLHttpRequest(); | |
| xhr.open(init.method, url); | |
| for (let header in init.headers) { | |
| xhr.setRequestHeader(header, init.headers[header]); | |
| } | |
| 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) | |
| } | |
| }); | |
| }; | |
| xhr.onerror = () => reject(new TypeError('Network request failed')); | |
| xhr.ontimeout = () => reject(new TypeError('Network request failed due to timeout')); | |
| xhr.upload.onprogress = (event) => { | |
| if (event.lengthComputable) { | |
| progressMap.set(url, event.loaded); | |
| totalUploaded = Array.from(progressMap.values()).reduce((a, b) => a + b, 0); | |
| // Calculate speed | |
| const elapsedSeconds = (Date.now() - startTime) / 1000; | |
| const speedInMBps = totalUploaded / elapsedSeconds / 1024 / 1024; | |
| // Update progress bar and speed display | |
| const progressBar = document.getElementById('progressBar'); | |
| progressBar.value = totalUploaded; | |
| // Update processing message | |
| const processingMessage = document.getElementById('processingMessage'); | |
| if (totalUploaded === 0) { | |
| processingMessage.textContent = 'Preparing your upload'; | |
| } else if (totalUploaded === progressBar.max) { | |
| processingMessage.textContent = 'Processing your upload'; | |
| } | |
| } | |
| }; | |
| xhr.send(init.body); | |
| }); | |
| } else { | |
| // Use the original fetch function for non-S3 requests | |
| return originalFetch(url, init); | |
| } | |
| }; | |
| async function upload() { | |
| const fileInput = document.getElementById('fileUpload'); | |
| const files = Array.from(fileInput.files); | |
| const tokenInput = document.getElementById('tokenInput'); | |
| const HF_ACCESS_TOKEN = tokenInput.value; | |
| const repoInput = document.getElementById('repoInput'); | |
| const REPO_ID = repoInput.value; | |
| const progressBar = document.getElementById('progressBar'); | |
| const messageDiv = document.getElementById('message'); | |
| const errorDiv = document.getElementById('error'); | |
| const processingMessage = document.getElementById('processingMessage'); | |
| progressBar.value = 0; | |
| messageDiv.textContent = ''; | |
| errorDiv.textContent = ''; | |
| processingMessage.textContent = ''; | |
| if (files.length > 0) { | |
| let totalSize = 0; | |
| for (let file of files) { | |
| totalSize += file.size; | |
| } | |
| progressBar.max = totalSize; // Set the max value of progress bar | |
| totalUploaded = 0; // Reset total uploaded size | |
| const startTime = Date.now(); | |
| try { | |
| await createRepo({ | |
| repo: REPO_ID, | |
| credentials: { accessToken: HF_ACCESS_TOKEN }, | |
| }); | |
| } catch (error) { | |
| if (error.message === 'You already created this model repo') { | |
| console.log('Repository already exists, proceeding to upload files'); | |
| } else { | |
| console.error('Error creating repository', error); | |
| errorDiv.textContent = 'Error creating repository'; | |
| return; | |
| } | |
| } | |
| try { | |
| await uploadFiles({ | |
| repo: REPO_ID, | |
| credentials: { accessToken: HF_ACCESS_TOKEN }, | |
| files: files.map(file => ({path: file.name, content: file})) | |
| }); | |
| console.log(`All files uploaded successfully`); | |
| progressBar.value = totalSize; | |
| } catch (error) { | |
| console.error('Error uploading files', error); | |
| errorDiv.textContent = 'Error uploading files'; | |
| return; | |
| } | |
| const elapsedTime = (Date.now() - startTime) / 1000; | |
| let totalSizeMB = totalSize / (1024 * 1024) | |
| let speed = totalSizeMB / elapsedTime; | |
| let time1GB = (1024 / speed) / 60; | |
| let time5GB = (5 * 1024 / speed) / 60; | |
| let time10GB = (10 * 1024 / speed) / 60; | |
| 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.`; | |
| processingMessage.textContent = "All files processed"; | |
| } else { | |
| messageDiv.textContent = 'Please select files to upload'; | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> | |