Spaces:
Running
Running
Fix name collision: rename fetch helper to req, export object to moduleApi (v1.0.1)
Browse files- modules_repos.js +16 -14
modules_repos.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
// HuggingBot Module: Repo Creation & Multi-Type Ops v1.0.
|
| 2 |
// ============================================================================
|
| 3 |
// Companion to the HuggingFace Hub module (modules_huggingface.js).
|
| 4 |
// Adds what the Hub module lacks:
|
|
@@ -6,10 +6,12 @@
|
|
| 6 |
// * hf_upload_repo_file β upload a file to ANY repo type (space|model|dataset)
|
| 7 |
// * hf_delete_repo β delete any repo type
|
| 8 |
// * hf_list_my_datasets β list datasets you own
|
| 9 |
-
// * hf_list_my_models
|
| 10 |
// Tool names are unique (no clash with the Hub module), so both modules
|
| 11 |
// can be registered side by side in the ModuleSystem.
|
| 12 |
// Token is shared with the Hub module via localStorage key 'chatseed_hf_token'.
|
|
|
|
|
|
|
| 13 |
// ============================================================================
|
| 14 |
(function (MS) {
|
| 15 |
'use strict';
|
|
@@ -25,7 +27,7 @@
|
|
| 25 |
return { 'Authorization': 'Bearer ' + t, 'Content-Type': 'application/json' };
|
| 26 |
}
|
| 27 |
|
| 28 |
-
async function
|
| 29 |
opts = opts || {};
|
| 30 |
var headers = Object.assign({}, hdr(), opts.headers || {});
|
| 31 |
var res = await fetch(HF + path, Object.assign({}, opts, { headers: headers }));
|
|
@@ -58,13 +60,13 @@
|
|
| 58 |
var body = { summary: summary || 'Update via HuggingBot', description: '',
|
| 59 |
files: (files || []).map(function (f) { return { path: f.path, content: f.content, encoding: f.encoding || 'utf-8' }; }) };
|
| 60 |
if (deleted && deleted.length) body.deletedEntries = deleted;
|
| 61 |
-
return
|
| 62 |
}
|
| 63 |
|
| 64 |
async function createRepo(a) {
|
| 65 |
var type = a.type || 'space';
|
| 66 |
if (['space', 'model', 'dataset'].indexOf(type) < 0) throw new Error('Invalid type "' + type + '". Use space|model|dataset.');
|
| 67 |
-
var me = await
|
| 68 |
var ns = a.org || me.name;
|
| 69 |
var payload = { name: a.name, type: type, private: !!a.private };
|
| 70 |
if (type === 'space') payload.sdk = a.sdk || 'gradio';
|
|
@@ -75,7 +77,7 @@
|
|
| 75 |
var sec = parseJson(a.secrets), vars = parseJson(a.variables);
|
| 76 |
if (Object.keys(sec).length) payload.secrets = sec;
|
| 77 |
if (Object.keys(vars).length) payload.variables = vars;
|
| 78 |
-
await
|
| 79 |
var repoId = ns + '/' + a.name;
|
| 80 |
var base = pathFor(type);
|
| 81 |
var label = type.charAt(0).toUpperCase() + type.slice(1);
|
|
@@ -94,19 +96,19 @@
|
|
| 94 |
var base = pathFor(type);
|
| 95 |
if (type === 'space') {
|
| 96 |
try {
|
| 97 |
-
var rt = await
|
| 98 |
if (rt.stage === 'PAUSED' || rt.stage === 'SLEEPING') {
|
| 99 |
-
await
|
| 100 |
await new Promise(function (r) { setTimeout(r, 2000); });
|
| 101 |
}
|
| 102 |
} catch (e) { /* best-effort */ }
|
| 103 |
}
|
| 104 |
try {
|
| 105 |
-
await
|
| 106 |
return 'β
**' + type + ' deleted:** ' + a.repo_id;
|
| 107 |
} catch (e) {
|
| 108 |
try {
|
| 109 |
-
await
|
| 110 |
return 'β
**' + type + ' deleted (via repos endpoint):** ' + a.repo_id;
|
| 111 |
} catch (e2) {
|
| 112 |
throw new Error('Could not delete ' + type + ': ' + e.message + (type === 'space' ? '. Try restarting it first with hf_restart_space.' : ''));
|
|
@@ -125,11 +127,11 @@
|
|
| 125 |
}
|
| 126 |
|
| 127 |
async function listMine(kind, a) {
|
| 128 |
-
var me = await
|
| 129 |
var name = me.name || me.user;
|
| 130 |
var limit = Math.min(Math.max((a && a.limit) || 10, 1), 100);
|
| 131 |
var base = kind === 'dataset' ? 'datasets' : kind === 'model' ? 'models' : 'spaces';
|
| 132 |
-
var res = await
|
| 133 |
var label = kind.charAt(0).toUpperCase() + kind.slice(1);
|
| 134 |
if (!res || !res.length) return 'π No ' + kind + 's found under **' + name + '**.\nCreate one with `hf_create_repo` (type: "' + kind + '")!';
|
| 135 |
var lines = ['π **Your ' + label + 's (' + name + '):**', ''];
|
|
@@ -175,7 +177,7 @@
|
|
| 175 |
hf_list_my_models: function (a) { return listMine('models', a); }
|
| 176 |
};
|
| 177 |
|
| 178 |
-
var
|
| 179 |
name: 'Repo Creation & Multi-Type Ops',
|
| 180 |
description: 'Create HF datasets/models/spaces and upload files to any repo type.',
|
| 181 |
tools: TOOLS.map(function (t) { return { type: 'function', function: t }; }),
|
|
@@ -184,5 +186,5 @@
|
|
| 184 |
return null;
|
| 185 |
}
|
| 186 |
};
|
| 187 |
-
MS.register('huggingface-repos',
|
| 188 |
})(ModuleSystem);
|
|
|
|
| 1 |
+
// HuggingBot Module: Repo Creation & Multi-Type Ops v1.0.1
|
| 2 |
// ============================================================================
|
| 3 |
// Companion to the HuggingFace Hub module (modules_huggingface.js).
|
| 4 |
// Adds what the Hub module lacks:
|
|
|
|
| 6 |
// * hf_upload_repo_file β upload a file to ANY repo type (space|model|dataset)
|
| 7 |
// * hf_delete_repo β delete any repo type
|
| 8 |
// * hf_list_my_datasets β list datasets you own
|
| 9 |
+
// * hf_list_my_models -- list models you own
|
| 10 |
// Tool names are unique (no clash with the Hub module), so both modules
|
| 11 |
// can be registered side by side in the ModuleSystem.
|
| 12 |
// Token is shared with the Hub module via localStorage key 'chatseed_hf_token'.
|
| 13 |
+
// v1.0.1: renamed the fetch helper from `api` to `req` to avoid colliding
|
| 14 |
+
// with the exported module object `api`.
|
| 15 |
// ============================================================================
|
| 16 |
(function (MS) {
|
| 17 |
'use strict';
|
|
|
|
| 27 |
return { 'Authorization': 'Bearer ' + t, 'Content-Type': 'application/json' };
|
| 28 |
}
|
| 29 |
|
| 30 |
+
async function req(path, opts) {
|
| 31 |
opts = opts || {};
|
| 32 |
var headers = Object.assign({}, hdr(), opts.headers || {});
|
| 33 |
var res = await fetch(HF + path, Object.assign({}, opts, { headers: headers }));
|
|
|
|
| 60 |
var body = { summary: summary || 'Update via HuggingBot', description: '',
|
| 61 |
files: (files || []).map(function (f) { return { path: f.path, content: f.content, encoding: f.encoding || 'utf-8' }; }) };
|
| 62 |
if (deleted && deleted.length) body.deletedEntries = deleted;
|
| 63 |
+
return req('/api/' + pathFor(type) + '/' + p.ns + '/' + p.name + '/commit/main', { method: 'POST', body: JSON.stringify(body) });
|
| 64 |
}
|
| 65 |
|
| 66 |
async function createRepo(a) {
|
| 67 |
var type = a.type || 'space';
|
| 68 |
if (['space', 'model', 'dataset'].indexOf(type) < 0) throw new Error('Invalid type "' + type + '". Use space|model|dataset.');
|
| 69 |
+
var me = await req('/api/whoami-v2');
|
| 70 |
var ns = a.org || me.name;
|
| 71 |
var payload = { name: a.name, type: type, private: !!a.private };
|
| 72 |
if (type === 'space') payload.sdk = a.sdk || 'gradio';
|
|
|
|
| 77 |
var sec = parseJson(a.secrets), vars = parseJson(a.variables);
|
| 78 |
if (Object.keys(sec).length) payload.secrets = sec;
|
| 79 |
if (Object.keys(vars).length) payload.variables = vars;
|
| 80 |
+
await req('/api/repos/create', { method: 'POST', body: JSON.stringify(payload) });
|
| 81 |
var repoId = ns + '/' + a.name;
|
| 82 |
var base = pathFor(type);
|
| 83 |
var label = type.charAt(0).toUpperCase() + type.slice(1);
|
|
|
|
| 96 |
var base = pathFor(type);
|
| 97 |
if (type === 'space') {
|
| 98 |
try {
|
| 99 |
+
var rt = await req('/api/spaces/' + p.ns + '/' + p.name + '/runtime');
|
| 100 |
if (rt.stage === 'PAUSED' || rt.stage === 'SLEEPING') {
|
| 101 |
+
await req('/api/spaces/' + p.ns + '/' + p.name + '/restart', { method: 'POST' });
|
| 102 |
await new Promise(function (r) { setTimeout(r, 2000); });
|
| 103 |
}
|
| 104 |
} catch (e) { /* best-effort */ }
|
| 105 |
}
|
| 106 |
try {
|
| 107 |
+
await req('/api/' + base + '/' + p.ns + '/' + p.name, { method: 'DELETE' });
|
| 108 |
return 'β
**' + type + ' deleted:** ' + a.repo_id;
|
| 109 |
} catch (e) {
|
| 110 |
try {
|
| 111 |
+
await req('/api/repos/' + p.ns + '/' + p.name + '?type=' + type, { method: 'DELETE' });
|
| 112 |
return 'β
**' + type + ' deleted (via repos endpoint):** ' + a.repo_id;
|
| 113 |
} catch (e2) {
|
| 114 |
throw new Error('Could not delete ' + type + ': ' + e.message + (type === 'space' ? '. Try restarting it first with hf_restart_space.' : ''));
|
|
|
|
| 127 |
}
|
| 128 |
|
| 129 |
async function listMine(kind, a) {
|
| 130 |
+
var me = await req('/api/whoami-v2');
|
| 131 |
var name = me.name || me.user;
|
| 132 |
var limit = Math.min(Math.max((a && a.limit) || 10, 1), 100);
|
| 133 |
var base = kind === 'dataset' ? 'datasets' : kind === 'model' ? 'models' : 'spaces';
|
| 134 |
+
var res = await req('/api/' + base + '?author=' + encodeURIComponent(name) + '&limit=' + limit + '&sort=lastModified');
|
| 135 |
var label = kind.charAt(0).toUpperCase() + kind.slice(1);
|
| 136 |
if (!res || !res.length) return 'π No ' + kind + 's found under **' + name + '**.\nCreate one with `hf_create_repo` (type: "' + kind + '")!';
|
| 137 |
var lines = ['π **Your ' + label + 's (' + name + '):**', ''];
|
|
|
|
| 177 |
hf_list_my_models: function (a) { return listMine('models', a); }
|
| 178 |
};
|
| 179 |
|
| 180 |
+
var moduleApi = {
|
| 181 |
name: 'Repo Creation & Multi-Type Ops',
|
| 182 |
description: 'Create HF datasets/models/spaces and upload files to any repo type.',
|
| 183 |
tools: TOOLS.map(function (t) { return { type: 'function', function: t }; }),
|
|
|
|
| 186 |
return null;
|
| 187 |
}
|
| 188 |
};
|
| 189 |
+
MS.register('huggingface-repos', moduleApi);
|
| 190 |
})(ModuleSystem);
|