/** * Upload .safetensors files directly to HuggingFace using Node.js * * Install dependencies first: * npm install @huggingface/hub fs-extra * * Usage: * node upload-model.js */ const { HfApi } = require("@huggingface/hub"); const fs = require("fs-extra"); const path = require("path"); // Configuration const CONFIG = { // Your HuggingFace access token (get it from https://huggingface.co/settings/tokens) token: "hf_YOUR_TOKEN_HERE", // Your model repository repoId: "Trouter-Library/Trouter-20B", // Local path where your .safetensors files are located localModelPath: "./model_files", // Files to upload (add all your safetensors files here) filesToUpload: [ "model-00001-of-00008.safetensors", "model-00002-of-00008.safetensors", "model-00003-of-00008.safetensors", "model-00004-of-00008.safetensors", "model-00005-of-00008.safetensors", "model-00006-of-00008.safetensors", "model-00007-of-00008.safetensors", "model-00008-of-00008.safetensors", "model.safetensors.index.json" ] }; async function uploadFile(hf, filePath, repoPath) { try { console.log(`Uploading ${repoPath}...`); const fileContent = await fs.readFile(filePath); await hf.uploadFile({ repo: CONFIG.repoId, path: repoPath, file: fileContent, }); console.log(`✓ Successfully uploaded ${repoPath}`); return true; } catch (error) { console.error(`✗ Failed to upload ${repoPath}:`, error.message); return false; } } async function main() { console.log("=".repeat(60)); console.log("Trouter-20B Model Upload Script"); console.log("=".repeat(60)); // Initialize HuggingFace API const hf = new HfApi(CONFIG.token); console.log(`\nRepository: ${CONFIG.repoId}`); console.log(`Local path: ${CONFIG.localModelPath}\n`); // Check if repository exists try { await hf.whoami(); console.log("✓ Authentication successful\n"); } catch (error) { console.error("✗ Authentication failed. Check your token!"); process.exit(1); } // Upload each file let successCount = 0; let failCount = 0; for (const fileName of CONFIG.filesToUpload) { const localPath = path.join(CONFIG.localModelPath, fileName); // Check if file exists if (!await fs.pathExists(localPath)) { console.log(`⚠ File not found: ${localPath}`); failCount++; continue; } // Get file size const stats = await fs.stat(localPath); const fileSizeGB = (stats.size / (1024 * 1024 * 1024)).toFixed(2); console.log(`File size: ${fileSizeGB} GB`); // Upload file const success = await uploadFile(hf, localPath, fileName); if (success) { successCount++; } else { failCount++; } console.log(""); } // Summary console.log("=".repeat(60)); console.log("Upload Summary"); console.log("=".repeat(60)); console.log(`✓ Successful: ${successCount}`); console.log(`✗ Failed: ${failCount}`); console.log(`Total: ${CONFIG.filesToUpload.length}`); console.log("\nModel repository:", `https://huggingface.co/${CONFIG.repoId}`); } // Run the script main().catch(console.error);