@gradio/client 0.16.0 → 0.18.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 +57 -0
- package/README.md +49 -43
- package/dist/client.d.ts +63 -70
- package/dist/client.d.ts.map +1 -1
- package/dist/constants.d.ts +23 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/helpers/api_info.d.ts +25 -0
- package/dist/helpers/api_info.d.ts.map +1 -0
- package/dist/helpers/data.d.ts +8 -0
- package/dist/helpers/data.d.ts.map +1 -0
- package/dist/{utils.d.ts → helpers/init_helpers.d.ts} +6 -18
- package/dist/helpers/init_helpers.d.ts.map +1 -0
- package/dist/helpers/spaces.d.ts +7 -0
- package/dist/helpers/spaces.d.ts.map +1 -0
- package/dist/index.d.ts +8 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1628 -1398
- package/dist/test/handlers.d.ts +3 -0
- package/dist/test/handlers.d.ts.map +1 -0
- package/dist/test/mock_eventsource.d.ts +2 -0
- package/dist/test/mock_eventsource.d.ts.map +1 -0
- package/dist/test/server.d.ts +2 -0
- package/dist/test/server.d.ts.map +1 -0
- package/dist/test/test_data.d.ts +76 -0
- package/dist/test/test_data.d.ts.map +1 -0
- package/dist/types.d.ts +153 -49
- package/dist/types.d.ts.map +1 -1
- package/dist/upload.d.ts +2 -2
- package/dist/upload.d.ts.map +1 -1
- package/dist/utils/duplicate.d.ts +4 -0
- package/dist/utils/duplicate.d.ts.map +1 -0
- package/dist/utils/handle_blob.d.ts +4 -0
- package/dist/utils/handle_blob.d.ts.map +1 -0
- package/dist/utils/post_data.d.ts +4 -0
- package/dist/utils/post_data.d.ts.map +1 -0
- package/dist/utils/predict.d.ts +4 -0
- package/dist/utils/predict.d.ts.map +1 -0
- package/dist/utils/stream.d.ts +8 -0
- package/dist/utils/stream.d.ts.map +1 -0
- package/dist/utils/submit.d.ts +4 -0
- package/dist/utils/submit.d.ts.map +1 -0
- package/dist/utils/upload_files.d.ts +4 -0
- package/dist/utils/upload_files.d.ts.map +1 -0
- package/dist/utils/view_api.d.ts +3 -0
- package/dist/utils/view_api.d.ts.map +1 -0
- package/dist/{wrapper-6f348d45.js → wrapper-CviSselG.js} +259 -17
- package/package.json +10 -3
- package/src/client.ts +314 -1691
- package/src/constants.ts +27 -0
- package/src/globals.d.ts +2 -21
- package/src/helpers/api_info.ts +300 -0
- package/src/helpers/data.ts +133 -0
- package/src/helpers/init_helpers.ts +130 -0
- package/src/helpers/spaces.ts +197 -0
- package/src/index.ts +16 -10
- package/src/test/api_info.test.ts +456 -0
- package/src/test/data.test.ts +281 -0
- package/src/test/handlers.ts +438 -0
- package/src/test/init.test.ts +139 -0
- package/src/test/init_helpers.test.ts +94 -0
- package/src/test/mock_eventsource.ts +11 -0
- package/src/test/post_data.test.ts +45 -0
- package/src/test/server.ts +6 -0
- package/src/test/spaces.test.ts +145 -0
- package/src/test/stream.test.ts +67 -0
- package/src/test/test_data.ts +557 -0
- package/src/test/upload_files.test.ts +42 -0
- package/src/test/view_api.test.ts +53 -0
- package/src/types.ts +201 -59
- package/src/upload.ts +19 -15
- package/src/utils/duplicate.ts +104 -0
- package/src/utils/handle_blob.ts +47 -0
- package/src/utils/post_data.ts +37 -0
- package/src/utils/predict.ts +56 -0
- package/src/utils/stream.ts +175 -0
- package/src/utils/submit.ts +697 -0
- package/src/utils/upload_files.ts +51 -0
- package/src/utils/view_api.ts +67 -0
- package/src/vite-env.d.ts +1 -0
- package/tsconfig.json +15 -2
- package/vite.config.js +11 -17
- package/dist/utils.d.ts.map +0 -1
- package/src/client.node-test.ts +0 -172
- package/src/utils.ts +0 -314
package/src/constants.ts
ADDED
@@ -0,0 +1,27 @@
|
|
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 RUNTIME_URL = "runtime";
|
13
|
+
export const SLEEPTIME_URL = "sleeptime";
|
14
|
+
export const RAW_API_INFO_URL = "info?serialize=False";
|
15
|
+
export const SPACE_FETCHER_URL =
|
16
|
+
"https://gradio-space-api-fetcher-v2.hf.space/api";
|
17
|
+
export const RESET_URL = "reset";
|
18
|
+
export const SPACE_URL = "https://hf.space/{}";
|
19
|
+
|
20
|
+
// messages
|
21
|
+
export const QUEUE_FULL_MSG =
|
22
|
+
"This application is currently busy. Please try again. ";
|
23
|
+
export const BROKEN_CONNECTION_MSG = "Connection errored out. ";
|
24
|
+
export const CONFIG_ERROR_MSG = "Could not resolve app config. ";
|
25
|
+
export const SPACE_STATUS_ERROR_MSG = "Could not get space status. ";
|
26
|
+
export const API_INFO_ERROR_MSG = "Could not get API info. ";
|
27
|
+
export const SPACE_METADATA_ERROR_MSG = "Space metadata could not be loaded. ";
|
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
|
+
const _host = (await res.json()).host;
|
33
|
+
|
34
|
+
return {
|
35
|
+
space_id: app_reference,
|
36
|
+
...determine_protocol(_host)
|
37
|
+
};
|
38
|
+
} catch (e) {
|
39
|
+
throw new Error(
|
40
|
+
"Space metadata could not be loaded. " + (e as Error).message
|
41
|
+
);
|
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,133 @@
|
|
1
|
+
import { NodeBlob } from "../client";
|
2
|
+
import type {
|
3
|
+
ApiData,
|
4
|
+
BlobRef,
|
5
|
+
Config,
|
6
|
+
EndpointInfo,
|
7
|
+
JsApiData,
|
8
|
+
DataType
|
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
|
+
data: DataType,
|
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(data)) {
|
41
|
+
let blob_refs: BlobRef[] = [];
|
42
|
+
|
43
|
+
await Promise.all(
|
44
|
+
data.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
|
+
data[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 (
|
62
|
+
(globalThis.Buffer && data instanceof globalThis.Buffer) ||
|
63
|
+
data instanceof Blob
|
64
|
+
) {
|
65
|
+
const is_image = type === "Image";
|
66
|
+
return [
|
67
|
+
{
|
68
|
+
path: path,
|
69
|
+
blob: is_image ? false : new NodeBlob([data]),
|
70
|
+
type
|
71
|
+
}
|
72
|
+
];
|
73
|
+
} else if (typeof data === "object" && data !== null) {
|
74
|
+
let blob_refs: BlobRef[] = [];
|
75
|
+
for (const key of Object.keys(data) as (keyof typeof data)[]) {
|
76
|
+
const new_path = [...path, key];
|
77
|
+
const value = data[key];
|
78
|
+
|
79
|
+
blob_refs = blob_refs.concat(
|
80
|
+
await walk_and_store_blobs(
|
81
|
+
value,
|
82
|
+
undefined,
|
83
|
+
new_path,
|
84
|
+
false,
|
85
|
+
endpoint_info
|
86
|
+
)
|
87
|
+
);
|
88
|
+
}
|
89
|
+
|
90
|
+
if (
|
91
|
+
!blob_refs.length &&
|
92
|
+
!(
|
93
|
+
data instanceof Blob ||
|
94
|
+
data instanceof ArrayBuffer ||
|
95
|
+
data instanceof Uint8Array
|
96
|
+
)
|
97
|
+
) {
|
98
|
+
return [
|
99
|
+
{
|
100
|
+
path: path,
|
101
|
+
blob: new NodeBlob([JSON.stringify(data)]),
|
102
|
+
type: typeof data
|
103
|
+
}
|
104
|
+
];
|
105
|
+
}
|
106
|
+
return blob_refs;
|
107
|
+
}
|
108
|
+
|
109
|
+
return [];
|
110
|
+
}
|
111
|
+
|
112
|
+
export function skip_queue(id: number, config: Config): boolean {
|
113
|
+
if (config?.dependencies?.[id]?.queue !== null) {
|
114
|
+
return !config.dependencies[id].queue;
|
115
|
+
}
|
116
|
+
return !config.enable_queue;
|
117
|
+
}
|
118
|
+
|
119
|
+
// todo: add jsdoc for this function
|
120
|
+
|
121
|
+
export function post_message<Res = any>(
|
122
|
+
message: any,
|
123
|
+
origin: string
|
124
|
+
): Promise<Res> {
|
125
|
+
return new Promise((res, _rej) => {
|
126
|
+
const channel = new MessageChannel();
|
127
|
+
channel.port1.onmessage = (({ data }) => {
|
128
|
+
channel.port1.close();
|
129
|
+
res(data);
|
130
|
+
}) as (ev: MessageEvent<Res>) => void;
|
131
|
+
window.parent.postMessage(message, origin, [channel.port2]);
|
132
|
+
});
|
133
|
+
}
|
@@ -0,0 +1,130 @@
|
|
1
|
+
import type { Config } from "../types";
|
2
|
+
import { CONFIG_ERROR_MSG, 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
|
+
return false;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
export function map_names_to_ids(
|
46
|
+
fns: Config["dependencies"]
|
47
|
+
): Record<string, number> {
|
48
|
+
let apis: Record<string, number> = {};
|
49
|
+
|
50
|
+
fns.forEach(({ api_name }, i: number) => {
|
51
|
+
if (api_name) apis[api_name] = i;
|
52
|
+
});
|
53
|
+
return apis;
|
54
|
+
}
|
55
|
+
|
56
|
+
export async function resolve_config(
|
57
|
+
this: Client,
|
58
|
+
endpoint: string
|
59
|
+
): Promise<Config | undefined> {
|
60
|
+
const headers: Record<string, string> = this.options.hf_token
|
61
|
+
? { Authorization: `Bearer ${this.options.hf_token}` }
|
62
|
+
: {};
|
63
|
+
|
64
|
+
headers["Content-Type"] = "application/json";
|
65
|
+
|
66
|
+
if (
|
67
|
+
typeof window !== "undefined" &&
|
68
|
+
window.gradio_config &&
|
69
|
+
location.origin !== "http://localhost:9876" &&
|
70
|
+
!window.gradio_config.dev_mode
|
71
|
+
) {
|
72
|
+
const path = window.gradio_config.root;
|
73
|
+
const config = window.gradio_config;
|
74
|
+
let config_root = resolve_root(endpoint, config.root, false);
|
75
|
+
config.root = config_root;
|
76
|
+
return { ...config, path } as Config;
|
77
|
+
} else if (endpoint) {
|
78
|
+
const response = await this.fetch(`${endpoint}/${CONFIG_URL}`, {
|
79
|
+
headers
|
80
|
+
});
|
81
|
+
|
82
|
+
if (response?.status === 200) {
|
83
|
+
let config = await response.json();
|
84
|
+
config.path = config.path ?? "";
|
85
|
+
config.root = endpoint;
|
86
|
+
return config;
|
87
|
+
}
|
88
|
+
throw new Error(CONFIG_ERROR_MSG);
|
89
|
+
}
|
90
|
+
|
91
|
+
throw new Error(CONFIG_ERROR_MSG);
|
92
|
+
}
|
93
|
+
|
94
|
+
export function determine_protocol(endpoint: string): {
|
95
|
+
ws_protocol: "ws" | "wss";
|
96
|
+
http_protocol: "http:" | "https:";
|
97
|
+
host: string;
|
98
|
+
} {
|
99
|
+
if (endpoint.startsWith("http")) {
|
100
|
+
const { protocol, host } = new URL(endpoint);
|
101
|
+
|
102
|
+
if (host.endsWith("hf.space")) {
|
103
|
+
return {
|
104
|
+
ws_protocol: "wss",
|
105
|
+
host: host,
|
106
|
+
http_protocol: protocol as "http:" | "https:"
|
107
|
+
};
|
108
|
+
}
|
109
|
+
return {
|
110
|
+
ws_protocol: protocol === "https:" ? "wss" : "ws",
|
111
|
+
http_protocol: protocol as "http:" | "https:",
|
112
|
+
host
|
113
|
+
};
|
114
|
+
} else if (endpoint.startsWith("file:")) {
|
115
|
+
// This case is only expected to be used for the Wasm mode (Gradio-lite),
|
116
|
+
// where users can create a local HTML file using it and open the page in a browser directly via the `file:` protocol.
|
117
|
+
return {
|
118
|
+
ws_protocol: "ws",
|
119
|
+
http_protocol: "http:",
|
120
|
+
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`.
|
121
|
+
};
|
122
|
+
}
|
123
|
+
|
124
|
+
// default to secure if no protocol is provided
|
125
|
+
return {
|
126
|
+
ws_protocol: "wss",
|
127
|
+
http_protocol: "https:",
|
128
|
+
host: endpoint
|
129
|
+
};
|
130
|
+
}
|