@gradio/client 0.0.1 → 0.1.0
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 +13 -0
- package/LICENSE +201 -0
- package/README.md +323 -30
- package/dist/client.d.ts +35 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1016 -0
- package/dist/types.d.ts +85 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +21 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/wrapper-b7460963.js +3468 -0
- package/package.json +26 -3
- package/src/client.node-test.ts +172 -0
- package/src/client.ts +785 -151
- package/src/globals.d.ts +31 -0
- package/src/index.ts +2 -2
- package/src/types.ts +9 -4
- package/src/utils.ts +121 -11
- package/tsconfig.json +14 -0
- package/vite.config.js +23 -0
package/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# @gradio/client
|
2
|
+
|
3
|
+
## 0.1.0
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- [#4185](https://github.com/gradio-app/gradio/pull/4185) [`67239ca9`](https://github.com/gradio-app/gradio/commit/67239ca9b2fe3796853fbf7bf865c9e4b383200d) Thanks [@pngwn](https://github.com/pngwn)! - Update client for initial release
|
8
|
+
|
9
|
+
### Patch Changes
|
10
|
+
|
11
|
+
- [#3692](https://github.com/gradio-app/gradio/pull/3692) [`48e8b113`](https://github.com/gradio-app/gradio/commit/48e8b113f4b55e461d9da4f153bf72aeb4adf0f1) Thanks [@pngwn](https://github.com/pngwn)! - Ensure client works in node, create ESM bundle and generate typescript declaration files.
|
12
|
+
|
13
|
+
- [#3605](https://github.com/gradio-app/gradio/pull/3605) [`ae4277a9`](https://github.com/gradio-app/gradio/commit/ae4277a9a83d49bdadfe523b0739ba988128e73b) Thanks [@pngwn](https://github.com/pngwn)! - Update readme.
|
package/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
@@ -1,46 +1,339 @@
|
|
1
|
-
|
1
|
+
## JavaScript Client Library
|
2
2
|
|
3
|
-
A javascript client to call Gradio APIs.
|
3
|
+
A javascript (and typescript) client to call Gradio APIs.
|
4
4
|
|
5
|
-
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
The Gradio JavaScript client is available on npm as `@gradio/client`. You can install it as below:
|
8
|
+
|
9
|
+
```sh
|
10
|
+
npm i -D @gradio/client
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
The JavaScript Gradio Client exposes two named imports, `client` and `duplicate`.
|
16
|
+
|
17
|
+
### `client`
|
18
|
+
|
19
|
+
The client function connects to the API of a hosted Gradio space and returns an object that allows you to make calls to that API.
|
20
|
+
|
21
|
+
The simplest example looks like this:
|
22
|
+
|
23
|
+
```ts
|
24
|
+
import { client } from "@gradio/client";
|
25
|
+
|
26
|
+
const app = await client("user/space-name");
|
27
|
+
const result = await app.predict("/predict");
|
28
|
+
```
|
29
|
+
|
30
|
+
This function accepts two arguments: `source` and `options`:
|
31
|
+
|
32
|
+
#### `source`
|
33
|
+
|
34
|
+
This is the url or name of the gradio app whose API you wish to connect to. This parameter is required and should always be a string. For example:
|
35
|
+
|
36
|
+
```ts
|
37
|
+
client("user/space-name");
|
38
|
+
```
|
39
|
+
|
40
|
+
#### `options`
|
41
|
+
|
42
|
+
The options object can optionally be passed a second parameter. This object has two properties, `hf_token` and `status_callback`.
|
43
|
+
|
44
|
+
##### `hf_token`
|
45
|
+
|
46
|
+
This should be a Hugging Face personal access token and is required if you wish to make calls to a private gradio api. This option is optional and should be a string starting with `"hf_"`.
|
47
|
+
|
48
|
+
Example:
|
49
|
+
|
50
|
+
```ts
|
51
|
+
import { client } from "@gradio/client";
|
52
|
+
|
53
|
+
const app = await client("user/space-name", { hf_token: "hf_..." });
|
54
|
+
```
|
55
|
+
|
56
|
+
##### `status_callback`
|
57
|
+
|
58
|
+
This should be a function which will notify your of the status of a space if it is not running. If the gradio API you are connecting to is awake and running or is not hosted on Hugging Face space then this function will do nothing.
|
59
|
+
|
60
|
+
**Additional context**
|
61
|
+
|
62
|
+
Applications hosted on Hugging Face spaces can be in a number of different states. As spaces are a GitOps tool and will rebuild when new changes are pushed to the repository, they have various building, running and error states. If a space is not 'running' then the function passed as the `status_callback` will notify you of the current state of the space and the status of the space as it changes. Spaces that are building or sleeping can take longer than usual to respond, so you can use this information to give users feedback about the progress of their action.
|
63
|
+
|
64
|
+
```ts
|
65
|
+
import { client, type SpaceStatus } from "@gradio/client";
|
66
|
+
|
67
|
+
const app = await client("user/space-name", {
|
68
|
+
// The space_status parameter does not need to be manually annotated, this is just for illustration.
|
69
|
+
space_status: (space_status: SpaceStatus) => console.log(space_status),
|
70
|
+
});
|
71
|
+
```
|
72
|
+
|
73
|
+
```ts
|
74
|
+
interface SpaceStatusNormal {
|
75
|
+
status: "sleeping" | "running" | "building" | "error" | "stopped";
|
76
|
+
detail:
|
77
|
+
| "SLEEPING"
|
78
|
+
| "RUNNING"
|
79
|
+
| "RUNNING_BUILDING"
|
80
|
+
| "BUILDING"
|
81
|
+
| "NOT_FOUND";
|
82
|
+
load_status: "pending" | "error" | "complete" | "generating";
|
83
|
+
message: string;
|
84
|
+
}
|
85
|
+
|
86
|
+
interface SpaceStatusError {
|
87
|
+
status: "space_error";
|
88
|
+
detail: "NO_APP_FILE" | "CONFIG_ERROR" | "BUILD_ERROR" | "RUNTIME_ERROR";
|
89
|
+
load_status: "error";
|
90
|
+
message: string;
|
91
|
+
discussions_enabled: boolean;
|
92
|
+
|
93
|
+
type SpaceStatus = SpaceStatusNormal | SpaceStatusError;
|
94
|
+
```
|
95
|
+
|
96
|
+
The gradio client returns an object with a number of methods and properties:
|
97
|
+
|
98
|
+
#### `predict`
|
99
|
+
|
100
|
+
The `predict` method allows you to call an api endpoint and get a prediction result:
|
101
|
+
|
102
|
+
```ts
|
103
|
+
import { client } from "@gradio/client";
|
104
|
+
|
105
|
+
const app = await client("user/space-name");
|
106
|
+
const result = await app.predict("/predict");
|
107
|
+
```
|
108
|
+
|
109
|
+
`predict` accepts two parameters, `endpoint` and `payload`. It returns a promise that resolves to the prediction result.
|
110
|
+
|
111
|
+
##### `endpoint`
|
112
|
+
|
113
|
+
This is the endpoint for an api request and is required. The default endpoint for a `gradio.Interface` is `"/predict"`. Explicitly named endpoints have a custom name. The endpoint names can be found on the "View API" page of a space.
|
114
|
+
|
115
|
+
```ts
|
116
|
+
import { client } from "@gradio/client";
|
117
|
+
|
118
|
+
const app = await client("user/space-name");
|
119
|
+
const result = await app.predict("/predict");
|
120
|
+
```
|
121
|
+
|
122
|
+
##### `payload`
|
123
|
+
|
124
|
+
The `payload` argument is generally optional but this depends on the API itself. If the API endpoint depends on values being passed in then it is required for the API request to succeed. The data that should be passed in is detailed on the "View API" page of a space, or accessible via the `view_api()` method of the client.
|
6
125
|
|
7
126
|
```ts
|
8
127
|
import { client } from "@gradio/client";
|
9
128
|
|
10
|
-
const app = client();
|
129
|
+
const app = await client("user/space-name");
|
130
|
+
const result = await app.predict("/predict", [1, "Hello", "friends"]);
|
131
|
+
```
|
132
|
+
|
133
|
+
#### `submit`
|
11
134
|
|
12
|
-
|
135
|
+
The `submit` method provides a more flexible way to call an API endpoint, providing you with status updates about the current progress of the prediction as well as supporting more complex endpoint types.
|
13
136
|
|
14
|
-
|
15
|
-
|
137
|
+
```ts
|
138
|
+
import { client } from "@gradio/client";
|
139
|
+
|
140
|
+
const app = await client("user/space-name");
|
141
|
+
const submission = app.submit("/predict", payload);
|
142
|
+
```
|
16
143
|
|
17
|
-
|
18
|
-
prediction.on("status", (event: { data: Status; type: "data" }) => {});
|
144
|
+
The `submit` method accepts the same [`endpoint`](#endpoint) and [`payload`](#payload) arguments as `predict`.
|
19
145
|
|
146
|
+
The `submit` method does not return a promise and should not be awaited, instead it returns an object with a `on`, `off`, and `cancel` methods.
|
147
|
+
|
148
|
+
##### `on`
|
149
|
+
|
150
|
+
The `on` method allows you to subscribe to events related to the submitted API request. There are two types of event that can be subscribed to: `"data"` updates and `"status"` updates.
|
151
|
+
|
152
|
+
`"data"` updates are issued when the API computes a value, the callback provided as the second argument will be called when such a value is sent to the client. The shape of the data depends on the way the API itself is constructed. This event may fire more than once if that endpoint supports emmitting new values over time.
|
153
|
+
|
154
|
+
`"status` updates are issued when the status of a request changes. This information allows you to offer feedback to users when the queue position of the request changes, or when the request changes from queued to processing.
|
155
|
+
|
156
|
+
The status payload look like this:
|
157
|
+
|
158
|
+
```ts
|
20
159
|
interface Status {
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
160
|
+
queue: boolean;
|
161
|
+
code?: string;
|
162
|
+
success?: boolean;
|
163
|
+
stage: "pending" | "error" | "complete" | "generating";
|
164
|
+
size?: number;
|
165
|
+
position?: number;
|
166
|
+
eta?: number;
|
167
|
+
message?: string;
|
168
|
+
progress_data?: Array<{
|
169
|
+
progress: number | null;
|
170
|
+
index: number | null;
|
171
|
+
length: number | null;
|
172
|
+
unit: string | null;
|
173
|
+
desc: string | null;
|
174
|
+
}>;
|
175
|
+
time?: Date;
|
33
176
|
}
|
177
|
+
```
|
34
178
|
|
35
|
-
|
36
|
-
prediction.off("data");
|
179
|
+
Usage of these subscribe callback looks like this:
|
37
180
|
|
38
|
-
|
39
|
-
|
181
|
+
```ts
|
182
|
+
import { client } from "@gradio/client";
|
183
|
+
|
184
|
+
const app = await client("user/space-name");
|
185
|
+
const submission = app
|
186
|
+
.submit("/predict", payload)
|
187
|
+
.on("data", (data) => console.log(data))
|
188
|
+
.on("status", (status: Status) => console.log(status));
|
189
|
+
```
|
190
|
+
|
191
|
+
##### `off`
|
192
|
+
|
193
|
+
The `off` method unsubscribes from a specific event of the submitted job and works similarly to `document.removeEventListener`; both the event name and the original callback must be passed in to successfully unsubscribe:
|
194
|
+
|
195
|
+
```ts
|
196
|
+
import { client } from "@gradio/client";
|
197
|
+
|
198
|
+
const app = await client("user/space-name");
|
199
|
+
const handle_data = (data) => console.log(data);
|
200
|
+
|
201
|
+
const submission = app.submit("/predict", payload).on("data", handle_data);
|
202
|
+
|
203
|
+
// later
|
204
|
+
submission.off("/predict", handle_data);
|
205
|
+
```
|
206
|
+
|
207
|
+
##### `destroy`
|
208
|
+
|
209
|
+
The `destroy` method will remove all subscriptions to a job, regardless of whether or not they are `"data"` or `"status"` events. This is a convenience method for when you do not wnat to unsubscribe use the `off` method.
|
210
|
+
|
211
|
+
```js
|
212
|
+
import { client } from "@gradio/client";
|
213
|
+
|
214
|
+
const app = await client("user/space-name");
|
215
|
+
const handle_data = (data) => console.log(data);
|
216
|
+
|
217
|
+
const submission = app.submit("/predict", payload).on("data", handle_data);
|
218
|
+
|
219
|
+
// later
|
220
|
+
submission.destroy();
|
221
|
+
```
|
222
|
+
|
223
|
+
##### `cancel`
|
224
|
+
|
225
|
+
Certain types of gradio function can run repeatedly and in some cases indefinitely. the `cancel` method will stop such an endpoints and prevent the API from issuing additional updates.
|
226
|
+
|
227
|
+
```ts
|
228
|
+
import { client } from "@gradio/client";
|
229
|
+
|
230
|
+
const app = await client("user/space-name");
|
231
|
+
const submission = app
|
232
|
+
.submit("/predict", payload)
|
233
|
+
.on("data", (data) => console.log(data));
|
234
|
+
|
235
|
+
// later
|
236
|
+
|
237
|
+
submission.cancel();
|
238
|
+
```
|
239
|
+
|
240
|
+
#### `view_api`
|
241
|
+
|
242
|
+
The `view_api` method provides details about the API you are connected too. It returns a JavaScript object of all named endpoints, unnamed endpoints and what values they accept and return. This method does not accept arguments.
|
243
|
+
|
244
|
+
```ts
|
245
|
+
import { client } from "@gradio/client";
|
246
|
+
|
247
|
+
const app = await client("user/space-name");
|
248
|
+
const api_info = await app.view_api();
|
249
|
+
|
250
|
+
console.log(api_info);
|
251
|
+
```
|
252
|
+
|
253
|
+
#### `config`
|
254
|
+
|
255
|
+
The `config` property contains the configuration for the gradio application you are connected to. This object may contain useful meta information about the application.
|
256
|
+
|
257
|
+
```ts
|
258
|
+
import { client } from "@gradio/client";
|
259
|
+
|
260
|
+
const app = await client("user/space-name");
|
261
|
+
console.log(app.config);
|
262
|
+
```
|
263
|
+
|
264
|
+
### `duplicate`
|
265
|
+
|
266
|
+
The duplicate function will attempt to duplicate the space that is referenced and return an instance of `client` connected to that space. If the space has already been duplicated then it will not create a new duplicate and will instead connect to the existing duplicated space. The huggingface token that is passed in will dictate the user under which the space is created.
|
267
|
+
|
268
|
+
`duplicate` accepts the same arguments as `client` with the addition of a `private` options property dictating whether the duplicated space should be private or public. A huggingface token is required for duplication to work.
|
269
|
+
|
270
|
+
```ts
|
271
|
+
import { duplicate } from "@gradio/client";
|
272
|
+
|
273
|
+
const app = await duplicate("user/space-name", {
|
274
|
+
hf_token: "hf_...",
|
275
|
+
});
|
276
|
+
```
|
277
|
+
|
278
|
+
This function accepts two arguments: `source` and `options`:
|
279
|
+
|
280
|
+
#### `source`
|
281
|
+
|
282
|
+
The space to duplicate and connect to. [See `client`'s `source` parameter](#source).
|
283
|
+
|
284
|
+
#### `options`
|
285
|
+
|
286
|
+
Accepts all options that `client` accepts, except `hf_token` is required. [See `client`'s `options` parameter](#source).
|
287
|
+
|
288
|
+
`duplicate` also accepts one additional `options` property.
|
289
|
+
|
290
|
+
##### `private`
|
291
|
+
|
292
|
+
This is an optional property specific to `duplicate`'s options object and will determine whether the space should be public or private. Spaces duplicated via the `duplicate` method are public by default.
|
293
|
+
|
294
|
+
```ts
|
295
|
+
import { duplicate } from "@gradio/client";
|
296
|
+
|
297
|
+
const app = await duplicate("user/space-name", {
|
298
|
+
hf_token: "hf_...",
|
299
|
+
private: true,
|
300
|
+
});
|
301
|
+
```
|
302
|
+
|
303
|
+
##### `timeout`
|
304
|
+
|
305
|
+
This is an optional property specific to `duplicate`'s options object and will set the timeout in minutes before the duplicated space will go to sleep.
|
306
|
+
|
307
|
+
```ts
|
308
|
+
import { duplicate } from "@gradio/client";
|
309
|
+
|
310
|
+
const app = await duplicate("user/space-name", {
|
311
|
+
hf_token: "hf_...",
|
312
|
+
private: true,
|
313
|
+
timeout: 5,
|
314
|
+
});
|
315
|
+
```
|
316
|
+
|
317
|
+
##### `hardware`
|
318
|
+
|
319
|
+
This is an optional property specific to `duplicate`'s options object and will set the hardware for the duplicated space. By default the hardware used will match that of the original space. If this cannot be obtained it will default to `"cpu-basic"`. For hardware upgrades (beyond the basic CPU tier), you may be required to provide [billing information on Hugging Face](https://huggingface.co/settings/billing).
|
320
|
+
|
321
|
+
Possible hardware options are:
|
322
|
+
|
323
|
+
- `"cpu-basic"`
|
324
|
+
- `"cpu-upgrade"`
|
325
|
+
- `"t4-small"`
|
326
|
+
- `"t4-medium"`
|
327
|
+
- `"a10g-small"`
|
328
|
+
- `"a10g-large"`
|
329
|
+
- `"a100-large"`
|
330
|
+
|
331
|
+
```ts
|
332
|
+
import { duplicate } from "@gradio/client";
|
40
333
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
334
|
+
const app = await duplicate("user/space-name", {
|
335
|
+
hf_token: "hf_...",
|
336
|
+
private: true,
|
337
|
+
hardware: "a10g-small",
|
338
|
+
});
|
46
339
|
```
|
package/dist/client.d.ts
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
import { hardware_types } from "./utils.js";
|
2
|
+
import type { EventType, EventListener, PostResponse, UploadResponse, SpaceStatusCallback } from "./types.js";
|
3
|
+
import type { Config } from "./types.js";
|
4
|
+
declare type event = <K extends EventType>(eventType: K, listener: EventListener<K>) => SubmitReturn;
|
5
|
+
declare type predict = (endpoint: string | number, data?: unknown[], event_data?: unknown) => Promise<unknown>;
|
6
|
+
declare type client_return = {
|
7
|
+
predict: predict;
|
8
|
+
config: Config;
|
9
|
+
submit: (endpoint: string | number, data?: unknown[], event_data?: unknown) => SubmitReturn;
|
10
|
+
view_api: (c?: Config) => Promise<Record<string, any>>;
|
11
|
+
};
|
12
|
+
declare type SubmitReturn = {
|
13
|
+
on: event;
|
14
|
+
off: event;
|
15
|
+
cancel: () => void;
|
16
|
+
destroy: () => void;
|
17
|
+
};
|
18
|
+
export declare function post_data(url: string, body: unknown, token?: `hf_${string}`): Promise<[PostResponse, number]>;
|
19
|
+
export declare let NodeBlob: any;
|
20
|
+
export declare function upload_files(root: string, files: Array<File>, token?: `hf_${string}`): Promise<UploadResponse>;
|
21
|
+
export declare function duplicate(app_reference: string, options: {
|
22
|
+
hf_token: `hf_${string}`;
|
23
|
+
private?: boolean;
|
24
|
+
status_callback: SpaceStatusCallback;
|
25
|
+
hardware?: typeof hardware_types[number];
|
26
|
+
timeout?: number;
|
27
|
+
}): Promise<client_return>;
|
28
|
+
export declare function client(app_reference: string, options?: {
|
29
|
+
hf_token?: `hf_${string}`;
|
30
|
+
status_callback?: SpaceStatusCallback;
|
31
|
+
}): Promise<client_return>;
|
32
|
+
export declare function handle_blob(endpoint: string, data: unknown[], api_info: any, token?: `hf_${string}`): Promise<unknown[]>;
|
33
|
+
export declare function walk_and_store_blobs(param: any, type?: any, path?: any[], root?: boolean, api_info?: any): Promise<any[]>;
|
34
|
+
export {};
|
35
|
+
//# sourceMappingURL=client.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAQN,cAAc,EACd,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EACX,SAAS,EACT,aAAa,EAIb,YAAY,EACZ,cAAc,EAGd,mBAAmB,EACnB,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,aAAK,KAAK,GAAG,CAAC,CAAC,SAAS,SAAS,EAChC,SAAS,EAAE,CAAC,EACZ,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,KACtB,YAAY,CAAC;AAClB,aAAK,OAAO,GAAG,CACd,QAAQ,EAAE,MAAM,GAAG,MAAM,EACzB,IAAI,CAAC,EAAE,OAAO,EAAE,EAChB,UAAU,CAAC,EAAE,OAAO,KAChB,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB,aAAK,aAAa,GAAG;IACpB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,CACP,QAAQ,EAAE,MAAM,GAAG,MAAM,EACzB,IAAI,CAAC,EAAE,OAAO,EAAE,EAChB,UAAU,CAAC,EAAE,OAAO,KAChB,YAAY,CAAC;IAClB,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;CACvD,CAAC;AAEF,aAAK,YAAY,GAAG;IACnB,EAAE,EAAE,KAAK,CAAC;IACV,GAAG,EAAE,KAAK,CAAC;IACX,MAAM,EAAE,MAAM,IAAI,CAAC;IACnB,OAAO,EAAE,MAAM,IAAI,CAAC;CACpB,CAAC;AAKF,wBAAsB,SAAS,CAC9B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,OAAO,EACb,KAAK,CAAC,EAAE,MAAM,MAAM,EAAE,GACpB,OAAO,CAAC,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC,CAmBjC;AAED,eAAO,IAAI,QAAQ,KAAA,CAAC;AAEpB,wBAAsB,YAAY,CACjC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,KAAK,CAAC,EAAE,MAAM,MAAM,EAAE,GACpB,OAAO,CAAC,cAAc,CAAC,CAuBzB;AAED,wBAAsB,SAAS,CAC9B,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE;IACR,QAAQ,EAAE,MAAM,MAAM,EAAE,CAAC;IACzB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,eAAe,EAAE,mBAAmB,CAAC;IACrC,QAAQ,CAAC,EAAE,OAAO,cAAc,CAAC,MAAM,CAAC,CAAC;IACzC,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB,0BAyED;AAED,wBAAsB,MAAM,CAC3B,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE;IACR,QAAQ,CAAC,EAAE,MAAM,MAAM,EAAE,CAAC;IAC1B,eAAe,CAAC,EAAE,mBAAmB,CAAC;CACjC,GACJ,OAAO,CAAC,aAAa,CAAC,CAucxB;AAyJD,wBAAsB,WAAW,CAChC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,OAAO,EAAE,EACf,QAAQ,KAAA,EACR,KAAK,CAAC,EAAE,MAAM,MAAM,EAAE,GACpB,OAAO,CAAC,OAAO,EAAE,CAAC,CA0CpB;AAUD,wBAAsB,oBAAoB,CACzC,KAAK,KAAA,EACL,IAAI,MAAY,EAChB,IAAI,QAAK,EACT,IAAI,UAAQ,EACZ,QAAQ,MAAY,kBAyEpB"}
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACzE,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC"}
|