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" multiple> | |
<button onclick="uploadFiles()">Upload Files</button> | |
<progress id="progressBar" value="0" max="100"></progress> | |
<div id="message"></div> | |
<div id="error"></div> | |
<script type="module"> | |
import { createRepo, commit, deleteRepo, listFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm"; | |
async function uploadFiles() { | |
const fileInput = document.getElementById('fileUpload'); | |
const files = Array.from(fileInput.files); // convert FileList to Array | |
const tokenInput = document.getElementById('tokenInput'); | |
const HF_ACCESS_TOKEN = tokenInput.value; // get the entered token | |
const repoInput = document.getElementById('repoInput'); | |
const REPO_ID = repoInput.value; // get the entered repo id | |
const progressBar = document.getElementById('progressBar'); | |
const messageDiv = document.getElementById('message'); | |
const errorDiv = document.getElementById('error'); | |
progressBar.value = 0; // reset progress bar | |
messageDiv.textContent = ''; // clear previous messages | |
errorDiv.textContent = ''; // clear previous errors | |
if (files.length > 0) { | |
// assuming repo creation is successful and we have the necessary rights | |
// if not, repo creation and proper error handling should be done | |
await createRepo({ | |
repo: REPO_ID, | |
credentials: { accessToken: HF_ACCESS_TOKEN }, | |
}); | |
for (let i = 0; i < files.length; i++) { | |
const file = files[i]; | |
// read file content | |
const reader = new FileReader(); | |
reader.readAsArrayBuffer(file); | |
reader.onload = async () => { | |
const arrayBuffer = reader.result; | |
const uint8Array = new Uint8Array(arrayBuffer); | |
try { | |
// upload file | |
await commit({ | |
repo: REPO_ID, | |
credentials: { accessToken: HF_ACCESS_TOKEN }, | |
message: `committing file: ${file.name}`, | |
files: [{ | |
path: file.name, | |
content: uint8Array, | |
}], | |
}); | |
console.log(`File ${file.name} uploaded successfully`); | |
// update progress bar | |
progressBar.value = ((i + 1) / files.length) * 100; | |
} catch (error) { | |
console.error('Error uploading file', error); | |
errorDiv.textContent = 'Error uploading file'; | |
return; // stop uploading further files on error | |
} | |
}; | |
} | |
messageDiv.textContent = 'All files uploaded successfully'; | |
} else { | |
messageDiv.textContent = 'Please select a file to upload'; | |
} | |
} | |
</script> | |
</body> | |
</html> | |