@gradio/client 0.4.2 → 0.5.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 +32 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/index.js +67 -9
- package/dist/utils.d.ts +11 -0
- package/dist/utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +77 -11
- package/src/utils.ts +21 -0
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,37 @@
|
|
1
1
|
# @gradio/client
|
2
2
|
|
3
|
+
## 0.5.1
|
4
|
+
|
5
|
+
### Fixes
|
6
|
+
|
7
|
+
- [#5816](https://github.com/gradio-app/gradio/pull/5816) [`796145e2c`](https://github.com/gradio-app/gradio/commit/796145e2c48c4087bec17f8ec0be4ceee47170cb) - Fix calls to the component server so that `gr.FileExplorer` works on Spaces. Thanks [@abidlabs](https://github.com/abidlabs)!
|
8
|
+
|
9
|
+
## 0.5.0
|
10
|
+
|
11
|
+
### Highlights
|
12
|
+
|
13
|
+
#### new `FileExplorer` component ([#5672](https://github.com/gradio-app/gradio/pull/5672) [`e4a307ed6`](https://github.com/gradio-app/gradio/commit/e4a307ed6cde3bbdf4ff2f17655739addeec941e))
|
14
|
+
|
15
|
+
Thanks to a new capability that allows components to communicate directly with the server _without_ passing data via the value, we have created a new `FileExplorer` component.
|
16
|
+
|
17
|
+
This component allows you to populate the explorer by passing a glob, but only provides the selected file(s) in your prediction function.
|
18
|
+
|
19
|
+
Users can then navigate the virtual filesystem and select files which will be accessible in your predict function. This component will allow developers to build more complex spaces, with more flexible input options.
|
20
|
+
|
21
|
+

|
22
|
+
|
23
|
+
For more information check the [`FileExplorer` documentation](https://gradio.app/docs/fileexplorer).
|
24
|
+
|
25
|
+
Thanks [@aliabid94](https://github.com/aliabid94)!
|
26
|
+
|
27
|
+
### Features
|
28
|
+
|
29
|
+
- [#5787](https://github.com/gradio-app/gradio/pull/5787) [`caeee8bf7`](https://github.com/gradio-app/gradio/commit/caeee8bf7821fd5fe2f936ed82483bed00f613ec) - ensure the client does not depend on `window` when running in a node environment. Thanks [@gibiee](https://github.com/gibiee)!
|
30
|
+
|
31
|
+
### Fixes
|
32
|
+
|
33
|
+
- [#5776](https://github.com/gradio-app/gradio/pull/5776) [`c0fef4454`](https://github.com/gradio-app/gradio/commit/c0fef44541bfa61568bdcfcdfc7d7d79869ab1df) - Revert replica proxy logic and instead implement using the `root` variable. Thanks [@freddyaboulton](https://github.com/freddyaboulton)!
|
34
|
+
|
3
35
|
## 0.4.2
|
4
36
|
|
5
37
|
### Features
|
package/dist/client.d.ts
CHANGED
@@ -7,6 +7,7 @@ type client_return = {
|
|
7
7
|
predict: predict;
|
8
8
|
config: Config;
|
9
9
|
submit: (endpoint: string | number, data?: unknown[], event_data?: unknown) => SubmitReturn;
|
10
|
+
component_server: (component_id: number, fn_name: string, data: unknown[]) => any;
|
10
11
|
view_api: (c?: Config) => Promise<ApiInfo<JsApiData>>;
|
11
12
|
};
|
12
13
|
type SubmitReturn = {
|
package/dist/client.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAQN,cAAc,
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAQN,cAAc,EAEd,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EACX,SAAS,EACT,aAAa,EAIb,YAAY,EACZ,cAAc,EAGd,mBAAmB,EAEnB,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,KAAK,KAAK,GAAG,CAAC,CAAC,SAAS,SAAS,EAChC,SAAS,EAAE,CAAC,EACZ,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,KACtB,YAAY,CAAC;AAClB,KAAK,O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.split(".");
|
5
5
|
return fn(a[0], b[0]) || fn(a[1], b[1]) || (b[2] = b.slice(2).join("."), bool = /[.-]/.test(a[2] = a.slice(2).join(".")), bool == /[.-]/.test(b[2]) ? fn(a[2], b[2]) : bool ? -1 : 1);
|
6
6
|
}
|
7
|
+
function resolve_root(base_url, root_path, prioritize_base) {
|
8
|
+
if (root_path.startsWith("http://") || root_path.startsWith("https://")) {
|
9
|
+
return prioritize_base ? base_url : root_path;
|
10
|
+
}
|
11
|
+
return base_url + root_path;
|
12
|
+
}
|
7
13
|
function determine_protocol(endpoint) {
|
8
14
|
if (endpoint.startsWith("http")) {
|
9
15
|
const { protocol, host } = new URL(endpoint);
|
@@ -261,7 +267,8 @@ function api_factory(fetch_implementation, WebSocket_factory) {
|
|
261
267
|
const return_obj = {
|
262
268
|
predict,
|
263
269
|
submit,
|
264
|
-
view_api
|
270
|
+
view_api,
|
271
|
+
component_server
|
265
272
|
// duplicate
|
266
273
|
};
|
267
274
|
const transform_files = normalise_files ?? true;
|
@@ -408,11 +415,14 @@ function api_factory(fetch_implementation, WebSocket_factory) {
|
|
408
415
|
let payload;
|
409
416
|
let complete = false;
|
410
417
|
const listener_map = {};
|
411
|
-
|
412
|
-
|
413
|
-
|
418
|
+
let url_params = "";
|
419
|
+
if (typeof window !== "undefined") {
|
420
|
+
url_params = new URLSearchParams(
|
421
|
+
window.location.search
|
422
|
+
).toString();
|
423
|
+
}
|
414
424
|
handle_blob2(
|
415
|
-
`${http_protocol}//${host
|
425
|
+
`${http_protocol}//${resolve_root(host, config.path, true)}`,
|
416
426
|
data,
|
417
427
|
api_info,
|
418
428
|
hf_token
|
@@ -428,7 +438,7 @@ function api_factory(fetch_implementation, WebSocket_factory) {
|
|
428
438
|
time: /* @__PURE__ */ new Date()
|
429
439
|
});
|
430
440
|
post_data2(
|
431
|
-
`${http_protocol}//${host
|
441
|
+
`${http_protocol}//${resolve_root(host, config.path, true)}/run${_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`}${url_params ? "?" + url_params : ""}`,
|
432
442
|
{
|
433
443
|
...payload,
|
434
444
|
session_hash
|
@@ -489,7 +499,11 @@ function api_factory(fetch_implementation, WebSocket_factory) {
|
|
489
499
|
fn_index,
|
490
500
|
time: /* @__PURE__ */ new Date()
|
491
501
|
});
|
492
|
-
let url = new URL(`${ws_protocol}://${
|
502
|
+
let url = new URL(`${ws_protocol}://${resolve_root(
|
503
|
+
host,
|
504
|
+
config.path,
|
505
|
+
true
|
506
|
+
)}
|
493
507
|
/queue/join${url_params ? "?" + url_params : ""}`);
|
494
508
|
if (jwt) {
|
495
509
|
url.searchParams.set("__sign", jwt);
|
@@ -628,7 +642,11 @@ function api_factory(fetch_implementation, WebSocket_factory) {
|
|
628
642
|
}
|
629
643
|
try {
|
630
644
|
await fetch_implementation(
|
631
|
-
`${http_protocol}//${
|
645
|
+
`${http_protocol}//${resolve_root(
|
646
|
+
host,
|
647
|
+
config.path,
|
648
|
+
true
|
649
|
+
)}/reset`,
|
632
650
|
{
|
633
651
|
headers: { "Content-Type": "application/json" },
|
634
652
|
method: "POST",
|
@@ -655,6 +673,46 @@ function api_factory(fetch_implementation, WebSocket_factory) {
|
|
655
673
|
destroy
|
656
674
|
};
|
657
675
|
}
|
676
|
+
async function component_server(component_id, fn_name, data) {
|
677
|
+
var _a;
|
678
|
+
const headers = { "Content-Type": "application/json" };
|
679
|
+
if (hf_token) {
|
680
|
+
headers.Authorization = `Bearer ${hf_token}`;
|
681
|
+
}
|
682
|
+
let root_url;
|
683
|
+
let component = config.components.find(
|
684
|
+
(comp) => comp.id === component_id
|
685
|
+
);
|
686
|
+
if ((_a = component == null ? void 0 : component.props) == null ? void 0 : _a.root_url) {
|
687
|
+
root_url = component.props.root_url;
|
688
|
+
} else {
|
689
|
+
root_url = `${http_protocol}//${resolve_root(
|
690
|
+
host,
|
691
|
+
config.path,
|
692
|
+
true
|
693
|
+
)}/`;
|
694
|
+
}
|
695
|
+
const response = await fetch_implementation(
|
696
|
+
`${root_url}component_server/`,
|
697
|
+
{
|
698
|
+
method: "POST",
|
699
|
+
body: JSON.stringify({
|
700
|
+
data,
|
701
|
+
component_id,
|
702
|
+
fn_name,
|
703
|
+
session_hash
|
704
|
+
}),
|
705
|
+
headers
|
706
|
+
}
|
707
|
+
);
|
708
|
+
if (!response.ok) {
|
709
|
+
throw new Error(
|
710
|
+
"Could not connect to component server: " + response.statusText
|
711
|
+
);
|
712
|
+
}
|
713
|
+
const output = await response.json();
|
714
|
+
return output;
|
715
|
+
}
|
658
716
|
async function view_api(config2) {
|
659
717
|
if (api)
|
660
718
|
return api;
|
@@ -946,7 +1004,7 @@ async function resolve_config(fetch_implementation, endpoint, token) {
|
|
946
1004
|
if (typeof window !== "undefined" && window.gradio_config && location.origin !== "http://localhost:9876" && !window.gradio_config.dev_mode) {
|
947
1005
|
const path = window.gradio_config.root;
|
948
1006
|
const config = window.gradio_config;
|
949
|
-
config.root = endpoint
|
1007
|
+
config.root = resolve_root(endpoint, config.root, false);
|
950
1008
|
return { ...config, path };
|
951
1009
|
} else if (endpoint) {
|
952
1010
|
let response = await fetch_implementation(`${endpoint}/config`, {
|
package/dist/utils.d.ts
CHANGED
@@ -1,4 +1,15 @@
|
|
1
1
|
import type { Config } from "./types.js";
|
2
|
+
/**
|
3
|
+
* This function is used to resolve the URL for making requests when the app has a root path.
|
4
|
+
* The root path could be a path suffix like "/app" which is appended to the end of the base URL. Or
|
5
|
+
* it could be a full URL like "https://abidlabs-test-client-replica--gqf2x.hf.space" which is used when hosting
|
6
|
+
* Gradio apps on Hugging Face Spaces.
|
7
|
+
* @param {string} base_url The base URL at which the Gradio server is hosted
|
8
|
+
* @param {string} root_path The root path, which could be a path suffix (e.g. mounted in FastAPI app) or a full URL (e.g. hosted on Hugging Face Spaces)
|
9
|
+
* @param {boolean} prioritize_base Whether to prioritize the base URL over the root path. This is used when both the base path and root paths are full URLs. For example, for fetching files the root path should be prioritized, but for making requests, the base URL should be prioritized.
|
10
|
+
* @returns {string} the resolved URL
|
11
|
+
*/
|
12
|
+
export declare function resolve_root(base_url: string, root_path: string, prioritize_base: boolean): string;
|
2
13
|
export declare function determine_protocol(endpoint: string): {
|
3
14
|
ws_protocol: "ws" | "wss";
|
4
15
|
http_protocol: "http:" | "https:";
|
package/dist/utils.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG;IACrD,WAAW,EAAE,IAAI,GAAG,KAAK,CAAC;IAC1B,aAAa,EAAE,OAAO,GAAG,QAAQ,CAAC;IAClC,IAAI,EAAE,MAAM,CAAC;CACb,CAwBA;AAED,eAAO,MAAM,aAAa,QAAqB,CAAC;AAChD,eAAO,MAAM,eAAe,QAAwB,CAAC;AACrD,wBAAsB,gBAAgB,CACrC,aAAa,EAAE,MAAM,EACrB,KAAK,CAAC,EAAE,MAAM,MAAM,EAAE,GACpB,OAAO,CAAC;IACV,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,IAAI,GAAG,KAAK,CAAC;IAC1B,aAAa,EAAE,OAAO,GAAG,QAAQ,CAAC;CAClC,CAAC,CA4CD;AAED,wBAAgB,gBAAgB,CAC/B,GAAG,EAAE,MAAM,CAAC,cAAc,CAAC,GACzB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQxB;AAID,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAe5E;AAED,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,CAqB1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,GAAG,EAAE,MAAM,CAAC,cAAc,CAAC,GACzB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQxB;AAID,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAe5E;AAED,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,CAqB1C;AAED,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,EAC7C,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,CAuB1C;AAED,wBAAsB,iBAAiB,CACtC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,MAAM,CAAC,CAuBjB;AAED,eAAO,MAAM,cAAc,0GAQjB,CAAC"}
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
@@ -8,7 +8,8 @@ import {
|
|
8
8
|
get_space_hardware,
|
9
9
|
set_space_hardware,
|
10
10
|
set_space_timeout,
|
11
|
-
hardware_types
|
11
|
+
hardware_types,
|
12
|
+
resolve_root
|
12
13
|
} from "./utils.js";
|
13
14
|
|
14
15
|
import type {
|
@@ -45,6 +46,11 @@ type client_return = {
|
|
45
46
|
data?: unknown[],
|
46
47
|
event_data?: unknown
|
47
48
|
) => SubmitReturn;
|
49
|
+
component_server: (
|
50
|
+
component_id: number,
|
51
|
+
fn_name: string,
|
52
|
+
data: unknown[]
|
53
|
+
) => any;
|
48
54
|
view_api: (c?: Config) => Promise<ApiInfo<JsApiData>>;
|
49
55
|
};
|
50
56
|
|
@@ -243,7 +249,8 @@ export function api_factory(
|
|
243
249
|
const return_obj = {
|
244
250
|
predict,
|
245
251
|
submit,
|
246
|
-
view_api
|
252
|
+
view_api,
|
253
|
+
component_server
|
247
254
|
// duplicate
|
248
255
|
};
|
249
256
|
|
@@ -422,12 +429,15 @@ export function api_factory(
|
|
422
429
|
let payload: Payload;
|
423
430
|
let complete: false | Record<string, any> = false;
|
424
431
|
const listener_map: ListenerMap<EventType> = {};
|
425
|
-
|
426
|
-
|
427
|
-
|
432
|
+
let url_params = ""
|
433
|
+
if (typeof(window) !== "undefined") {
|
434
|
+
url_params = new URLSearchParams(
|
435
|
+
window.location.search
|
436
|
+
).toString();
|
437
|
+
}
|
428
438
|
|
429
439
|
handle_blob(
|
430
|
-
`${http_protocol}//${host
|
440
|
+
`${http_protocol}//${resolve_root(host, config.path, true)}`,
|
431
441
|
data,
|
432
442
|
api_info,
|
433
443
|
hf_token
|
@@ -444,7 +454,7 @@ export function api_factory(
|
|
444
454
|
});
|
445
455
|
|
446
456
|
post_data(
|
447
|
-
`${http_protocol}//${host
|
457
|
+
`${http_protocol}//${resolve_root(host, config.path, true)}/run${
|
448
458
|
_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`
|
449
459
|
}${url_params ? "?" + url_params : ""}`,
|
450
460
|
{
|
@@ -512,8 +522,11 @@ export function api_factory(
|
|
512
522
|
fn_index,
|
513
523
|
time: new Date()
|
514
524
|
});
|
515
|
-
|
516
|
-
|
525
|
+
let url = new URL(`${ws_protocol}://${resolve_root(
|
526
|
+
host,
|
527
|
+
config.path,
|
528
|
+
true
|
529
|
+
)}
|
517
530
|
/queue/join${url_params ? "?" + url_params : ""}`);
|
518
531
|
|
519
532
|
if (jwt) {
|
@@ -677,7 +690,11 @@ export function api_factory(
|
|
677
690
|
|
678
691
|
try {
|
679
692
|
await fetch_implementation(
|
680
|
-
`${http_protocol}//${
|
693
|
+
`${http_protocol}//${resolve_root(
|
694
|
+
host,
|
695
|
+
config.path,
|
696
|
+
true
|
697
|
+
)}/reset`,
|
681
698
|
{
|
682
699
|
headers: { "Content-Type": "application/json" },
|
683
700
|
method: "POST",
|
@@ -707,6 +724,55 @@ export function api_factory(
|
|
707
724
|
};
|
708
725
|
}
|
709
726
|
|
727
|
+
async function component_server(
|
728
|
+
component_id: number,
|
729
|
+
fn_name: string,
|
730
|
+
data: unknown[]
|
731
|
+
): Promise<any> {
|
732
|
+
const headers: {
|
733
|
+
Authorization?: string;
|
734
|
+
"Content-Type": "application/json";
|
735
|
+
} = { "Content-Type": "application/json" };
|
736
|
+
if (hf_token) {
|
737
|
+
headers.Authorization = `Bearer ${hf_token}`;
|
738
|
+
}
|
739
|
+
let root_url: string;
|
740
|
+
let component = config.components.find(
|
741
|
+
(comp) => comp.id === component_id
|
742
|
+
);
|
743
|
+
if (component?.props?.root_url) {
|
744
|
+
root_url = component.props.root_url;
|
745
|
+
} else {
|
746
|
+
root_url = `${http_protocol}//${resolve_root(
|
747
|
+
host,
|
748
|
+
config.path,
|
749
|
+
true
|
750
|
+
)}/`;
|
751
|
+
}
|
752
|
+
const response = await fetch_implementation(
|
753
|
+
`${root_url}component_server/`,
|
754
|
+
{
|
755
|
+
method: "POST",
|
756
|
+
body: JSON.stringify({
|
757
|
+
data: data,
|
758
|
+
component_id: component_id,
|
759
|
+
fn_name: fn_name,
|
760
|
+
session_hash: session_hash
|
761
|
+
}),
|
762
|
+
headers
|
763
|
+
}
|
764
|
+
);
|
765
|
+
|
766
|
+
if (!response.ok) {
|
767
|
+
throw new Error(
|
768
|
+
"Could not connect to component server: " + response.statusText
|
769
|
+
);
|
770
|
+
}
|
771
|
+
|
772
|
+
const output = await response.json();
|
773
|
+
return output;
|
774
|
+
}
|
775
|
+
|
710
776
|
async function view_api(config?: Config): Promise<ApiInfo<JsApiData>> {
|
711
777
|
if (api) return api;
|
712
778
|
|
@@ -1152,7 +1218,7 @@ async function resolve_config(
|
|
1152
1218
|
) {
|
1153
1219
|
const path = window.gradio_config.root;
|
1154
1220
|
const config = window.gradio_config;
|
1155
|
-
config.root = endpoint
|
1221
|
+
config.root = resolve_root(endpoint, config.root, false);
|
1156
1222
|
return { ...config, path: path };
|
1157
1223
|
} else if (endpoint) {
|
1158
1224
|
let response = await fetch_implementation(`${endpoint}/config`, {
|
package/src/utils.ts
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
import type { Config } from "./types.js";
|
2
2
|
|
3
|
+
/**
|
4
|
+
* This function is used to resolve the URL for making requests when the app has a root path.
|
5
|
+
* The root path could be a path suffix like "/app" which is appended to the end of the base URL. Or
|
6
|
+
* it could be a full URL like "https://abidlabs-test-client-replica--gqf2x.hf.space" which is used when hosting
|
7
|
+
* Gradio apps on Hugging Face Spaces.
|
8
|
+
* @param {string} base_url The base URL at which the Gradio server is hosted
|
9
|
+
* @param {string} root_path The root path, which could be a path suffix (e.g. mounted in FastAPI app) or a full URL (e.g. hosted on Hugging Face Spaces)
|
10
|
+
* @param {boolean} prioritize_base Whether to prioritize the base URL over the root path. This is used when both the base path and root paths are full URLs. For example, for fetching files the root path should be prioritized, but for making requests, the base URL should be prioritized.
|
11
|
+
* @returns {string} the resolved URL
|
12
|
+
*/
|
13
|
+
export function resolve_root(
|
14
|
+
base_url: string,
|
15
|
+
root_path: string,
|
16
|
+
prioritize_base: boolean
|
17
|
+
): string {
|
18
|
+
if (root_path.startsWith("http://") || root_path.startsWith("https://")) {
|
19
|
+
return prioritize_base ? base_url : root_path;
|
20
|
+
}
|
21
|
+
return base_url + root_path;
|
22
|
+
}
|
23
|
+
|
3
24
|
export function determine_protocol(endpoint: string): {
|
4
25
|
ws_protocol: "ws" | "wss";
|
5
26
|
http_protocol: "http:" | "https:";
|