@gradio/client 2.3.1 → 2.5.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 +21 -0
- package/README.md +4 -2
- package/dist/__vite-browser-external-Cgmn0awE.js +7 -0
- package/dist/browser.js +1949 -1963
- 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 +11 -2
- package/dist/helpers/init_helpers.d.ts.map +1 -1
- package/dist/index.cjs +2584 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2471 -2466
- package/dist/index.min.js +1962 -0
- package/dist/types.d.ts +29 -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/run_history.d.ts +64 -0
- package/dist/utils/run_history.d.ts.map +1 -0
- package/dist/utils/stream.d.ts.map +1 -1
- package/dist/utils/submit.d.ts.map +1 -1
- package/package.json +11 -4
- package/scripts/verify_dist.mjs +46 -0
- package/src/client.ts +54 -5
- package/src/constants.ts +10 -0
- package/src/helpers/api_info.ts +46 -19
- package/src/helpers/data.ts +8 -3
- package/src/helpers/init_helpers.ts +54 -7
- package/src/index.ts +12 -0
- package/src/test/api_info.test.ts +77 -12
- package/src/test/data.test.ts +26 -3
- package/src/test/init.test.ts +21 -3
- package/src/test/init_helpers.test.ts +67 -1
- package/src/test/refresh.test.ts +31 -0
- package/src/test/run_history.test.ts +345 -0
- package/src/test/submit.test.ts +1 -3
- package/src/types.ts +29 -0
- package/src/utils/duplicate.ts +2 -0
- package/src/utils/predict.ts +14 -1
- package/src/utils/run_history.ts +520 -0
- package/src/utils/stream.ts +17 -4
- package/src/utils/submit.ts +105 -12
- package/vite.config.ts +20 -4
- package/dist/__vite-browser-external-DYxpcVy9.js +0 -4
|
@@ -0,0 +1,1962 @@
|
|
|
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", ee = "sleeptime", _ = "heartbeat", te = "component_server", ne = "reset", re = "cancel", v = "app_id", ie = "This application is currently busy. Please try again. ", y = "Connection errored out. ", b = "Could not resolve app config. ", x = "Could not get space status. ", S = "Could not get API info. ", C = "Space metadata could not be loaded. ", w = "Invalid URL. A full URL path is required.", T = "Not authorized to access this space. ", E = "Invalid credentials. Could not login. ", ae = "Login credentials are required to access this space.", D = "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.", oe = (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.`, se = "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. ", ce = "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.", le = "File system access is only available in Node.js environments", O = "Root URL not found in client config", ue = "Error uploading file";
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/helpers/init_helpers.ts
|
|
14
|
+
async function de(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 k(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 fe(e) {
|
|
28
|
+
let t = {};
|
|
29
|
+
return e.forEach(({ api_name: e, id: n }) => {
|
|
30
|
+
e && (t[e] = n);
|
|
31
|
+
}), t;
|
|
32
|
+
}
|
|
33
|
+
function A(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 j(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 = me(e, this.deep_link ? m + "?deep_link=" + this.deep_link : m), r = await M(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 = A(r.root, location.href), r;
|
|
52
|
+
} else if (e) {
|
|
53
|
+
let t = me(e, this.deep_link ? m + "?deep_link=" + this.deep_link : m), r = await M(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(b);
|
|
60
|
+
}
|
|
61
|
+
async function M(e, t) {
|
|
62
|
+
if (e?.status === 401 && !t) {
|
|
63
|
+
let t = null;
|
|
64
|
+
try {
|
|
65
|
+
t = await e.json();
|
|
66
|
+
} catch {
|
|
67
|
+
throw Error(D);
|
|
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(E);
|
|
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(T);
|
|
78
|
+
throw Error(`${b}(received status ${e?.status} when fetching the app config)`);
|
|
79
|
+
}
|
|
80
|
+
async function N() {
|
|
81
|
+
let { http_protocol: e, host: t } = await R(this.app_reference, this.options.token);
|
|
82
|
+
try {
|
|
83
|
+
if (this.options.auth) {
|
|
84
|
+
let n = await P(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 P(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(E) : Error(C);
|
|
104
|
+
}
|
|
105
|
+
function F(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 I = (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
|
+
}, L = /^[a-zA-Z0-9_\-\.]+\/[a-zA-Z0-9_\-\.]+$/, pe = /.*hf\.space\/{0,1}.*$/;
|
|
127
|
+
async function R(e, t) {
|
|
128
|
+
let n = {};
|
|
129
|
+
t && (n.Authorization = `Bearer ${t}`);
|
|
130
|
+
let r = e.trim().replace(/\/$/, "");
|
|
131
|
+
if (L.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(C);
|
|
137
|
+
}
|
|
138
|
+
if (e.status === 401 || e.status === 404) throw Error(oe(r, e.status));
|
|
139
|
+
let t;
|
|
140
|
+
try {
|
|
141
|
+
t = (await e.json()).host;
|
|
142
|
+
} catch {
|
|
143
|
+
throw Error(C);
|
|
144
|
+
}
|
|
145
|
+
if (!t) throw Error(C);
|
|
146
|
+
return {
|
|
147
|
+
space_id: r,
|
|
148
|
+
...F(t)
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
if (pe.test(r)) {
|
|
152
|
+
let { ws_protocol: e, http_protocol: t, host: n } = F(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
|
+
...F(r)
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
var me = (...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(w);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
function he(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: _e(e?.type, n),
|
|
203
|
+
type: ge(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 ge(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 _e(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 ve(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: ie,
|
|
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 ye = (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 be() {
|
|
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 = me(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(y);
|
|
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"]), he(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 z(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(y + 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 B = {
|
|
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 V(e, t = 1, n = "jedec") {
|
|
477
|
+
e = Math.abs(e);
|
|
478
|
+
let { radix: r, unit: i } = B[n] || B.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 H(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 ${V(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 U({
|
|
490
|
+
...e[r],
|
|
491
|
+
path: n,
|
|
492
|
+
url: `${t}${this.api_prefix}/file=${n}`
|
|
493
|
+
})) : [];
|
|
494
|
+
}));
|
|
495
|
+
}
|
|
496
|
+
async function xe(e, t) {
|
|
497
|
+
return e.map((e) => new U({
|
|
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 U = 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
|
+
}, W = 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
|
+
}, Se = typeof process < "u" && process.versions && process.versions.node;
|
|
529
|
+
function Ce(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 we(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 we(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 we(o, void 0, a, !1, i));
|
|
558
|
+
}
|
|
559
|
+
return t;
|
|
560
|
+
}
|
|
561
|
+
return [];
|
|
562
|
+
}
|
|
563
|
+
function Te(e, t) {
|
|
564
|
+
let n = t?.dependencies?.find((t) => t.id == e)?.queue;
|
|
565
|
+
return n == null ? !t.enable_queue : !n;
|
|
566
|
+
}
|
|
567
|
+
function Ee(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 De(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 (Se) return new W("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 G(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 Oe = /* @__PURE__ */ o(((e, t) => {
|
|
619
|
+
t.exports = {};
|
|
620
|
+
}));
|
|
621
|
+
//#endregion
|
|
622
|
+
//#region src/utils/handle_blob.ts
|
|
623
|
+
async function ke(e, t, n) {
|
|
624
|
+
let r = this;
|
|
625
|
+
await Ae(r, t);
|
|
626
|
+
let i = await we(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" ? Ce(t, n, e) : n && Ce(t, new U({
|
|
641
|
+
path: n,
|
|
642
|
+
orig_name: i
|
|
643
|
+
}), e);
|
|
644
|
+
}), t;
|
|
645
|
+
}
|
|
646
|
+
async function Ae(e, t) {
|
|
647
|
+
if (!(e.config?.root || e.config?.root_url)) throw Error(O);
|
|
648
|
+
await je(e, t);
|
|
649
|
+
}
|
|
650
|
+
async function je(e, t, n = []) {
|
|
651
|
+
for (let r in t) t[r] instanceof W ? await Me(e, t, r) : typeof t[r] == "object" && t[r] !== null && await je(e, t[r], [...n, r]);
|
|
652
|
+
}
|
|
653
|
+
async function Me(e, t, n) {
|
|
654
|
+
let r = t[n], i = e.config?.root || e.config?.root_url;
|
|
655
|
+
if (!i) throw Error(O);
|
|
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(Oe(), 1));
|
|
660
|
+
o = (await Promise.resolve().then(() => /* @__PURE__ */ c(Oe(), 1))).resolve(process.cwd(), r.meta.path), a = await e.readFile(o);
|
|
661
|
+
} else throw Error(le);
|
|
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 U({
|
|
664
|
+
path: u,
|
|
665
|
+
orig_name: r.meta.name || ""
|
|
666
|
+
}));
|
|
667
|
+
} catch (e) {
|
|
668
|
+
console.error(ue, e);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
//#endregion
|
|
672
|
+
//#region src/utils/post_data.ts
|
|
673
|
+
async function Ne(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: y }, 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 Pe(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 K(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: x,
|
|
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
|
+
K(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 Le(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
|
+
K(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
|
+
K(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 Le(s)
|
|
795
|
+
});
|
|
796
|
+
break;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
var Fe = async (e, t) => {
|
|
800
|
+
let n = 0;
|
|
801
|
+
return new Promise((r) => {
|
|
802
|
+
K(e, L.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
|
+
Fe(e, t).then(r);
|
|
805
|
+
}, 5e3)) : r());
|
|
806
|
+
});
|
|
807
|
+
});
|
|
808
|
+
}, Ie = /^(?=[^]*\b[dD]iscussions{0,1}\b)(?=[^]*\b[dD]isabled\b)[^]*$/;
|
|
809
|
+
async function Le(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 && Ie.test(n));
|
|
813
|
+
} catch {
|
|
814
|
+
return !1;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
async function Re(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 ze(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}/${ee}`, {
|
|
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 Be = [
|
|
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 Ve(e, t) {
|
|
866
|
+
k(t);
|
|
867
|
+
let { token: n, private: r, hardware: i, timeout: a, auth: o } = t;
|
|
868
|
+
if (i && !Be.includes(i)) throw Error(`Invalid hardware type provided. Valid types are: ${Be.map((e) => `"${e}"`).join(",")}.`);
|
|
869
|
+
let { http_protocol: s, host: c } = await R(e, n), l = null;
|
|
870
|
+
if (o) {
|
|
871
|
+
let e = await P(s, c, o, fetch, void 0, t.credentials);
|
|
872
|
+
e && (l = I(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 Re(e, n));
|
|
883
|
+
} catch (e) {
|
|
884
|
+
throw Error(C + 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 ze(`${d}/${f}`, a || 300, n), await $.connect(He(i.url), t);
|
|
901
|
+
} catch (e) {
|
|
902
|
+
throw Error(e);
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
function He(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 Ue = "supports-zerogpu-headers", We = !1;
|
|
912
|
+
function Ge() {
|
|
913
|
+
return typeof window < "u" && typeof document < "u" && typeof window.addEventListener == "function";
|
|
914
|
+
}
|
|
915
|
+
function Ke(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 qe() {
|
|
919
|
+
if (!Ge() || We) return;
|
|
920
|
+
window.addEventListener("message", (e) => {
|
|
921
|
+
e.data === Ue && (window.supports_zerogpu_headers = !0);
|
|
922
|
+
}), We = !0;
|
|
923
|
+
let e = Ke(window.location.hostname);
|
|
924
|
+
e && window.parent !== window && window.parent.postMessage(Ue, 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 Je = 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 Ye(e) {
|
|
956
|
+
let t = new TextDecoderStream(), n = new Je({ allowCR: !0 });
|
|
957
|
+
return e.pipeThrough(t).pipeThrough(n);
|
|
958
|
+
}
|
|
959
|
+
function Xe(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 Ze(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 = Ye(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] = Xe(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 $e(e, t) {
|
|
983
|
+
let n = new Request(e, t);
|
|
984
|
+
Ze(n.headers, "Accept", "text/event-stream"), Ze(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 et() {
|
|
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
|
+
tt(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: y
|
|
1023
|
+
})));
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
1026
|
+
function tt(e, t) {
|
|
1027
|
+
e && (e.open = !1, t?.abort());
|
|
1028
|
+
}
|
|
1029
|
+
function nt(e, t, n) {
|
|
1030
|
+
e[t] ? n.data.forEach((r, i) => {
|
|
1031
|
+
let a = rt(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 rt(e, t) {
|
|
1038
|
+
return t.forEach(([t, n, r]) => {
|
|
1039
|
+
e = it(e, n, t, r);
|
|
1040
|
+
}), e;
|
|
1041
|
+
}
|
|
1042
|
+
function it(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 at(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 $e(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/run_history.ts
|
|
1106
|
+
var ot = "gradio:run-history:", q = `${ot}v2:`, st = `${ot}replay:v2:`, ct = 100, lt = 8;
|
|
1107
|
+
function J(e, t) {
|
|
1108
|
+
try {
|
|
1109
|
+
return e();
|
|
1110
|
+
} catch (e) {
|
|
1111
|
+
return console.warn("Could not update the run history.", e), t;
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
function ut(e, t = "/gradio_api") {
|
|
1115
|
+
return `${e.replace(/\/+$/, "")}${t}/runs`;
|
|
1116
|
+
}
|
|
1117
|
+
function Y(e) {
|
|
1118
|
+
if (typeof window > "u") return null;
|
|
1119
|
+
let t = e?.app_id;
|
|
1120
|
+
if (t == null || t === "") return null;
|
|
1121
|
+
try {
|
|
1122
|
+
return window.localStorage ? `${q}${t}${e?.username ? `:user:${encodeURIComponent(e.username)}` : ""}` : null;
|
|
1123
|
+
} catch {
|
|
1124
|
+
return null;
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
function dt(e) {
|
|
1128
|
+
let t = Y(e);
|
|
1129
|
+
return t ? t.replace(q, st) : null;
|
|
1130
|
+
}
|
|
1131
|
+
function ft(e) {
|
|
1132
|
+
try {
|
|
1133
|
+
let t = JSON.parse(window.localStorage.getItem(e) || "[]");
|
|
1134
|
+
return Array.isArray(t) && t.length && Date.parse(t[0]?.started_at) || 0;
|
|
1135
|
+
} catch {
|
|
1136
|
+
return 0;
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
function pt(e) {
|
|
1140
|
+
let t = Object.keys(window.localStorage).filter((e) => e.startsWith(ot)), n = [...t.filter((e) => !e.startsWith(q) && !e.startsWith(st)), ...t.filter((t) => t.startsWith(q) && t !== e).sort((e, t) => ft(t) - ft(e)).slice(lt - 1)];
|
|
1141
|
+
for (let e of n) try {
|
|
1142
|
+
window.localStorage.removeItem(e), window.sessionStorage.removeItem(e.replace(q, st));
|
|
1143
|
+
} catch {}
|
|
1144
|
+
}
|
|
1145
|
+
function mt() {
|
|
1146
|
+
return typeof crypto < "u" && crypto.randomUUID ? crypto.randomUUID() : `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
1147
|
+
}
|
|
1148
|
+
function X(e) {
|
|
1149
|
+
let t = /* @__PURE__ */ new WeakSet();
|
|
1150
|
+
try {
|
|
1151
|
+
return JSON.parse(JSON.stringify(e, (e, n) => {
|
|
1152
|
+
if (typeof n == "bigint") return n.toString();
|
|
1153
|
+
if (typeof n == "object" && n) {
|
|
1154
|
+
if (t.has(n)) return "[Circular]";
|
|
1155
|
+
t.add(n);
|
|
1156
|
+
}
|
|
1157
|
+
return n;
|
|
1158
|
+
}));
|
|
1159
|
+
} catch {
|
|
1160
|
+
return "[Unserializable value]";
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
function Z(e) {
|
|
1164
|
+
let t = Y(e);
|
|
1165
|
+
if (!t) return [];
|
|
1166
|
+
try {
|
|
1167
|
+
let e = JSON.parse(window.localStorage.getItem(t) || "[]");
|
|
1168
|
+
return Array.isArray(e) ? e : [];
|
|
1169
|
+
} catch {
|
|
1170
|
+
return [];
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
function ht(e, t) {
|
|
1174
|
+
let n = Y(e);
|
|
1175
|
+
if (!n) return;
|
|
1176
|
+
let r = t.slice(0, ct), i = !1;
|
|
1177
|
+
for (; r.length > 0;) try {
|
|
1178
|
+
window.localStorage.setItem(n, JSON.stringify(r));
|
|
1179
|
+
return;
|
|
1180
|
+
} catch {
|
|
1181
|
+
if (!i) {
|
|
1182
|
+
i = !0, pt(n);
|
|
1183
|
+
continue;
|
|
1184
|
+
}
|
|
1185
|
+
r = r.slice(0, -1);
|
|
1186
|
+
}
|
|
1187
|
+
try {
|
|
1188
|
+
window.localStorage.removeItem(n);
|
|
1189
|
+
} catch {}
|
|
1190
|
+
}
|
|
1191
|
+
function gt(e) {
|
|
1192
|
+
let t = Y(e);
|
|
1193
|
+
if (t) {
|
|
1194
|
+
try {
|
|
1195
|
+
window.localStorage.removeItem(t);
|
|
1196
|
+
} catch {}
|
|
1197
|
+
yt();
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
function _t(e, t) {
|
|
1201
|
+
ht(e, Z(e).filter((e) => e.id !== t)), yt();
|
|
1202
|
+
}
|
|
1203
|
+
var vt = "gradio:run-history-change";
|
|
1204
|
+
function yt() {
|
|
1205
|
+
if (!(typeof window > "u")) try {
|
|
1206
|
+
window.dispatchEvent(new Event(vt));
|
|
1207
|
+
} catch {}
|
|
1208
|
+
}
|
|
1209
|
+
function bt(e) {
|
|
1210
|
+
return typeof window > "u" ? () => {} : (window.addEventListener(vt, e), window.addEventListener("storage", e), () => {
|
|
1211
|
+
window.removeEventListener(vt, e), window.removeEventListener("storage", e);
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
1214
|
+
function xt(e, t) {
|
|
1215
|
+
let n = dt(e);
|
|
1216
|
+
if (n) try {
|
|
1217
|
+
window.sessionStorage.setItem(n, JSON.stringify(t));
|
|
1218
|
+
} catch {}
|
|
1219
|
+
}
|
|
1220
|
+
function St(e) {
|
|
1221
|
+
let t = dt(e);
|
|
1222
|
+
if (!t) return null;
|
|
1223
|
+
try {
|
|
1224
|
+
let e = window.sessionStorage.getItem(t);
|
|
1225
|
+
return window.sessionStorage.removeItem(t), e ? JSON.parse(e) : null;
|
|
1226
|
+
} catch {
|
|
1227
|
+
return null;
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
function Ct(e) {
|
|
1231
|
+
let t = Y(e);
|
|
1232
|
+
if (!t) return null;
|
|
1233
|
+
pt(t);
|
|
1234
|
+
let n = {
|
|
1235
|
+
id: mt(),
|
|
1236
|
+
endpoint: e.endpoint,
|
|
1237
|
+
api_name: e.api_name,
|
|
1238
|
+
fn_index: e.fn_index,
|
|
1239
|
+
page: `${window.location.pathname}${window.location.search}`,
|
|
1240
|
+
inputs: X(e.inputs),
|
|
1241
|
+
outputs: null,
|
|
1242
|
+
...e.input_components ? { input_components: X(e.input_components) } : {},
|
|
1243
|
+
...e.output_components ? { output_components: X(e.output_components) } : {},
|
|
1244
|
+
status: "running",
|
|
1245
|
+
started_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1246
|
+
};
|
|
1247
|
+
return ht(e, [n, ...Z(e)]), yt(), n.id;
|
|
1248
|
+
}
|
|
1249
|
+
function wt(e, t, n) {
|
|
1250
|
+
if (!t) return;
|
|
1251
|
+
let r = Z(e), i = r.find((e) => e.id === t);
|
|
1252
|
+
i && (i.inputs = X(n), ht(e, r));
|
|
1253
|
+
}
|
|
1254
|
+
function Tt(e, t, n) {
|
|
1255
|
+
if (!t) return;
|
|
1256
|
+
let r = Z(e), i = r.find((e) => e.id === t);
|
|
1257
|
+
if (!i) return;
|
|
1258
|
+
let a = !1;
|
|
1259
|
+
if (n.type === "data") i.outputs = X(n.data), Q.set(t, i.outputs);
|
|
1260
|
+
else if (n.type === "status" && n.original_msg === "process_starts") Ot(i, n.time);
|
|
1261
|
+
else if (n.type === "status" && (n.stage === "generating" || n.stage === "streaming")) i.streamed = !0;
|
|
1262
|
+
else if (n.type === "status" && n.stage === "complete") i.status = "completed", kt(i, n), a = !0;
|
|
1263
|
+
else if (n.type === "status" && n.stage === "error") i.status = "failed", i.error = typeof n.message == "string" ? n.message : JSON.stringify(n.message || "Unknown error"), kt(i, n), a = !0;
|
|
1264
|
+
else return;
|
|
1265
|
+
if (a) Q.has(t) && (i.outputs = Q.get(t)), Q.delete(t), Dt.delete(t);
|
|
1266
|
+
else if (i.streamed) {
|
|
1267
|
+
if (Date.now() - (Dt.get(t) ?? 0) < Et) return;
|
|
1268
|
+
Dt.set(t, Date.now()), Q.delete(t);
|
|
1269
|
+
}
|
|
1270
|
+
ht(e, r);
|
|
1271
|
+
}
|
|
1272
|
+
var Et = 500, Dt = /* @__PURE__ */ new Map(), Q = /* @__PURE__ */ new Map();
|
|
1273
|
+
function Ot(e, t) {
|
|
1274
|
+
if (e.process_started_at) return;
|
|
1275
|
+
let n = t || /* @__PURE__ */ new Date();
|
|
1276
|
+
e.process_started_at = n.toISOString(), e.queued_ms = Math.max(0, n.getTime() - Date.parse(e.started_at));
|
|
1277
|
+
}
|
|
1278
|
+
function kt(e, t) {
|
|
1279
|
+
let n = t.time || /* @__PURE__ */ new Date();
|
|
1280
|
+
e.completed_at = n.toISOString(), e.duration_ms = (!e.streamed && typeof t.cache_duration == "number" && t.cache_duration >= 0 ? t.cache_duration * 1e3 : null) ?? Math.max(0, n.getTime() - Date.parse(e.process_started_at || e.started_at));
|
|
1281
|
+
}
|
|
1282
|
+
function At(e) {
|
|
1283
|
+
return J(() => Z(e), []);
|
|
1284
|
+
}
|
|
1285
|
+
function jt(e) {
|
|
1286
|
+
J(() => gt(e), void 0);
|
|
1287
|
+
}
|
|
1288
|
+
function Mt(e, t) {
|
|
1289
|
+
J(() => _t(e, t), void 0);
|
|
1290
|
+
}
|
|
1291
|
+
function Nt(e, t) {
|
|
1292
|
+
J(() => xt(e, t), void 0);
|
|
1293
|
+
}
|
|
1294
|
+
function Pt(e) {
|
|
1295
|
+
return J(() => St(e), null);
|
|
1296
|
+
}
|
|
1297
|
+
function Ft(e) {
|
|
1298
|
+
return J(() => Ct(e), null);
|
|
1299
|
+
}
|
|
1300
|
+
function It(e, t, n) {
|
|
1301
|
+
J(() => wt(e, t, n), void 0);
|
|
1302
|
+
}
|
|
1303
|
+
function Lt(e, t, n) {
|
|
1304
|
+
J(() => Tt(e, t, n), void 0);
|
|
1305
|
+
}
|
|
1306
|
+
function Rt(e) {
|
|
1307
|
+
return J(() => bt(() => J(e, void 0)), () => {});
|
|
1308
|
+
}
|
|
1309
|
+
//#endregion
|
|
1310
|
+
//#region src/utils/submit.ts
|
|
1311
|
+
function zt(e, t = {}, n, r, i, a) {
|
|
1312
|
+
try {
|
|
1313
|
+
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: ee, event_callbacks: _, unclosed_events: te, post_data: v, options: b, api_prefix: x } = this, S = a || { "x-gradio-user": "api" }, C = this;
|
|
1314
|
+
if (!p) throw Error(se);
|
|
1315
|
+
if (!l) throw Error("Could not resolve app config");
|
|
1316
|
+
let { fn_index: w, endpoint_info: T, dependency: E } = Vt(p, e, m, l), ae = ye(t, T), D = l.protocol ?? "ws";
|
|
1317
|
+
if (D === "ws") throw Error(ce);
|
|
1318
|
+
let oe = typeof E.api_name == "string" ? `/${E.api_name}` : e, le = typeof E.api_name == "string" ? `/${E.api_name}` : `Function ${w}`, O = (e) => {
|
|
1319
|
+
let t = l.components.find((t) => t.id === e);
|
|
1320
|
+
return t ? {
|
|
1321
|
+
type: t.type,
|
|
1322
|
+
component_class_id: t.component_class_id,
|
|
1323
|
+
props: t.props
|
|
1324
|
+
} : null;
|
|
1325
|
+
}, ue = E.api_visibility === "public", de = l.run_history !== !1 && this.options.record_history !== !1, k = {
|
|
1326
|
+
app_id: l.app_id,
|
|
1327
|
+
username: l.username
|
|
1328
|
+
}, fe = !de || !ue ? null : Ft({
|
|
1329
|
+
...k,
|
|
1330
|
+
endpoint: oe,
|
|
1331
|
+
api_name: le,
|
|
1332
|
+
fn_index: w,
|
|
1333
|
+
inputs: G(ae, E, l.components, "input", !0),
|
|
1334
|
+
input_components: E.inputs.map(O),
|
|
1335
|
+
output_components: E.outputs.map(O)
|
|
1336
|
+
}), A, j = "", M = typeof e == "number" ? "/predict" : e, N, P = null, F = !1, I = {}, L = typeof window < "u" && typeof document < "u" ? new URLSearchParams(window.location.search).toString() : "", pe = b?.events?.reduce((e, t) => (e[t] = !0, e), {}) || {};
|
|
1337
|
+
function R(e) {
|
|
1338
|
+
Lt(k, fe, e), (i || pe[e.type]) && U(e);
|
|
1339
|
+
}
|
|
1340
|
+
async function me() {
|
|
1341
|
+
let e = {}, t = {};
|
|
1342
|
+
e = { event_id: P }, t = {
|
|
1343
|
+
event_id: P,
|
|
1344
|
+
session_hash: f,
|
|
1345
|
+
fn_index: w
|
|
1346
|
+
};
|
|
1347
|
+
try {
|
|
1348
|
+
if (!l) throw Error("Could not resolve app config");
|
|
1349
|
+
"event_id" in t && await s(`${l.root}${x}/${re}`, {
|
|
1350
|
+
headers: { "Content-Type": "application/json" },
|
|
1351
|
+
method: "POST",
|
|
1352
|
+
body: JSON.stringify(t)
|
|
1353
|
+
}), await s(`${l.root}${x}/${ne}`, {
|
|
1354
|
+
headers: { "Content-Type": "application/json" },
|
|
1355
|
+
method: "POST",
|
|
1356
|
+
body: JSON.stringify(e)
|
|
1357
|
+
});
|
|
1358
|
+
} catch {
|
|
1359
|
+
console.warn("The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable.");
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
let he = async (e) => {
|
|
1363
|
+
await this._resolve_heartbeat(e);
|
|
1364
|
+
};
|
|
1365
|
+
async function ge(e) {
|
|
1366
|
+
if (!l) return;
|
|
1367
|
+
let t = e.render_id;
|
|
1368
|
+
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];
|
|
1369
|
+
let n = l.components.some((e) => e.type === "state"), r = l.dependencies.some((e) => e.targets.some((e) => e[1] === "unload"));
|
|
1370
|
+
l.connect_heartbeat = n || r, await he(l), R({
|
|
1371
|
+
type: "render",
|
|
1372
|
+
data: e,
|
|
1373
|
+
endpoint: M,
|
|
1374
|
+
fn_index: w
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
let _e = this.handle_blob(l.root, ae, T).then(async (e) => {
|
|
1378
|
+
let t = G(e, E, l.components, "input", !0);
|
|
1379
|
+
if (It(k, fe, t || []), N = {
|
|
1380
|
+
data: t || [],
|
|
1381
|
+
event_data: n,
|
|
1382
|
+
fn_index: w,
|
|
1383
|
+
trigger_id: r,
|
|
1384
|
+
...b.oauth_token && T?.oauth_token ? { oauth_token: b.oauth_token } : {}
|
|
1385
|
+
}, Te(w, l)) R({
|
|
1386
|
+
type: "status",
|
|
1387
|
+
endpoint: M,
|
|
1388
|
+
stage: "pending",
|
|
1389
|
+
queue: !1,
|
|
1390
|
+
fn_index: w,
|
|
1391
|
+
time: /* @__PURE__ */ new Date()
|
|
1392
|
+
}), v(`${l.root}${x}/run${M.startsWith("/") ? M : `/${M}`}${L ? "?" + L : ""}`, {
|
|
1393
|
+
...N,
|
|
1394
|
+
session_hash: f
|
|
1395
|
+
}, S).then(async ([e, t]) => {
|
|
1396
|
+
let i = e.data;
|
|
1397
|
+
if (t == 200) R({
|
|
1398
|
+
type: "data",
|
|
1399
|
+
endpoint: M,
|
|
1400
|
+
fn_index: w,
|
|
1401
|
+
data: G(i, E, l.components, "output", b.with_null_state),
|
|
1402
|
+
time: /* @__PURE__ */ new Date(),
|
|
1403
|
+
event_data: n,
|
|
1404
|
+
trigger_id: r
|
|
1405
|
+
}), e.render_config && await ge(e.render_config), R({
|
|
1406
|
+
type: "status",
|
|
1407
|
+
endpoint: M,
|
|
1408
|
+
fn_index: w,
|
|
1409
|
+
stage: "complete",
|
|
1410
|
+
eta: e.average_duration,
|
|
1411
|
+
queue: !1,
|
|
1412
|
+
time: /* @__PURE__ */ new Date()
|
|
1413
|
+
});
|
|
1414
|
+
else {
|
|
1415
|
+
let t = e?.error === y;
|
|
1416
|
+
R({
|
|
1417
|
+
type: "status",
|
|
1418
|
+
stage: "error",
|
|
1419
|
+
endpoint: M,
|
|
1420
|
+
fn_index: w,
|
|
1421
|
+
message: e.error,
|
|
1422
|
+
broken: t,
|
|
1423
|
+
queue: !1,
|
|
1424
|
+
time: /* @__PURE__ */ new Date()
|
|
1425
|
+
});
|
|
1426
|
+
}
|
|
1427
|
+
}).catch((e) => {
|
|
1428
|
+
R({
|
|
1429
|
+
type: "status",
|
|
1430
|
+
stage: "error",
|
|
1431
|
+
message: e.message,
|
|
1432
|
+
endpoint: M,
|
|
1433
|
+
fn_index: w,
|
|
1434
|
+
queue: !1,
|
|
1435
|
+
time: /* @__PURE__ */ new Date()
|
|
1436
|
+
});
|
|
1437
|
+
});
|
|
1438
|
+
else if (D == "sse") {
|
|
1439
|
+
R({
|
|
1440
|
+
type: "status",
|
|
1441
|
+
stage: "pending",
|
|
1442
|
+
queue: !0,
|
|
1443
|
+
endpoint: M,
|
|
1444
|
+
fn_index: w,
|
|
1445
|
+
time: /* @__PURE__ */ new Date()
|
|
1446
|
+
});
|
|
1447
|
+
var i = new URLSearchParams({
|
|
1448
|
+
fn_index: w.toString(),
|
|
1449
|
+
session_hash: f
|
|
1450
|
+
}).toString();
|
|
1451
|
+
let e = new URL(`${l.root}${x}/${u}?${L ? L + "&" : ""}${i}`);
|
|
1452
|
+
if (this.jwt && e.searchParams.set("__sign", this.jwt), A = this.stream(e), !A) return Promise.reject(/* @__PURE__ */ Error("Cannot connect to SSE endpoint: " + e.toString()));
|
|
1453
|
+
A.onmessage = async function(e) {
|
|
1454
|
+
let { type: t, status: i, data: a } = ve(JSON.parse(e.data), I[w]);
|
|
1455
|
+
if (t === "update" && i && !F) R({
|
|
1456
|
+
type: "status",
|
|
1457
|
+
endpoint: M,
|
|
1458
|
+
fn_index: w,
|
|
1459
|
+
time: /* @__PURE__ */ new Date(),
|
|
1460
|
+
...i
|
|
1461
|
+
}), i.stage === "error" && (A?.close(), V());
|
|
1462
|
+
else if (t === "data") {
|
|
1463
|
+
let [e, t] = await v(`${l.root}${x}/queue/data`, {
|
|
1464
|
+
...N,
|
|
1465
|
+
session_hash: f,
|
|
1466
|
+
event_id: P
|
|
1467
|
+
});
|
|
1468
|
+
t !== 200 && (R({
|
|
1469
|
+
type: "status",
|
|
1470
|
+
stage: "error",
|
|
1471
|
+
message: y,
|
|
1472
|
+
queue: !0,
|
|
1473
|
+
endpoint: M,
|
|
1474
|
+
fn_index: w,
|
|
1475
|
+
time: /* @__PURE__ */ new Date()
|
|
1476
|
+
}), A?.close(), V());
|
|
1477
|
+
} else t === "complete" ? F = i : t === "log" ? R({
|
|
1478
|
+
type: "log",
|
|
1479
|
+
title: a.title,
|
|
1480
|
+
log: a.log,
|
|
1481
|
+
level: a.level,
|
|
1482
|
+
endpoint: M,
|
|
1483
|
+
duration: a.duration,
|
|
1484
|
+
visible: a.visible,
|
|
1485
|
+
fn_index: w
|
|
1486
|
+
}) : (t === "generating" || t === "streaming") && R({
|
|
1487
|
+
type: "status",
|
|
1488
|
+
time: /* @__PURE__ */ new Date(),
|
|
1489
|
+
...i,
|
|
1490
|
+
stage: i?.stage,
|
|
1491
|
+
queue: !0,
|
|
1492
|
+
endpoint: M,
|
|
1493
|
+
fn_index: w
|
|
1494
|
+
});
|
|
1495
|
+
a && (R({
|
|
1496
|
+
type: "data",
|
|
1497
|
+
time: /* @__PURE__ */ new Date(),
|
|
1498
|
+
data: G(a.data, E, l.components, "output", b.with_null_state),
|
|
1499
|
+
endpoint: M,
|
|
1500
|
+
fn_index: w,
|
|
1501
|
+
event_data: n,
|
|
1502
|
+
trigger_id: r
|
|
1503
|
+
}), F && (R({
|
|
1504
|
+
type: "status",
|
|
1505
|
+
time: /* @__PURE__ */ new Date(),
|
|
1506
|
+
...F,
|
|
1507
|
+
stage: i?.stage,
|
|
1508
|
+
queue: !0,
|
|
1509
|
+
endpoint: M,
|
|
1510
|
+
fn_index: w
|
|
1511
|
+
}), A?.close(), V()));
|
|
1512
|
+
};
|
|
1513
|
+
} else if (D == "sse_v1" || D == "sse_v2" || D == "sse_v2.1" || D == "sse_v3") {
|
|
1514
|
+
R({
|
|
1515
|
+
type: "status",
|
|
1516
|
+
stage: "pending",
|
|
1517
|
+
queue: !0,
|
|
1518
|
+
endpoint: M,
|
|
1519
|
+
fn_index: w,
|
|
1520
|
+
time: /* @__PURE__ */ new Date()
|
|
1521
|
+
});
|
|
1522
|
+
let e = "";
|
|
1523
|
+
typeof window < "u" && typeof document < "u" && (e = window?.location?.hostname);
|
|
1524
|
+
let t = Ke(e);
|
|
1525
|
+
return (typeof window < "u" && typeof document < "u" && window.parent != window && t && window.supports_zerogpu_headers ? Ee("zerogpu-headers", t) : Promise.resolve(null)).then((e) => {
|
|
1526
|
+
let t = {
|
|
1527
|
+
...S,
|
|
1528
|
+
...e || {}
|
|
1529
|
+
};
|
|
1530
|
+
return v(`${l.root}${x}/${d}?${L}`, {
|
|
1531
|
+
...N,
|
|
1532
|
+
session_hash: f
|
|
1533
|
+
}, t);
|
|
1534
|
+
}).then(async ([e, t]) => {
|
|
1535
|
+
if (e.event_id && (j = e.event_id), t === 503) R({
|
|
1536
|
+
type: "status",
|
|
1537
|
+
stage: "error",
|
|
1538
|
+
message: ie,
|
|
1539
|
+
queue: !0,
|
|
1540
|
+
endpoint: M,
|
|
1541
|
+
fn_index: w,
|
|
1542
|
+
time: /* @__PURE__ */ new Date(),
|
|
1543
|
+
visible: !0
|
|
1544
|
+
}), V();
|
|
1545
|
+
else if (t === 422) R({
|
|
1546
|
+
type: "status",
|
|
1547
|
+
stage: "error",
|
|
1548
|
+
message: e.detail,
|
|
1549
|
+
queue: !0,
|
|
1550
|
+
endpoint: M,
|
|
1551
|
+
fn_index: w,
|
|
1552
|
+
code: "validation_error",
|
|
1553
|
+
time: /* @__PURE__ */ new Date(),
|
|
1554
|
+
visible: !0
|
|
1555
|
+
}), V();
|
|
1556
|
+
else if (t !== 200) {
|
|
1557
|
+
let t = e?.error === y;
|
|
1558
|
+
R({
|
|
1559
|
+
type: "status",
|
|
1560
|
+
stage: "error",
|
|
1561
|
+
broken: t,
|
|
1562
|
+
message: t ? y : e.detail || e.error,
|
|
1563
|
+
queue: !0,
|
|
1564
|
+
endpoint: M,
|
|
1565
|
+
fn_index: w,
|
|
1566
|
+
time: /* @__PURE__ */ new Date(),
|
|
1567
|
+
visible: !0
|
|
1568
|
+
}), V();
|
|
1569
|
+
} else {
|
|
1570
|
+
P = e.event_id, j = P;
|
|
1571
|
+
let t = async function(e) {
|
|
1572
|
+
try {
|
|
1573
|
+
let { type: t, status: n, data: r, original_msg: i } = ve(e, I[w]);
|
|
1574
|
+
if (t == "heartbeat") return;
|
|
1575
|
+
if (t === "update" && n && !F) R({
|
|
1576
|
+
type: "status",
|
|
1577
|
+
endpoint: M,
|
|
1578
|
+
fn_index: w,
|
|
1579
|
+
time: /* @__PURE__ */ new Date(),
|
|
1580
|
+
original_msg: i,
|
|
1581
|
+
...n
|
|
1582
|
+
});
|
|
1583
|
+
else if (t === "complete") F = n;
|
|
1584
|
+
else if (t == "unexpected_error" || t == "broken_connection") {
|
|
1585
|
+
console.error("Unexpected error", n?.message);
|
|
1586
|
+
let e = t === "broken_connection";
|
|
1587
|
+
R({
|
|
1588
|
+
type: "status",
|
|
1589
|
+
stage: "error",
|
|
1590
|
+
message: n?.message || "An Unexpected Error Occurred!",
|
|
1591
|
+
queue: !0,
|
|
1592
|
+
endpoint: M,
|
|
1593
|
+
broken: e,
|
|
1594
|
+
session_not_found: n?.session_not_found,
|
|
1595
|
+
fn_index: w,
|
|
1596
|
+
time: /* @__PURE__ */ new Date()
|
|
1597
|
+
});
|
|
1598
|
+
} else if (t === "log") {
|
|
1599
|
+
R({
|
|
1600
|
+
type: "log",
|
|
1601
|
+
title: r.title,
|
|
1602
|
+
log: r.log,
|
|
1603
|
+
level: r.level,
|
|
1604
|
+
endpoint: M,
|
|
1605
|
+
duration: r.duration,
|
|
1606
|
+
visible: r.visible,
|
|
1607
|
+
fn_index: w
|
|
1608
|
+
});
|
|
1609
|
+
return;
|
|
1610
|
+
} else (t === "generating" || t === "streaming") && (R({
|
|
1611
|
+
type: "status",
|
|
1612
|
+
time: /* @__PURE__ */ new Date(),
|
|
1613
|
+
...n,
|
|
1614
|
+
stage: n?.stage,
|
|
1615
|
+
queue: !0,
|
|
1616
|
+
endpoint: M,
|
|
1617
|
+
fn_index: w
|
|
1618
|
+
}), r && E.connection !== "stream" && [
|
|
1619
|
+
"sse_v2",
|
|
1620
|
+
"sse_v2.1",
|
|
1621
|
+
"sse_v3"
|
|
1622
|
+
].includes(D) && nt(ee, P, r));
|
|
1623
|
+
r && (R({
|
|
1624
|
+
type: "data",
|
|
1625
|
+
time: /* @__PURE__ */ new Date(),
|
|
1626
|
+
data: G(r.data, E, l.components, "output", b.with_null_state),
|
|
1627
|
+
endpoint: M,
|
|
1628
|
+
fn_index: w
|
|
1629
|
+
}), r.render_config && await ge(r.render_config), F && (R({
|
|
1630
|
+
type: "status",
|
|
1631
|
+
time: /* @__PURE__ */ new Date(),
|
|
1632
|
+
...F,
|
|
1633
|
+
stage: n?.stage,
|
|
1634
|
+
queue: !0,
|
|
1635
|
+
endpoint: M,
|
|
1636
|
+
fn_index: w
|
|
1637
|
+
}), V())), (n?.stage === "complete" || n?.stage === "error") && (_[P] && delete _[P], P in ee && delete ee[P], V());
|
|
1638
|
+
} catch (e) {
|
|
1639
|
+
console.error("Unexpected client exception", e), R({
|
|
1640
|
+
type: "status",
|
|
1641
|
+
stage: "error",
|
|
1642
|
+
message: "An Unexpected Error Occurred!",
|
|
1643
|
+
queue: !0,
|
|
1644
|
+
endpoint: M,
|
|
1645
|
+
fn_index: w,
|
|
1646
|
+
time: /* @__PURE__ */ new Date()
|
|
1647
|
+
}), [
|
|
1648
|
+
"sse_v2",
|
|
1649
|
+
"sse_v2.1",
|
|
1650
|
+
"sse_v3"
|
|
1651
|
+
].includes(D) && (tt(h, C.abort_controller), h.open = !1, V());
|
|
1652
|
+
}
|
|
1653
|
+
};
|
|
1654
|
+
P in g && (g[P].forEach((e) => t(e)), delete g[P]), _[P] = t, te.add(P), h.open || await this.open_stream();
|
|
1655
|
+
}
|
|
1656
|
+
});
|
|
1657
|
+
}
|
|
1658
|
+
});
|
|
1659
|
+
_e.catch((e) => {
|
|
1660
|
+
R({
|
|
1661
|
+
type: "status",
|
|
1662
|
+
stage: "error",
|
|
1663
|
+
message: e instanceof Error ? e.message : String(e),
|
|
1664
|
+
queue: !Te(w, l),
|
|
1665
|
+
endpoint: M,
|
|
1666
|
+
fn_index: w,
|
|
1667
|
+
time: /* @__PURE__ */ new Date()
|
|
1668
|
+
}), V();
|
|
1669
|
+
});
|
|
1670
|
+
let be = !1, z = [], B = [];
|
|
1671
|
+
function V() {
|
|
1672
|
+
for (be = !0; B.length > 0;) B.shift()({
|
|
1673
|
+
value: void 0,
|
|
1674
|
+
done: !0
|
|
1675
|
+
});
|
|
1676
|
+
}
|
|
1677
|
+
function H(e) {
|
|
1678
|
+
B.length > 0 ? B.shift()(e) : z.push(e);
|
|
1679
|
+
}
|
|
1680
|
+
function xe(e) {
|
|
1681
|
+
H(Bt(e)), V();
|
|
1682
|
+
}
|
|
1683
|
+
function U(e) {
|
|
1684
|
+
H({
|
|
1685
|
+
value: e,
|
|
1686
|
+
done: !1
|
|
1687
|
+
});
|
|
1688
|
+
}
|
|
1689
|
+
function W() {
|
|
1690
|
+
return z.length > 0 ? Promise.resolve(z.shift()) : be ? Promise.resolve({
|
|
1691
|
+
value: void 0,
|
|
1692
|
+
done: !0
|
|
1693
|
+
}) : new Promise((e) => B.push(e));
|
|
1694
|
+
}
|
|
1695
|
+
let Se = {
|
|
1696
|
+
[Symbol.asyncIterator]: () => Se,
|
|
1697
|
+
next: W,
|
|
1698
|
+
throw: async (e) => (xe(e), W()),
|
|
1699
|
+
return: async () => (V(), {
|
|
1700
|
+
value: void 0,
|
|
1701
|
+
done: !0
|
|
1702
|
+
}),
|
|
1703
|
+
cancel: me,
|
|
1704
|
+
send_chunk: (e) => {
|
|
1705
|
+
this.post_data(`${l.root}${x}/stream/${j}`, {
|
|
1706
|
+
...e,
|
|
1707
|
+
session_hash: this.session_hash
|
|
1708
|
+
});
|
|
1709
|
+
},
|
|
1710
|
+
close_stream: () => {
|
|
1711
|
+
this.post_data(`${l.root}${x}/stream/${j}/close`, {}), V();
|
|
1712
|
+
},
|
|
1713
|
+
event_id: () => j,
|
|
1714
|
+
wait_for_id: async () => (await _e, P)
|
|
1715
|
+
};
|
|
1716
|
+
return Se;
|
|
1717
|
+
} catch (e) {
|
|
1718
|
+
throw console.error("Submit function encountered an error:", e), e;
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
function Bt(e) {
|
|
1722
|
+
return { then: (t, n) => n(e) };
|
|
1723
|
+
}
|
|
1724
|
+
function Vt(e, t, n, r) {
|
|
1725
|
+
let i, a, o;
|
|
1726
|
+
if (typeof t == "number") i = t, a = e.unnamed_endpoints[i], o = r.dependencies.find((e) => e.id == t);
|
|
1727
|
+
else {
|
|
1728
|
+
let s = t.replace(/^\//, "");
|
|
1729
|
+
i = n[s], a = e.named_endpoints[t.trim()] ?? e.named_endpoints[`/${s}`], o = r.dependencies.find((e) => e.id == n[s]);
|
|
1730
|
+
}
|
|
1731
|
+
if (typeof i != "number" || !o) {
|
|
1732
|
+
let e = r.dependencies.filter((e) => e.api_name).map((e) => `"/${e.api_name}"`).join(", ");
|
|
1733
|
+
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.");
|
|
1734
|
+
}
|
|
1735
|
+
return {
|
|
1736
|
+
fn_index: i,
|
|
1737
|
+
endpoint_info: a,
|
|
1738
|
+
dependency: o
|
|
1739
|
+
};
|
|
1740
|
+
}
|
|
1741
|
+
//#endregion
|
|
1742
|
+
//#region src/client.ts
|
|
1743
|
+
var $ = class {
|
|
1744
|
+
app_reference;
|
|
1745
|
+
options;
|
|
1746
|
+
deep_link = null;
|
|
1747
|
+
config;
|
|
1748
|
+
api_prefix = "";
|
|
1749
|
+
api_info;
|
|
1750
|
+
api_map = {};
|
|
1751
|
+
session_hash = Math.random().toString(36).substring(2);
|
|
1752
|
+
jwt = !1;
|
|
1753
|
+
last_status = {};
|
|
1754
|
+
cookies = null;
|
|
1755
|
+
stream_status = { open: !1 };
|
|
1756
|
+
closed = !1;
|
|
1757
|
+
pending_stream_messages = {};
|
|
1758
|
+
pending_diff_streams = {};
|
|
1759
|
+
event_callbacks = {};
|
|
1760
|
+
unclosed_events = /* @__PURE__ */ new Set();
|
|
1761
|
+
heartbeat_event = null;
|
|
1762
|
+
abort_controller = null;
|
|
1763
|
+
stream_instance = null;
|
|
1764
|
+
current_payload;
|
|
1765
|
+
get_url_config(e = null) {
|
|
1766
|
+
if (!this.config) throw Error(b);
|
|
1767
|
+
e === null && (e = window.location.href);
|
|
1768
|
+
let t = (e) => e.replace(/^\/+|\/+$/g, ""), n = t(new URL(this.config.root).pathname), r = t(new URL(e).pathname), i;
|
|
1769
|
+
return i = r.startsWith(n) ? t(r.substring(n.length)) : "", this.get_page_config(i);
|
|
1770
|
+
}
|
|
1771
|
+
get_page_config(e) {
|
|
1772
|
+
if (!this.config) throw Error(b);
|
|
1773
|
+
let t = this.config;
|
|
1774
|
+
return e in t.page || (e = ""), {
|
|
1775
|
+
...t,
|
|
1776
|
+
current_page: e,
|
|
1777
|
+
layout: t.page[e].layout,
|
|
1778
|
+
components: t.components.filter((n) => t.page[e].components.includes(n.id)),
|
|
1779
|
+
dependencies: this.config.dependencies.filter((n) => t.page[e].dependencies.includes(n.id))
|
|
1780
|
+
};
|
|
1781
|
+
}
|
|
1782
|
+
fetch(e, t) {
|
|
1783
|
+
let n = new Headers(t?.headers || {});
|
|
1784
|
+
return this && this.cookies && n.append("Cookie", this.cookies), this && this.options.headers && new Headers(this.options.headers).forEach((e, t) => {
|
|
1785
|
+
n.append(t, e);
|
|
1786
|
+
}), fetch(e, {
|
|
1787
|
+
...t,
|
|
1788
|
+
headers: n
|
|
1789
|
+
});
|
|
1790
|
+
}
|
|
1791
|
+
stream(e) {
|
|
1792
|
+
let t = new Headers();
|
|
1793
|
+
return this && this.cookies && t.append("Cookie", this.cookies), this && this.options.headers && new Headers(this.options.headers).forEach((e, n) => {
|
|
1794
|
+
t.append(n, e);
|
|
1795
|
+
}), this && this.options.token && t.append("Authorization", `Bearer ${this.options.token}`), this.abort_controller = new AbortController(), this.stream_instance = at(e.toString(), {
|
|
1796
|
+
credentials: this.options.credentials ?? "same-origin",
|
|
1797
|
+
headers: t,
|
|
1798
|
+
signal: this.abort_controller.signal
|
|
1799
|
+
}), this.stream_instance;
|
|
1800
|
+
}
|
|
1801
|
+
view_api;
|
|
1802
|
+
upload_files;
|
|
1803
|
+
upload;
|
|
1804
|
+
handle_blob;
|
|
1805
|
+
post_data;
|
|
1806
|
+
submit;
|
|
1807
|
+
predict;
|
|
1808
|
+
open_stream;
|
|
1809
|
+
resolve_config;
|
|
1810
|
+
resolve_cookies;
|
|
1811
|
+
constructor(e, t = { events: ["data"] }) {
|
|
1812
|
+
this.app_reference = e, this.deep_link = t.query_params?.deep_link || null, t.events ||= ["data"], k(t), this.options = t, this.current_payload = {}, t.cookies && (this.cookies = t.cookies), this.view_api = be.bind(this), this.upload_files = z.bind(this), this.handle_blob = ke.bind(this), this.post_data = Ne.bind(this), this.submit = zt.bind(this), this.predict = Pe.bind(this), this.open_stream = et.bind(this), this.resolve_config = j.bind(this), this.resolve_cookies = N.bind(this), this.upload = H.bind(this), this.fetch = this.fetch.bind(this), this.handle_space_success = this.handle_space_success.bind(this), this.stream = this.stream.bind(this);
|
|
1813
|
+
}
|
|
1814
|
+
async init() {
|
|
1815
|
+
qe(), this.options.auth && await this.resolve_cookies(), await this._resolve_config().then((e) => e?.config && this._resolve_heartbeat(e.config));
|
|
1816
|
+
try {
|
|
1817
|
+
this.api_info = await this.view_api();
|
|
1818
|
+
} catch (e) {
|
|
1819
|
+
console.error(e.message);
|
|
1820
|
+
}
|
|
1821
|
+
this.api_map = fe(this.config?.dependencies || []);
|
|
1822
|
+
}
|
|
1823
|
+
async _resolve_heartbeat(e) {
|
|
1824
|
+
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 de(this.config.space_id, this.options.token, this.cookies))), e.space_id && this.options.token && (this.jwt = await de(e.space_id, this.options.token)), this.config && this.config.connect_heartbeat) {
|
|
1825
|
+
let e = new URL(`${this.config.root}${this.api_prefix}/${_}/${this.session_hash}`);
|
|
1826
|
+
this.jwt && e.searchParams.set("__sign", this.jwt), this.heartbeat_event ||= this.stream(e);
|
|
1827
|
+
}
|
|
1828
|
+
}
|
|
1829
|
+
static async connect(e, t = { events: ["data"] }) {
|
|
1830
|
+
let n = new this(e, t);
|
|
1831
|
+
return t.session_hash && (n.session_hash = t.session_hash), await n.init(), n;
|
|
1832
|
+
}
|
|
1833
|
+
async reconnect() {
|
|
1834
|
+
let e = new URL(`${this.config.root}${this.api_prefix}/${v}`), t;
|
|
1835
|
+
try {
|
|
1836
|
+
let n = await this.fetch(e);
|
|
1837
|
+
if (!n.ok) throw Error();
|
|
1838
|
+
t = (await n.json()).app_id;
|
|
1839
|
+
} catch {
|
|
1840
|
+
return "broken";
|
|
1841
|
+
}
|
|
1842
|
+
return t === this.config.app_id ? "connected" : "changed";
|
|
1843
|
+
}
|
|
1844
|
+
close() {
|
|
1845
|
+
this.closed = !0, tt(this.stream_status, this.abort_controller);
|
|
1846
|
+
}
|
|
1847
|
+
async refresh() {
|
|
1848
|
+
if (!this.config) throw Error(b);
|
|
1849
|
+
let e = await this.resolve_config(this.config.root, !1);
|
|
1850
|
+
if (!e) throw Error(b);
|
|
1851
|
+
this.config = e, this.api_prefix = e.api_prefix || "", this.api_map = fe(e.dependencies || []);
|
|
1852
|
+
try {
|
|
1853
|
+
this.api_info = await this.view_api();
|
|
1854
|
+
} catch (e) {
|
|
1855
|
+
console.error(S + e.message);
|
|
1856
|
+
}
|
|
1857
|
+
return this.get_url_config();
|
|
1858
|
+
}
|
|
1859
|
+
set_current_payload(e) {
|
|
1860
|
+
this.current_payload = e;
|
|
1861
|
+
}
|
|
1862
|
+
static async duplicate(e, t = { events: ["data"] }) {
|
|
1863
|
+
return Ve(e, t);
|
|
1864
|
+
}
|
|
1865
|
+
async _resolve_config() {
|
|
1866
|
+
let { http_protocol: e, host: t, space_id: n } = await R(this.app_reference, this.options.token), { status_callback: r } = this.options;
|
|
1867
|
+
n && r && await Fe(n, r);
|
|
1868
|
+
let i;
|
|
1869
|
+
try {
|
|
1870
|
+
let n = `${e}//${t}`;
|
|
1871
|
+
if (i = await this.resolve_config(n), !i) throw Error(b);
|
|
1872
|
+
return this.config_success(i);
|
|
1873
|
+
} catch (e) {
|
|
1874
|
+
if (n && r) K(n, L.test(n) ? "space_name" : "subdomain", this.handle_space_success);
|
|
1875
|
+
else throw r && r({
|
|
1876
|
+
status: "error",
|
|
1877
|
+
message: "Could not load this space.",
|
|
1878
|
+
load_status: "error",
|
|
1879
|
+
detail: "NOT_FOUND"
|
|
1880
|
+
}), e instanceof Error ? e : Error(String(e));
|
|
1881
|
+
}
|
|
1882
|
+
}
|
|
1883
|
+
async config_success(e) {
|
|
1884
|
+
if (this.config = e, this.api_prefix = e.api_prefix || "", e.run_history === !1 && jt({
|
|
1885
|
+
app_id: e.app_id,
|
|
1886
|
+
username: e.username
|
|
1887
|
+
}), this.config.auth_required) return this.prepare_return_obj();
|
|
1888
|
+
try {
|
|
1889
|
+
this.api_info = await this.view_api();
|
|
1890
|
+
} catch (e) {
|
|
1891
|
+
console.error(S + e.message);
|
|
1892
|
+
}
|
|
1893
|
+
return this.prepare_return_obj();
|
|
1894
|
+
}
|
|
1895
|
+
async handle_space_success(e) {
|
|
1896
|
+
if (!this) throw Error(b);
|
|
1897
|
+
let { status_callback: t } = this.options;
|
|
1898
|
+
if (t && t(e), e.status === "running") try {
|
|
1899
|
+
if (this.config = await this._resolve_config(), this.api_prefix = this?.config?.api_prefix || "", !this.config) throw Error(b);
|
|
1900
|
+
return await this.config_success(this.config);
|
|
1901
|
+
} catch (e) {
|
|
1902
|
+
throw t && t({
|
|
1903
|
+
status: "error",
|
|
1904
|
+
message: "Could not load this space.",
|
|
1905
|
+
load_status: "error",
|
|
1906
|
+
detail: "NOT_FOUND"
|
|
1907
|
+
}), e;
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
async component_server(e, t, n) {
|
|
1911
|
+
if (!this.config) throw Error(b);
|
|
1912
|
+
let r = {}, { token: i } = this.options, { session_hash: a } = this;
|
|
1913
|
+
i && (r.Authorization = `Bearer ${this.options.token}`);
|
|
1914
|
+
let o, s = this.config.components.find((t) => t.id === e);
|
|
1915
|
+
o = s?.props?.root_url ? s.props.root_url : this.config.root;
|
|
1916
|
+
let c;
|
|
1917
|
+
if (typeof n == "object" && n && "binary" in n) {
|
|
1918
|
+
let r = n;
|
|
1919
|
+
c = new FormData();
|
|
1920
|
+
for (let e in r.data) e !== "binary" && c.append(e, r.data[e]);
|
|
1921
|
+
c.set("component_id", e.toString()), c.set("fn_name", t), c.set("session_hash", a);
|
|
1922
|
+
} else c = JSON.stringify({
|
|
1923
|
+
data: n,
|
|
1924
|
+
component_id: e,
|
|
1925
|
+
fn_name: t,
|
|
1926
|
+
session_hash: a
|
|
1927
|
+
}), r["Content-Type"] = "application/json";
|
|
1928
|
+
i && (r.Authorization = `Bearer ${i}`);
|
|
1929
|
+
try {
|
|
1930
|
+
let e = await this.fetch(`${o}${this.api_prefix}/${te}/`, {
|
|
1931
|
+
method: "POST",
|
|
1932
|
+
body: c,
|
|
1933
|
+
headers: r,
|
|
1934
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
1935
|
+
});
|
|
1936
|
+
if (!e.ok) throw Error("Could not connect to component server: " + e.statusText);
|
|
1937
|
+
return await e.json();
|
|
1938
|
+
} catch (e) {
|
|
1939
|
+
console.warn(e);
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
set_cookies(e) {
|
|
1943
|
+
this.cookies = I(e).join("; ");
|
|
1944
|
+
}
|
|
1945
|
+
prepare_return_obj() {
|
|
1946
|
+
return {
|
|
1947
|
+
config: this.config,
|
|
1948
|
+
predict: this.predict,
|
|
1949
|
+
submit: this.submit,
|
|
1950
|
+
view_api: this.view_api,
|
|
1951
|
+
component_server: this.component_server
|
|
1952
|
+
};
|
|
1953
|
+
}
|
|
1954
|
+
};
|
|
1955
|
+
async function Ht(e, t = { events: ["data"] }) {
|
|
1956
|
+
return await $.connect(e, t);
|
|
1957
|
+
}
|
|
1958
|
+
async function Ut(e, t) {
|
|
1959
|
+
return await $.duplicate(e, t);
|
|
1960
|
+
}
|
|
1961
|
+
//#endregion
|
|
1962
|
+
export { $ as Client, U as FileData, ae as MISSING_CREDENTIALS_MSG, jt as clear_run_history, Ht as client, Pt as consume_run_history_replay, Mt as delete_run_history, Ut as duplicate, De as handle_file, Rt as on_run_history_change, Pe as predict, xe as prepare_files, At as read_run_history, ut as run_history_url, Nt as stage_run_history_replay, zt as submit, H as upload, z as upload_files };
|