@gradio/client 0.1.2 → 0.1.4
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 +12 -0
- package/dist/client.d.ts +47 -10
- 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 +426 -401
- package/dist/types.d.ts +10 -10
- package/dist/types.d.ts.map +1 -1
- package/dist/{wrapper-b7460963.js → wrapper-6f348d45.js} +16 -27
- package/package.json +3 -3
- package/src/client.ts +521 -491
- package/src/globals.d.ts +1 -1
- package/src/index.ts +7 -1
- package/src/types.ts +8 -3
package/dist/index.js
CHANGED
@@ -161,45 +161,7 @@ const hardware_types = [
|
|
161
161
|
];
|
162
162
|
const QUEUE_FULL_MSG = "This application is too busy. Keep trying!";
|
163
163
|
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
164
|
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
165
|
async function duplicate(app_reference, options) {
|
204
166
|
const { hf_token, private: _private, hardware, timeout } = options;
|
205
167
|
if (hardware && !hardware_types.includes(hardware)) {
|
@@ -254,371 +216,423 @@ async function duplicate(app_reference, options) {
|
|
254
216
|
throw new Error(e);
|
255
217
|
}
|
256
218
|
}
|
257
|
-
|
258
|
-
return
|
259
|
-
|
260
|
-
const
|
261
|
-
|
262
|
-
|
263
|
-
view_api
|
264
|
-
// duplicate
|
265
|
-
};
|
266
|
-
let transform_files = normalise_files ?? true;
|
267
|
-
if (typeof window === "undefined" || !("WebSocket" in window)) {
|
268
|
-
const ws = await import("./wrapper-b7460963.js");
|
269
|
-
NodeBlob = (await import("node:buffer")).Blob;
|
270
|
-
global.WebSocket = ws.WebSocket;
|
219
|
+
function api_factory(fetch_implementation) {
|
220
|
+
return { post_data: post_data2, upload_files: upload_files2, client: client2, handle_blob: handle_blob2 };
|
221
|
+
async function post_data2(url, body, token) {
|
222
|
+
const headers = { "Content-Type": "application/json" };
|
223
|
+
if (token) {
|
224
|
+
headers.Authorization = `Bearer ${token}`;
|
271
225
|
}
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
226
|
+
try {
|
227
|
+
var response = await fetch_implementation(url, {
|
228
|
+
method: "POST",
|
229
|
+
body: JSON.stringify(body),
|
230
|
+
headers
|
231
|
+
});
|
232
|
+
} catch (e) {
|
233
|
+
return [{ error: BROKEN_CONNECTION_MSG }, 500];
|
280
234
|
}
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
}
|
289
|
-
return {
|
290
|
-
config,
|
291
|
-
...return_obj
|
292
|
-
};
|
235
|
+
const output = await response.json();
|
236
|
+
return [output, response.status];
|
237
|
+
}
|
238
|
+
async function upload_files2(root, files, token) {
|
239
|
+
const headers = {};
|
240
|
+
if (token) {
|
241
|
+
headers.Authorization = `Bearer ${token}`;
|
293
242
|
}
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
243
|
+
const formData = new FormData();
|
244
|
+
files.forEach((file) => {
|
245
|
+
formData.append("files", file);
|
246
|
+
});
|
247
|
+
try {
|
248
|
+
var response = await fetch_implementation(`${root}/upload`, {
|
249
|
+
method: "POST",
|
250
|
+
body: formData,
|
251
|
+
headers
|
252
|
+
});
|
253
|
+
} catch (e) {
|
254
|
+
return { error: BROKEN_CONNECTION_MSG };
|
255
|
+
}
|
256
|
+
const output = await response.json();
|
257
|
+
return { files: output };
|
258
|
+
}
|
259
|
+
async function client2(app_reference, options = { normalise_files: true }) {
|
260
|
+
return new Promise(async (res) => {
|
261
|
+
const { status_callback, hf_token, normalise_files } = options;
|
262
|
+
const return_obj = {
|
263
|
+
predict,
|
264
|
+
submit,
|
265
|
+
view_api
|
266
|
+
// duplicate
|
267
|
+
};
|
268
|
+
const transform_files = normalise_files ?? true;
|
269
|
+
if (typeof window === "undefined" || !("WebSocket" in window)) {
|
270
|
+
const ws = await import("./wrapper-6f348d45.js");
|
271
|
+
NodeBlob = (await import("node:buffer")).Blob;
|
272
|
+
global.WebSocket = ws.WebSocket;
|
273
|
+
}
|
274
|
+
const { ws_protocol, http_protocol, host, space_id } = await process_endpoint(app_reference, hf_token);
|
275
|
+
const session_hash = Math.random().toString(36).substring(2);
|
276
|
+
const last_status = {};
|
277
|
+
let config;
|
278
|
+
let api_map = {};
|
279
|
+
let jwt = false;
|
280
|
+
if (hf_token && space_id) {
|
281
|
+
jwt = await get_jwt(space_id, hf_token);
|
282
|
+
}
|
283
|
+
async function config_success(_config) {
|
284
|
+
config = _config;
|
285
|
+
api_map = map_names_to_ids((_config == null ? void 0 : _config.dependencies) || []);
|
299
286
|
try {
|
300
|
-
|
301
|
-
const _config = await config_success(config);
|
302
|
-
res(_config);
|
287
|
+
api = await view_api(config);
|
303
288
|
} catch (e) {
|
304
|
-
|
289
|
+
console.error(`Could not get api details: ${e.message}`);
|
290
|
+
}
|
291
|
+
return {
|
292
|
+
config,
|
293
|
+
...return_obj
|
294
|
+
};
|
295
|
+
}
|
296
|
+
let api;
|
297
|
+
async function handle_space_sucess(status) {
|
298
|
+
if (status_callback)
|
299
|
+
status_callback(status);
|
300
|
+
if (status.status === "running")
|
301
|
+
try {
|
302
|
+
config = await resolve_config(
|
303
|
+
fetch_implementation,
|
304
|
+
`${http_protocol}//${host}`,
|
305
|
+
hf_token
|
306
|
+
);
|
307
|
+
const _config = await config_success(config);
|
308
|
+
res(_config);
|
309
|
+
} catch (e) {
|
310
|
+
console.error(e);
|
311
|
+
if (status_callback) {
|
312
|
+
status_callback({
|
313
|
+
status: "error",
|
314
|
+
message: "Could not load this space.",
|
315
|
+
load_status: "error",
|
316
|
+
detail: "NOT_FOUND"
|
317
|
+
});
|
318
|
+
}
|
319
|
+
}
|
320
|
+
}
|
321
|
+
try {
|
322
|
+
config = await resolve_config(
|
323
|
+
fetch_implementation,
|
324
|
+
`${http_protocol}//${host}`,
|
325
|
+
hf_token
|
326
|
+
);
|
327
|
+
const _config = await config_success(config);
|
328
|
+
res(_config);
|
329
|
+
} catch (e) {
|
330
|
+
console.error(e);
|
331
|
+
if (space_id) {
|
332
|
+
check_space_status(
|
333
|
+
space_id,
|
334
|
+
RE_SPACE_NAME.test(space_id) ? "space_name" : "subdomain",
|
335
|
+
handle_space_sucess
|
336
|
+
);
|
337
|
+
} else {
|
338
|
+
if (status_callback)
|
305
339
|
status_callback({
|
306
340
|
status: "error",
|
307
341
|
message: "Could not load this space.",
|
308
342
|
load_status: "error",
|
309
343
|
detail: "NOT_FOUND"
|
310
344
|
});
|
311
|
-
}
|
312
345
|
}
|
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
346
|
}
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
}
|
347
|
+
function predict(endpoint, data, event_data) {
|
348
|
+
let data_returned = false;
|
349
|
+
let status_complete = false;
|
350
|
+
return new Promise((res2, rej) => {
|
351
|
+
const app = submit(endpoint, data, event_data);
|
352
|
+
app.on("data", (d) => {
|
353
|
+
data_returned = true;
|
354
|
+
if (status_complete) {
|
355
|
+
app.destroy();
|
356
|
+
}
|
357
|
+
res2(d);
|
358
|
+
}).on("status", (status) => {
|
359
|
+
if (status.stage === "error")
|
360
|
+
rej(status);
|
361
|
+
if (status.stage === "complete" && data_returned) {
|
362
|
+
app.destroy();
|
363
|
+
}
|
364
|
+
if (status.stage === "complete") {
|
365
|
+
status_complete = true;
|
366
|
+
}
|
367
|
+
});
|
355
368
|
});
|
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
369
|
}
|
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
|
-
|
370
|
+
function submit(endpoint, data, event_data) {
|
371
|
+
let fn_index;
|
372
|
+
let api_info;
|
373
|
+
if (typeof endpoint === "number") {
|
374
|
+
fn_index = endpoint;
|
375
|
+
api_info = api.unnamed_endpoints[fn_index];
|
376
|
+
} else {
|
377
|
+
const trimmed_endpoint = endpoint.replace(/^\//, "");
|
378
|
+
fn_index = api_map[trimmed_endpoint];
|
379
|
+
api_info = api.named_endpoints[endpoint.trim()];
|
380
|
+
}
|
381
|
+
if (typeof fn_index !== "number") {
|
382
|
+
throw new Error(
|
383
|
+
"There is no endpoint matching that name of fn_index matching that number."
|
384
|
+
);
|
385
|
+
}
|
386
|
+
let websocket;
|
387
|
+
const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
|
388
|
+
let payload;
|
389
|
+
let complete = false;
|
390
|
+
const listener_map = {};
|
391
|
+
handle_blob2(
|
392
|
+
`${http_protocol}//${host + config.path}`,
|
393
|
+
data,
|
394
|
+
api_info,
|
395
|
+
hf_token
|
396
|
+
).then((_payload) => {
|
397
|
+
payload = { data: _payload || [], event_data, fn_index };
|
398
|
+
if (skip_queue(fn_index, config)) {
|
399
|
+
fire_event({
|
400
|
+
type: "status",
|
401
|
+
endpoint: _endpoint,
|
402
|
+
stage: "pending",
|
403
|
+
queue: false,
|
404
|
+
fn_index,
|
405
|
+
time: /* @__PURE__ */ new Date()
|
406
|
+
});
|
407
|
+
post_data2(
|
408
|
+
`${http_protocol}//${host + config.path}/run${_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`}`,
|
409
|
+
{
|
410
|
+
...payload,
|
411
|
+
session_hash
|
412
|
+
},
|
413
|
+
hf_token
|
414
|
+
).then(([output, status_code]) => {
|
415
|
+
const data2 = transform_files ? transform_output(
|
416
|
+
output.data,
|
417
|
+
api_info,
|
418
|
+
config.root,
|
419
|
+
config.root_url
|
420
|
+
) : output.data;
|
421
|
+
if (status_code == 200) {
|
422
|
+
fire_event({
|
423
|
+
type: "data",
|
424
|
+
endpoint: _endpoint,
|
425
|
+
fn_index,
|
426
|
+
data: data2,
|
427
|
+
time: /* @__PURE__ */ new Date()
|
428
|
+
});
|
429
|
+
fire_event({
|
430
|
+
type: "status",
|
431
|
+
endpoint: _endpoint,
|
432
|
+
fn_index,
|
433
|
+
stage: "complete",
|
434
|
+
eta: output.average_duration,
|
435
|
+
queue: false,
|
436
|
+
time: /* @__PURE__ */ new Date()
|
437
|
+
});
|
438
|
+
} else {
|
439
|
+
fire_event({
|
440
|
+
type: "status",
|
441
|
+
stage: "error",
|
442
|
+
endpoint: _endpoint,
|
443
|
+
fn_index,
|
444
|
+
message: output.error,
|
445
|
+
queue: false,
|
446
|
+
time: /* @__PURE__ */ new Date()
|
447
|
+
});
|
448
|
+
}
|
449
|
+
}).catch((e) => {
|
427
450
|
fire_event({
|
428
451
|
type: "status",
|
429
452
|
stage: "error",
|
453
|
+
message: e.message,
|
430
454
|
endpoint: _endpoint,
|
431
455
|
fn_index,
|
432
|
-
message: output.error,
|
433
456
|
queue: false,
|
434
457
|
time: /* @__PURE__ */ new Date()
|
435
458
|
});
|
436
|
-
}
|
437
|
-
}
|
459
|
+
});
|
460
|
+
} else {
|
438
461
|
fire_event({
|
439
462
|
type: "status",
|
440
|
-
stage: "
|
441
|
-
|
463
|
+
stage: "pending",
|
464
|
+
queue: true,
|
442
465
|
endpoint: _endpoint,
|
443
466
|
fn_index,
|
444
|
-
queue: false,
|
445
467
|
time: /* @__PURE__ */ new Date()
|
446
468
|
});
|
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
|
-
});
|
469
|
+
let url = new URL(`${ws_protocol}://${host}${config.path}
|
470
|
+
/queue/join`);
|
471
|
+
if (jwt) {
|
472
|
+
url.searchParams.set("__sign", jwt);
|
474
473
|
}
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
time: /* @__PURE__ */ new Date(),
|
488
|
-
...status
|
489
|
-
});
|
490
|
-
if (status.stage === "error") {
|
491
|
-
websocket.close();
|
474
|
+
websocket = new WebSocket(url);
|
475
|
+
websocket.onclose = (evt) => {
|
476
|
+
if (!evt.wasClean) {
|
477
|
+
fire_event({
|
478
|
+
type: "status",
|
479
|
+
stage: "error",
|
480
|
+
message: BROKEN_CONNECTION_MSG,
|
481
|
+
queue: true,
|
482
|
+
endpoint: _endpoint,
|
483
|
+
fn_index,
|
484
|
+
time: /* @__PURE__ */ new Date()
|
485
|
+
});
|
492
486
|
}
|
493
|
-
}
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
fire_event({
|
502
|
-
type: "status",
|
503
|
-
time: /* @__PURE__ */ new Date(),
|
504
|
-
...status,
|
505
|
-
stage: status == null ? void 0 : status.stage,
|
506
|
-
queue: true,
|
507
|
-
endpoint: _endpoint,
|
508
|
-
fn_index
|
509
|
-
});
|
510
|
-
}
|
511
|
-
if (data2) {
|
512
|
-
fire_event({
|
513
|
-
type: "data",
|
514
|
-
time: /* @__PURE__ */ new Date(),
|
515
|
-
data: transform_files ? transform_output(
|
516
|
-
data2.data,
|
517
|
-
api_info,
|
518
|
-
config.root,
|
519
|
-
config.root_url
|
520
|
-
) : data2.data,
|
521
|
-
endpoint: _endpoint,
|
522
|
-
fn_index
|
523
|
-
});
|
524
|
-
if (complete) {
|
487
|
+
};
|
488
|
+
websocket.onmessage = function(event) {
|
489
|
+
const _data = JSON.parse(event.data);
|
490
|
+
const { type, status, data: data2 } = handle_message(
|
491
|
+
_data,
|
492
|
+
last_status[fn_index]
|
493
|
+
);
|
494
|
+
if (type === "update" && status && !complete) {
|
525
495
|
fire_event({
|
526
496
|
type: "status",
|
497
|
+
endpoint: _endpoint,
|
498
|
+
fn_index,
|
527
499
|
time: /* @__PURE__ */ new Date(),
|
528
|
-
...
|
500
|
+
...status
|
501
|
+
});
|
502
|
+
if (status.stage === "error") {
|
503
|
+
websocket.close();
|
504
|
+
}
|
505
|
+
} else if (type === "hash") {
|
506
|
+
websocket.send(JSON.stringify({ fn_index, session_hash }));
|
507
|
+
return;
|
508
|
+
} else if (type === "data") {
|
509
|
+
websocket.send(JSON.stringify({ ...payload, session_hash }));
|
510
|
+
} else if (type === "complete") {
|
511
|
+
complete = status;
|
512
|
+
} else if (type === "generating") {
|
513
|
+
fire_event({
|
514
|
+
type: "status",
|
515
|
+
time: /* @__PURE__ */ new Date(),
|
516
|
+
...status,
|
529
517
|
stage: status == null ? void 0 : status.stage,
|
530
518
|
queue: true,
|
531
519
|
endpoint: _endpoint,
|
532
520
|
fn_index
|
533
521
|
});
|
534
|
-
websocket.close();
|
535
522
|
}
|
523
|
+
if (data2) {
|
524
|
+
fire_event({
|
525
|
+
type: "data",
|
526
|
+
time: /* @__PURE__ */ new Date(),
|
527
|
+
data: transform_files ? transform_output(
|
528
|
+
data2.data,
|
529
|
+
api_info,
|
530
|
+
config.root,
|
531
|
+
config.root_url
|
532
|
+
) : data2.data,
|
533
|
+
endpoint: _endpoint,
|
534
|
+
fn_index
|
535
|
+
});
|
536
|
+
if (complete) {
|
537
|
+
fire_event({
|
538
|
+
type: "status",
|
539
|
+
time: /* @__PURE__ */ new Date(),
|
540
|
+
...complete,
|
541
|
+
stage: status == null ? void 0 : status.stage,
|
542
|
+
queue: true,
|
543
|
+
endpoint: _endpoint,
|
544
|
+
fn_index
|
545
|
+
});
|
546
|
+
websocket.close();
|
547
|
+
}
|
548
|
+
}
|
549
|
+
};
|
550
|
+
if (semiver(config.version || "2.0.0", "3.6") < 0) {
|
551
|
+
addEventListener(
|
552
|
+
"open",
|
553
|
+
() => websocket.send(JSON.stringify({ hash: session_hash }))
|
554
|
+
);
|
536
555
|
}
|
537
|
-
};
|
538
|
-
if (semiver(config.version || "2.0.0", "3.6") < 0) {
|
539
|
-
addEventListener(
|
540
|
-
"open",
|
541
|
-
() => websocket.send(JSON.stringify({ hash: session_hash }))
|
542
|
-
);
|
543
556
|
}
|
544
|
-
}
|
545
|
-
});
|
546
|
-
function fire_event(event) {
|
547
|
-
const narrowed_listener_map = listener_map;
|
548
|
-
let listeners = narrowed_listener_map[event.type] || [];
|
549
|
-
listeners == null ? void 0 : listeners.forEach((l) => l(event));
|
550
|
-
}
|
551
|
-
function on(eventType, listener) {
|
552
|
-
const narrowed_listener_map = listener_map;
|
553
|
-
let listeners = narrowed_listener_map[eventType] || [];
|
554
|
-
narrowed_listener_map[eventType] = listeners;
|
555
|
-
listeners == null ? void 0 : listeners.push(listener);
|
556
|
-
return { on, off, cancel, destroy };
|
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()
|
570
|
-
};
|
571
|
-
complete = _status;
|
572
|
-
fire_event({
|
573
|
-
..._status,
|
574
|
-
type: "status",
|
575
|
-
endpoint: _endpoint,
|
576
|
-
fn_index
|
577
557
|
});
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
} else {
|
583
|
-
websocket.close();
|
558
|
+
function fire_event(event) {
|
559
|
+
const narrowed_listener_map = listener_map;
|
560
|
+
const listeners = narrowed_listener_map[event.type] || [];
|
561
|
+
listeners == null ? void 0 : listeners.forEach((l) => l(event));
|
584
562
|
}
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
}
|
591
|
-
} catch (e) {
|
592
|
-
console.warn(
|
593
|
-
"The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable."
|
594
|
-
);
|
563
|
+
function on(eventType, listener) {
|
564
|
+
const narrowed_listener_map = listener_map;
|
565
|
+
const listeners = narrowed_listener_map[eventType] || [];
|
566
|
+
narrowed_listener_map[eventType] = listeners;
|
567
|
+
listeners == null ? void 0 : listeners.push(listener);
|
568
|
+
return { on, off, cancel, destroy };
|
595
569
|
}
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
570
|
+
function off(eventType, listener) {
|
571
|
+
const narrowed_listener_map = listener_map;
|
572
|
+
let listeners = narrowed_listener_map[eventType] || [];
|
573
|
+
listeners = listeners == null ? void 0 : listeners.filter((l) => l !== listener);
|
574
|
+
narrowed_listener_map[eventType] = listeners;
|
575
|
+
return { on, off, cancel, destroy };
|
576
|
+
}
|
577
|
+
async function cancel() {
|
578
|
+
const _status = {
|
579
|
+
stage: "complete",
|
580
|
+
queue: false,
|
581
|
+
time: /* @__PURE__ */ new Date()
|
582
|
+
};
|
583
|
+
complete = _status;
|
584
|
+
fire_event({
|
585
|
+
..._status,
|
586
|
+
type: "status",
|
587
|
+
endpoint: _endpoint,
|
588
|
+
fn_index
|
601
589
|
});
|
590
|
+
if (websocket && websocket.readyState === 0) {
|
591
|
+
websocket.addEventListener("open", () => {
|
592
|
+
websocket.close();
|
593
|
+
});
|
594
|
+
} else {
|
595
|
+
websocket.close();
|
596
|
+
}
|
597
|
+
try {
|
598
|
+
await fetch_implementation(
|
599
|
+
`${http_protocol}//${host + config.path}/reset`,
|
600
|
+
{
|
601
|
+
headers: { "Content-Type": "application/json" },
|
602
|
+
method: "POST",
|
603
|
+
body: JSON.stringify({ fn_index, session_hash })
|
604
|
+
}
|
605
|
+
);
|
606
|
+
} catch (e) {
|
607
|
+
console.warn(
|
608
|
+
"The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable."
|
609
|
+
);
|
610
|
+
}
|
602
611
|
}
|
612
|
+
function destroy() {
|
613
|
+
for (const event_type in listener_map) {
|
614
|
+
listener_map[event_type].forEach((fn2) => {
|
615
|
+
off(event_type, fn2);
|
616
|
+
});
|
617
|
+
}
|
618
|
+
}
|
619
|
+
return {
|
620
|
+
on,
|
621
|
+
off,
|
622
|
+
cancel,
|
623
|
+
destroy
|
624
|
+
};
|
603
625
|
}
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
async function view_api(config2) {
|
612
|
-
if (api)
|
613
|
-
return api;
|
614
|
-
const headers = { "Content-Type": "application/json" };
|
615
|
-
if (hf_token) {
|
616
|
-
headers.Authorization = `Bearer ${hf_token}`;
|
617
|
-
}
|
618
|
-
try {
|
626
|
+
async function view_api(config2) {
|
627
|
+
if (api)
|
628
|
+
return api;
|
629
|
+
const headers = { "Content-Type": "application/json" };
|
630
|
+
if (hf_token) {
|
631
|
+
headers.Authorization = `Bearer ${hf_token}`;
|
632
|
+
}
|
619
633
|
let response;
|
620
634
|
if (semiver(config2.version || "2.0.0", "3.30") < 0) {
|
621
|
-
response = await
|
635
|
+
response = await fetch_implementation(
|
622
636
|
"https://gradio-space-api-fetcher-v2.hf.space/api",
|
623
637
|
{
|
624
638
|
method: "POST",
|
@@ -630,10 +644,13 @@ async function client(app_reference, options = { normalise_files: true }) {
|
|
630
644
|
}
|
631
645
|
);
|
632
646
|
} else {
|
633
|
-
response = await
|
647
|
+
response = await fetch_implementation(`${config2.root}/info`, {
|
634
648
|
headers
|
635
649
|
});
|
636
650
|
}
|
651
|
+
if (!response.ok) {
|
652
|
+
throw new Error(BROKEN_CONNECTION_MSG);
|
653
|
+
}
|
637
654
|
let api_info = await response.json();
|
638
655
|
if ("api" in api_info) {
|
639
656
|
api_info = api_info.api;
|
@@ -643,14 +660,49 @@ async function client(app_reference, options = { normalise_files: true }) {
|
|
643
660
|
}
|
644
661
|
const x = transform_api_info(api_info, config2, api_map);
|
645
662
|
return x;
|
646
|
-
} catch (e) {
|
647
|
-
return [{ error: BROKEN_CONNECTION_MSG }, 500];
|
648
663
|
}
|
649
|
-
}
|
650
|
-
}
|
664
|
+
});
|
665
|
+
}
|
666
|
+
async function handle_blob2(endpoint, data, api_info, token) {
|
667
|
+
const blob_refs = await walk_and_store_blobs(
|
668
|
+
data,
|
669
|
+
void 0,
|
670
|
+
[],
|
671
|
+
true,
|
672
|
+
api_info
|
673
|
+
);
|
674
|
+
return Promise.all(
|
675
|
+
blob_refs.map(async ({ path, blob, data: data2, type }) => {
|
676
|
+
if (blob) {
|
677
|
+
const file_url = (await upload_files2(endpoint, [blob], token)).files[0];
|
678
|
+
return { path, file_url, type };
|
679
|
+
} else {
|
680
|
+
return { path, base64: data2, type };
|
681
|
+
}
|
682
|
+
})
|
683
|
+
).then((r) => {
|
684
|
+
r.forEach(({ path, file_url, base64, type }) => {
|
685
|
+
if (base64) {
|
686
|
+
update_object(data, base64, path);
|
687
|
+
} else if (type === "Gallery") {
|
688
|
+
update_object(data, file_url, path);
|
689
|
+
} else if (file_url) {
|
690
|
+
const o = {
|
691
|
+
is_file: true,
|
692
|
+
name: `${file_url}`,
|
693
|
+
data: null
|
694
|
+
// orig_name: "file.csv"
|
695
|
+
};
|
696
|
+
update_object(data, o, path);
|
697
|
+
}
|
698
|
+
});
|
699
|
+
return data;
|
700
|
+
});
|
701
|
+
}
|
651
702
|
}
|
703
|
+
const { post_data, upload_files, client, handle_blob } = api_factory(fetch);
|
652
704
|
function transform_output(data, api_info, root_url, remote_url) {
|
653
|
-
|
705
|
+
return data.map((d, i) => {
|
654
706
|
var _a, _b, _c, _d;
|
655
707
|
if (((_b = (_a = api_info.returns) == null ? void 0 : _a[i]) == null ? void 0 : _b.component) === "File") {
|
656
708
|
return normalise_file(d, root_url, remote_url);
|
@@ -664,7 +716,6 @@ function transform_output(data, api_info, root_url, remote_url) {
|
|
664
716
|
return d;
|
665
717
|
}
|
666
718
|
});
|
667
|
-
return transformed_data;
|
668
719
|
}
|
669
720
|
function normalise_file(file, root, root_url) {
|
670
721
|
if (file == null)
|
@@ -688,7 +739,7 @@ function normalise_file(file, root, root_url) {
|
|
688
739
|
if (!root_url) {
|
689
740
|
file.data = root + "/file=" + file.name;
|
690
741
|
} else {
|
691
|
-
file.data = "/proxy=" + root_url + "
|
742
|
+
file.data = "/proxy=" + root_url + "file=" + file.name;
|
692
743
|
}
|
693
744
|
}
|
694
745
|
return file;
|
@@ -777,44 +828,6 @@ async function get_jwt(space, token) {
|
|
777
828
|
return false;
|
778
829
|
}
|
779
830
|
}
|
780
|
-
async function handle_blob(endpoint, data, api_info, token) {
|
781
|
-
const blob_refs = await walk_and_store_blobs(
|
782
|
-
data,
|
783
|
-
void 0,
|
784
|
-
[],
|
785
|
-
true,
|
786
|
-
api_info
|
787
|
-
);
|
788
|
-
return new Promise((res) => {
|
789
|
-
Promise.all(
|
790
|
-
blob_refs.map(async ({ path, blob, data: data2, type }) => {
|
791
|
-
if (blob) {
|
792
|
-
const file_url = (await upload_files(endpoint, [blob], token)).files[0];
|
793
|
-
return { path, file_url, type };
|
794
|
-
} else {
|
795
|
-
return { path, base64: data2, type };
|
796
|
-
}
|
797
|
-
})
|
798
|
-
).then((r) => {
|
799
|
-
r.forEach(({ path, file_url, base64, type }) => {
|
800
|
-
if (base64) {
|
801
|
-
update_object(data, base64, path);
|
802
|
-
} else if (type === "Gallery") {
|
803
|
-
update_object(data, file_url, path);
|
804
|
-
} else if (file_url) {
|
805
|
-
const o = {
|
806
|
-
is_file: true,
|
807
|
-
name: `${file_url}`,
|
808
|
-
data: null
|
809
|
-
// orig_name: "file.csv"
|
810
|
-
};
|
811
|
-
update_object(data, o, path);
|
812
|
-
}
|
813
|
-
});
|
814
|
-
res(data);
|
815
|
-
}).catch(console.log);
|
816
|
-
});
|
817
|
-
}
|
818
831
|
function update_object(object, newValue, stack) {
|
819
832
|
while (stack.length > 1) {
|
820
833
|
object = object[stack.shift()];
|
@@ -896,7 +909,7 @@ function skip_queue(id, config) {
|
|
896
909
|
var _a, _b, _c, _d;
|
897
910
|
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;
|
898
911
|
}
|
899
|
-
async function resolve_config(endpoint, token) {
|
912
|
+
async function resolve_config(fetch_implementation, endpoint, token) {
|
900
913
|
const headers = {};
|
901
914
|
if (token) {
|
902
915
|
headers.Authorization = `Bearer ${token}`;
|
@@ -907,7 +920,9 @@ async function resolve_config(endpoint, token) {
|
|
907
920
|
config.root = endpoint + config.root;
|
908
921
|
return { ...config, path };
|
909
922
|
} else if (endpoint) {
|
910
|
-
let response = await
|
923
|
+
let response = await fetch_implementation(`${endpoint}/config`, {
|
924
|
+
headers
|
925
|
+
});
|
911
926
|
if (response.status === 200) {
|
912
927
|
const config = await response.json();
|
913
928
|
config.path = config.path ?? "";
|
@@ -958,6 +973,15 @@ async function check_space_status(id, type, status_callback) {
|
|
958
973
|
check_space_status(id, type, status_callback);
|
959
974
|
}, 1e3);
|
960
975
|
break;
|
976
|
+
case "PAUSED":
|
977
|
+
status_callback({
|
978
|
+
status: "paused",
|
979
|
+
load_status: "error",
|
980
|
+
message: "This space has been paused by the author. If you would like to try this demo, consider duplicating the space.",
|
981
|
+
detail: stage,
|
982
|
+
discussions_enabled: await discussions_enabled(space_name)
|
983
|
+
});
|
984
|
+
break;
|
961
985
|
case "RUNNING":
|
962
986
|
case "RUNNING_BUILDING":
|
963
987
|
status_callback({
|
@@ -1086,6 +1110,7 @@ function handle_message(data, last_status) {
|
|
1086
1110
|
return { type: "none", status: { stage: "error", queue } };
|
1087
1111
|
}
|
1088
1112
|
export {
|
1113
|
+
api_factory,
|
1089
1114
|
client,
|
1090
1115
|
duplicate,
|
1091
1116
|
post_data,
|