Spaces:
Sleeping
Sleeping
Download script/sync-hf-env.ts from fouadkhalid123/asd: direct link, hf CLI and curl.
- Browser
- Download file 2.12 kB
-
https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/script/sync-hf-env.ts
- Command line
-
hf download hf://spaces/fouadkhalid123/asd/script/sync-hf-env.ts
-
curl -L -o sync-hf-env.ts https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/script/sync-hf-env.ts
2.12 kB
| import fs from "fs"; | |
| import path from "path"; | |
| import dotenv from "dotenv"; | |
| async function syncEnv() { | |
| const envPath = path.resolve(process.cwd(), ".env"); | |
| if (!fs.existsSync(envPath)) { | |
| console.error("β .env file not found at:", envPath); | |
| process.exit(1); | |
| } | |
| // Load .env content | |
| const envConfig = dotenv.parse(fs.readFileSync(envPath)); | |
| const hfToken = process.env.HF_TOKEN; | |
| if (!hfToken) { | |
| console.error("β HF_TOKEN environment variable is missing."); | |
| console.log("Please run: export HF_TOKEN=your_token (or set it in your environment)"); | |
| process.exit(1); | |
| } | |
| const repoId = "fouadkhalid123/asd"; | |
| const apiUrl = `https://huggingface.co/api/spaces/${repoId}/secrets`; | |
| console.log(`π Syncing ${Object.keys(envConfig).length} variables to Hugging Face Space: ${repoId}...`); | |
| for (const [key, value] of Object.entries(envConfig)) { | |
| if (!value) continue; | |
| try { | |
| console.log(`π€ Syncing: ${key}...`); | |
| const response = await fetch(apiUrl, { | |
| method: "POST", | |
| headers: { | |
| "Authorization": `Bearer ${hfToken}`, | |
| "Content-Type": "application/json", | |
| "User-Agent": "HF-Sync-Script/1.0", | |
| }, | |
| body: JSON.stringify({ key, value }), | |
| }); | |
| const responseData = await response.json().catch(() => ({})); | |
| if (!response.ok) { | |
| console.error(`β Failed to sync ${key}:`, response.status, responseData); | |
| // 403 often means missing scope or wrong headers | |
| if (response.status === 403) { | |
| console.error("π‘ TIP: Verify your HF_TOKEN has 'Write' access to this specific Space and its Secrets."); | |
| } | |
| } else { | |
| console.log(`β ${key} synced successfully.`); | |
| } | |
| } catch (error) { | |
| console.error(`β Error syncing ${key}:`, error); | |
| } | |
| } | |
| console.log("\n⨠Sync completed!"); | |
| } | |
| syncEnv(); | |