@gradio/client 0.16.0 → 0.17.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.
Files changed (63) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/README.md +49 -43
  3. package/dist/client.d.ts +52 -70
  4. package/dist/client.d.ts.map +1 -1
  5. package/dist/constants.d.ts +17 -0
  6. package/dist/constants.d.ts.map +1 -0
  7. package/dist/helpers/api_info.d.ts +25 -0
  8. package/dist/helpers/api_info.d.ts.map +1 -0
  9. package/dist/helpers/data.d.ts +8 -0
  10. package/dist/helpers/data.d.ts.map +1 -0
  11. package/dist/{utils.d.ts → helpers/init_helpers.d.ts} +6 -18
  12. package/dist/helpers/init_helpers.d.ts.map +1 -0
  13. package/dist/helpers/spaces.d.ts +7 -0
  14. package/dist/helpers/spaces.d.ts.map +1 -0
  15. package/dist/index.d.ts +8 -4
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +1544 -1357
  18. package/dist/types.d.ts +152 -49
  19. package/dist/types.d.ts.map +1 -1
  20. package/dist/upload.d.ts +2 -2
  21. package/dist/upload.d.ts.map +1 -1
  22. package/dist/utils/duplicate.d.ts +4 -0
  23. package/dist/utils/duplicate.d.ts.map +1 -0
  24. package/dist/utils/handle_blob.d.ts +4 -0
  25. package/dist/utils/handle_blob.d.ts.map +1 -0
  26. package/dist/utils/post_data.d.ts +4 -0
  27. package/dist/utils/post_data.d.ts.map +1 -0
  28. package/dist/utils/predict.d.ts +4 -0
  29. package/dist/utils/predict.d.ts.map +1 -0
  30. package/dist/utils/stream.d.ts +8 -0
  31. package/dist/utils/stream.d.ts.map +1 -0
  32. package/dist/utils/submit.d.ts +4 -0
  33. package/dist/utils/submit.d.ts.map +1 -0
  34. package/dist/utils/upload_files.d.ts +4 -0
  35. package/dist/utils/upload_files.d.ts.map +1 -0
  36. package/dist/utils/view_api.d.ts +3 -0
  37. package/dist/utils/view_api.d.ts.map +1 -0
  38. package/dist/{wrapper-6f348d45.js → wrapper-CviSselG.js} +259 -17
  39. package/package.json +3 -2
  40. package/src/client.ts +289 -1692
  41. package/src/constants.ts +20 -0
  42. package/src/globals.d.ts +2 -21
  43. package/src/helpers/api_info.ts +300 -0
  44. package/src/helpers/data.ts +115 -0
  45. package/src/helpers/init_helpers.ts +134 -0
  46. package/src/helpers/spaces.ts +192 -0
  47. package/src/index.ts +16 -10
  48. package/src/types.ts +200 -59
  49. package/src/upload.ts +23 -15
  50. package/src/{client.node-test.ts → utils/client.node-test.ts} +11 -10
  51. package/src/utils/duplicate.ts +87 -0
  52. package/src/utils/handle_blob.ts +47 -0
  53. package/src/utils/post_data.ts +37 -0
  54. package/src/utils/predict.ts +56 -0
  55. package/src/utils/stream.ts +166 -0
  56. package/src/utils/submit.ts +689 -0
  57. package/src/utils/upload_files.ts +46 -0
  58. package/src/utils/view_api.ts +70 -0
  59. package/src/vite-env.d.ts +1 -0
  60. package/tsconfig.json +15 -2
  61. package/vite.config.js +4 -17
  62. package/dist/utils.d.ts.map +0 -1
  63. package/src/utils.ts +0 -314
@@ -0,0 +1,20 @@
1
+ // endpoints
2
+ export const HOST_URL = "host";
3
+ export const API_URL = "api/predict/";
4
+ export const SSE_URL_V0 = "queue/join";
5
+ export const SSE_DATA_URL_V0 = "queue/data";
6
+ export const SSE_URL = "queue/data";
7
+ export const SSE_DATA_URL = "queue/join";
8
+ export const UPLOAD_URL = "upload";
9
+ export const LOGIN_URL = "login";
10
+ export const CONFIG_URL = "config";
11
+ export const API_INFO_URL = "info";
12
+ export const RAW_API_INFO_URL = "info?serialize=False";
13
+ export const SPACE_FETCHER_URL =
14
+ "https://gradio-space-api-fetcher-v2.hf.space/api";
15
+ export const RESET_URL = "reset";
16
+ export const SPACE_URL = "https://hf.space/{}";
17
+
18
+ // messages
19
+ export const QUEUE_FULL_MSG = "This application is too busy. Keep trying!";
20
+ export const BROKEN_CONNECTION_MSG = "Connection errored out.";
package/src/globals.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Config } from "./types";
2
+
1
3
  declare global {
2
4
  interface Window {
3
5
  __gradio_mode__: "app" | "website";
@@ -6,24 +8,3 @@ declare global {
6
8
  __gradio_space__: string | null;
7
9
  }
8
10
  }
9
-
10
- export interface Config {
11
- auth_required: boolean | undefined;
12
- auth_message: string;
13
- components: any[];
14
- css: string | null;
15
- dependencies: any[];
16
- dev_mode: boolean;
17
- enable_queue: boolean;
18
- layout: any;
19
- mode: "blocks" | "interface";
20
- root: string;
21
- theme: string;
22
- title: string;
23
- version: string;
24
- space_id: string | null;
25
- is_colab: boolean;
26
- show_api: boolean;
27
- stylesheets: string[];
28
- path: string;
29
- }
@@ -0,0 +1,300 @@
1
+ import type { Status } from "../types";
2
+ import { QUEUE_FULL_MSG } from "../constants";
3
+ import type { ApiData, ApiInfo, Config, JsApiData } from "../types";
4
+ import { determine_protocol } from "./init_helpers";
5
+
6
+ export const RE_SPACE_NAME = /^[^\/]*\/[^\/]*$/;
7
+ export const RE_SPACE_DOMAIN = /.*hf\.space\/{0,1}$/;
8
+
9
+ export async function process_endpoint(
10
+ app_reference: string,
11
+ hf_token?: `hf_${string}`
12
+ ): Promise<{
13
+ space_id: string | false;
14
+ host: string;
15
+ ws_protocol: "ws" | "wss";
16
+ http_protocol: "http:" | "https:";
17
+ }> {
18
+ const headers: { Authorization?: string } = {};
19
+ if (hf_token) {
20
+ headers.Authorization = `Bearer ${hf_token}`;
21
+ }
22
+
23
+ const _app_reference = app_reference.trim();
24
+
25
+ if (RE_SPACE_NAME.test(_app_reference)) {
26
+ try {
27
+ const res = await fetch(
28
+ `https://huggingface.co/api/spaces/${_app_reference}/host`,
29
+ { headers }
30
+ );
31
+
32
+ if (res.status !== 200)
33
+ throw new Error("Space metadata could not be loaded.");
34
+ const _host = (await res.json()).host;
35
+
36
+ return {
37
+ space_id: app_reference,
38
+ ...determine_protocol(_host)
39
+ };
40
+ } catch (e: any) {
41
+ throw new Error("Space metadata could not be loaded." + e.message);
42
+ }
43
+ }
44
+
45
+ if (RE_SPACE_DOMAIN.test(_app_reference)) {
46
+ const { ws_protocol, http_protocol, host } =
47
+ determine_protocol(_app_reference);
48
+
49
+ return {
50
+ space_id: host.replace(".hf.space", ""),
51
+ ws_protocol,
52
+ http_protocol,
53
+ host
54
+ };
55
+ }
56
+
57
+ return {
58
+ space_id: false,
59
+ ...determine_protocol(_app_reference)
60
+ };
61
+ }
62
+
63
+ export function transform_api_info(
64
+ api_info: ApiInfo<ApiData>,
65
+ config: Config,
66
+ api_map: Record<string, number>
67
+ ): ApiInfo<JsApiData> {
68
+ const transformed_info: ApiInfo<JsApiData> = {
69
+ named_endpoints: {},
70
+ unnamed_endpoints: {}
71
+ };
72
+
73
+ Object.keys(api_info).forEach((category) => {
74
+ if (category === "named_endpoints" || category === "unnamed_endpoints") {
75
+ transformed_info[category] = {};
76
+
77
+ Object.entries(api_info[category]).forEach(
78
+ ([endpoint, { parameters, returns }]) => {
79
+ const dependencyIndex =
80
+ config.dependencies.findIndex((dep) => dep.api_name === endpoint) ||
81
+ api_map[endpoint.replace("/", "")] ||
82
+ -1;
83
+
84
+ const dependencyTypes =
85
+ dependencyIndex !== -1
86
+ ? config.dependencies[dependencyIndex].types
87
+ : { continuous: false, generator: false };
88
+
89
+ const transform_type = (
90
+ data: ApiData,
91
+ component: string,
92
+ serializer: string,
93
+ signature_type: "return" | "parameter"
94
+ ): JsApiData => ({
95
+ ...data,
96
+ description: get_description(data.type, serializer),
97
+ type:
98
+ get_type(data.type, component, serializer, signature_type) || ""
99
+ });
100
+
101
+ transformed_info[category][endpoint] = {
102
+ parameters: parameters.map((p: ApiData) =>
103
+ transform_type(p, p.component, p.serializer, "parameter")
104
+ ),
105
+ returns: returns.map((r: ApiData) =>
106
+ transform_type(r, r.component, r.serializer, "return")
107
+ ),
108
+ type: dependencyTypes
109
+ };
110
+ }
111
+ );
112
+ }
113
+ });
114
+
115
+ return transformed_info;
116
+ }
117
+
118
+ export function get_type(
119
+ type: { type: any; description: string },
120
+ component: string,
121
+ serializer: string,
122
+ signature_type: "return" | "parameter"
123
+ ): string | undefined {
124
+ switch (type.type) {
125
+ case "string":
126
+ return "string";
127
+ case "boolean":
128
+ return "boolean";
129
+ case "number":
130
+ return "number";
131
+ }
132
+
133
+ if (
134
+ serializer === "JSONSerializable" ||
135
+ serializer === "StringSerializable"
136
+ ) {
137
+ return "any";
138
+ } else if (serializer === "ListStringSerializable") {
139
+ return "string[]";
140
+ } else if (component === "Image") {
141
+ return signature_type === "parameter" ? "Blob | File | Buffer" : "string";
142
+ } else if (serializer === "FileSerializable") {
143
+ if (type?.type === "array") {
144
+ return signature_type === "parameter"
145
+ ? "(Blob | File | Buffer)[]"
146
+ : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}[]`;
147
+ }
148
+ return signature_type === "parameter"
149
+ ? "Blob | File | Buffer"
150
+ : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}`;
151
+ } else if (serializer === "GallerySerializable") {
152
+ return signature_type === "parameter"
153
+ ? "[(Blob | File | Buffer), (string | null)][]"
154
+ : `[{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}, (string | null))][]`;
155
+ }
156
+ }
157
+
158
+ export function get_description(
159
+ type: { type: any; description: string },
160
+ serializer: string
161
+ ): string {
162
+ if (serializer === "GallerySerializable") {
163
+ return "array of [file, label] tuples";
164
+ } else if (serializer === "ListStringSerializable") {
165
+ return "array of strings";
166
+ } else if (serializer === "FileSerializable") {
167
+ return "array of files or single file";
168
+ }
169
+ return type.description;
170
+ }
171
+
172
+ export function handle_message(
173
+ data: any,
174
+ last_status: Status["stage"]
175
+ ): {
176
+ type:
177
+ | "hash"
178
+ | "data"
179
+ | "update"
180
+ | "complete"
181
+ | "generating"
182
+ | "log"
183
+ | "none"
184
+ | "heartbeat"
185
+ | "unexpected_error";
186
+ data?: any;
187
+ status?: Status;
188
+ } {
189
+ const queue = true;
190
+ switch (data.msg) {
191
+ case "send_data":
192
+ return { type: "data" };
193
+ case "send_hash":
194
+ return { type: "hash" };
195
+ case "queue_full":
196
+ return {
197
+ type: "update",
198
+ status: {
199
+ queue,
200
+ message: QUEUE_FULL_MSG,
201
+ stage: "error",
202
+ code: data.code,
203
+ success: data.success
204
+ }
205
+ };
206
+ case "heartbeat":
207
+ return {
208
+ type: "heartbeat"
209
+ };
210
+ case "unexpected_error":
211
+ return {
212
+ type: "unexpected_error",
213
+ status: {
214
+ queue,
215
+ message: data.message,
216
+ stage: "error",
217
+ success: false
218
+ }
219
+ };
220
+ case "estimation":
221
+ return {
222
+ type: "update",
223
+ status: {
224
+ queue,
225
+ stage: last_status || "pending",
226
+ code: data.code,
227
+ size: data.queue_size,
228
+ position: data.rank,
229
+ eta: data.rank_eta,
230
+ success: data.success
231
+ }
232
+ };
233
+ case "progress":
234
+ return {
235
+ type: "update",
236
+ status: {
237
+ queue,
238
+ stage: "pending",
239
+ code: data.code,
240
+ progress_data: data.progress_data,
241
+ success: data.success
242
+ }
243
+ };
244
+ case "log":
245
+ return { type: "log", data: data };
246
+ case "process_generating":
247
+ return {
248
+ type: "generating",
249
+ status: {
250
+ queue,
251
+ message: !data.success ? data.output.error : null,
252
+ stage: data.success ? "generating" : "error",
253
+ code: data.code,
254
+ progress_data: data.progress_data,
255
+ eta: data.average_duration
256
+ },
257
+ data: data.success ? data.output : null
258
+ };
259
+ case "process_completed":
260
+ if ("error" in data.output) {
261
+ return {
262
+ type: "update",
263
+ status: {
264
+ queue,
265
+ message: data.output.error as string,
266
+ stage: "error",
267
+ code: data.code,
268
+ success: data.success
269
+ }
270
+ };
271
+ }
272
+ return {
273
+ type: "complete",
274
+ status: {
275
+ queue,
276
+ message: !data.success ? data.output.error : undefined,
277
+ stage: data.success ? "complete" : "error",
278
+ code: data.code,
279
+ progress_data: data.progress_data
280
+ },
281
+ data: data.success ? data.output : null
282
+ };
283
+
284
+ case "process_starts":
285
+ return {
286
+ type: "update",
287
+ status: {
288
+ queue,
289
+ stage: "pending",
290
+ code: data.code,
291
+ size: data.rank,
292
+ position: 0,
293
+ success: data.success,
294
+ eta: data.eta
295
+ }
296
+ };
297
+ }
298
+
299
+ return { type: "none", status: { stage: "error", queue } };
300
+ }
@@ -0,0 +1,115 @@
1
+ import { NodeBlob } from "../client";
2
+ import type {
3
+ ApiData,
4
+ BlobRef,
5
+ Config,
6
+ EndpointInfo,
7
+ JsApiData,
8
+ ParamType
9
+ } from "../types";
10
+
11
+ export function update_object(
12
+ object: { [x: string]: any },
13
+ newValue: any,
14
+ stack: (string | number)[]
15
+ ): void {
16
+ while (stack.length > 1) {
17
+ const key = stack.shift();
18
+ if (typeof key === "string" || typeof key === "number") {
19
+ object = object[key];
20
+ } else {
21
+ throw new Error("Invalid key type");
22
+ }
23
+ }
24
+
25
+ const key = stack.shift();
26
+ if (typeof key === "string" || typeof key === "number") {
27
+ object[key] = newValue;
28
+ } else {
29
+ throw new Error("Invalid key type");
30
+ }
31
+ }
32
+
33
+ export async function walk_and_store_blobs(
34
+ param: ParamType,
35
+ type: string | undefined = undefined,
36
+ path: string[] = [],
37
+ root = false,
38
+ endpoint_info: EndpointInfo<ApiData | JsApiData> | undefined = undefined
39
+ ): Promise<BlobRef[]> {
40
+ if (Array.isArray(param)) {
41
+ let blob_refs: BlobRef[] = [];
42
+
43
+ await Promise.all(
44
+ param.map(async (item) => {
45
+ let new_path = path.slice();
46
+ new_path.push(item);
47
+
48
+ const array_refs = await walk_and_store_blobs(
49
+ param[item],
50
+ root ? endpoint_info?.parameters[item]?.component || undefined : type,
51
+ new_path,
52
+ false,
53
+ endpoint_info
54
+ );
55
+
56
+ blob_refs = blob_refs.concat(array_refs);
57
+ })
58
+ );
59
+
60
+ return blob_refs;
61
+ } else if (globalThis.Buffer && param instanceof globalThis.Buffer) {
62
+ const is_image = type === "Image";
63
+ return [
64
+ {
65
+ path: path,
66
+ blob: is_image ? false : new NodeBlob([param]),
67
+ type
68
+ }
69
+ ];
70
+ } else if (typeof param === "object") {
71
+ let blob_refs: BlobRef[] = [];
72
+ for (let key in param) {
73
+ if (param.hasOwnProperty(key)) {
74
+ let new_path = path.slice();
75
+ new_path.push(key);
76
+ blob_refs = blob_refs.concat(
77
+ await walk_and_store_blobs(
78
+ // @ts-ignore
79
+ param[key],
80
+ undefined,
81
+ new_path,
82
+ false,
83
+ endpoint_info
84
+ )
85
+ );
86
+ }
87
+ }
88
+ return blob_refs;
89
+ }
90
+ return [];
91
+ }
92
+
93
+ export function skip_queue(id: number, config: Config): boolean {
94
+ return (
95
+ !(config?.dependencies?.[id]?.queue === null
96
+ ? config.enable_queue
97
+ : config?.dependencies?.[id]?.queue) || false
98
+ );
99
+ }
100
+
101
+ // todo: add jsdoc for this function
102
+
103
+ export function post_message<Res = any>(
104
+ message: any,
105
+ origin: string
106
+ ): Promise<Res> {
107
+ return new Promise((res, _rej) => {
108
+ const channel = new MessageChannel();
109
+ channel.port1.onmessage = (({ data }) => {
110
+ channel.port1.close();
111
+ res(data);
112
+ }) as (ev: MessageEvent<Res>) => void;
113
+ window.parent.postMessage(message, origin, [channel.port2]);
114
+ });
115
+ }
@@ -0,0 +1,134 @@
1
+ import type { Config } from "../types";
2
+ import { CONFIG_URL } from "../constants";
3
+ import { Client } from "..";
4
+
5
+ /**
6
+ * This function is used to resolve the URL for making requests when the app has a root path.
7
+ * The root path could be a path suffix like "/app" which is appended to the end of the base URL. Or
8
+ * it could be a full URL like "https://abidlabs-test-client-replica--gqf2x.hf.space" which is used when hosting
9
+ * Gradio apps on Hugging Face Spaces.
10
+ * @param {string} base_url The base URL at which the Gradio server is hosted
11
+ * @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)
12
+ * @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.
13
+ * @returns {string} the resolved URL
14
+ */
15
+ export function resolve_root(
16
+ base_url: string,
17
+ root_path: string,
18
+ prioritize_base: boolean
19
+ ): string {
20
+ if (root_path.startsWith("http://") || root_path.startsWith("https://")) {
21
+ return prioritize_base ? base_url : root_path;
22
+ }
23
+ return base_url + root_path;
24
+ }
25
+
26
+ export async function get_jwt(
27
+ space: string,
28
+ token: `hf_${string}`
29
+ ): Promise<string | false> {
30
+ try {
31
+ const r = await fetch(`https://huggingface.co/api/spaces/${space}/jwt`, {
32
+ headers: {
33
+ Authorization: `Bearer ${token}`
34
+ }
35
+ });
36
+
37
+ const jwt = (await r.json()).token;
38
+
39
+ return jwt || false;
40
+ } catch (e) {
41
+ console.error(e);
42
+ return false;
43
+ }
44
+ }
45
+
46
+ export function map_names_to_ids(
47
+ fns: Config["dependencies"]
48
+ ): Record<string, number> {
49
+ let apis: Record<string, number> = {};
50
+
51
+ fns.forEach(({ api_name }, i: number) => {
52
+ if (api_name) apis[api_name] = i;
53
+ });
54
+ return apis;
55
+ }
56
+
57
+ export async function resolve_config(
58
+ this: Client,
59
+ endpoint: string
60
+ ): Promise<Config | undefined> {
61
+ const headers: Record<string, string> = this.options.hf_token
62
+ ? { Authorization: `Bearer ${this.options.hf_token}` }
63
+ : {};
64
+
65
+ headers["Content-Type"] = "application/json";
66
+
67
+ if (
68
+ typeof window !== "undefined" &&
69
+ window.gradio_config &&
70
+ location.origin !== "http://localhost:9876" &&
71
+ !window.gradio_config.dev_mode
72
+ ) {
73
+ const path = window.gradio_config.root;
74
+ const config = window.gradio_config;
75
+ let config_root = resolve_root(endpoint, config.root, false);
76
+ config.root = config_root;
77
+ return { ...config, path };
78
+ } else if (endpoint) {
79
+ const response = await this.fetch_implementation(
80
+ `${endpoint}/${CONFIG_URL}`,
81
+ {
82
+ headers
83
+ }
84
+ );
85
+
86
+ if (response?.status === 200) {
87
+ let config = await response.json();
88
+ config.path = config.path ?? "";
89
+ config.root = endpoint;
90
+ return config;
91
+ }
92
+ throw new Error("Could not get config.");
93
+ }
94
+
95
+ throw new Error("No config or app endpoint found");
96
+ }
97
+
98
+ export function determine_protocol(endpoint: string): {
99
+ ws_protocol: "ws" | "wss";
100
+ http_protocol: "http:" | "https:";
101
+ host: string;
102
+ } {
103
+ if (endpoint.startsWith("http")) {
104
+ const { protocol, host } = new URL(endpoint);
105
+
106
+ if (host.endsWith("hf.space")) {
107
+ return {
108
+ ws_protocol: "wss",
109
+ host: host,
110
+ http_protocol: protocol as "http:" | "https:"
111
+ };
112
+ }
113
+ return {
114
+ ws_protocol: protocol === "https:" ? "wss" : "ws",
115
+ http_protocol: protocol as "http:" | "https:",
116
+ host
117
+ };
118
+ } else if (endpoint.startsWith("file:")) {
119
+ // This case is only expected to be used for the Wasm mode (Gradio-lite),
120
+ // where users can create a local HTML file using it and open the page in a browser directly via the `file:` protocol.
121
+ return {
122
+ ws_protocol: "ws",
123
+ http_protocol: "http:",
124
+ host: "lite.local" // Special fake hostname only used for this case. This matches the hostname allowed in `is_self_host()` in `js/wasm/network/host.ts`.
125
+ };
126
+ }
127
+
128
+ // default to secure if no protocol is provided
129
+ return {
130
+ ws_protocol: "wss",
131
+ http_protocol: "https:",
132
+ host: endpoint
133
+ };
134
+ }