@gradio/client 0.1.3 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -1
- package/README.md +32 -32
- package/dist/client.d.ts +24 -11
- package/dist/client.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +507 -470
- package/dist/types.d.ts +18 -12
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +3 -3
- package/dist/utils.d.ts.map +1 -1
- package/package.json +4 -3
- package/src/client.ts +645 -571
- package/src/globals.d.ts +2 -4
- package/src/index.ts +7 -1
- package/src/types.ts +13 -6
- package/src/utils.ts +14 -13
package/dist/index.js
CHANGED
@@ -13,13 +13,12 @@ function determine_protocol(endpoint) {
|
|
13
13
|
host,
|
14
14
|
http_protocol: protocol
|
15
15
|
};
|
16
|
-
} else {
|
17
|
-
return {
|
18
|
-
ws_protocol: protocol === "https:" ? "wss" : "ws",
|
19
|
-
http_protocol: protocol,
|
20
|
-
host
|
21
|
-
};
|
22
16
|
}
|
17
|
+
return {
|
18
|
+
ws_protocol: protocol === "https:" ? "wss" : "ws",
|
19
|
+
http_protocol: protocol,
|
20
|
+
host
|
21
|
+
};
|
23
22
|
}
|
24
23
|
return {
|
25
24
|
ws_protocol: "wss",
|
@@ -86,8 +85,7 @@ async function discussions_enabled(space_id) {
|
|
86
85
|
const error = r.headers.get("x-error-message");
|
87
86
|
if (error && RE_DISABLED_DISCUSSION.test(error))
|
88
87
|
return false;
|
89
|
-
|
90
|
-
return true;
|
88
|
+
return true;
|
91
89
|
} catch (e) {
|
92
90
|
return false;
|
93
91
|
}
|
@@ -161,45 +159,7 @@ const hardware_types = [
|
|
161
159
|
];
|
162
160
|
const QUEUE_FULL_MSG = "This application is too busy. Keep trying!";
|
163
161
|
const BROKEN_CONNECTION_MSG = "Connection errored out.";
|
164
|
-
async function post_data(url, body, token) {
|
165
|
-
const headers = { "Content-Type": "application/json" };
|
166
|
-
if (token) {
|
167
|
-
headers.Authorization = `Bearer ${token}`;
|
168
|
-
}
|
169
|
-
try {
|
170
|
-
var response = await fetch(url, {
|
171
|
-
method: "POST",
|
172
|
-
body: JSON.stringify(body),
|
173
|
-
headers
|
174
|
-
});
|
175
|
-
} catch (e) {
|
176
|
-
return [{ error: BROKEN_CONNECTION_MSG }, 500];
|
177
|
-
}
|
178
|
-
const output = await response.json();
|
179
|
-
return [output, response.status];
|
180
|
-
}
|
181
162
|
let NodeBlob;
|
182
|
-
async function upload_files(root, files, token) {
|
183
|
-
const headers = {};
|
184
|
-
if (token) {
|
185
|
-
headers.Authorization = `Bearer ${token}`;
|
186
|
-
}
|
187
|
-
const formData = new FormData();
|
188
|
-
files.forEach((file) => {
|
189
|
-
formData.append("files", file);
|
190
|
-
});
|
191
|
-
try {
|
192
|
-
var response = await fetch(`${root}/upload`, {
|
193
|
-
method: "POST",
|
194
|
-
body: formData,
|
195
|
-
headers
|
196
|
-
});
|
197
|
-
} catch (e) {
|
198
|
-
return { error: BROKEN_CONNECTION_MSG };
|
199
|
-
}
|
200
|
-
const output = await response.json();
|
201
|
-
return { files: output };
|
202
|
-
}
|
203
163
|
async function duplicate(app_reference, options) {
|
204
164
|
const { hf_token, private: _private, hardware, timeout } = options;
|
205
165
|
if (hardware && !hardware_types.includes(hardware)) {
|
@@ -231,437 +191,551 @@ async function duplicate(app_reference, options) {
|
|
231
191
|
);
|
232
192
|
if (response.status === 409) {
|
233
193
|
return client(`${user}/${space_name}`, options);
|
234
|
-
} else {
|
235
|
-
const duplicated_space = await response.json();
|
236
|
-
let original_hardware;
|
237
|
-
if (!hardware) {
|
238
|
-
original_hardware = await get_space_hardware(app_reference, hf_token);
|
239
|
-
}
|
240
|
-
const requested_hardware = hardware || original_hardware || "cpu-basic";
|
241
|
-
await set_space_hardware(
|
242
|
-
`${user}/${space_name}`,
|
243
|
-
requested_hardware,
|
244
|
-
hf_token
|
245
|
-
);
|
246
|
-
await set_space_timeout(
|
247
|
-
`${user}/${space_name}`,
|
248
|
-
timeout || 300,
|
249
|
-
hf_token
|
250
|
-
);
|
251
|
-
return client(duplicated_space.url, options);
|
252
194
|
}
|
195
|
+
const duplicated_space = await response.json();
|
196
|
+
let original_hardware;
|
197
|
+
if (!hardware) {
|
198
|
+
original_hardware = await get_space_hardware(app_reference, hf_token);
|
199
|
+
}
|
200
|
+
const requested_hardware = hardware || original_hardware || "cpu-basic";
|
201
|
+
await set_space_hardware(
|
202
|
+
`${user}/${space_name}`,
|
203
|
+
requested_hardware,
|
204
|
+
hf_token
|
205
|
+
);
|
206
|
+
await set_space_timeout(`${user}/${space_name}`, timeout || 300, hf_token);
|
207
|
+
return client(duplicated_space.url, options);
|
253
208
|
} catch (e) {
|
254
209
|
throw new Error(e);
|
255
210
|
}
|
256
211
|
}
|
257
|
-
|
258
|
-
return
|
259
|
-
|
260
|
-
const
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
212
|
+
function api_factory(fetch_implementation) {
|
213
|
+
return { post_data: post_data2, upload_files: upload_files2, client: client2, handle_blob: handle_blob2 };
|
214
|
+
async function post_data2(url, body, token) {
|
215
|
+
const headers = { "Content-Type": "application/json" };
|
216
|
+
if (token) {
|
217
|
+
headers.Authorization = `Bearer ${token}`;
|
218
|
+
}
|
219
|
+
try {
|
220
|
+
var response = await fetch_implementation(url, {
|
221
|
+
method: "POST",
|
222
|
+
body: JSON.stringify(body),
|
223
|
+
headers
|
224
|
+
});
|
225
|
+
} catch (e) {
|
226
|
+
return [{ error: BROKEN_CONNECTION_MSG }, 500];
|
271
227
|
}
|
272
|
-
const
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
jwt = await get_jwt(space_id, hf_token);
|
228
|
+
const output = await response.json();
|
229
|
+
return [output, response.status];
|
230
|
+
}
|
231
|
+
async function upload_files2(root, files, token) {
|
232
|
+
const headers = {};
|
233
|
+
if (token) {
|
234
|
+
headers.Authorization = `Bearer ${token}`;
|
280
235
|
}
|
281
|
-
|
282
|
-
|
283
|
-
|
236
|
+
const chunkSize = 1e3;
|
237
|
+
const uploadResponses = [];
|
238
|
+
for (let i = 0; i < files.length; i += chunkSize) {
|
239
|
+
const chunk = files.slice(i, i + chunkSize);
|
240
|
+
const formData = new FormData();
|
241
|
+
chunk.forEach((file) => {
|
242
|
+
formData.append("files", file);
|
243
|
+
});
|
284
244
|
try {
|
285
|
-
|
245
|
+
var response = await fetch_implementation(`${root}/upload`, {
|
246
|
+
method: "POST",
|
247
|
+
body: formData,
|
248
|
+
headers
|
249
|
+
});
|
286
250
|
} catch (e) {
|
287
|
-
|
251
|
+
return { error: BROKEN_CONNECTION_MSG };
|
288
252
|
}
|
289
|
-
|
290
|
-
|
291
|
-
...return_obj
|
292
|
-
};
|
253
|
+
const output = await response.json();
|
254
|
+
uploadResponses.push(...output);
|
293
255
|
}
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
256
|
+
return { files: uploadResponses };
|
257
|
+
}
|
258
|
+
async function client2(app_reference, options = { normalise_files: true }) {
|
259
|
+
return new Promise(async (res) => {
|
260
|
+
const { status_callback, hf_token, normalise_files } = options;
|
261
|
+
const return_obj = {
|
262
|
+
predict,
|
263
|
+
submit,
|
264
|
+
view_api
|
265
|
+
// duplicate
|
266
|
+
};
|
267
|
+
const transform_files = normalise_files ?? true;
|
268
|
+
if (typeof window === "undefined" || !("WebSocket" in window)) {
|
269
|
+
const ws = await import("./wrapper-6f348d45.js");
|
270
|
+
NodeBlob = (await import("node:buffer")).Blob;
|
271
|
+
global.WebSocket = ws.WebSocket;
|
272
|
+
}
|
273
|
+
const { ws_protocol, http_protocol, host, space_id } = await process_endpoint(app_reference, hf_token);
|
274
|
+
const session_hash = Math.random().toString(36).substring(2);
|
275
|
+
const last_status = {};
|
276
|
+
let config;
|
277
|
+
let api_map = {};
|
278
|
+
let jwt = false;
|
279
|
+
if (hf_token && space_id) {
|
280
|
+
jwt = await get_jwt(space_id, hf_token);
|
281
|
+
}
|
282
|
+
async function config_success(_config) {
|
283
|
+
config = _config;
|
284
|
+
api_map = map_names_to_ids((_config == null ? void 0 : _config.dependencies) || []);
|
299
285
|
try {
|
300
|
-
|
301
|
-
const _config = await config_success(config);
|
302
|
-
res(_config);
|
286
|
+
api = await view_api(config);
|
303
287
|
} catch (e) {
|
304
|
-
|
288
|
+
console.error(`Could not get api details: ${e.message}`);
|
289
|
+
}
|
290
|
+
return {
|
291
|
+
config,
|
292
|
+
...return_obj
|
293
|
+
};
|
294
|
+
}
|
295
|
+
let api;
|
296
|
+
async function handle_space_sucess(status) {
|
297
|
+
if (status_callback)
|
298
|
+
status_callback(status);
|
299
|
+
if (status.status === "running")
|
300
|
+
try {
|
301
|
+
config = await resolve_config(
|
302
|
+
fetch_implementation,
|
303
|
+
`${http_protocol}//${host}`,
|
304
|
+
hf_token
|
305
|
+
);
|
306
|
+
const _config = await config_success(config);
|
307
|
+
res(_config);
|
308
|
+
} catch (e) {
|
309
|
+
console.error(e);
|
310
|
+
if (status_callback) {
|
311
|
+
status_callback({
|
312
|
+
status: "error",
|
313
|
+
message: "Could not load this space.",
|
314
|
+
load_status: "error",
|
315
|
+
detail: "NOT_FOUND"
|
316
|
+
});
|
317
|
+
}
|
318
|
+
}
|
319
|
+
}
|
320
|
+
try {
|
321
|
+
config = await resolve_config(
|
322
|
+
fetch_implementation,
|
323
|
+
`${http_protocol}//${host}`,
|
324
|
+
hf_token
|
325
|
+
);
|
326
|
+
const _config = await config_success(config);
|
327
|
+
res(_config);
|
328
|
+
} catch (e) {
|
329
|
+
console.error(e);
|
330
|
+
if (space_id) {
|
331
|
+
check_space_status(
|
332
|
+
space_id,
|
333
|
+
RE_SPACE_NAME.test(space_id) ? "space_name" : "subdomain",
|
334
|
+
handle_space_sucess
|
335
|
+
);
|
336
|
+
} else {
|
337
|
+
if (status_callback)
|
305
338
|
status_callback({
|
306
339
|
status: "error",
|
307
340
|
message: "Could not load this space.",
|
308
341
|
load_status: "error",
|
309
342
|
detail: "NOT_FOUND"
|
310
343
|
});
|
311
|
-
}
|
312
344
|
}
|
313
|
-
}
|
314
|
-
try {
|
315
|
-
config = await resolve_config(`${http_protocol}//${host}`, hf_token);
|
316
|
-
const _config = await config_success(config);
|
317
|
-
res(_config);
|
318
|
-
} catch (e) {
|
319
|
-
if (space_id) {
|
320
|
-
check_space_status(
|
321
|
-
space_id,
|
322
|
-
RE_SPACE_NAME.test(space_id) ? "space_name" : "subdomain",
|
323
|
-
handle_space_sucess
|
324
|
-
);
|
325
|
-
} else {
|
326
|
-
if (status_callback)
|
327
|
-
status_callback({
|
328
|
-
status: "error",
|
329
|
-
message: "Could not load this space.",
|
330
|
-
load_status: "error",
|
331
|
-
detail: "NOT_FOUND"
|
332
|
-
});
|
333
345
|
}
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
status_complete
|
354
|
-
|
346
|
+
function predict(endpoint, data, event_data) {
|
347
|
+
let data_returned = false;
|
348
|
+
let status_complete = false;
|
349
|
+
let dependency;
|
350
|
+
if (typeof endpoint === "number") {
|
351
|
+
dependency = config.dependencies[endpoint];
|
352
|
+
} else {
|
353
|
+
const trimmed_endpoint = endpoint.replace(/^\//, "");
|
354
|
+
dependency = config.dependencies[api_map[trimmed_endpoint]];
|
355
|
+
}
|
356
|
+
if (dependency.types.continuous) {
|
357
|
+
throw new Error(
|
358
|
+
"Cannot call predict on this function as it may run forever. Use submit instead"
|
359
|
+
);
|
360
|
+
}
|
361
|
+
return new Promise((res2, rej) => {
|
362
|
+
const app = submit(endpoint, data, event_data);
|
363
|
+
let result;
|
364
|
+
app.on("data", (d) => {
|
365
|
+
if (status_complete) {
|
366
|
+
app.destroy();
|
367
|
+
res2(d);
|
368
|
+
}
|
369
|
+
data_returned = true;
|
370
|
+
result = d;
|
371
|
+
}).on("status", (status) => {
|
372
|
+
if (status.stage === "error")
|
373
|
+
rej(status);
|
374
|
+
if (status.stage === "complete") {
|
375
|
+
status_complete = true;
|
376
|
+
app.destroy();
|
377
|
+
if (data_returned) {
|
378
|
+
res2(result);
|
379
|
+
}
|
380
|
+
}
|
381
|
+
});
|
355
382
|
});
|
356
|
-
});
|
357
|
-
}
|
358
|
-
function submit(endpoint, data, event_data) {
|
359
|
-
let fn_index;
|
360
|
-
let api_info;
|
361
|
-
if (typeof endpoint === "number") {
|
362
|
-
fn_index = endpoint;
|
363
|
-
api_info = api.unnamed_endpoints[fn_index];
|
364
|
-
} else {
|
365
|
-
const trimmed_endpoint = endpoint.replace(/^\//, "");
|
366
|
-
fn_index = api_map[trimmed_endpoint];
|
367
|
-
api_info = api.named_endpoints[endpoint.trim()];
|
368
|
-
}
|
369
|
-
if (typeof fn_index !== "number") {
|
370
|
-
throw new Error(
|
371
|
-
"There is no endpoint matching that name of fn_index matching that number."
|
372
|
-
);
|
373
383
|
}
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
}
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
384
|
+
function submit(endpoint, data, event_data) {
|
385
|
+
let fn_index;
|
386
|
+
let api_info;
|
387
|
+
if (typeof endpoint === "number") {
|
388
|
+
fn_index = endpoint;
|
389
|
+
api_info = api.unnamed_endpoints[fn_index];
|
390
|
+
} else {
|
391
|
+
const trimmed_endpoint = endpoint.replace(/^\//, "");
|
392
|
+
fn_index = api_map[trimmed_endpoint];
|
393
|
+
api_info = api.named_endpoints[endpoint.trim()];
|
394
|
+
}
|
395
|
+
if (typeof fn_index !== "number") {
|
396
|
+
throw new Error(
|
397
|
+
"There is no endpoint matching that name of fn_index matching that number."
|
398
|
+
);
|
399
|
+
}
|
400
|
+
let websocket;
|
401
|
+
const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
|
402
|
+
let payload;
|
403
|
+
let complete = false;
|
404
|
+
const listener_map = {};
|
405
|
+
handle_blob2(
|
406
|
+
`${http_protocol}//${host + config.path}`,
|
407
|
+
data,
|
408
|
+
api_info,
|
409
|
+
hf_token
|
410
|
+
).then((_payload) => {
|
411
|
+
payload = { data: _payload || [], event_data, fn_index };
|
412
|
+
if (skip_queue(fn_index, config)) {
|
413
|
+
fire_event({
|
414
|
+
type: "status",
|
415
|
+
endpoint: _endpoint,
|
416
|
+
stage: "pending",
|
417
|
+
queue: false,
|
418
|
+
fn_index,
|
419
|
+
time: /* @__PURE__ */ new Date()
|
420
|
+
});
|
421
|
+
post_data2(
|
422
|
+
`${http_protocol}//${host + config.path}/run${_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`}`,
|
423
|
+
{
|
424
|
+
...payload,
|
425
|
+
session_hash
|
426
|
+
},
|
427
|
+
hf_token
|
428
|
+
).then(([output, status_code]) => {
|
429
|
+
const data2 = transform_files ? transform_output(
|
430
|
+
output.data,
|
431
|
+
api_info,
|
432
|
+
config.root,
|
433
|
+
config.root_url
|
434
|
+
) : output.data;
|
435
|
+
if (status_code == 200) {
|
436
|
+
fire_event({
|
437
|
+
type: "data",
|
438
|
+
endpoint: _endpoint,
|
439
|
+
fn_index,
|
440
|
+
data: data2,
|
441
|
+
time: /* @__PURE__ */ new Date()
|
442
|
+
});
|
443
|
+
fire_event({
|
444
|
+
type: "status",
|
445
|
+
endpoint: _endpoint,
|
446
|
+
fn_index,
|
447
|
+
stage: "complete",
|
448
|
+
eta: output.average_duration,
|
449
|
+
queue: false,
|
450
|
+
time: /* @__PURE__ */ new Date()
|
451
|
+
});
|
452
|
+
} else {
|
453
|
+
fire_event({
|
454
|
+
type: "status",
|
455
|
+
stage: "error",
|
456
|
+
endpoint: _endpoint,
|
457
|
+
fn_index,
|
458
|
+
message: output.error,
|
459
|
+
queue: false,
|
460
|
+
time: /* @__PURE__ */ new Date()
|
461
|
+
});
|
462
|
+
}
|
463
|
+
}).catch((e) => {
|
427
464
|
fire_event({
|
428
465
|
type: "status",
|
429
466
|
stage: "error",
|
467
|
+
message: e.message,
|
430
468
|
endpoint: _endpoint,
|
431
469
|
fn_index,
|
432
|
-
message: output.error,
|
433
470
|
queue: false,
|
434
471
|
time: /* @__PURE__ */ new Date()
|
435
472
|
});
|
436
|
-
}
|
437
|
-
}
|
473
|
+
});
|
474
|
+
} else {
|
438
475
|
fire_event({
|
439
476
|
type: "status",
|
440
|
-
stage: "
|
441
|
-
|
477
|
+
stage: "pending",
|
478
|
+
queue: true,
|
442
479
|
endpoint: _endpoint,
|
443
480
|
fn_index,
|
444
|
-
queue: false,
|
445
481
|
time: /* @__PURE__ */ new Date()
|
446
482
|
});
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
stage: "pending",
|
452
|
-
queue: true,
|
453
|
-
endpoint: _endpoint,
|
454
|
-
fn_index,
|
455
|
-
time: /* @__PURE__ */ new Date()
|
456
|
-
});
|
457
|
-
let url = new URL(`${ws_protocol}://${host}${config.path}
|
458
|
-
/queue/join`);
|
459
|
-
if (jwt) {
|
460
|
-
url.searchParams.set("__sign", jwt);
|
461
|
-
}
|
462
|
-
websocket = new WebSocket(url);
|
463
|
-
websocket.onclose = (evt) => {
|
464
|
-
if (!evt.wasClean) {
|
465
|
-
fire_event({
|
466
|
-
type: "status",
|
467
|
-
stage: "error",
|
468
|
-
message: BROKEN_CONNECTION_MSG,
|
469
|
-
queue: true,
|
470
|
-
endpoint: _endpoint,
|
471
|
-
fn_index,
|
472
|
-
time: /* @__PURE__ */ new Date()
|
473
|
-
});
|
483
|
+
let url = new URL(`${ws_protocol}://${host}${config.path}
|
484
|
+
/queue/join`);
|
485
|
+
if (jwt) {
|
486
|
+
url.searchParams.set("__sign", jwt);
|
474
487
|
}
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
...status
|
489
|
-
});
|
490
|
-
if (status.stage === "error") {
|
491
|
-
websocket.close();
|
488
|
+
websocket = new WebSocket(url);
|
489
|
+
websocket.onclose = (evt) => {
|
490
|
+
if (!evt.wasClean) {
|
491
|
+
fire_event({
|
492
|
+
type: "status",
|
493
|
+
stage: "error",
|
494
|
+
broken: true,
|
495
|
+
message: BROKEN_CONNECTION_MSG,
|
496
|
+
queue: true,
|
497
|
+
endpoint: _endpoint,
|
498
|
+
fn_index,
|
499
|
+
time: /* @__PURE__ */ new Date()
|
500
|
+
});
|
492
501
|
}
|
493
|
-
}
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
502
|
+
};
|
503
|
+
websocket.onmessage = function(event) {
|
504
|
+
const _data = JSON.parse(event.data);
|
505
|
+
const { type, status, data: data2 } = handle_message(
|
506
|
+
_data,
|
507
|
+
last_status[fn_index]
|
508
|
+
);
|
509
|
+
if (type === "update" && status && !complete) {
|
510
|
+
fire_event({
|
511
|
+
type: "status",
|
512
|
+
endpoint: _endpoint,
|
513
|
+
fn_index,
|
514
|
+
time: /* @__PURE__ */ new Date(),
|
515
|
+
...status
|
516
|
+
});
|
517
|
+
if (status.stage === "error") {
|
518
|
+
websocket.close();
|
519
|
+
}
|
520
|
+
} else if (type === "hash") {
|
521
|
+
websocket.send(JSON.stringify({ fn_index, session_hash }));
|
522
|
+
return;
|
523
|
+
} else if (type === "data") {
|
524
|
+
websocket.send(JSON.stringify({ ...payload, session_hash }));
|
525
|
+
} else if (type === "complete") {
|
526
|
+
complete = status;
|
527
|
+
} else if (type === "log") {
|
528
|
+
fire_event({
|
529
|
+
type: "log",
|
530
|
+
log: data2.log,
|
531
|
+
level: data2.level,
|
532
|
+
endpoint: _endpoint,
|
533
|
+
fn_index
|
534
|
+
});
|
535
|
+
} else if (type === "generating") {
|
525
536
|
fire_event({
|
526
537
|
type: "status",
|
527
538
|
time: /* @__PURE__ */ new Date(),
|
528
|
-
...
|
539
|
+
...status,
|
529
540
|
stage: status == null ? void 0 : status.stage,
|
530
541
|
queue: true,
|
531
542
|
endpoint: _endpoint,
|
532
543
|
fn_index
|
533
544
|
});
|
534
|
-
websocket.close();
|
535
545
|
}
|
546
|
+
if (data2) {
|
547
|
+
fire_event({
|
548
|
+
type: "data",
|
549
|
+
time: /* @__PURE__ */ new Date(),
|
550
|
+
data: transform_files ? transform_output(
|
551
|
+
data2.data,
|
552
|
+
api_info,
|
553
|
+
config.root,
|
554
|
+
config.root_url
|
555
|
+
) : data2.data,
|
556
|
+
endpoint: _endpoint,
|
557
|
+
fn_index
|
558
|
+
});
|
559
|
+
if (complete) {
|
560
|
+
fire_event({
|
561
|
+
type: "status",
|
562
|
+
time: /* @__PURE__ */ new Date(),
|
563
|
+
...complete,
|
564
|
+
stage: status == null ? void 0 : status.stage,
|
565
|
+
queue: true,
|
566
|
+
endpoint: _endpoint,
|
567
|
+
fn_index
|
568
|
+
});
|
569
|
+
websocket.close();
|
570
|
+
}
|
571
|
+
}
|
572
|
+
};
|
573
|
+
if (semiver(config.version || "2.0.0", "3.6") < 0) {
|
574
|
+
addEventListener(
|
575
|
+
"open",
|
576
|
+
() => websocket.send(JSON.stringify({ hash: session_hash }))
|
577
|
+
);
|
536
578
|
}
|
579
|
+
}
|
580
|
+
});
|
581
|
+
function fire_event(event) {
|
582
|
+
const narrowed_listener_map = listener_map;
|
583
|
+
const listeners = narrowed_listener_map[event.type] || [];
|
584
|
+
listeners == null ? void 0 : listeners.forEach((l) => l(event));
|
585
|
+
}
|
586
|
+
function on(eventType, listener) {
|
587
|
+
const narrowed_listener_map = listener_map;
|
588
|
+
const listeners = narrowed_listener_map[eventType] || [];
|
589
|
+
narrowed_listener_map[eventType] = listeners;
|
590
|
+
listeners == null ? void 0 : listeners.push(listener);
|
591
|
+
return { on, off, cancel, destroy };
|
592
|
+
}
|
593
|
+
function off(eventType, listener) {
|
594
|
+
const narrowed_listener_map = listener_map;
|
595
|
+
let listeners = narrowed_listener_map[eventType] || [];
|
596
|
+
listeners = listeners == null ? void 0 : listeners.filter((l) => l !== listener);
|
597
|
+
narrowed_listener_map[eventType] = listeners;
|
598
|
+
return { on, off, cancel, destroy };
|
599
|
+
}
|
600
|
+
async function cancel() {
|
601
|
+
const _status = {
|
602
|
+
stage: "complete",
|
603
|
+
queue: false,
|
604
|
+
time: /* @__PURE__ */ new Date()
|
537
605
|
};
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
606
|
+
complete = _status;
|
607
|
+
fire_event({
|
608
|
+
..._status,
|
609
|
+
type: "status",
|
610
|
+
endpoint: _endpoint,
|
611
|
+
fn_index
|
612
|
+
});
|
613
|
+
if (websocket && websocket.readyState === 0) {
|
614
|
+
websocket.addEventListener("open", () => {
|
615
|
+
websocket.close();
|
616
|
+
});
|
617
|
+
} else {
|
618
|
+
websocket.close();
|
619
|
+
}
|
620
|
+
try {
|
621
|
+
await fetch_implementation(
|
622
|
+
`${http_protocol}//${host + config.path}/reset`,
|
623
|
+
{
|
624
|
+
headers: { "Content-Type": "application/json" },
|
625
|
+
method: "POST",
|
626
|
+
body: JSON.stringify({ fn_index, session_hash })
|
627
|
+
}
|
628
|
+
);
|
629
|
+
} catch (e) {
|
630
|
+
console.warn(
|
631
|
+
"The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable."
|
542
632
|
);
|
543
633
|
}
|
544
634
|
}
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
}
|
558
|
-
function off(eventType, listener) {
|
559
|
-
const narrowed_listener_map = listener_map;
|
560
|
-
let listeners = narrowed_listener_map[eventType] || [];
|
561
|
-
listeners = listeners == null ? void 0 : listeners.filter((l) => l !== listener);
|
562
|
-
narrowed_listener_map[eventType] = listeners;
|
563
|
-
return { on, off, cancel, destroy };
|
564
|
-
}
|
565
|
-
async function cancel() {
|
566
|
-
const _status = {
|
567
|
-
stage: "complete",
|
568
|
-
queue: false,
|
569
|
-
time: /* @__PURE__ */ new Date()
|
635
|
+
function destroy() {
|
636
|
+
for (const event_type in listener_map) {
|
637
|
+
listener_map[event_type].forEach((fn2) => {
|
638
|
+
off(event_type, fn2);
|
639
|
+
});
|
640
|
+
}
|
641
|
+
}
|
642
|
+
return {
|
643
|
+
on,
|
644
|
+
off,
|
645
|
+
cancel,
|
646
|
+
destroy
|
570
647
|
};
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
if (websocket && websocket.readyState === 0) {
|
579
|
-
websocket.addEventListener("open", () => {
|
580
|
-
websocket.close();
|
581
|
-
});
|
582
|
-
} else {
|
583
|
-
websocket.close();
|
648
|
+
}
|
649
|
+
async function view_api(config2) {
|
650
|
+
if (api)
|
651
|
+
return api;
|
652
|
+
const headers = { "Content-Type": "application/json" };
|
653
|
+
if (hf_token) {
|
654
|
+
headers.Authorization = `Bearer ${hf_token}`;
|
584
655
|
}
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
656
|
+
let response;
|
657
|
+
if (semiver(config2.version || "2.0.0", "3.30") < 0) {
|
658
|
+
response = await fetch_implementation(
|
659
|
+
"https://gradio-space-api-fetcher-v2.hf.space/api",
|
660
|
+
{
|
661
|
+
method: "POST",
|
662
|
+
body: JSON.stringify({
|
663
|
+
serialize: false,
|
664
|
+
config: JSON.stringify(config2)
|
665
|
+
}),
|
666
|
+
headers
|
667
|
+
}
|
594
668
|
);
|
595
|
-
}
|
596
|
-
|
597
|
-
|
598
|
-
for (const event_type in listener_map) {
|
599
|
-
listener_map[event_type].forEach((fn2) => {
|
600
|
-
off(event_type, fn2);
|
669
|
+
} else {
|
670
|
+
response = await fetch_implementation(`${config2.root}/info`, {
|
671
|
+
headers
|
601
672
|
});
|
602
673
|
}
|
674
|
+
if (!response.ok) {
|
675
|
+
throw new Error(BROKEN_CONNECTION_MSG);
|
676
|
+
}
|
677
|
+
let api_info = await response.json();
|
678
|
+
if ("api" in api_info) {
|
679
|
+
api_info = api_info.api;
|
680
|
+
}
|
681
|
+
if (api_info.named_endpoints["/predict"] && !api_info.unnamed_endpoints["0"]) {
|
682
|
+
api_info.unnamed_endpoints[0] = api_info.named_endpoints["/predict"];
|
683
|
+
}
|
684
|
+
const x = transform_api_info(api_info, config2, api_map);
|
685
|
+
return x;
|
603
686
|
}
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
api_info = api_info.api;
|
642
|
-
}
|
643
|
-
if (api_info.named_endpoints["/predict"] && !api_info.unnamed_endpoints["0"]) {
|
644
|
-
api_info.unnamed_endpoints[0] = api_info.named_endpoints["/predict"];
|
645
|
-
}
|
646
|
-
const x = transform_api_info(api_info, config2, api_map);
|
647
|
-
return x;
|
648
|
-
}
|
649
|
-
});
|
687
|
+
});
|
688
|
+
}
|
689
|
+
async function handle_blob2(endpoint, data, api_info, token) {
|
690
|
+
const blob_refs = await walk_and_store_blobs(
|
691
|
+
data,
|
692
|
+
void 0,
|
693
|
+
[],
|
694
|
+
true,
|
695
|
+
api_info
|
696
|
+
);
|
697
|
+
return Promise.all(
|
698
|
+
blob_refs.map(async ({ path, blob, data: data2, type }) => {
|
699
|
+
if (blob) {
|
700
|
+
const file_url = (await upload_files2(endpoint, [blob], token)).files[0];
|
701
|
+
return { path, file_url, type };
|
702
|
+
}
|
703
|
+
return { path, base64: data2, type };
|
704
|
+
})
|
705
|
+
).then((r) => {
|
706
|
+
r.forEach(({ path, file_url, base64, type }) => {
|
707
|
+
if (base64) {
|
708
|
+
update_object(data, base64, path);
|
709
|
+
} else if (type === "Gallery") {
|
710
|
+
update_object(data, file_url, path);
|
711
|
+
} else if (file_url) {
|
712
|
+
const o = {
|
713
|
+
is_file: true,
|
714
|
+
name: `${file_url}`,
|
715
|
+
data: null
|
716
|
+
// orig_name: "file.csv"
|
717
|
+
};
|
718
|
+
update_object(data, o, path);
|
719
|
+
}
|
720
|
+
});
|
721
|
+
return data;
|
722
|
+
});
|
723
|
+
}
|
650
724
|
}
|
725
|
+
const { post_data, upload_files, client, handle_blob } = api_factory(fetch);
|
651
726
|
function transform_output(data, api_info, root_url, remote_url) {
|
652
727
|
return data.map((d, i) => {
|
653
728
|
var _a, _b, _c, _d;
|
654
|
-
if (((_b = (_a = api_info.returns) == null ? void 0 : _a[i]) == null ? void 0 : _b.component) === "File") {
|
729
|
+
if (((_b = (_a = api_info == null ? void 0 : api_info.returns) == null ? void 0 : _a[i]) == null ? void 0 : _b.component) === "File") {
|
655
730
|
return normalise_file(d, root_url, remote_url);
|
656
|
-
} else if (((_d = (_c = api_info.returns) == null ? void 0 : _c[i]) == null ? void 0 : _d.component) === "Gallery") {
|
731
|
+
} else if (((_d = (_c = api_info == null ? void 0 : api_info.returns) == null ? void 0 : _c[i]) == null ? void 0 : _d.component) === "Gallery") {
|
657
732
|
return d.map((img) => {
|
658
733
|
return Array.isArray(img) ? [normalise_file(img[0], root_url, remote_url), img[1]] : [normalise_file(img, root_url, remote_url), null];
|
659
734
|
});
|
660
|
-
} else if (typeof d === "object" && d.is_file) {
|
735
|
+
} else if (typeof d === "object" && (d == null ? void 0 : d.is_file)) {
|
661
736
|
return normalise_file(d, root_url, remote_url);
|
662
|
-
} else {
|
663
|
-
return d;
|
664
737
|
}
|
738
|
+
return d;
|
665
739
|
});
|
666
740
|
}
|
667
741
|
function normalise_file(file, root, root_url) {
|
@@ -709,9 +783,8 @@ function get_type(type, component, serializer, signature_type) {
|
|
709
783
|
} else if (serializer === "FileSerializable") {
|
710
784
|
if ((type == null ? void 0 : type.type) === "array") {
|
711
785
|
return signature_type === "parameter" ? "(Blob | File | Buffer)[]" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}[]`;
|
712
|
-
} else {
|
713
|
-
return signature_type === "parameter" ? "Blob | File | Buffer" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}`;
|
714
786
|
}
|
787
|
+
return signature_type === "parameter" ? "Blob | File | Buffer" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}`;
|
715
788
|
} else if (serializer === "GallerySerializable") {
|
716
789
|
return signature_type === "parameter" ? "[(Blob | File | Buffer), (string | null)][]" : `[{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}, (string | null))][]`;
|
717
790
|
}
|
@@ -723,9 +796,8 @@ function get_description(type, serializer) {
|
|
723
796
|
return "array of strings";
|
724
797
|
} else if (serializer === "FileSerializable") {
|
725
798
|
return "array of files or single file";
|
726
|
-
} else {
|
727
|
-
return type.description;
|
728
799
|
}
|
800
|
+
return type.description;
|
729
801
|
}
|
730
802
|
function transform_api_info(api_info, config, api_map) {
|
731
803
|
const new_data = {
|
@@ -775,42 +847,6 @@ async function get_jwt(space, token) {
|
|
775
847
|
return false;
|
776
848
|
}
|
777
849
|
}
|
778
|
-
async function handle_blob(endpoint, data, api_info, token) {
|
779
|
-
const blob_refs = await walk_and_store_blobs(
|
780
|
-
data,
|
781
|
-
void 0,
|
782
|
-
[],
|
783
|
-
true,
|
784
|
-
api_info
|
785
|
-
);
|
786
|
-
return Promise.all(
|
787
|
-
blob_refs.map(async ({ path, blob, data: data2, type }) => {
|
788
|
-
if (blob) {
|
789
|
-
const file_url = (await upload_files(endpoint, [blob], token)).files[0];
|
790
|
-
return { path, file_url, type };
|
791
|
-
} else {
|
792
|
-
return { path, base64: data2, type };
|
793
|
-
}
|
794
|
-
})
|
795
|
-
).then((r) => {
|
796
|
-
r.forEach(({ path, file_url, base64, type }) => {
|
797
|
-
if (base64) {
|
798
|
-
update_object(data, base64, path);
|
799
|
-
} else if (type === "Gallery") {
|
800
|
-
update_object(data, file_url, path);
|
801
|
-
} else if (file_url) {
|
802
|
-
const o = {
|
803
|
-
is_file: true,
|
804
|
-
name: `${file_url}`,
|
805
|
-
data: null
|
806
|
-
// orig_name: "file.csv"
|
807
|
-
};
|
808
|
-
update_object(data, o, path);
|
809
|
-
}
|
810
|
-
});
|
811
|
-
return data;
|
812
|
-
});
|
813
|
-
}
|
814
850
|
function update_object(object, newValue, stack) {
|
815
851
|
while (stack.length > 1) {
|
816
852
|
object = object[stack.shift()];
|
@@ -855,10 +891,9 @@ async function walk_and_store_blobs(param, type = void 0, path = [], root = fals
|
|
855
891
|
const buffer = await param.arrayBuffer();
|
856
892
|
data = Buffer.from(buffer).toString("base64");
|
857
893
|
}
|
858
|
-
return [{ path, data, type }];
|
859
|
-
} else {
|
860
|
-
return [{ path, blob: param, type }];
|
894
|
+
return [{ path, data, type, blob: false }];
|
861
895
|
}
|
896
|
+
return [{ path, blob: param, type, data: false }];
|
862
897
|
} else if (typeof param === "object") {
|
863
898
|
let blob_refs = [];
|
864
899
|
for (let key in param) {
|
@@ -877,9 +912,8 @@ async function walk_and_store_blobs(param, type = void 0, path = [], root = fals
|
|
877
912
|
}
|
878
913
|
}
|
879
914
|
return blob_refs;
|
880
|
-
} else {
|
881
|
-
return [];
|
882
915
|
}
|
916
|
+
return [];
|
883
917
|
}
|
884
918
|
function image_to_data_uri(blob) {
|
885
919
|
return new Promise((resolve, _) => {
|
@@ -892,7 +926,7 @@ function skip_queue(id, config) {
|
|
892
926
|
var _a, _b, _c, _d;
|
893
927
|
return !(((_b = (_a = config == null ? void 0 : config.dependencies) == null ? void 0 : _a[id]) == null ? void 0 : _b.queue) === null ? config.enable_queue : (_d = (_c = config == null ? void 0 : config.dependencies) == null ? void 0 : _c[id]) == null ? void 0 : _d.queue) || false;
|
894
928
|
}
|
895
|
-
async function resolve_config(endpoint, token) {
|
929
|
+
async function resolve_config(fetch_implementation, endpoint, token) {
|
896
930
|
const headers = {};
|
897
931
|
if (token) {
|
898
932
|
headers.Authorization = `Bearer ${token}`;
|
@@ -903,15 +937,16 @@ async function resolve_config(endpoint, token) {
|
|
903
937
|
config.root = endpoint + config.root;
|
904
938
|
return { ...config, path };
|
905
939
|
} else if (endpoint) {
|
906
|
-
let response = await
|
940
|
+
let response = await fetch_implementation(`${endpoint}/config`, {
|
941
|
+
headers
|
942
|
+
});
|
907
943
|
if (response.status === 200) {
|
908
944
|
const config = await response.json();
|
909
945
|
config.path = config.path ?? "";
|
910
946
|
config.root = endpoint;
|
911
947
|
return config;
|
912
|
-
} else {
|
913
|
-
throw new Error("Could not get config.");
|
914
948
|
}
|
949
|
+
throw new Error("Could not get config.");
|
915
950
|
}
|
916
951
|
throw new Error("No config or app endpoint found");
|
917
952
|
}
|
@@ -1036,6 +1071,8 @@ function handle_message(data, last_status) {
|
|
1036
1071
|
success: data.success
|
1037
1072
|
}
|
1038
1073
|
};
|
1074
|
+
case "log":
|
1075
|
+
return { type: "log", data };
|
1039
1076
|
case "process_generating":
|
1040
1077
|
return {
|
1041
1078
|
type: "generating",
|
@@ -1061,20 +1098,19 @@ function handle_message(data, last_status) {
|
|
1061
1098
|
success: data.success
|
1062
1099
|
}
|
1063
1100
|
};
|
1064
|
-
} else {
|
1065
|
-
return {
|
1066
|
-
type: "complete",
|
1067
|
-
status: {
|
1068
|
-
queue,
|
1069
|
-
message: !data.success ? data.output.error : void 0,
|
1070
|
-
stage: data.success ? "complete" : "error",
|
1071
|
-
code: data.code,
|
1072
|
-
progress_data: data.progress_data,
|
1073
|
-
eta: data.output.average_duration
|
1074
|
-
},
|
1075
|
-
data: data.success ? data.output : null
|
1076
|
-
};
|
1077
1101
|
}
|
1102
|
+
return {
|
1103
|
+
type: "complete",
|
1104
|
+
status: {
|
1105
|
+
queue,
|
1106
|
+
message: !data.success ? data.output.error : void 0,
|
1107
|
+
stage: data.success ? "complete" : "error",
|
1108
|
+
code: data.code,
|
1109
|
+
progress_data: data.progress_data,
|
1110
|
+
eta: data.output.average_duration
|
1111
|
+
},
|
1112
|
+
data: data.success ? data.output : null
|
1113
|
+
};
|
1078
1114
|
case "process_starts":
|
1079
1115
|
return {
|
1080
1116
|
type: "update",
|
@@ -1091,6 +1127,7 @@ function handle_message(data, last_status) {
|
|
1091
1127
|
return { type: "none", status: { stage: "error", queue } };
|
1092
1128
|
}
|
1093
1129
|
export {
|
1130
|
+
api_factory,
|
1094
1131
|
client,
|
1095
1132
|
duplicate,
|
1096
1133
|
post_data,
|