@gradio/client 2.3.0 → 2.4.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 +23 -0
- package/README.md +4 -2
- package/dist/__vite-browser-external-Cgmn0awE.js +7 -0
- package/dist/browser.js +1672 -1902
- package/dist/client.d.ts +5 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/constants.d.ts +4 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/helpers/api_info.d.ts.map +1 -1
- package/dist/helpers/data.d.ts.map +1 -1
- package/dist/helpers/init_helpers.d.ts +12 -3
- package/dist/helpers/init_helpers.d.ts.map +1 -1
- package/dist/index.cjs +2274 -0
- package/dist/index.js +2155 -2449
- package/dist/index.min.js +1735 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/duplicate.d.ts.map +1 -1
- package/dist/utils/predict.d.ts.map +1 -1
- package/dist/utils/stream.d.ts.map +1 -1
- package/dist/utils/submit.d.ts.map +1 -1
- package/dist/utils/view_api.d.ts.map +1 -1
- package/package.json +11 -4
- package/scripts/verify_dist.mjs +46 -0
- package/src/client.ts +46 -7
- package/src/constants.ts +10 -0
- package/src/helpers/api_info.ts +47 -19
- package/src/helpers/data.ts +8 -3
- package/src/helpers/init_helpers.ts +61 -14
- package/src/test/api_info.test.ts +105 -12
- package/src/test/data.test.ts +26 -3
- package/src/test/init.test.ts +21 -3
- package/src/test/init_helpers.test.ts +94 -1
- package/src/test/post_data.test.ts +21 -0
- package/src/test/refresh.test.ts +31 -0
- package/src/test/submit.test.ts +14 -0
- package/src/types.ts +16 -0
- package/src/utils/duplicate.ts +5 -1
- package/src/utils/post_data.ts +1 -1
- package/src/utils/predict.ts +34 -19
- package/src/utils/stream.ts +17 -4
- package/src/utils/submit.ts +43 -10
- package/src/utils/upload_files.ts +1 -1
- package/src/utils/view_api.ts +2 -3
- package/vite.config.ts +20 -4
- package/dist/__vite-browser-external-DYxpcVy9.js +0 -4
|
@@ -0,0 +1,1735 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescriptor, r = Object.getOwnPropertyNames, i = Object.getPrototypeOf, a = Object.prototype.hasOwnProperty, o = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), s = (e, i, o, s) => {
|
|
3
|
+
if (i && typeof i == "object" || typeof i == "function") for (var c = r(i), l = 0, u = c.length, d; l < u; l++) d = c[l], !a.call(e, d) && d !== o && t(e, d, {
|
|
4
|
+
get: ((e) => i[e]).bind(null, d),
|
|
5
|
+
enumerable: !(s = n(i, d)) || s.enumerable
|
|
6
|
+
});
|
|
7
|
+
return e;
|
|
8
|
+
}, c = (n, r, a) => (a = n == null ? {} : e(i(n)), s(r || !n || !n.__esModule ? t(a, "default", {
|
|
9
|
+
value: n,
|
|
10
|
+
enumerable: !0
|
|
11
|
+
}) : a, n)), l = "host", u = "queue/data", d = "queue/join", f = "upload", p = "login", m = "config", h = "info", g = "runtime", _ = "sleeptime", v = "heartbeat", ee = "component_server", te = "reset", ne = "cancel", y = "app_id", re = "This application is currently busy. Please try again. ", b = "Connection errored out. ", x = "Could not resolve app config. ", S = "Could not get space status. ", C = "Could not get API info. ", w = "Space metadata could not be loaded. ", T = "Invalid URL. A full URL path is required.", E = "Not authorized to access this space. ", D = "Invalid credentials. Could not login. ", ie = "Login credentials are required to access this space.", O = "Could not access this app (received a 401 response). If it is a private Hugging Face Space, pass a valid Hugging Face token to the `token` option of `Client.connect`. You can generate a token at https://huggingface.co/settings/tokens.", k = (e, t) => `Space "${e}" could not be accessed (received a ${t} response from the Hugging Face API). Check that the Space name is spelled correctly and that the Space exists. If the Space is private, pass a valid Hugging Face token to the \`token\` option of \`Client.connect\`. You can generate a token at https://huggingface.co/settings/tokens.`, ae = "No API information is available for this app. This can happen when the app's `/info` endpoint cannot be reached, or when the app is running a legacy version of Gradio that is not supported by this client. ", oe = "This app appears to be running a legacy version of Gradio (3.x or earlier) that communicates over WebSockets, which is not supported by this version of @gradio/client. Please upgrade the app to a newer version of Gradio, or connect to it with @gradio/client version 0.x.", A = "File system access is only available in Node.js environments", j = "Root URL not found in client config", M = "Error uploading file";
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/helpers/init_helpers.ts
|
|
14
|
+
async function N(e, t, n) {
|
|
15
|
+
try {
|
|
16
|
+
return (await (await fetch(`https://huggingface.co/api/spaces/${e}/jwt`, { headers: {
|
|
17
|
+
Authorization: `Bearer ${t}`,
|
|
18
|
+
...n ? { Cookie: n } : {}
|
|
19
|
+
} })).json()).token || !1;
|
|
20
|
+
} catch {
|
|
21
|
+
return !1;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function P(e) {
|
|
25
|
+
e.hf_token && !e.token && (e.token = e.hf_token, console.warn("The `hf_token` option has been renamed to `token`. Support for `hf_token` will be removed in a future version of @gradio/client."));
|
|
26
|
+
}
|
|
27
|
+
function F(e) {
|
|
28
|
+
let t = {};
|
|
29
|
+
return e.forEach(({ api_name: e, id: n }) => {
|
|
30
|
+
e && (t[e] = n);
|
|
31
|
+
}), t;
|
|
32
|
+
}
|
|
33
|
+
function I(e, t) {
|
|
34
|
+
let n = new URL(e, t), r = new URL(t);
|
|
35
|
+
return n.hostname === r.hostname ? (n.protocol = r.protocol, n.host = r.host, n.toString().replace(/\/$/, "")) : e;
|
|
36
|
+
}
|
|
37
|
+
async function se(e, t = !0) {
|
|
38
|
+
let n = this.options.token ? { Authorization: `Bearer ${this.options.token}` } : {};
|
|
39
|
+
if (typeof window < "u" && window.gradio_config && location.origin !== "http://localhost:9876") {
|
|
40
|
+
if (t && window.gradio_config.current_page && (e = e.substring(0, e.lastIndexOf("/"))), window.gradio_config.dev_mode || typeof window < "u" && window?.BUILD_MODE === "dev") {
|
|
41
|
+
let t = W(e, this.deep_link ? m + "?deep_link=" + this.deep_link : m), r = await L(await this.fetch(t, {
|
|
42
|
+
headers: n,
|
|
43
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
44
|
+
}), !!this.options.auth);
|
|
45
|
+
r.root = e || r.root, window.gradio_config = {
|
|
46
|
+
...r,
|
|
47
|
+
current_page: window.gradio_config.current_page
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
let r = { ...window.gradio_config };
|
|
51
|
+
return r.root = I(r.root, location.href), r;
|
|
52
|
+
} else if (e) {
|
|
53
|
+
let t = W(e, this.deep_link ? m + "?deep_link=" + this.deep_link : m), r = await L(await this.fetch(t, {
|
|
54
|
+
headers: n,
|
|
55
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
56
|
+
}), !!this.options.auth);
|
|
57
|
+
return r.root ||= e, r;
|
|
58
|
+
}
|
|
59
|
+
throw Error(x);
|
|
60
|
+
}
|
|
61
|
+
async function L(e, t) {
|
|
62
|
+
if (e?.status === 401 && !t) {
|
|
63
|
+
let t = null;
|
|
64
|
+
try {
|
|
65
|
+
t = await e.json();
|
|
66
|
+
} catch {
|
|
67
|
+
throw Error(O);
|
|
68
|
+
}
|
|
69
|
+
let n = t?.detail?.auth_message;
|
|
70
|
+
throw Error(n || "Login credentials are required to access this space.");
|
|
71
|
+
} else if (e?.status === 401 && t) throw Error(D);
|
|
72
|
+
if (e?.status === 200) {
|
|
73
|
+
let t = await e.json();
|
|
74
|
+
return t.dependencies?.forEach((e, t) => {
|
|
75
|
+
e.id === void 0 && (e.id = t);
|
|
76
|
+
}), t;
|
|
77
|
+
} else if (e?.status === 401) throw Error(E);
|
|
78
|
+
throw Error(`${x}(received status ${e?.status} when fetching the app config)`);
|
|
79
|
+
}
|
|
80
|
+
async function ce() {
|
|
81
|
+
let { http_protocol: e, host: t } = await U(this.app_reference, this.options.token);
|
|
82
|
+
try {
|
|
83
|
+
if (this.options.auth) {
|
|
84
|
+
let n = await R(e, t, this.options.auth, this.fetch, this.options.token, this.options.credentials);
|
|
85
|
+
n && this.set_cookies(n);
|
|
86
|
+
}
|
|
87
|
+
} catch (e) {
|
|
88
|
+
throw Error(e.message);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async function R(e, t, n, r, i, a) {
|
|
92
|
+
let o = new FormData();
|
|
93
|
+
o.append("username", n?.[0]), o.append("password", n?.[1]);
|
|
94
|
+
let s = {};
|
|
95
|
+
i && (s.Authorization = `Bearer ${i}`);
|
|
96
|
+
let c = await r(`${e}//${t}/${p}`, {
|
|
97
|
+
headers: s,
|
|
98
|
+
method: "POST",
|
|
99
|
+
body: o,
|
|
100
|
+
credentials: a ?? "same-origin"
|
|
101
|
+
});
|
|
102
|
+
if (c.status === 200) return c.headers.get("set-cookie");
|
|
103
|
+
throw c.status === 401 ? Error(D) : Error(w);
|
|
104
|
+
}
|
|
105
|
+
function z(e) {
|
|
106
|
+
if (e.startsWith("http")) {
|
|
107
|
+
let { protocol: t, host: n, pathname: r } = new URL(e);
|
|
108
|
+
return {
|
|
109
|
+
ws_protocol: t === "https:" ? "wss" : "ws",
|
|
110
|
+
http_protocol: t,
|
|
111
|
+
host: n + (r === "/" ? "" : r)
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
ws_protocol: "wss",
|
|
116
|
+
http_protocol: "https:",
|
|
117
|
+
host: new URL(e).host
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
var B = (e) => {
|
|
121
|
+
let t = [];
|
|
122
|
+
return e.split(/,(?=\s*[^\s=;]+=[^\s=;]+)/).forEach((e) => {
|
|
123
|
+
let [n, r] = e.split(";")[0].split("=");
|
|
124
|
+
n && r && t.push(`${n.trim()}=${r.trim()}`);
|
|
125
|
+
}), t;
|
|
126
|
+
}, V = /^[a-zA-Z0-9_\-\.]+\/[a-zA-Z0-9_\-\.]+$/, H = /.*hf\.space\/{0,1}.*$/;
|
|
127
|
+
async function U(e, t) {
|
|
128
|
+
let n = {};
|
|
129
|
+
t && (n.Authorization = `Bearer ${t}`);
|
|
130
|
+
let r = e.trim().replace(/\/$/, "");
|
|
131
|
+
if (V.test(r)) {
|
|
132
|
+
let e;
|
|
133
|
+
try {
|
|
134
|
+
e = await fetch(`https://huggingface.co/api/spaces/${r}/${l}`, { headers: n });
|
|
135
|
+
} catch {
|
|
136
|
+
throw Error(w);
|
|
137
|
+
}
|
|
138
|
+
if (e.status === 401 || e.status === 404) throw Error(k(r, e.status));
|
|
139
|
+
let t;
|
|
140
|
+
try {
|
|
141
|
+
t = (await e.json()).host;
|
|
142
|
+
} catch {
|
|
143
|
+
throw Error(w);
|
|
144
|
+
}
|
|
145
|
+
if (!t) throw Error(w);
|
|
146
|
+
return {
|
|
147
|
+
space_id: r,
|
|
148
|
+
...z(t)
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
if (H.test(r)) {
|
|
152
|
+
let { ws_protocol: e, http_protocol: t, host: n } = z(r);
|
|
153
|
+
return {
|
|
154
|
+
space_id: n.split("/")[0].replace(".hf.space", ""),
|
|
155
|
+
ws_protocol: e,
|
|
156
|
+
http_protocol: t,
|
|
157
|
+
host: n
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
space_id: !1,
|
|
162
|
+
...z(r)
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
var W = (...e) => {
|
|
166
|
+
try {
|
|
167
|
+
return e.reduce((e, t) => (e = e.replace(/\/+$/, ""), t = t.replace(/^\/+/, ""), new URL(t, e + "/").toString()));
|
|
168
|
+
} catch {
|
|
169
|
+
throw Error(T);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
function G(e, t, n) {
|
|
173
|
+
let r = {
|
|
174
|
+
named_endpoints: {},
|
|
175
|
+
unnamed_endpoints: {}
|
|
176
|
+
};
|
|
177
|
+
return Object.keys(e).forEach((i) => {
|
|
178
|
+
(i === "named_endpoints" || i === "unnamed_endpoints") && (r[i] = {}, Object.entries(e[i]).forEach(([e, a]) => {
|
|
179
|
+
let o = a?.parameters ?? [], s = a?.returns ?? [], c = t.dependencies.find((t) => t.api_name === e || t.api_name === e.replace("/", ""))?.id || n[e.replace("/", "")] || -1, l = c === -1 ? void 0 : t.dependencies.find((e) => e.id == c), u = c === -1 ? {
|
|
180
|
+
generator: !1,
|
|
181
|
+
cancel: !1
|
|
182
|
+
} : l?.types;
|
|
183
|
+
if (l && Array.isArray(l.inputs) && l.inputs.length !== o.length) {
|
|
184
|
+
let e = l.inputs.map((e) => t.components.find((t) => t.id === e)?.type);
|
|
185
|
+
try {
|
|
186
|
+
e.forEach((e, t) => {
|
|
187
|
+
e === "state" && o.splice(t, 0, {
|
|
188
|
+
component: "state",
|
|
189
|
+
example: null,
|
|
190
|
+
parameter_default: null,
|
|
191
|
+
parameter_has_default: !0,
|
|
192
|
+
parameter_name: null,
|
|
193
|
+
hidden: !0
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
} catch (e) {
|
|
197
|
+
console.error(e);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
let d = (e, t, n, r) => ({
|
|
201
|
+
...e,
|
|
202
|
+
description: ue(e?.type, n),
|
|
203
|
+
type: le(e?.type, t, n, r) || ""
|
|
204
|
+
});
|
|
205
|
+
r[i][e] = {
|
|
206
|
+
parameters: o.map((e) => d(e, e?.component, e?.serializer, "parameter")),
|
|
207
|
+
returns: s.map((e) => d(e, e?.component, e?.serializer, "return")),
|
|
208
|
+
type: u,
|
|
209
|
+
...a?.oauth_token ? { oauth_token: a.oauth_token } : {}
|
|
210
|
+
};
|
|
211
|
+
}));
|
|
212
|
+
}), r;
|
|
213
|
+
}
|
|
214
|
+
function le(e, t, n, r) {
|
|
215
|
+
if (t === "Api") return e.type;
|
|
216
|
+
switch (e?.type) {
|
|
217
|
+
case "string": return "string";
|
|
218
|
+
case "boolean": return "boolean";
|
|
219
|
+
case "number": return "number";
|
|
220
|
+
}
|
|
221
|
+
if (n === "JSONSerializable" || n === "StringSerializable") return "any";
|
|
222
|
+
if (n === "ListStringSerializable") return "string[]";
|
|
223
|
+
if (t === "Image") return r === "parameter" ? "Blob | File | Buffer" : "string";
|
|
224
|
+
if (n === "FileSerializable") return e?.type === "array" ? r === "parameter" ? "(Blob | File | Buffer)[]" : "{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}[]" : r === "parameter" ? "Blob | File | Buffer" : "{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}";
|
|
225
|
+
if (n === "GallerySerializable") return r === "parameter" ? "[(Blob | File | Buffer), (string | null)][]" : "[{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}, (string | null))][]";
|
|
226
|
+
}
|
|
227
|
+
function ue(e, t) {
|
|
228
|
+
return t === "GallerySerializable" ? "array of [file, label] tuples" : t === "ListStringSerializable" ? "array of strings" : t === "FileSerializable" ? "array of files or single file" : e?.description;
|
|
229
|
+
}
|
|
230
|
+
function de(e, t) {
|
|
231
|
+
switch (e.msg) {
|
|
232
|
+
case "send_data": return { type: "data" };
|
|
233
|
+
case "send_hash": return { type: "hash" };
|
|
234
|
+
case "queue_full": return {
|
|
235
|
+
type: "update",
|
|
236
|
+
status: {
|
|
237
|
+
queue: !0,
|
|
238
|
+
message: re,
|
|
239
|
+
stage: "error",
|
|
240
|
+
code: e.code,
|
|
241
|
+
success: e.success
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
case "heartbeat": return { type: "heartbeat" };
|
|
245
|
+
case "unexpected_error": return {
|
|
246
|
+
type: "unexpected_error",
|
|
247
|
+
status: {
|
|
248
|
+
queue: !0,
|
|
249
|
+
message: e.message,
|
|
250
|
+
session_not_found: e.session_not_found,
|
|
251
|
+
stage: "error",
|
|
252
|
+
success: !1
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
case "broken_connection": return {
|
|
256
|
+
type: "broken_connection",
|
|
257
|
+
status: {
|
|
258
|
+
queue: !0,
|
|
259
|
+
message: e.message,
|
|
260
|
+
stage: "error",
|
|
261
|
+
success: !1
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
case "estimation": return {
|
|
265
|
+
type: "update",
|
|
266
|
+
status: {
|
|
267
|
+
queue: !0,
|
|
268
|
+
stage: t || "pending",
|
|
269
|
+
code: e.code,
|
|
270
|
+
size: e.queue_size,
|
|
271
|
+
position: e.rank,
|
|
272
|
+
eta: e.rank_eta,
|
|
273
|
+
success: e.success
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
case "progress": return {
|
|
277
|
+
type: "update",
|
|
278
|
+
status: {
|
|
279
|
+
queue: !0,
|
|
280
|
+
stage: "pending",
|
|
281
|
+
code: e.code,
|
|
282
|
+
progress_data: e.progress_data,
|
|
283
|
+
success: e.success
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
case "log": return {
|
|
287
|
+
type: "log",
|
|
288
|
+
data: e
|
|
289
|
+
};
|
|
290
|
+
case "process_generating": return {
|
|
291
|
+
type: "generating",
|
|
292
|
+
status: {
|
|
293
|
+
queue: !0,
|
|
294
|
+
message: e.success ? null : e.output.error,
|
|
295
|
+
stage: e.success ? "generating" : "error",
|
|
296
|
+
code: e.code,
|
|
297
|
+
progress_data: e.progress_data,
|
|
298
|
+
eta: e.average_duration,
|
|
299
|
+
changed_state_ids: e.success ? e.output.changed_state_ids : void 0
|
|
300
|
+
},
|
|
301
|
+
data: e.success ? e.output : null
|
|
302
|
+
};
|
|
303
|
+
case "process_streaming": return {
|
|
304
|
+
type: "streaming",
|
|
305
|
+
status: {
|
|
306
|
+
queue: !0,
|
|
307
|
+
message: e.output.error,
|
|
308
|
+
stage: "streaming",
|
|
309
|
+
time_limit: e.time_limit,
|
|
310
|
+
code: e.code,
|
|
311
|
+
progress_data: e.progress_data,
|
|
312
|
+
changed_state_ids: e.output.changed_state_ids,
|
|
313
|
+
eta: e.eta
|
|
314
|
+
},
|
|
315
|
+
data: e.output
|
|
316
|
+
};
|
|
317
|
+
case "process_completed": return "error" in e.output ? {
|
|
318
|
+
type: "update",
|
|
319
|
+
status: {
|
|
320
|
+
queue: !0,
|
|
321
|
+
title: e.output.title ?? "Error",
|
|
322
|
+
message: e.output.error ?? "An error occurred",
|
|
323
|
+
visible: e.output.visible,
|
|
324
|
+
duration: e.output.duration,
|
|
325
|
+
stage: "error",
|
|
326
|
+
code: e.code,
|
|
327
|
+
success: e.success
|
|
328
|
+
}
|
|
329
|
+
} : {
|
|
330
|
+
type: "complete",
|
|
331
|
+
status: {
|
|
332
|
+
queue: !0,
|
|
333
|
+
message: e.success ? void 0 : e.output.error,
|
|
334
|
+
stage: e.success ? "complete" : "error",
|
|
335
|
+
code: e.code,
|
|
336
|
+
progress_data: e.progress_data,
|
|
337
|
+
changed_state_ids: e.success ? e.output.changed_state_ids : void 0,
|
|
338
|
+
used_cache: e.used_cache,
|
|
339
|
+
cache_duration: e.cache_duration,
|
|
340
|
+
avg_time: e.avg_time
|
|
341
|
+
},
|
|
342
|
+
data: e.success ? e.output : null
|
|
343
|
+
};
|
|
344
|
+
case "process_starts": return {
|
|
345
|
+
type: "update",
|
|
346
|
+
status: {
|
|
347
|
+
queue: !0,
|
|
348
|
+
stage: "pending",
|
|
349
|
+
code: e.code,
|
|
350
|
+
size: e.rank,
|
|
351
|
+
position: 0,
|
|
352
|
+
success: e.success,
|
|
353
|
+
eta: e.eta
|
|
354
|
+
},
|
|
355
|
+
original_msg: "process_starts"
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
return {
|
|
359
|
+
type: "none",
|
|
360
|
+
status: {
|
|
361
|
+
stage: "error",
|
|
362
|
+
queue: !0
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
var fe = (e = [], t) => {
|
|
367
|
+
let n = t ? t.parameters : [];
|
|
368
|
+
if (Array.isArray(e)) return t && n.length > 0 && e.length > n.length && console.warn("Too many arguments provided for the endpoint."), e;
|
|
369
|
+
let r = [], i = Object.keys(e);
|
|
370
|
+
return n.forEach((t, n) => {
|
|
371
|
+
if (e.hasOwnProperty(t.parameter_name)) r[n] = e[t.parameter_name];
|
|
372
|
+
else if (t.parameter_has_default) r[n] = t.parameter_default;
|
|
373
|
+
else throw Error(`No value provided for required parameter: ${t.parameter_name}`);
|
|
374
|
+
}), i.forEach((e) => {
|
|
375
|
+
if (!n.some((t) => t.parameter_name === e)) throw Error(`Parameter \`${e}\` is not a valid keyword argument. Please refer to the API for usage.`);
|
|
376
|
+
}), r.forEach((e, t) => {
|
|
377
|
+
if (e === void 0 && !n[t].parameter_has_default) throw Error(`No value provided for required parameter: ${n[t].parameter_name}`);
|
|
378
|
+
}), r;
|
|
379
|
+
};
|
|
380
|
+
//#endregion
|
|
381
|
+
//#region src/utils/view_api.ts
|
|
382
|
+
async function K() {
|
|
383
|
+
if (this.api_info) return this.api_info;
|
|
384
|
+
let { token: e } = this.options, { config: t } = this, n = {};
|
|
385
|
+
if (e && (n.Authorization = `Bearer ${e}`), t) try {
|
|
386
|
+
let e, r;
|
|
387
|
+
if (typeof window < "u" && window.gradio_api_info) r = window.gradio_api_info;
|
|
388
|
+
else {
|
|
389
|
+
let i = W(t.root, this.api_prefix, h);
|
|
390
|
+
if (e = await this.fetch(i, {
|
|
391
|
+
headers: n,
|
|
392
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
393
|
+
}), !e.ok) throw Error(b);
|
|
394
|
+
r = await e.json();
|
|
395
|
+
}
|
|
396
|
+
return "api" in r && (r = r.api), r.named_endpoints["/predict"] && !r.unnamed_endpoints[0] && (r.unnamed_endpoints[0] = r.named_endpoints["/predict"]), G(r, t, this.api_map);
|
|
397
|
+
} catch (e) {
|
|
398
|
+
throw Error("Could not get API info. " + e.message);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
//#endregion
|
|
402
|
+
//#region src/utils/upload_files.ts
|
|
403
|
+
async function q(e, t, n) {
|
|
404
|
+
let r = {};
|
|
405
|
+
this?.options?.token && (r.Authorization = `Bearer ${this.options.token}`);
|
|
406
|
+
let i = 1e3, a = [], o;
|
|
407
|
+
for (let s = 0; s < t.length; s += i) {
|
|
408
|
+
let c = t.slice(s, s + i), l = new FormData();
|
|
409
|
+
c.forEach((e) => {
|
|
410
|
+
l.append("files", e);
|
|
411
|
+
});
|
|
412
|
+
try {
|
|
413
|
+
let t = n ? `${e}${this.api_prefix}/${f}?upload_id=${n}` : `${e}${this.api_prefix}/${f}`;
|
|
414
|
+
o = await this.fetch(t, {
|
|
415
|
+
method: "POST",
|
|
416
|
+
body: l,
|
|
417
|
+
headers: r,
|
|
418
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
419
|
+
});
|
|
420
|
+
} catch (e) {
|
|
421
|
+
throw Error(b + e.message);
|
|
422
|
+
}
|
|
423
|
+
if (!o.ok) {
|
|
424
|
+
let e = await o.text();
|
|
425
|
+
return { error: `HTTP ${o.status}: ${e}` };
|
|
426
|
+
}
|
|
427
|
+
let u = await o.json();
|
|
428
|
+
u && a.push(...u);
|
|
429
|
+
}
|
|
430
|
+
return { files: a };
|
|
431
|
+
}
|
|
432
|
+
var pe = {
|
|
433
|
+
si: {
|
|
434
|
+
radix: 1e3,
|
|
435
|
+
unit: [
|
|
436
|
+
"b",
|
|
437
|
+
"kb",
|
|
438
|
+
"Mb",
|
|
439
|
+
"Gb",
|
|
440
|
+
"Tb",
|
|
441
|
+
"Pb",
|
|
442
|
+
"Eb",
|
|
443
|
+
"Zb",
|
|
444
|
+
"Yb"
|
|
445
|
+
]
|
|
446
|
+
},
|
|
447
|
+
iec: {
|
|
448
|
+
radix: 1024,
|
|
449
|
+
unit: [
|
|
450
|
+
"b",
|
|
451
|
+
"Kib",
|
|
452
|
+
"Mib",
|
|
453
|
+
"Gib",
|
|
454
|
+
"Tib",
|
|
455
|
+
"Pib",
|
|
456
|
+
"Eib",
|
|
457
|
+
"Zib",
|
|
458
|
+
"Yib"
|
|
459
|
+
]
|
|
460
|
+
},
|
|
461
|
+
jedec: {
|
|
462
|
+
radix: 1024,
|
|
463
|
+
unit: [
|
|
464
|
+
"b",
|
|
465
|
+
"Kb",
|
|
466
|
+
"Mb",
|
|
467
|
+
"Gb",
|
|
468
|
+
"Tb",
|
|
469
|
+
"Pb",
|
|
470
|
+
"Eb",
|
|
471
|
+
"Zb",
|
|
472
|
+
"Yb"
|
|
473
|
+
]
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
function me(e, t = 1, n = "jedec") {
|
|
477
|
+
e = Math.abs(e);
|
|
478
|
+
let { radix: r, unit: i } = pe[n] || pe.jedec, a = 0;
|
|
479
|
+
for (; e >= r;) e /= r, ++a;
|
|
480
|
+
return `${e.toFixed(t)} ${i[a]}`;
|
|
481
|
+
}
|
|
482
|
+
//#endregion
|
|
483
|
+
//#region src/upload.ts
|
|
484
|
+
async function he(e, t, n, r) {
|
|
485
|
+
let i = (Array.isArray(e) ? e : [e]).map((e) => e.blob), a = i.filter((e) => e.size > (r ?? Infinity));
|
|
486
|
+
if (a.length) throw Error(`File(s) exceed the maximum allowed size of ${me(r || Infinity)}: ${a.map((e) => `"${e.name}"`).join(", ")}`);
|
|
487
|
+
return await Promise.all(await this.upload_files(t, i, n).then(async (n) => {
|
|
488
|
+
if (n.error) throw Error(n.error);
|
|
489
|
+
return n.files ? n.files.map((n, r) => new J({
|
|
490
|
+
...e[r],
|
|
491
|
+
path: n,
|
|
492
|
+
url: `${t}${this.api_prefix}/file=${n}`
|
|
493
|
+
})) : [];
|
|
494
|
+
}));
|
|
495
|
+
}
|
|
496
|
+
async function ge(e, t) {
|
|
497
|
+
return e.map((e) => new J({
|
|
498
|
+
path: e.name,
|
|
499
|
+
orig_name: e.name,
|
|
500
|
+
blob: e,
|
|
501
|
+
size: e.size,
|
|
502
|
+
mime_type: e.type,
|
|
503
|
+
is_stream: t
|
|
504
|
+
}));
|
|
505
|
+
}
|
|
506
|
+
var J = class {
|
|
507
|
+
path;
|
|
508
|
+
url;
|
|
509
|
+
orig_name;
|
|
510
|
+
size;
|
|
511
|
+
blob;
|
|
512
|
+
is_stream;
|
|
513
|
+
mime_type;
|
|
514
|
+
alt_text;
|
|
515
|
+
b64;
|
|
516
|
+
meta = { _type: "gradio.FileData" };
|
|
517
|
+
constructor({ path: e, url: t, orig_name: n, size: r, blob: i, is_stream: a, mime_type: o, alt_text: s, b64: c }) {
|
|
518
|
+
this.path = e, this.url = t, this.orig_name = n, this.size = r, this.blob = t ? void 0 : i, this.is_stream = a, this.mime_type = o, this.alt_text = s, this.b64 = c;
|
|
519
|
+
}
|
|
520
|
+
}, _e = class {
|
|
521
|
+
type;
|
|
522
|
+
command;
|
|
523
|
+
meta;
|
|
524
|
+
fileData;
|
|
525
|
+
constructor(e, t) {
|
|
526
|
+
this.type = "command", this.command = e, this.meta = t;
|
|
527
|
+
}
|
|
528
|
+
}, ve = typeof process < "u" && process.versions && process.versions.node;
|
|
529
|
+
function ye(e, t, n) {
|
|
530
|
+
for (; n.length > 1;) {
|
|
531
|
+
let t = n.shift();
|
|
532
|
+
if (typeof t == "string" || typeof t == "number") e = e[t];
|
|
533
|
+
else throw Error("Invalid key type");
|
|
534
|
+
}
|
|
535
|
+
let r = n.shift();
|
|
536
|
+
if (typeof r == "string" || typeof r == "number") e[r] = t;
|
|
537
|
+
else throw Error("Invalid key type");
|
|
538
|
+
}
|
|
539
|
+
async function Y(e, t = void 0, n = [], r = !1, i = void 0) {
|
|
540
|
+
if (Array.isArray(e)) {
|
|
541
|
+
let a = [];
|
|
542
|
+
return await Promise.all(e.map(async (o, s) => {
|
|
543
|
+
let c = n.slice();
|
|
544
|
+
c.push(String(s));
|
|
545
|
+
let l = await Y(e[s], r ? i?.parameters[s]?.component || void 0 : t, c, !1, i);
|
|
546
|
+
a = a.concat(l);
|
|
547
|
+
})), a;
|
|
548
|
+
} else if (globalThis.Buffer && e instanceof globalThis.Buffer || e instanceof Blob) return [{
|
|
549
|
+
path: n,
|
|
550
|
+
blob: e instanceof Blob ? e : new Blob([e]),
|
|
551
|
+
type: t
|
|
552
|
+
}];
|
|
553
|
+
else if (typeof e == "object" && e) {
|
|
554
|
+
let t = [];
|
|
555
|
+
for (let r of Object.keys(e)) {
|
|
556
|
+
let a = [...n, r], o = e[r];
|
|
557
|
+
t = t.concat(await Y(o, void 0, a, !1, i));
|
|
558
|
+
}
|
|
559
|
+
return t;
|
|
560
|
+
}
|
|
561
|
+
return [];
|
|
562
|
+
}
|
|
563
|
+
function be(e, t) {
|
|
564
|
+
let n = t?.dependencies?.find((t) => t.id == e)?.queue;
|
|
565
|
+
return n == null ? !t.enable_queue : !n;
|
|
566
|
+
}
|
|
567
|
+
function xe(e, t) {
|
|
568
|
+
return new Promise((n, r) => {
|
|
569
|
+
let i = new MessageChannel();
|
|
570
|
+
i.port1.onmessage = (({ data: e }) => {
|
|
571
|
+
i.port1.close(), n(e);
|
|
572
|
+
}), window.parent.postMessage(e, t, [i.port2]);
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
function Se(e) {
|
|
576
|
+
if (typeof e == "string") {
|
|
577
|
+
if (e.startsWith("http://") || e.startsWith("https://")) return {
|
|
578
|
+
path: e,
|
|
579
|
+
url: e,
|
|
580
|
+
orig_name: e.split("/").pop() ?? "unknown",
|
|
581
|
+
meta: { _type: "gradio.FileData" }
|
|
582
|
+
};
|
|
583
|
+
if (ve) return new _e("upload_file", {
|
|
584
|
+
path: e,
|
|
585
|
+
name: e,
|
|
586
|
+
orig_path: e
|
|
587
|
+
});
|
|
588
|
+
} else if (typeof File < "u" && e instanceof File) return e;
|
|
589
|
+
else if (globalThis.Buffer && e instanceof globalThis.Buffer) return new Blob([e]);
|
|
590
|
+
else if (e instanceof Blob) return e;
|
|
591
|
+
throw Error("Invalid input: must be a URL, File, Blob, or Buffer object.");
|
|
592
|
+
}
|
|
593
|
+
function X(e, t, n, r, i = !1) {
|
|
594
|
+
if (r === "input" && !i) throw Error("Invalid code path. Cannot skip state inputs for input.");
|
|
595
|
+
if (r === "output" && i) return e;
|
|
596
|
+
let a = [], o = 0, s = r === "input" ? t.inputs : t.outputs;
|
|
597
|
+
for (let t = 0; t < s.length; t++) {
|
|
598
|
+
let r = s[t];
|
|
599
|
+
if (n.find((e) => e.id === r)?.type === "state") {
|
|
600
|
+
if (i) if (e.length === s.length) {
|
|
601
|
+
let t = e[o];
|
|
602
|
+
a.push(t), o++;
|
|
603
|
+
} else a.push(null);
|
|
604
|
+
else {
|
|
605
|
+
o++;
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
continue;
|
|
609
|
+
} else {
|
|
610
|
+
let t = e[o];
|
|
611
|
+
a.push(t), o++;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return a;
|
|
615
|
+
}
|
|
616
|
+
//#endregion
|
|
617
|
+
//#region __vite-browser-external
|
|
618
|
+
var Ce = /* @__PURE__ */ o(((e, t) => {
|
|
619
|
+
t.exports = {};
|
|
620
|
+
}));
|
|
621
|
+
//#endregion
|
|
622
|
+
//#region src/utils/handle_blob.ts
|
|
623
|
+
async function we(e, t, n) {
|
|
624
|
+
let r = this;
|
|
625
|
+
await Te(r, t);
|
|
626
|
+
let i = await Y(t, void 0, [], !0, n);
|
|
627
|
+
return (await Promise.all(i.map(async ({ path: t, blob: n, type: i }) => {
|
|
628
|
+
if (!n) return {
|
|
629
|
+
path: t,
|
|
630
|
+
type: i
|
|
631
|
+
};
|
|
632
|
+
let a = await r.upload_files(e, [n]);
|
|
633
|
+
return {
|
|
634
|
+
path: t,
|
|
635
|
+
file_url: a.files && a.files[0],
|
|
636
|
+
type: i,
|
|
637
|
+
name: typeof File < "u" && n instanceof File ? n?.name : void 0
|
|
638
|
+
};
|
|
639
|
+
}))).forEach(({ path: e, file_url: n, type: r, name: i }) => {
|
|
640
|
+
r === "Gallery" ? ye(t, n, e) : n && ye(t, new J({
|
|
641
|
+
path: n,
|
|
642
|
+
orig_name: i
|
|
643
|
+
}), e);
|
|
644
|
+
}), t;
|
|
645
|
+
}
|
|
646
|
+
async function Te(e, t) {
|
|
647
|
+
if (!(e.config?.root || e.config?.root_url)) throw Error(j);
|
|
648
|
+
await Ee(e, t);
|
|
649
|
+
}
|
|
650
|
+
async function Ee(e, t, n = []) {
|
|
651
|
+
for (let r in t) t[r] instanceof _e ? await De(e, t, r) : typeof t[r] == "object" && t[r] !== null && await Ee(e, t[r], [...n, r]);
|
|
652
|
+
}
|
|
653
|
+
async function De(e, t, n) {
|
|
654
|
+
let r = t[n], i = e.config?.root || e.config?.root_url;
|
|
655
|
+
if (!i) throw Error(j);
|
|
656
|
+
try {
|
|
657
|
+
let a, o;
|
|
658
|
+
if (typeof process < "u" && process.versions && process.versions.node) {
|
|
659
|
+
let e = await Promise.resolve().then(() => /* @__PURE__ */ c(Ce(), 1));
|
|
660
|
+
o = (await Promise.resolve().then(() => /* @__PURE__ */ c(Ce(), 1))).resolve(process.cwd(), r.meta.path), a = await e.readFile(o);
|
|
661
|
+
} else throw Error(A);
|
|
662
|
+
let s = new Blob([a], { type: "application/octet-stream" }), l = await e.upload_files(i, [s]), u = l.files && l.files[0];
|
|
663
|
+
u && (t[n] = new J({
|
|
664
|
+
path: u,
|
|
665
|
+
orig_name: r.meta.name || ""
|
|
666
|
+
}));
|
|
667
|
+
} catch (e) {
|
|
668
|
+
console.error(M, e);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
//#endregion
|
|
672
|
+
//#region src/utils/post_data.ts
|
|
673
|
+
async function Oe(e, t, n) {
|
|
674
|
+
let r = { "Content-Type": "application/json" };
|
|
675
|
+
this.options.token && (r.Authorization = `Bearer ${this.options.token}`);
|
|
676
|
+
try {
|
|
677
|
+
var i = await this.fetch(e, {
|
|
678
|
+
method: "POST",
|
|
679
|
+
body: JSON.stringify(t),
|
|
680
|
+
headers: {
|
|
681
|
+
...r,
|
|
682
|
+
...n
|
|
683
|
+
},
|
|
684
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
685
|
+
});
|
|
686
|
+
} catch {
|
|
687
|
+
return [{ error: b }, 500];
|
|
688
|
+
}
|
|
689
|
+
let a, o;
|
|
690
|
+
try {
|
|
691
|
+
a = await i.json(), o = i.status;
|
|
692
|
+
} catch (e) {
|
|
693
|
+
a = { error: `Could not parse server response: ${e}` }, o = 500;
|
|
694
|
+
}
|
|
695
|
+
return [a, o];
|
|
696
|
+
}
|
|
697
|
+
//#endregion
|
|
698
|
+
//#region src/utils/predict.ts
|
|
699
|
+
async function ke(e, t = {}) {
|
|
700
|
+
let n = !1, r = !1;
|
|
701
|
+
if (!this.config) throw Error("Could not resolve app config");
|
|
702
|
+
if (typeof e == "number") this.config.dependencies.find((t) => t.id == e);
|
|
703
|
+
else {
|
|
704
|
+
let t = e.replace(/^\//, "");
|
|
705
|
+
this.config.dependencies.find((e) => e.id == this.api_map[t]);
|
|
706
|
+
}
|
|
707
|
+
let i = this.submit(e, t, null, null, !0), a;
|
|
708
|
+
for await (let e of i) {
|
|
709
|
+
if (e.type === "data" && (n = !0, a = e, r)) return a;
|
|
710
|
+
if (e.type === "status") {
|
|
711
|
+
if (e.stage === "error") {
|
|
712
|
+
let { message: t, ...n } = e, r = Error((typeof t == "string" ? t : t && JSON.stringify(t)) || "An unknown error occurred while making a prediction.");
|
|
713
|
+
throw Object.assign(r, n), r;
|
|
714
|
+
}
|
|
715
|
+
if (e.stage === "complete" && (r = !0, n)) return a;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
return a;
|
|
719
|
+
}
|
|
720
|
+
//#endregion
|
|
721
|
+
//#region src/helpers/spaces.ts
|
|
722
|
+
async function Z(e, t, n) {
|
|
723
|
+
let r = t === "subdomain" ? `https://huggingface.co/api/spaces/by-subdomain/${e}` : `https://huggingface.co/api/spaces/${e}`, i, a;
|
|
724
|
+
try {
|
|
725
|
+
if (i = await fetch(r), a = i.status, a !== 200) throw Error();
|
|
726
|
+
i = await i.json();
|
|
727
|
+
} catch {
|
|
728
|
+
n({
|
|
729
|
+
status: "error",
|
|
730
|
+
load_status: "error",
|
|
731
|
+
message: S,
|
|
732
|
+
detail: "NOT_FOUND"
|
|
733
|
+
});
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
if (!i || a !== 200) return;
|
|
737
|
+
let { runtime: { stage: o }, id: s } = i;
|
|
738
|
+
switch (o) {
|
|
739
|
+
case "STOPPED":
|
|
740
|
+
case "SLEEPING":
|
|
741
|
+
n({
|
|
742
|
+
status: "sleeping",
|
|
743
|
+
load_status: "pending",
|
|
744
|
+
message: "Space is asleep. Waking it up...",
|
|
745
|
+
detail: o
|
|
746
|
+
}), setTimeout(() => {
|
|
747
|
+
Z(e, t, n);
|
|
748
|
+
}, 1e3);
|
|
749
|
+
break;
|
|
750
|
+
case "PAUSED":
|
|
751
|
+
n({
|
|
752
|
+
status: "paused",
|
|
753
|
+
load_status: "error",
|
|
754
|
+
message: "This space has been paused by the author. If you would like to try this demo, consider duplicating the space.",
|
|
755
|
+
detail: o,
|
|
756
|
+
discussions_enabled: await Me(s)
|
|
757
|
+
});
|
|
758
|
+
break;
|
|
759
|
+
case "RUNNING":
|
|
760
|
+
case "RUNNING_BUILDING":
|
|
761
|
+
n({
|
|
762
|
+
status: "running",
|
|
763
|
+
load_status: "complete",
|
|
764
|
+
message: "Space is running.",
|
|
765
|
+
detail: o
|
|
766
|
+
});
|
|
767
|
+
break;
|
|
768
|
+
case "BUILDING":
|
|
769
|
+
n({
|
|
770
|
+
status: "building",
|
|
771
|
+
load_status: "pending",
|
|
772
|
+
message: "Space is building...",
|
|
773
|
+
detail: o
|
|
774
|
+
}), setTimeout(() => {
|
|
775
|
+
Z(e, t, n);
|
|
776
|
+
}, 1e3);
|
|
777
|
+
break;
|
|
778
|
+
case "APP_STARTING":
|
|
779
|
+
n({
|
|
780
|
+
status: "starting",
|
|
781
|
+
load_status: "pending",
|
|
782
|
+
message: "Space is starting...",
|
|
783
|
+
detail: o
|
|
784
|
+
}), setTimeout(() => {
|
|
785
|
+
Z(e, t, n);
|
|
786
|
+
}, 1e3);
|
|
787
|
+
break;
|
|
788
|
+
default:
|
|
789
|
+
n({
|
|
790
|
+
status: "space_error",
|
|
791
|
+
load_status: "error",
|
|
792
|
+
message: "This space is experiencing an issue.",
|
|
793
|
+
detail: o,
|
|
794
|
+
discussions_enabled: await Me(s)
|
|
795
|
+
});
|
|
796
|
+
break;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
var Ae = async (e, t) => {
|
|
800
|
+
let n = 0;
|
|
801
|
+
return new Promise((r) => {
|
|
802
|
+
Z(e, V.test(e) ? "space_name" : "subdomain", (i) => {
|
|
803
|
+
t(i), i.status === "running" || i.status === "error" || i.status === "paused" || i.status === "space_error" ? r() : (i.status === "sleeping" || i.status === "building") && (n < 12 ? (n++, setTimeout(() => {
|
|
804
|
+
Ae(e, t).then(r);
|
|
805
|
+
}, 5e3)) : r());
|
|
806
|
+
});
|
|
807
|
+
});
|
|
808
|
+
}, je = /^(?=[^]*\b[dD]iscussions{0,1}\b)(?=[^]*\b[dD]isabled\b)[^]*$/;
|
|
809
|
+
async function Me(e) {
|
|
810
|
+
try {
|
|
811
|
+
let t = await fetch(`https://huggingface.co/api/spaces/${e}/discussions`, { method: "HEAD" }), n = t.headers.get("x-error-message");
|
|
812
|
+
return !(!t.ok || n && je.test(n));
|
|
813
|
+
} catch {
|
|
814
|
+
return !1;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
async function Ne(e, t) {
|
|
818
|
+
let n = {};
|
|
819
|
+
t && (n.Authorization = `Bearer ${t}`);
|
|
820
|
+
try {
|
|
821
|
+
let t = await fetch(`https://huggingface.co/api/spaces/${e}/${g}`, { headers: n });
|
|
822
|
+
if (t.status !== 200) throw Error("Space hardware could not be obtained.");
|
|
823
|
+
let { hardware: r } = await t.json();
|
|
824
|
+
return r.current;
|
|
825
|
+
} catch (e) {
|
|
826
|
+
throw Error(e.message);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
async function Pe(e, t, n) {
|
|
830
|
+
let r = {};
|
|
831
|
+
n && (r.Authorization = `Bearer ${n}`);
|
|
832
|
+
let i = { seconds: t };
|
|
833
|
+
try {
|
|
834
|
+
let t = await fetch(`https://huggingface.co/api/spaces/${e}/${_}`, {
|
|
835
|
+
method: "POST",
|
|
836
|
+
headers: {
|
|
837
|
+
"Content-Type": "application/json",
|
|
838
|
+
...r
|
|
839
|
+
},
|
|
840
|
+
body: JSON.stringify(i)
|
|
841
|
+
});
|
|
842
|
+
if (t.status !== 200) throw Error("Could not set sleep timeout on duplicated Space. Please visit *ADD HF LINK TO SETTINGS* to set a timeout manually to reduce billing charges.");
|
|
843
|
+
return await t.json();
|
|
844
|
+
} catch (e) {
|
|
845
|
+
throw Error(e.message);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
var Fe = [
|
|
849
|
+
"cpu-basic",
|
|
850
|
+
"cpu-upgrade",
|
|
851
|
+
"cpu-xl",
|
|
852
|
+
"t4-small",
|
|
853
|
+
"t4-medium",
|
|
854
|
+
"a10g-small",
|
|
855
|
+
"a10g-large",
|
|
856
|
+
"a10g-largex2",
|
|
857
|
+
"a10g-largex4",
|
|
858
|
+
"a100-large",
|
|
859
|
+
"zero-a10g",
|
|
860
|
+
"h100",
|
|
861
|
+
"h100x8"
|
|
862
|
+
];
|
|
863
|
+
//#endregion
|
|
864
|
+
//#region src/utils/duplicate.ts
|
|
865
|
+
async function Ie(e, t) {
|
|
866
|
+
P(t);
|
|
867
|
+
let { token: n, private: r, hardware: i, timeout: a, auth: o } = t;
|
|
868
|
+
if (i && !Fe.includes(i)) throw Error(`Invalid hardware type provided. Valid types are: ${Fe.map((e) => `"${e}"`).join(",")}.`);
|
|
869
|
+
let { http_protocol: s, host: c } = await U(e, n), l = null;
|
|
870
|
+
if (o) {
|
|
871
|
+
let e = await R(s, c, o, fetch, void 0, t.credentials);
|
|
872
|
+
e && (l = B(e));
|
|
873
|
+
}
|
|
874
|
+
let u = {
|
|
875
|
+
Authorization: `Bearer ${n}`,
|
|
876
|
+
"Content-Type": "application/json",
|
|
877
|
+
...l ? { Cookie: l.join("; ") } : {}
|
|
878
|
+
}, d = (await (await fetch("https://huggingface.co/api/whoami-v2", { headers: u })).json()).name, f = e.split("/")[1], p = { repository: `${d}/${f}` };
|
|
879
|
+
r && (p.private = !0);
|
|
880
|
+
let m;
|
|
881
|
+
try {
|
|
882
|
+
i || (m = await Ne(e, n));
|
|
883
|
+
} catch (e) {
|
|
884
|
+
throw Error(w + e.message);
|
|
885
|
+
}
|
|
886
|
+
p.hardware = i || m || "cpu-basic";
|
|
887
|
+
try {
|
|
888
|
+
let r = await fetch(`https://huggingface.co/api/spaces/${e}/duplicate`, {
|
|
889
|
+
method: "POST",
|
|
890
|
+
headers: u,
|
|
891
|
+
body: JSON.stringify(p)
|
|
892
|
+
});
|
|
893
|
+
if (r.status === 409) try {
|
|
894
|
+
return await $.connect(`${d}/${f}`, t);
|
|
895
|
+
} catch (e) {
|
|
896
|
+
throw console.error("Failed to connect Client instance:", e), e;
|
|
897
|
+
}
|
|
898
|
+
else if (r.status !== 200) throw Error(r.statusText);
|
|
899
|
+
let i = await r.json();
|
|
900
|
+
return await Pe(`${d}/${f}`, a || 300, n), await $.connect(Le(i.url), t);
|
|
901
|
+
} catch (e) {
|
|
902
|
+
throw Error(e);
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
function Le(e) {
|
|
906
|
+
let t = e.match(/https:\/\/huggingface.co\/spaces\/([^/]+\/[^/]+)/);
|
|
907
|
+
if (t) return t[1];
|
|
908
|
+
}
|
|
909
|
+
//#endregion
|
|
910
|
+
//#region src/helpers/zerogpu.ts
|
|
911
|
+
var Re = "supports-zerogpu-headers", ze = !1;
|
|
912
|
+
function Be() {
|
|
913
|
+
return typeof window < "u" && typeof document < "u" && typeof window.addEventListener == "function";
|
|
914
|
+
}
|
|
915
|
+
function Ve(e) {
|
|
916
|
+
return e.includes(".dev.") ? `https://moon-${e.split(".")[1]}.dev.spaces.huggingface.tech` : e.endsWith(".hf.space") ? "https://huggingface.co" : null;
|
|
917
|
+
}
|
|
918
|
+
function He() {
|
|
919
|
+
if (!Be() || ze) return;
|
|
920
|
+
window.addEventListener("message", (e) => {
|
|
921
|
+
e.data === Re && (window.supports_zerogpu_headers = !0);
|
|
922
|
+
}), ze = !0;
|
|
923
|
+
let e = Ve(window.location.hostname);
|
|
924
|
+
e && window.parent !== window && window.parent.postMessage(Re, e);
|
|
925
|
+
}
|
|
926
|
+
//#endregion
|
|
927
|
+
//#region ../../node_modules/.pnpm/fetch-event-stream@0.1.6/node_modules/fetch-event-stream/esm/deps/jsr.io/@std/streams/0.221.0/text_line_stream.js
|
|
928
|
+
var Ue = class extends TransformStream {
|
|
929
|
+
#e = "";
|
|
930
|
+
constructor(e = { allowCR: !1 }) {
|
|
931
|
+
super({
|
|
932
|
+
transform: (t, n) => {
|
|
933
|
+
for (t = this.#e + t;;) {
|
|
934
|
+
let r = t.indexOf("\n"), i = e.allowCR ? t.indexOf("\r") : -1;
|
|
935
|
+
if (i !== -1 && i !== t.length - 1 && (r === -1 || r - 1 > i)) {
|
|
936
|
+
n.enqueue(t.slice(0, i)), t = t.slice(i + 1);
|
|
937
|
+
continue;
|
|
938
|
+
}
|
|
939
|
+
if (r === -1) break;
|
|
940
|
+
let a = t[r - 1] === "\r" ? r - 1 : r;
|
|
941
|
+
n.enqueue(t.slice(0, a)), t = t.slice(r + 1);
|
|
942
|
+
}
|
|
943
|
+
this.#e = t;
|
|
944
|
+
},
|
|
945
|
+
flush: (t) => {
|
|
946
|
+
if (this.#e === "") return;
|
|
947
|
+
let n = e.allowCR && this.#e.endsWith("\r") ? this.#e.slice(0, -1) : this.#e;
|
|
948
|
+
t.enqueue(n);
|
|
949
|
+
}
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
};
|
|
953
|
+
//#endregion
|
|
954
|
+
//#region ../../node_modules/.pnpm/fetch-event-stream@0.1.6/node_modules/fetch-event-stream/esm/utils.js
|
|
955
|
+
function We(e) {
|
|
956
|
+
let t = new TextDecoderStream(), n = new Ue({ allowCR: !0 });
|
|
957
|
+
return e.pipeThrough(t).pipeThrough(n);
|
|
958
|
+
}
|
|
959
|
+
function Ge(e) {
|
|
960
|
+
let t = /[:]\s*/.exec(e), n = t && t.index;
|
|
961
|
+
if (n) return [e.substring(0, n), e.substring(n + t[0].length)];
|
|
962
|
+
}
|
|
963
|
+
function Ke(e, t, n) {
|
|
964
|
+
e.get(t) || e.set(t, n);
|
|
965
|
+
}
|
|
966
|
+
//#endregion
|
|
967
|
+
//#region ../../node_modules/.pnpm/fetch-event-stream@0.1.6/node_modules/fetch-event-stream/esm/mod.js
|
|
968
|
+
async function* qe(e, t) {
|
|
969
|
+
if (!e.body) return;
|
|
970
|
+
let n = We(e.body), r, i = n.getReader(), a;
|
|
971
|
+
for (;;) {
|
|
972
|
+
if (t && t.aborted) return i.cancel();
|
|
973
|
+
if (r = await i.read(), r.done) return;
|
|
974
|
+
if (!r.value) {
|
|
975
|
+
a && (yield a), a = void 0;
|
|
976
|
+
continue;
|
|
977
|
+
}
|
|
978
|
+
let [e, n] = Ge(r.value) || [];
|
|
979
|
+
e === "data" ? (a ||= {}, a[e] = a[e] ? a[e] + "\n" + n : n) : e === "event" ? (a ||= {}, a[e] = n) : e === "id" ? (a ||= {}, a[e] = String(+n) === n ? +n : n) : e === "retry" && (a ||= {}, a[e] = +n || void 0);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
async function Je(e, t) {
|
|
983
|
+
let n = new Request(e, t);
|
|
984
|
+
Ke(n.headers, "Accept", "text/event-stream"), Ke(n.headers, "Content-Type", "application/json");
|
|
985
|
+
let r = await fetch(n);
|
|
986
|
+
if (!r.ok) throw r;
|
|
987
|
+
return qe(r, n.signal);
|
|
988
|
+
}
|
|
989
|
+
//#endregion
|
|
990
|
+
//#region src/utils/stream.ts
|
|
991
|
+
async function Ye() {
|
|
992
|
+
let { event_callbacks: e, unclosed_events: t, pending_stream_messages: n, stream_status: r, config: i, jwt: a } = this, o = this;
|
|
993
|
+
if (!i) throw Error("Could not resolve app config");
|
|
994
|
+
r.open = !0;
|
|
995
|
+
let s = null, c = new URLSearchParams({ session_hash: this.session_hash }).toString(), l = new URL(`${i.root}${this.api_prefix}/${u}?${c}`);
|
|
996
|
+
if (a && l.searchParams.set("__sign", a), s = this.stream(l), !s) {
|
|
997
|
+
console.warn("Cannot connect to SSE endpoint: " + l.toString());
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
s.onmessage = async function(a) {
|
|
1001
|
+
let s = JSON.parse(a.data);
|
|
1002
|
+
if (s.msg === "close_stream") {
|
|
1003
|
+
Q(r, o.abort_controller);
|
|
1004
|
+
return;
|
|
1005
|
+
}
|
|
1006
|
+
let c = s.event_id;
|
|
1007
|
+
if (!c) await Promise.all(Object.keys(e).map((t) => e[t](s)));
|
|
1008
|
+
else if (e[c] && i) {
|
|
1009
|
+
s.msg === "process_completed" && [
|
|
1010
|
+
"sse",
|
|
1011
|
+
"sse_v1",
|
|
1012
|
+
"sse_v2",
|
|
1013
|
+
"sse_v2.1",
|
|
1014
|
+
"sse_v3"
|
|
1015
|
+
].includes(i.protocol) && t.delete(c);
|
|
1016
|
+
let n = e[c];
|
|
1017
|
+
typeof window < "u" && typeof document < "u" && document.visibilityState !== "hidden" ? setTimeout(n, 0, s) : n(s);
|
|
1018
|
+
} else n[c] || (n[c] = []), n[c].push(s);
|
|
1019
|
+
}, s.onerror = async function(t) {
|
|
1020
|
+
console.error(t), await Promise.all(Object.keys(e).map((t) => e[t]({
|
|
1021
|
+
msg: "broken_connection",
|
|
1022
|
+
message: b
|
|
1023
|
+
})));
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
1026
|
+
function Q(e, t) {
|
|
1027
|
+
e && (e.open = !1, t?.abort());
|
|
1028
|
+
}
|
|
1029
|
+
function Xe(e, t, n) {
|
|
1030
|
+
e[t] ? n.data.forEach((r, i) => {
|
|
1031
|
+
let a = Ze(i < e[t].length ? e[t][i] : null, r);
|
|
1032
|
+
e[t][i] = a, n.data[i] = a;
|
|
1033
|
+
}) : (e[t] = [], n.data.forEach((n, r) => {
|
|
1034
|
+
e[t][r] = n;
|
|
1035
|
+
}));
|
|
1036
|
+
}
|
|
1037
|
+
function Ze(e, t) {
|
|
1038
|
+
return t.forEach(([t, n, r]) => {
|
|
1039
|
+
e = Qe(e, n, t, r);
|
|
1040
|
+
}), e;
|
|
1041
|
+
}
|
|
1042
|
+
function Qe(e, t, n, r) {
|
|
1043
|
+
if (t.length === 0) {
|
|
1044
|
+
if (n === "replace") return r;
|
|
1045
|
+
if (n === "append") return e + r;
|
|
1046
|
+
throw Error(`Unsupported action: ${n}`);
|
|
1047
|
+
}
|
|
1048
|
+
let i = e;
|
|
1049
|
+
for (let e = 0; e < t.length - 1; e++) i = i[t[e]];
|
|
1050
|
+
let a = t[t.length - 1];
|
|
1051
|
+
switch (n) {
|
|
1052
|
+
case "replace":
|
|
1053
|
+
i[a] = r;
|
|
1054
|
+
break;
|
|
1055
|
+
case "append":
|
|
1056
|
+
i[a] += r;
|
|
1057
|
+
break;
|
|
1058
|
+
case "add":
|
|
1059
|
+
Array.isArray(i) ? i.splice(Number(a), 0, r) : i[a] = r;
|
|
1060
|
+
break;
|
|
1061
|
+
case "delete":
|
|
1062
|
+
Array.isArray(i) ? i.splice(Number(a), 1) : delete i[a];
|
|
1063
|
+
break;
|
|
1064
|
+
default: throw Error(`Unknown action: ${n}`);
|
|
1065
|
+
}
|
|
1066
|
+
return e;
|
|
1067
|
+
}
|
|
1068
|
+
function $e(e, t = {}) {
|
|
1069
|
+
let n = {
|
|
1070
|
+
close: () => {
|
|
1071
|
+
console.warn("Method not implemented.");
|
|
1072
|
+
},
|
|
1073
|
+
onerror: null,
|
|
1074
|
+
onmessage: null,
|
|
1075
|
+
onopen: null,
|
|
1076
|
+
readyState: 0,
|
|
1077
|
+
url: e.toString(),
|
|
1078
|
+
withCredentials: !1,
|
|
1079
|
+
CONNECTING: 0,
|
|
1080
|
+
OPEN: 1,
|
|
1081
|
+
CLOSED: 2,
|
|
1082
|
+
addEventListener: () => {
|
|
1083
|
+
throw Error("Method not implemented.");
|
|
1084
|
+
},
|
|
1085
|
+
dispatchEvent: () => {
|
|
1086
|
+
throw Error("Method not implemented.");
|
|
1087
|
+
},
|
|
1088
|
+
removeEventListener: () => {
|
|
1089
|
+
throw Error("Method not implemented.");
|
|
1090
|
+
}
|
|
1091
|
+
};
|
|
1092
|
+
return Je(e, t).then(async (e) => {
|
|
1093
|
+
n.readyState = n.OPEN;
|
|
1094
|
+
try {
|
|
1095
|
+
for await (let t of e) n.onmessage && n.onmessage(t);
|
|
1096
|
+
n.readyState = n.CLOSED;
|
|
1097
|
+
} catch (e) {
|
|
1098
|
+
n.onerror && n.onerror(e), n.readyState = n.CLOSED;
|
|
1099
|
+
}
|
|
1100
|
+
}).catch((e) => {
|
|
1101
|
+
console.error(e), n.onerror && n.onerror(e), n.readyState = n.CLOSED;
|
|
1102
|
+
}), n;
|
|
1103
|
+
}
|
|
1104
|
+
//#endregion
|
|
1105
|
+
//#region src/utils/submit.ts
|
|
1106
|
+
function et(e, t = {}, n, r, i, a) {
|
|
1107
|
+
try {
|
|
1108
|
+
let { token: o } = this.options, { fetch: s, app_reference: c, config: l, session_hash: f, api_info: p, api_map: m, stream_status: h, pending_stream_messages: g, pending_diff_streams: _, event_callbacks: v, unclosed_events: ee, post_data: y, options: x, api_prefix: S } = this, C = a || { "x-gradio-user": "api" }, w = this;
|
|
1109
|
+
if (!p) throw Error(ae);
|
|
1110
|
+
if (!l) throw Error("Could not resolve app config");
|
|
1111
|
+
let { fn_index: T, endpoint_info: E, dependency: D } = nt(p, e, m, l), ie = fe(t, E), O, k = l.protocol ?? "ws";
|
|
1112
|
+
if (k === "ws") throw Error(oe);
|
|
1113
|
+
let A = "", j = typeof e == "number" ? "/predict" : e, M, N = null, P = !1, F = {}, I = typeof window < "u" && typeof document < "u" ? new URLSearchParams(window.location.search).toString() : "", se = x?.events?.reduce((e, t) => (e[t] = !0, e), {}) || {};
|
|
1114
|
+
function L(e) {
|
|
1115
|
+
(i || se[e.type]) && ue(e);
|
|
1116
|
+
}
|
|
1117
|
+
async function ce() {
|
|
1118
|
+
let e = {}, t = {};
|
|
1119
|
+
e = { event_id: N }, t = {
|
|
1120
|
+
event_id: N,
|
|
1121
|
+
session_hash: f,
|
|
1122
|
+
fn_index: T
|
|
1123
|
+
};
|
|
1124
|
+
try {
|
|
1125
|
+
if (!l) throw Error("Could not resolve app config");
|
|
1126
|
+
"event_id" in t && await s(`${l.root}${S}/${ne}`, {
|
|
1127
|
+
headers: { "Content-Type": "application/json" },
|
|
1128
|
+
method: "POST",
|
|
1129
|
+
body: JSON.stringify(t)
|
|
1130
|
+
}), await s(`${l.root}${S}/${te}`, {
|
|
1131
|
+
headers: { "Content-Type": "application/json" },
|
|
1132
|
+
method: "POST",
|
|
1133
|
+
body: JSON.stringify(e)
|
|
1134
|
+
});
|
|
1135
|
+
} catch {
|
|
1136
|
+
console.warn("The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable.");
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
let R = async (e) => {
|
|
1140
|
+
await this._resolve_heartbeat(e);
|
|
1141
|
+
};
|
|
1142
|
+
async function z(e) {
|
|
1143
|
+
if (!l) return;
|
|
1144
|
+
let t = e.render_id;
|
|
1145
|
+
l.components = [...l.components.filter((e) => e.props.rendered_in !== t), ...e.components], l.dependencies = [...l.dependencies.filter((e) => e.rendered_in !== t), ...e.dependencies];
|
|
1146
|
+
let n = l.components.some((e) => e.type === "state"), r = l.dependencies.some((e) => e.targets.some((e) => e[1] === "unload"));
|
|
1147
|
+
l.connect_heartbeat = n || r, await R(l), L({
|
|
1148
|
+
type: "render",
|
|
1149
|
+
data: e,
|
|
1150
|
+
endpoint: j,
|
|
1151
|
+
fn_index: T
|
|
1152
|
+
});
|
|
1153
|
+
}
|
|
1154
|
+
let B = this.handle_blob(l.root, ie, E).then(async (e) => {
|
|
1155
|
+
if (M = {
|
|
1156
|
+
data: X(e, D, l.components, "input", !0) || [],
|
|
1157
|
+
event_data: n,
|
|
1158
|
+
fn_index: T,
|
|
1159
|
+
trigger_id: r,
|
|
1160
|
+
...x.oauth_token && E?.oauth_token ? { oauth_token: x.oauth_token } : {}
|
|
1161
|
+
}, be(T, l)) L({
|
|
1162
|
+
type: "status",
|
|
1163
|
+
endpoint: j,
|
|
1164
|
+
stage: "pending",
|
|
1165
|
+
queue: !1,
|
|
1166
|
+
fn_index: T,
|
|
1167
|
+
time: /* @__PURE__ */ new Date()
|
|
1168
|
+
}), y(`${l.root}${S}/run${j.startsWith("/") ? j : `/${j}`}${I ? "?" + I : ""}`, {
|
|
1169
|
+
...M,
|
|
1170
|
+
session_hash: f
|
|
1171
|
+
}, C).then(async ([e, t]) => {
|
|
1172
|
+
let i = e.data;
|
|
1173
|
+
if (t == 200) L({
|
|
1174
|
+
type: "data",
|
|
1175
|
+
endpoint: j,
|
|
1176
|
+
fn_index: T,
|
|
1177
|
+
data: X(i, D, l.components, "output", x.with_null_state),
|
|
1178
|
+
time: /* @__PURE__ */ new Date(),
|
|
1179
|
+
event_data: n,
|
|
1180
|
+
trigger_id: r
|
|
1181
|
+
}), e.render_config && await z(e.render_config), L({
|
|
1182
|
+
type: "status",
|
|
1183
|
+
endpoint: j,
|
|
1184
|
+
fn_index: T,
|
|
1185
|
+
stage: "complete",
|
|
1186
|
+
eta: e.average_duration,
|
|
1187
|
+
queue: !1,
|
|
1188
|
+
time: /* @__PURE__ */ new Date()
|
|
1189
|
+
});
|
|
1190
|
+
else {
|
|
1191
|
+
let t = e?.error === b;
|
|
1192
|
+
L({
|
|
1193
|
+
type: "status",
|
|
1194
|
+
stage: "error",
|
|
1195
|
+
endpoint: j,
|
|
1196
|
+
fn_index: T,
|
|
1197
|
+
message: e.error,
|
|
1198
|
+
broken: t,
|
|
1199
|
+
queue: !1,
|
|
1200
|
+
time: /* @__PURE__ */ new Date()
|
|
1201
|
+
});
|
|
1202
|
+
}
|
|
1203
|
+
}).catch((e) => {
|
|
1204
|
+
L({
|
|
1205
|
+
type: "status",
|
|
1206
|
+
stage: "error",
|
|
1207
|
+
message: e.message,
|
|
1208
|
+
endpoint: j,
|
|
1209
|
+
fn_index: T,
|
|
1210
|
+
queue: !1,
|
|
1211
|
+
time: /* @__PURE__ */ new Date()
|
|
1212
|
+
});
|
|
1213
|
+
});
|
|
1214
|
+
else if (k == "sse") {
|
|
1215
|
+
L({
|
|
1216
|
+
type: "status",
|
|
1217
|
+
stage: "pending",
|
|
1218
|
+
queue: !0,
|
|
1219
|
+
endpoint: j,
|
|
1220
|
+
fn_index: T,
|
|
1221
|
+
time: /* @__PURE__ */ new Date()
|
|
1222
|
+
});
|
|
1223
|
+
var t = new URLSearchParams({
|
|
1224
|
+
fn_index: T.toString(),
|
|
1225
|
+
session_hash: f
|
|
1226
|
+
}).toString();
|
|
1227
|
+
let e = new URL(`${l.root}${S}/${u}?${I ? I + "&" : ""}${t}`);
|
|
1228
|
+
if (this.jwt && e.searchParams.set("__sign", this.jwt), O = this.stream(e), !O) return Promise.reject(/* @__PURE__ */ Error("Cannot connect to SSE endpoint: " + e.toString()));
|
|
1229
|
+
O.onmessage = async function(e) {
|
|
1230
|
+
let { type: t, status: i, data: a } = de(JSON.parse(e.data), F[T]);
|
|
1231
|
+
if (t === "update" && i && !P) L({
|
|
1232
|
+
type: "status",
|
|
1233
|
+
endpoint: j,
|
|
1234
|
+
fn_index: T,
|
|
1235
|
+
time: /* @__PURE__ */ new Date(),
|
|
1236
|
+
...i
|
|
1237
|
+
}), i.stage === "error" && (O?.close(), W());
|
|
1238
|
+
else if (t === "data") {
|
|
1239
|
+
let [e, t] = await y(`${l.root}${S}/queue/data`, {
|
|
1240
|
+
...M,
|
|
1241
|
+
session_hash: f,
|
|
1242
|
+
event_id: N
|
|
1243
|
+
});
|
|
1244
|
+
t !== 200 && (L({
|
|
1245
|
+
type: "status",
|
|
1246
|
+
stage: "error",
|
|
1247
|
+
message: b,
|
|
1248
|
+
queue: !0,
|
|
1249
|
+
endpoint: j,
|
|
1250
|
+
fn_index: T,
|
|
1251
|
+
time: /* @__PURE__ */ new Date()
|
|
1252
|
+
}), O?.close(), W());
|
|
1253
|
+
} else t === "complete" ? P = i : t === "log" ? L({
|
|
1254
|
+
type: "log",
|
|
1255
|
+
title: a.title,
|
|
1256
|
+
log: a.log,
|
|
1257
|
+
level: a.level,
|
|
1258
|
+
endpoint: j,
|
|
1259
|
+
duration: a.duration,
|
|
1260
|
+
visible: a.visible,
|
|
1261
|
+
fn_index: T
|
|
1262
|
+
}) : (t === "generating" || t === "streaming") && L({
|
|
1263
|
+
type: "status",
|
|
1264
|
+
time: /* @__PURE__ */ new Date(),
|
|
1265
|
+
...i,
|
|
1266
|
+
stage: i?.stage,
|
|
1267
|
+
queue: !0,
|
|
1268
|
+
endpoint: j,
|
|
1269
|
+
fn_index: T
|
|
1270
|
+
});
|
|
1271
|
+
a && (L({
|
|
1272
|
+
type: "data",
|
|
1273
|
+
time: /* @__PURE__ */ new Date(),
|
|
1274
|
+
data: X(a.data, D, l.components, "output", x.with_null_state),
|
|
1275
|
+
endpoint: j,
|
|
1276
|
+
fn_index: T,
|
|
1277
|
+
event_data: n,
|
|
1278
|
+
trigger_id: r
|
|
1279
|
+
}), P && (L({
|
|
1280
|
+
type: "status",
|
|
1281
|
+
time: /* @__PURE__ */ new Date(),
|
|
1282
|
+
...P,
|
|
1283
|
+
stage: i?.stage,
|
|
1284
|
+
queue: !0,
|
|
1285
|
+
endpoint: j,
|
|
1286
|
+
fn_index: T
|
|
1287
|
+
}), O?.close(), W()));
|
|
1288
|
+
};
|
|
1289
|
+
} else if (k == "sse_v1" || k == "sse_v2" || k == "sse_v2.1" || k == "sse_v3") {
|
|
1290
|
+
L({
|
|
1291
|
+
type: "status",
|
|
1292
|
+
stage: "pending",
|
|
1293
|
+
queue: !0,
|
|
1294
|
+
endpoint: j,
|
|
1295
|
+
fn_index: T,
|
|
1296
|
+
time: /* @__PURE__ */ new Date()
|
|
1297
|
+
});
|
|
1298
|
+
let e = "";
|
|
1299
|
+
typeof window < "u" && typeof document < "u" && (e = window?.location?.hostname);
|
|
1300
|
+
let t = Ve(e);
|
|
1301
|
+
return (typeof window < "u" && typeof document < "u" && window.parent != window && t && window.supports_zerogpu_headers ? xe("zerogpu-headers", t) : Promise.resolve(null)).then((e) => {
|
|
1302
|
+
let t = {
|
|
1303
|
+
...C,
|
|
1304
|
+
...e || {}
|
|
1305
|
+
};
|
|
1306
|
+
return y(`${l.root}${S}/${d}?${I}`, {
|
|
1307
|
+
...M,
|
|
1308
|
+
session_hash: f
|
|
1309
|
+
}, t);
|
|
1310
|
+
}).then(async ([e, t]) => {
|
|
1311
|
+
if (e.event_id && (A = e.event_id), t === 503) L({
|
|
1312
|
+
type: "status",
|
|
1313
|
+
stage: "error",
|
|
1314
|
+
message: re,
|
|
1315
|
+
queue: !0,
|
|
1316
|
+
endpoint: j,
|
|
1317
|
+
fn_index: T,
|
|
1318
|
+
time: /* @__PURE__ */ new Date(),
|
|
1319
|
+
visible: !0
|
|
1320
|
+
}), W();
|
|
1321
|
+
else if (t === 422) L({
|
|
1322
|
+
type: "status",
|
|
1323
|
+
stage: "error",
|
|
1324
|
+
message: e.detail,
|
|
1325
|
+
queue: !0,
|
|
1326
|
+
endpoint: j,
|
|
1327
|
+
fn_index: T,
|
|
1328
|
+
code: "validation_error",
|
|
1329
|
+
time: /* @__PURE__ */ new Date(),
|
|
1330
|
+
visible: !0
|
|
1331
|
+
}), W();
|
|
1332
|
+
else if (t !== 200) {
|
|
1333
|
+
let t = e?.error === b;
|
|
1334
|
+
L({
|
|
1335
|
+
type: "status",
|
|
1336
|
+
stage: "error",
|
|
1337
|
+
broken: t,
|
|
1338
|
+
message: t ? b : e.detail || e.error,
|
|
1339
|
+
queue: !0,
|
|
1340
|
+
endpoint: j,
|
|
1341
|
+
fn_index: T,
|
|
1342
|
+
time: /* @__PURE__ */ new Date(),
|
|
1343
|
+
visible: !0
|
|
1344
|
+
}), W();
|
|
1345
|
+
} else {
|
|
1346
|
+
N = e.event_id, A = N;
|
|
1347
|
+
let t = async function(e) {
|
|
1348
|
+
try {
|
|
1349
|
+
let { type: t, status: n, data: r, original_msg: i } = de(e, F[T]);
|
|
1350
|
+
if (t == "heartbeat") return;
|
|
1351
|
+
if (t === "update" && n && !P) L({
|
|
1352
|
+
type: "status",
|
|
1353
|
+
endpoint: j,
|
|
1354
|
+
fn_index: T,
|
|
1355
|
+
time: /* @__PURE__ */ new Date(),
|
|
1356
|
+
original_msg: i,
|
|
1357
|
+
...n
|
|
1358
|
+
});
|
|
1359
|
+
else if (t === "complete") P = n;
|
|
1360
|
+
else if (t == "unexpected_error" || t == "broken_connection") {
|
|
1361
|
+
console.error("Unexpected error", n?.message);
|
|
1362
|
+
let e = t === "broken_connection";
|
|
1363
|
+
L({
|
|
1364
|
+
type: "status",
|
|
1365
|
+
stage: "error",
|
|
1366
|
+
message: n?.message || "An Unexpected Error Occurred!",
|
|
1367
|
+
queue: !0,
|
|
1368
|
+
endpoint: j,
|
|
1369
|
+
broken: e,
|
|
1370
|
+
session_not_found: n?.session_not_found,
|
|
1371
|
+
fn_index: T,
|
|
1372
|
+
time: /* @__PURE__ */ new Date()
|
|
1373
|
+
});
|
|
1374
|
+
} else if (t === "log") {
|
|
1375
|
+
L({
|
|
1376
|
+
type: "log",
|
|
1377
|
+
title: r.title,
|
|
1378
|
+
log: r.log,
|
|
1379
|
+
level: r.level,
|
|
1380
|
+
endpoint: j,
|
|
1381
|
+
duration: r.duration,
|
|
1382
|
+
visible: r.visible,
|
|
1383
|
+
fn_index: T
|
|
1384
|
+
});
|
|
1385
|
+
return;
|
|
1386
|
+
} else (t === "generating" || t === "streaming") && (L({
|
|
1387
|
+
type: "status",
|
|
1388
|
+
time: /* @__PURE__ */ new Date(),
|
|
1389
|
+
...n,
|
|
1390
|
+
stage: n?.stage,
|
|
1391
|
+
queue: !0,
|
|
1392
|
+
endpoint: j,
|
|
1393
|
+
fn_index: T
|
|
1394
|
+
}), r && D.connection !== "stream" && [
|
|
1395
|
+
"sse_v2",
|
|
1396
|
+
"sse_v2.1",
|
|
1397
|
+
"sse_v3"
|
|
1398
|
+
].includes(k) && Xe(_, N, r));
|
|
1399
|
+
r && (L({
|
|
1400
|
+
type: "data",
|
|
1401
|
+
time: /* @__PURE__ */ new Date(),
|
|
1402
|
+
data: X(r.data, D, l.components, "output", x.with_null_state),
|
|
1403
|
+
endpoint: j,
|
|
1404
|
+
fn_index: T
|
|
1405
|
+
}), r.render_config && await z(r.render_config), P && (L({
|
|
1406
|
+
type: "status",
|
|
1407
|
+
time: /* @__PURE__ */ new Date(),
|
|
1408
|
+
...P,
|
|
1409
|
+
stage: n?.stage,
|
|
1410
|
+
queue: !0,
|
|
1411
|
+
endpoint: j,
|
|
1412
|
+
fn_index: T
|
|
1413
|
+
}), W())), (n?.stage === "complete" || n?.stage === "error") && (v[N] && delete v[N], N in _ && delete _[N], W());
|
|
1414
|
+
} catch (e) {
|
|
1415
|
+
console.error("Unexpected client exception", e), L({
|
|
1416
|
+
type: "status",
|
|
1417
|
+
stage: "error",
|
|
1418
|
+
message: "An Unexpected Error Occurred!",
|
|
1419
|
+
queue: !0,
|
|
1420
|
+
endpoint: j,
|
|
1421
|
+
fn_index: T,
|
|
1422
|
+
time: /* @__PURE__ */ new Date()
|
|
1423
|
+
}), [
|
|
1424
|
+
"sse_v2",
|
|
1425
|
+
"sse_v2.1",
|
|
1426
|
+
"sse_v3"
|
|
1427
|
+
].includes(k) && (Q(h, w.abort_controller), h.open = !1, W());
|
|
1428
|
+
}
|
|
1429
|
+
};
|
|
1430
|
+
N in g && (g[N].forEach((e) => t(e)), delete g[N]), v[N] = t, ee.add(N), h.open || await this.open_stream();
|
|
1431
|
+
}
|
|
1432
|
+
});
|
|
1433
|
+
}
|
|
1434
|
+
});
|
|
1435
|
+
B.catch((e) => {
|
|
1436
|
+
L({
|
|
1437
|
+
type: "status",
|
|
1438
|
+
stage: "error",
|
|
1439
|
+
message: e instanceof Error ? e.message : String(e),
|
|
1440
|
+
queue: !be(T, l),
|
|
1441
|
+
endpoint: j,
|
|
1442
|
+
fn_index: T,
|
|
1443
|
+
time: /* @__PURE__ */ new Date()
|
|
1444
|
+
}), W();
|
|
1445
|
+
});
|
|
1446
|
+
let V = !1, H = [], U = [];
|
|
1447
|
+
function W() {
|
|
1448
|
+
for (V = !0; U.length > 0;) U.shift()({
|
|
1449
|
+
value: void 0,
|
|
1450
|
+
done: !0
|
|
1451
|
+
});
|
|
1452
|
+
}
|
|
1453
|
+
function G(e) {
|
|
1454
|
+
U.length > 0 ? U.shift()(e) : H.push(e);
|
|
1455
|
+
}
|
|
1456
|
+
function le(e) {
|
|
1457
|
+
G(tt(e)), W();
|
|
1458
|
+
}
|
|
1459
|
+
function ue(e) {
|
|
1460
|
+
G({
|
|
1461
|
+
value: e,
|
|
1462
|
+
done: !1
|
|
1463
|
+
});
|
|
1464
|
+
}
|
|
1465
|
+
function K() {
|
|
1466
|
+
return H.length > 0 ? Promise.resolve(H.shift()) : V ? Promise.resolve({
|
|
1467
|
+
value: void 0,
|
|
1468
|
+
done: !0
|
|
1469
|
+
}) : new Promise((e) => U.push(e));
|
|
1470
|
+
}
|
|
1471
|
+
let q = {
|
|
1472
|
+
[Symbol.asyncIterator]: () => q,
|
|
1473
|
+
next: K,
|
|
1474
|
+
throw: async (e) => (le(e), K()),
|
|
1475
|
+
return: async () => (W(), {
|
|
1476
|
+
value: void 0,
|
|
1477
|
+
done: !0
|
|
1478
|
+
}),
|
|
1479
|
+
cancel: ce,
|
|
1480
|
+
send_chunk: (e) => {
|
|
1481
|
+
this.post_data(`${l.root}${S}/stream/${A}`, {
|
|
1482
|
+
...e,
|
|
1483
|
+
session_hash: this.session_hash
|
|
1484
|
+
});
|
|
1485
|
+
},
|
|
1486
|
+
close_stream: () => {
|
|
1487
|
+
this.post_data(`${l.root}${S}/stream/${A}/close`, {}), W();
|
|
1488
|
+
},
|
|
1489
|
+
event_id: () => A,
|
|
1490
|
+
wait_for_id: async () => (await B, N)
|
|
1491
|
+
};
|
|
1492
|
+
return q;
|
|
1493
|
+
} catch (e) {
|
|
1494
|
+
throw console.error("Submit function encountered an error:", e), e;
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
function tt(e) {
|
|
1498
|
+
return { then: (t, n) => n(e) };
|
|
1499
|
+
}
|
|
1500
|
+
function nt(e, t, n, r) {
|
|
1501
|
+
let i, a, o;
|
|
1502
|
+
if (typeof t == "number") i = t, a = e.unnamed_endpoints[i], o = r.dependencies.find((e) => e.id == t);
|
|
1503
|
+
else {
|
|
1504
|
+
let s = t.replace(/^\//, "");
|
|
1505
|
+
i = n[s], a = e.named_endpoints[t.trim()] ?? e.named_endpoints[`/${s}`], o = r.dependencies.find((e) => e.id == n[s]);
|
|
1506
|
+
}
|
|
1507
|
+
if (typeof i != "number" || !o) {
|
|
1508
|
+
let e = r.dependencies.filter((e) => e.api_name).map((e) => `"/${e.api_name}"`).join(", ");
|
|
1509
|
+
throw Error(`No endpoint matching ${JSON.stringify(t)} was found. ` + (e ? `Valid named endpoints are: ${e}. ` : "This app exposes no named endpoints. ") + "An fn_index (number) of an existing dependency can also be used.");
|
|
1510
|
+
}
|
|
1511
|
+
return {
|
|
1512
|
+
fn_index: i,
|
|
1513
|
+
endpoint_info: a,
|
|
1514
|
+
dependency: o
|
|
1515
|
+
};
|
|
1516
|
+
}
|
|
1517
|
+
//#endregion
|
|
1518
|
+
//#region src/client.ts
|
|
1519
|
+
var $ = class {
|
|
1520
|
+
app_reference;
|
|
1521
|
+
options;
|
|
1522
|
+
deep_link = null;
|
|
1523
|
+
config;
|
|
1524
|
+
api_prefix = "";
|
|
1525
|
+
api_info;
|
|
1526
|
+
api_map = {};
|
|
1527
|
+
session_hash = Math.random().toString(36).substring(2);
|
|
1528
|
+
jwt = !1;
|
|
1529
|
+
last_status = {};
|
|
1530
|
+
cookies = null;
|
|
1531
|
+
stream_status = { open: !1 };
|
|
1532
|
+
closed = !1;
|
|
1533
|
+
pending_stream_messages = {};
|
|
1534
|
+
pending_diff_streams = {};
|
|
1535
|
+
event_callbacks = {};
|
|
1536
|
+
unclosed_events = /* @__PURE__ */ new Set();
|
|
1537
|
+
heartbeat_event = null;
|
|
1538
|
+
abort_controller = null;
|
|
1539
|
+
stream_instance = null;
|
|
1540
|
+
current_payload;
|
|
1541
|
+
get_url_config(e = null) {
|
|
1542
|
+
if (!this.config) throw Error(x);
|
|
1543
|
+
e === null && (e = window.location.href);
|
|
1544
|
+
let t = (e) => e.replace(/^\/+|\/+$/g, ""), n = t(new URL(this.config.root).pathname), r = t(new URL(e).pathname), i;
|
|
1545
|
+
return i = r.startsWith(n) ? t(r.substring(n.length)) : "", this.get_page_config(i);
|
|
1546
|
+
}
|
|
1547
|
+
get_page_config(e) {
|
|
1548
|
+
if (!this.config) throw Error(x);
|
|
1549
|
+
let t = this.config;
|
|
1550
|
+
return e in t.page || (e = ""), {
|
|
1551
|
+
...t,
|
|
1552
|
+
current_page: e,
|
|
1553
|
+
layout: t.page[e].layout,
|
|
1554
|
+
components: t.components.filter((n) => t.page[e].components.includes(n.id)),
|
|
1555
|
+
dependencies: this.config.dependencies.filter((n) => t.page[e].dependencies.includes(n.id))
|
|
1556
|
+
};
|
|
1557
|
+
}
|
|
1558
|
+
fetch(e, t) {
|
|
1559
|
+
let n = new Headers(t?.headers || {});
|
|
1560
|
+
return this && this.cookies && n.append("Cookie", this.cookies), this && this.options.headers && new Headers(this.options.headers).forEach((e, t) => {
|
|
1561
|
+
n.append(t, e);
|
|
1562
|
+
}), fetch(e, {
|
|
1563
|
+
...t,
|
|
1564
|
+
headers: n
|
|
1565
|
+
});
|
|
1566
|
+
}
|
|
1567
|
+
stream(e) {
|
|
1568
|
+
let t = new Headers();
|
|
1569
|
+
return this && this.cookies && t.append("Cookie", this.cookies), this && this.options.headers && new Headers(this.options.headers).forEach((e, n) => {
|
|
1570
|
+
t.append(n, e);
|
|
1571
|
+
}), this && this.options.token && t.append("Authorization", `Bearer ${this.options.token}`), this.abort_controller = new AbortController(), this.stream_instance = $e(e.toString(), {
|
|
1572
|
+
credentials: this.options.credentials ?? "same-origin",
|
|
1573
|
+
headers: t,
|
|
1574
|
+
signal: this.abort_controller.signal
|
|
1575
|
+
}), this.stream_instance;
|
|
1576
|
+
}
|
|
1577
|
+
view_api;
|
|
1578
|
+
upload_files;
|
|
1579
|
+
upload;
|
|
1580
|
+
handle_blob;
|
|
1581
|
+
post_data;
|
|
1582
|
+
submit;
|
|
1583
|
+
predict;
|
|
1584
|
+
open_stream;
|
|
1585
|
+
resolve_config;
|
|
1586
|
+
resolve_cookies;
|
|
1587
|
+
constructor(e, t = { events: ["data"] }) {
|
|
1588
|
+
this.app_reference = e, this.deep_link = t.query_params?.deep_link || null, t.events ||= ["data"], P(t), this.options = t, this.current_payload = {}, t.cookies && (this.cookies = t.cookies), this.view_api = K.bind(this), this.upload_files = q.bind(this), this.handle_blob = we.bind(this), this.post_data = Oe.bind(this), this.submit = et.bind(this), this.predict = ke.bind(this), this.open_stream = Ye.bind(this), this.resolve_config = se.bind(this), this.resolve_cookies = ce.bind(this), this.upload = he.bind(this), this.fetch = this.fetch.bind(this), this.handle_space_success = this.handle_space_success.bind(this), this.stream = this.stream.bind(this);
|
|
1589
|
+
}
|
|
1590
|
+
async init() {
|
|
1591
|
+
He(), this.options.auth && await this.resolve_cookies(), await this._resolve_config().then((e) => e?.config && this._resolve_heartbeat(e.config));
|
|
1592
|
+
try {
|
|
1593
|
+
this.api_info = await this.view_api();
|
|
1594
|
+
} catch (e) {
|
|
1595
|
+
console.error(e.message);
|
|
1596
|
+
}
|
|
1597
|
+
this.api_map = F(this.config?.dependencies || []);
|
|
1598
|
+
}
|
|
1599
|
+
async _resolve_heartbeat(e) {
|
|
1600
|
+
if (e && (this.config = e, this.api_prefix = e.api_prefix || "", this.config && this.config.connect_heartbeat && this.config.space_id && this.options.token && (this.jwt = await N(this.config.space_id, this.options.token, this.cookies))), e.space_id && this.options.token && (this.jwt = await N(e.space_id, this.options.token)), this.config && this.config.connect_heartbeat) {
|
|
1601
|
+
let e = new URL(`${this.config.root}${this.api_prefix}/${v}/${this.session_hash}`);
|
|
1602
|
+
this.jwt && e.searchParams.set("__sign", this.jwt), this.heartbeat_event ||= this.stream(e);
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
static async connect(e, t = { events: ["data"] }) {
|
|
1606
|
+
let n = new this(e, t);
|
|
1607
|
+
return t.session_hash && (n.session_hash = t.session_hash), await n.init(), n;
|
|
1608
|
+
}
|
|
1609
|
+
async reconnect() {
|
|
1610
|
+
let e = new URL(`${this.config.root}${this.api_prefix}/${y}`), t;
|
|
1611
|
+
try {
|
|
1612
|
+
let n = await this.fetch(e);
|
|
1613
|
+
if (!n.ok) throw Error();
|
|
1614
|
+
t = (await n.json()).app_id;
|
|
1615
|
+
} catch {
|
|
1616
|
+
return "broken";
|
|
1617
|
+
}
|
|
1618
|
+
return t === this.config.app_id ? "connected" : "changed";
|
|
1619
|
+
}
|
|
1620
|
+
close() {
|
|
1621
|
+
this.closed = !0, Q(this.stream_status, this.abort_controller);
|
|
1622
|
+
}
|
|
1623
|
+
async refresh() {
|
|
1624
|
+
if (!this.config) throw Error(x);
|
|
1625
|
+
let e = await this.resolve_config(this.config.root, !1);
|
|
1626
|
+
if (!e) throw Error(x);
|
|
1627
|
+
this.config = e, this.api_prefix = e.api_prefix || "", this.api_map = F(e.dependencies || []);
|
|
1628
|
+
try {
|
|
1629
|
+
this.api_info = await this.view_api();
|
|
1630
|
+
} catch (e) {
|
|
1631
|
+
console.error(C + e.message);
|
|
1632
|
+
}
|
|
1633
|
+
return this.get_url_config();
|
|
1634
|
+
}
|
|
1635
|
+
set_current_payload(e) {
|
|
1636
|
+
this.current_payload = e;
|
|
1637
|
+
}
|
|
1638
|
+
static async duplicate(e, t = { events: ["data"] }) {
|
|
1639
|
+
return Ie(e, t);
|
|
1640
|
+
}
|
|
1641
|
+
async _resolve_config() {
|
|
1642
|
+
let { http_protocol: e, host: t, space_id: n } = await U(this.app_reference, this.options.token), { status_callback: r } = this.options;
|
|
1643
|
+
n && r && await Ae(n, r);
|
|
1644
|
+
let i;
|
|
1645
|
+
try {
|
|
1646
|
+
let n = `${e}//${t}`;
|
|
1647
|
+
if (i = await this.resolve_config(n), !i) throw Error(x);
|
|
1648
|
+
return this.config_success(i);
|
|
1649
|
+
} catch (e) {
|
|
1650
|
+
if (n && r) Z(n, V.test(n) ? "space_name" : "subdomain", this.handle_space_success);
|
|
1651
|
+
else throw r && r({
|
|
1652
|
+
status: "error",
|
|
1653
|
+
message: "Could not load this space.",
|
|
1654
|
+
load_status: "error",
|
|
1655
|
+
detail: "NOT_FOUND"
|
|
1656
|
+
}), e instanceof Error ? e : Error(String(e));
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
async config_success(e) {
|
|
1660
|
+
if (this.config = e, this.api_prefix = e.api_prefix || "", this.config.auth_required) return this.prepare_return_obj();
|
|
1661
|
+
try {
|
|
1662
|
+
this.api_info = await this.view_api();
|
|
1663
|
+
} catch (e) {
|
|
1664
|
+
console.error(C + e.message);
|
|
1665
|
+
}
|
|
1666
|
+
return this.prepare_return_obj();
|
|
1667
|
+
}
|
|
1668
|
+
async handle_space_success(e) {
|
|
1669
|
+
if (!this) throw Error(x);
|
|
1670
|
+
let { status_callback: t } = this.options;
|
|
1671
|
+
if (t && t(e), e.status === "running") try {
|
|
1672
|
+
if (this.config = await this._resolve_config(), this.api_prefix = this?.config?.api_prefix || "", !this.config) throw Error(x);
|
|
1673
|
+
return await this.config_success(this.config);
|
|
1674
|
+
} catch (e) {
|
|
1675
|
+
throw t && t({
|
|
1676
|
+
status: "error",
|
|
1677
|
+
message: "Could not load this space.",
|
|
1678
|
+
load_status: "error",
|
|
1679
|
+
detail: "NOT_FOUND"
|
|
1680
|
+
}), e;
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
async component_server(e, t, n) {
|
|
1684
|
+
if (!this.config) throw Error(x);
|
|
1685
|
+
let r = {}, { token: i } = this.options, { session_hash: a } = this;
|
|
1686
|
+
i && (r.Authorization = `Bearer ${this.options.token}`);
|
|
1687
|
+
let o, s = this.config.components.find((t) => t.id === e);
|
|
1688
|
+
o = s?.props?.root_url ? s.props.root_url : this.config.root;
|
|
1689
|
+
let c;
|
|
1690
|
+
if (typeof n == "object" && n && "binary" in n) {
|
|
1691
|
+
let r = n;
|
|
1692
|
+
c = new FormData();
|
|
1693
|
+
for (let e in r.data) e !== "binary" && c.append(e, r.data[e]);
|
|
1694
|
+
c.set("component_id", e.toString()), c.set("fn_name", t), c.set("session_hash", a);
|
|
1695
|
+
} else c = JSON.stringify({
|
|
1696
|
+
data: n,
|
|
1697
|
+
component_id: e,
|
|
1698
|
+
fn_name: t,
|
|
1699
|
+
session_hash: a
|
|
1700
|
+
}), r["Content-Type"] = "application/json";
|
|
1701
|
+
i && (r.Authorization = `Bearer ${i}`);
|
|
1702
|
+
try {
|
|
1703
|
+
let e = await this.fetch(`${o}${this.api_prefix}/${ee}/`, {
|
|
1704
|
+
method: "POST",
|
|
1705
|
+
body: c,
|
|
1706
|
+
headers: r,
|
|
1707
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
1708
|
+
});
|
|
1709
|
+
if (!e.ok) throw Error("Could not connect to component server: " + e.statusText);
|
|
1710
|
+
return await e.json();
|
|
1711
|
+
} catch (e) {
|
|
1712
|
+
console.warn(e);
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
set_cookies(e) {
|
|
1716
|
+
this.cookies = B(e).join("; ");
|
|
1717
|
+
}
|
|
1718
|
+
prepare_return_obj() {
|
|
1719
|
+
return {
|
|
1720
|
+
config: this.config,
|
|
1721
|
+
predict: this.predict,
|
|
1722
|
+
submit: this.submit,
|
|
1723
|
+
view_api: this.view_api,
|
|
1724
|
+
component_server: this.component_server
|
|
1725
|
+
};
|
|
1726
|
+
}
|
|
1727
|
+
};
|
|
1728
|
+
async function rt(e, t = { events: ["data"] }) {
|
|
1729
|
+
return await $.connect(e, t);
|
|
1730
|
+
}
|
|
1731
|
+
async function it(e, t) {
|
|
1732
|
+
return await $.duplicate(e, t);
|
|
1733
|
+
}
|
|
1734
|
+
//#endregion
|
|
1735
|
+
export { $ as Client, J as FileData, ie as MISSING_CREDENTIALS_MSG, rt as client, it as duplicate, Se as handle_file, ke as predict, ge as prepare_files, et as submit, he as upload, q as upload_files };
|