Spaces:
Sleeping
Sleeping
| const We = "host", Te = "queue/data", Ze = "queue/join", ye = "upload", Ke = "login", K = "config", Ve = "info", Ye = "runtime", Qe = "sleeptime", Xe = "heartbeat", et = "component_server", tt = "reset", st = "cancel", nt = "app_id", Ae = "This application is currently busy. Please try again. ", M = "Connection errored out. ", D = "Could not resolve app config. ", it = "Could not get space status. ", ot = "Could not get API info. ", ue = "Space metadata could not be loaded. ", rt = "Invalid URL. A full URL path is required.", at = "Not authorized to access this space. ", xe = "Invalid credentials. Could not login. ", ct = "Login credentials are required to access this space.", ut = "File system access is only available in Node.js environments", Ce = "Root URL not found in client config", lt = "Error uploading file"; | |
| async function be(e, s, t) { | |
| try { | |
| return (await (await fetch(`https://huggingface.co/api/spaces/${e}/jwt`, { | |
| headers: { | |
| Authorization: `Bearer ${s}`, | |
| ...t ? { Cookie: t } : {} | |
| } | |
| })).json()).token || !1; | |
| } catch { | |
| return !1; | |
| } | |
| } | |
| function pt(e) { | |
| let s = {}; | |
| return e.forEach(({ api_name: t, id: n }) => { | |
| t && (s[t] = n); | |
| }), s; | |
| } | |
| async function dt(e) { | |
| const s = this.options.token ? { Authorization: `Bearer ${this.options.token}` } : {}; | |
| if (s["Content-Type"] = "application/json", typeof window < "u" && window.gradio_config && location.origin !== "http://localhost:9876") { | |
| if (window.gradio_config.current_page && (e = e.substring(0, e.lastIndexOf("/"))), window.gradio_config.dev_mode || typeof window < "u" && window?.BUILD_MODE === "dev") { | |
| let t = ae( | |
| e, | |
| this.deep_link ? K + "?deep_link=" + this.deep_link : K | |
| ); | |
| const n = await this.fetch(t, { | |
| headers: s, | |
| credentials: "include" | |
| }), i = await ve(n, !!this.options.auth); | |
| i.root = e || i.root, window.gradio_config = { | |
| ...i, | |
| current_page: window.gradio_config.current_page | |
| }; | |
| } | |
| return { ...window.gradio_config }; | |
| } else if (e) { | |
| let t = ae( | |
| e, | |
| this.deep_link ? K + "?deep_link=" + this.deep_link : K | |
| ); | |
| const n = await this.fetch(t, { | |
| headers: s, | |
| credentials: "include" | |
| }), i = await ve(n, !!this.options.auth); | |
| return i.root || (i.root = e), i; | |
| } | |
| throw new Error(D); | |
| } | |
| async function ve(e, s) { | |
| if (e?.status === 401 && !s) { | |
| const n = (await e.json())?.detail?.auth_message; | |
| throw new Error(n || ct); | |
| } else if (e?.status === 401 && s) | |
| throw new Error(xe); | |
| if (e?.status === 200) { | |
| let t = await e.json(); | |
| return t.dependencies?.forEach((n, i) => { | |
| n.id === void 0 && (n.id = i); | |
| }), t; | |
| } else if (e?.status === 401) | |
| throw new Error(at); | |
| throw new Error(D); | |
| } | |
| async function ht() { | |
| const { http_protocol: e, host: s } = await pe( | |
| this.app_reference, | |
| this.options.token | |
| ); | |
| try { | |
| if (this.options.auth) { | |
| const t = await De( | |
| e, | |
| s, | |
| this.options.auth, | |
| this.fetch, | |
| this.options.token | |
| ); | |
| t && this.set_cookies(t); | |
| } | |
| } catch (t) { | |
| throw Error(t.message); | |
| } | |
| } | |
| async function De(e, s, t, n, i) { | |
| const r = new FormData(); | |
| r.append("username", t?.[0]), r.append("password", t?.[1]); | |
| let o = {}; | |
| i && (o.Authorization = `Bearer ${i}`); | |
| const a = await n(`${e}//${s}/${Ke}`, { | |
| headers: o, | |
| method: "POST", | |
| body: r, | |
| credentials: "include" | |
| }); | |
| if (a.status === 200) | |
| return a.headers.get("set-cookie"); | |
| throw a.status === 401 ? new Error(xe) : new Error(ue); | |
| } | |
| function re(e) { | |
| if (e.startsWith("http")) { | |
| const { protocol: s, host: t, pathname: n } = new URL(e); | |
| return { | |
| ws_protocol: s === "https:" ? "wss" : "ws", | |
| http_protocol: s, | |
| host: t + (n !== "/" ? n : "") | |
| }; | |
| } | |
| return { | |
| ws_protocol: "wss", | |
| http_protocol: "https:", | |
| host: new URL(e).host | |
| }; | |
| } | |
| const Ne = (e) => { | |
| let s = []; | |
| return e.split(/,(?=\s*[^\s=;]+=[^\s=;]+)/).forEach((n) => { | |
| const [i, r] = n.split(";")[0].split("="); | |
| i && r && s.push(`${i.trim()}=${r.trim()}`); | |
| }), s; | |
| }, le = /^[a-zA-Z0-9_\-\.]+\/[a-zA-Z0-9_\-\.]+$/, ft = /.*hf\.space\/{0,1}.*$/; | |
| async function pe(e, s) { | |
| const t = {}; | |
| s && (t.Authorization = `Bearer ${s}`); | |
| const n = e.trim().replace(/\/$/, ""); | |
| if (le.test(n)) | |
| try { | |
| const r = (await (await fetch( | |
| `https://huggingface.co/api/spaces/${n}/${We}`, | |
| { headers: t } | |
| )).json()).host; | |
| return { | |
| space_id: e, | |
| ...re(r) | |
| }; | |
| } catch { | |
| throw new Error(ue); | |
| } | |
| if (ft.test(n)) { | |
| const { ws_protocol: i, http_protocol: r, host: o } = re(n); | |
| return { | |
| space_id: o.split("/")[0].replace(".hf.space", ""), | |
| ws_protocol: i, | |
| http_protocol: r, | |
| host: o | |
| }; | |
| } | |
| return { | |
| space_id: !1, | |
| ...re(n) | |
| }; | |
| } | |
| const ae = (...e) => { | |
| try { | |
| return e.reduce((s, t) => (s = s.replace(/\/+$/, ""), t = t.replace(/^\/+/, ""), new URL(t, s + "/").toString())); | |
| } catch { | |
| throw new Error(rt); | |
| } | |
| }; | |
| function _t(e, s, t) { | |
| const n = { | |
| named_endpoints: {}, | |
| unnamed_endpoints: {} | |
| }; | |
| return Object.keys(e).forEach((i) => { | |
| (i === "named_endpoints" || i === "unnamed_endpoints") && (n[i] = {}, Object.entries(e[i]).forEach( | |
| ([r, { parameters: o, returns: a }]) => { | |
| const u = s.dependencies.find( | |
| (c) => c.api_name === r || c.api_name === r.replace("/", "") | |
| )?.id || t[r.replace("/", "")] || -1, l = u !== -1 ? s.dependencies.find((c) => c.id == u)?.types : { generator: !1, cancel: !1 }; | |
| if (u !== -1 && s.dependencies.find((c) => c.id == u)?.inputs?.length !== o.length) { | |
| const c = s.dependencies.find((m) => m.id == u).inputs.map( | |
| (m) => s.components.find((w) => w.id === m)?.type | |
| ); | |
| try { | |
| c.forEach((m, w) => { | |
| if (m === "state") { | |
| const N = { | |
| component: "state", | |
| example: null, | |
| parameter_default: null, | |
| parameter_has_default: !0, | |
| parameter_name: null, | |
| hidden: !0 | |
| }; | |
| o.splice(w, 0, N); | |
| } | |
| }); | |
| } catch (m) { | |
| console.error(m); | |
| } | |
| } | |
| const p = (c, m, w, N) => ({ | |
| ...c, | |
| description: gt(c?.type, w), | |
| type: mt(c?.type, m, w, N) || "" | |
| }); | |
| n[i][r] = { | |
| parameters: o.map( | |
| (c) => p(c, c?.component, c?.serializer, "parameter") | |
| ), | |
| returns: a.map( | |
| (c) => p(c, c?.component, c?.serializer, "return") | |
| ), | |
| type: l | |
| }; | |
| } | |
| )); | |
| }), n; | |
| } | |
| function mt(e, s, t, n) { | |
| if (s === "Api") return e.type; | |
| switch (e?.type) { | |
| case "string": | |
| return "string"; | |
| case "boolean": | |
| return "boolean"; | |
| case "number": | |
| return "number"; | |
| } | |
| if (t === "JSONSerializable" || t === "StringSerializable") | |
| return "any"; | |
| if (t === "ListStringSerializable") | |
| return "string[]"; | |
| if (s === "Image") | |
| return n === "parameter" ? "Blob | File | Buffer" : "string"; | |
| if (t === "FileSerializable") | |
| return e?.type === "array" ? n === "parameter" ? "(Blob | File | Buffer)[]" : "{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}[]" : n === "parameter" ? "Blob | File | Buffer" : "{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}"; | |
| if (t === "GallerySerializable") | |
| return n === "parameter" ? "[(Blob | File | Buffer), (string | null)][]" : "[{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}, (string | null))][]"; | |
| } | |
| function gt(e, s) { | |
| return s === "GallerySerializable" ? "array of [file, label] tuples" : s === "ListStringSerializable" ? "array of strings" : s === "FileSerializable" ? "array of files or single file" : e?.description; | |
| } | |
| function Ee(e, s) { | |
| switch (e.msg) { | |
| case "send_data": | |
| return { type: "data" }; | |
| case "send_hash": | |
| return { type: "hash" }; | |
| case "queue_full": | |
| return { | |
| type: "update", | |
| status: { | |
| queue: !0, | |
| message: Ae, | |
| stage: "error", | |
| code: e.code, | |
| success: e.success | |
| } | |
| }; | |
| case "heartbeat": | |
| return { | |
| type: "heartbeat" | |
| }; | |
| case "unexpected_error": | |
| return { | |
| type: "unexpected_error", | |
| status: { | |
| queue: !0, | |
| message: e.message, | |
| session_not_found: e.session_not_found, | |
| stage: "error", | |
| success: !1 | |
| } | |
| }; | |
| case "broken_connection": | |
| return { | |
| type: "broken_connection", | |
| status: { | |
| queue: !0, | |
| message: e.message, | |
| stage: "error", | |
| success: !1 | |
| } | |
| }; | |
| case "estimation": | |
| return { | |
| type: "update", | |
| status: { | |
| queue: !0, | |
| stage: s || "pending", | |
| code: e.code, | |
| size: e.queue_size, | |
| position: e.rank, | |
| eta: e.rank_eta, | |
| success: e.success | |
| } | |
| }; | |
| case "progress": | |
| return { | |
| type: "update", | |
| status: { | |
| queue: !0, | |
| stage: "pending", | |
| code: e.code, | |
| progress_data: e.progress_data, | |
| success: e.success | |
| } | |
| }; | |
| case "log": | |
| return { type: "log", data: e }; | |
| case "process_generating": | |
| return { | |
| type: "generating", | |
| status: { | |
| queue: !0, | |
| message: e.success ? null : e.output.error, | |
| stage: e.success ? "generating" : "error", | |
| code: e.code, | |
| progress_data: e.progress_data, | |
| eta: e.average_duration, | |
| changed_state_ids: e.success ? e.output.changed_state_ids : void 0 | |
| }, | |
| data: e.success ? e.output : null | |
| }; | |
| case "process_streaming": | |
| return { | |
| type: "streaming", | |
| status: { | |
| queue: !0, | |
| message: e.output.error, | |
| stage: "streaming", | |
| time_limit: e.time_limit, | |
| code: e.code, | |
| progress_data: e.progress_data, | |
| eta: e.eta | |
| }, | |
| data: e.output | |
| }; | |
| case "process_completed": | |
| return "error" in e.output ? { | |
| type: "update", | |
| status: { | |
| queue: !0, | |
| title: e.output.title ?? "Error", | |
| message: e.output.error ?? "An error occurred", | |
| visible: e.output.visible, | |
| duration: e.output.duration, | |
| stage: "error", | |
| code: e.code, | |
| success: e.success | |
| } | |
| } : { | |
| type: "complete", | |
| status: { | |
| queue: !0, | |
| message: e.success ? void 0 : e.output.error, | |
| stage: e.success ? "complete" : "error", | |
| code: e.code, | |
| progress_data: e.progress_data, | |
| changed_state_ids: e.success ? e.output.changed_state_ids : void 0 | |
| }, | |
| data: e.success ? e.output : null | |
| }; | |
| case "process_starts": | |
| return { | |
| type: "update", | |
| status: { | |
| queue: !0, | |
| stage: "pending", | |
| code: e.code, | |
| size: e.rank, | |
| position: 0, | |
| success: e.success, | |
| eta: e.eta | |
| }, | |
| original_msg: "process_starts" | |
| }; | |
| } | |
| return { type: "none", status: { stage: "error", queue: !0 } }; | |
| } | |
| const wt = (e = [], s) => { | |
| const t = s ? s.parameters : []; | |
| if (Array.isArray(e)) | |
| return s && t.length > 0 && e.length > t.length && console.warn("Too many arguments provided for the endpoint."), e; | |
| const n = [], i = Object.keys(e); | |
| return t.forEach((r, o) => { | |
| if (e.hasOwnProperty(r.parameter_name)) | |
| n[o] = e[r.parameter_name]; | |
| else if (r.parameter_has_default) | |
| n[o] = r.parameter_default; | |
| else | |
| throw new Error( | |
| `No value provided for required parameter: ${r.parameter_name}` | |
| ); | |
| }), i.forEach((r) => { | |
| if (!t.some((o) => o.parameter_name === r)) | |
| throw new Error( | |
| `Parameter \`${r}\` is not a valid keyword argument. Please refer to the API for usage.` | |
| ); | |
| }), n.forEach((r, o) => { | |
| if (r === void 0 && !t[o].parameter_has_default) | |
| throw new Error( | |
| `No value provided for required parameter: ${t[o].parameter_name}` | |
| ); | |
| }), n; | |
| }; | |
| async function yt() { | |
| if (this.api_info) return this.api_info; | |
| const { token: e } = this.options, { config: s } = this, t = { "Content-Type": "application/json" }; | |
| if (e && (t.Authorization = `Bearer ${e}`), !!s) | |
| try { | |
| let n, i; | |
| if (typeof window < "u" && window.gradio_api_info) | |
| i = window.gradio_api_info; | |
| else { | |
| const r = ae(s.root, this.api_prefix, Ve); | |
| if (n = await this.fetch(r, { | |
| headers: t, | |
| credentials: "include" | |
| }), !n.ok) | |
| throw new Error(M); | |
| i = await n.json(); | |
| } | |
| return "api" in i && (i = i.api), i.named_endpoints["/predict"] && !i.unnamed_endpoints[0] && (i.unnamed_endpoints[0] = i.named_endpoints["/predict"]), _t(i, s, this.api_map); | |
| } catch (n) { | |
| throw new Error("Could not get API info. " + n.message); | |
| } | |
| } | |
| async function bt(e, s, t) { | |
| const n = {}; | |
| this?.options?.token && (n.Authorization = `Bearer ${this.options.token}`); | |
| const i = 1e3, r = []; | |
| let o; | |
| for (let a = 0; a < s.length; a += i) { | |
| const u = s.slice(a, a + i), l = new FormData(); | |
| u.forEach((c) => { | |
| l.append("files", c); | |
| }); | |
| try { | |
| const c = t ? `${e}${this.api_prefix}/${ye}?upload_id=${t}` : `${e}${this.api_prefix}/${ye}`; | |
| o = await this.fetch(c, { | |
| method: "POST", | |
| body: l, | |
| headers: n, | |
| credentials: "include" | |
| }); | |
| } catch (c) { | |
| throw new Error(M + c.message); | |
| } | |
| if (!o.ok) { | |
| const c = await o.text(); | |
| return { error: `HTTP ${o.status}: ${c}` }; | |
| } | |
| const p = await o.json(); | |
| p && r.push(...p); | |
| } | |
| return { files: r }; | |
| } | |
| const vt = { | |
| radix: 1e3, | |
| unit: ["b", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"] | |
| }, Et = { | |
| radix: 1024, | |
| unit: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"] | |
| }, St = { | |
| radix: 1024, | |
| unit: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"] | |
| }, Se = { | |
| si: vt, | |
| iec: Et, | |
| jedec: St | |
| }; | |
| function $t(e, s = 1, t = "jedec") { | |
| e = Math.abs(e); | |
| const { radix: n, unit: i } = Se[t] || Se.jedec; | |
| let r = 0; | |
| for (; e >= n; ) | |
| e /= n, ++r; | |
| return `${e.toFixed(s)} ${i[r]}`; | |
| } | |
| async function kt(e, s, t, n) { | |
| let i = (Array.isArray(e) ? e : [e]).map( | |
| (o) => o.blob | |
| ); | |
| const r = i.filter( | |
| (o) => o.size > (n ?? 1 / 0) | |
| ); | |
| if (r.length) | |
| throw new Error( | |
| `File(s) exceed the maximum allowed size of ${$t(n || 1 / 0)}: ${r.map((o) => `"${o.name}"`).join(", ")}` | |
| ); | |
| return await Promise.all( | |
| await this.upload_files(s, i, t).then( | |
| async (o) => { | |
| if (o.error) | |
| throw new Error(o.error); | |
| return o.files ? o.files.map((a, u) => new Q({ | |
| ...e[u], | |
| path: a, | |
| url: `${s}${this.api_prefix}/file=${a}` | |
| })) : []; | |
| } | |
| ) | |
| ); | |
| } | |
| async function Xt(e, s) { | |
| return e.map( | |
| (t) => new Q({ | |
| path: t.name, | |
| orig_name: t.name, | |
| blob: t, | |
| size: t.size, | |
| mime_type: t.type, | |
| is_stream: s | |
| }) | |
| ); | |
| } | |
| class Q { | |
| path; | |
| url; | |
| orig_name; | |
| size; | |
| blob; | |
| is_stream; | |
| mime_type; | |
| alt_text; | |
| b64; | |
| meta = { _type: "gradio.FileData" }; | |
| constructor({ | |
| path: s, | |
| url: t, | |
| orig_name: n, | |
| size: i, | |
| blob: r, | |
| is_stream: o, | |
| mime_type: a, | |
| alt_text: u, | |
| b64: l | |
| }) { | |
| this.path = s, this.url = t, this.orig_name = n, this.size = i, this.blob = t ? void 0 : r, this.is_stream = o, this.mime_type = a, this.alt_text = u, this.b64 = l; | |
| } | |
| } | |
| class Le { | |
| type; | |
| command; | |
| meta; | |
| fileData; | |
| constructor(s, t) { | |
| this.type = "command", this.command = s, this.meta = t; | |
| } | |
| } | |
| const Rt = typeof process < "u" && process.versions && process.versions.node; | |
| function $e(e, s, t) { | |
| for (; t.length > 1; ) { | |
| const i = t.shift(); | |
| if (typeof i == "string" || typeof i == "number") | |
| e = e[i]; | |
| else | |
| throw new Error("Invalid key type"); | |
| } | |
| const n = t.shift(); | |
| if (typeof n == "string" || typeof n == "number") | |
| e[n] = s; | |
| else | |
| throw new Error("Invalid key type"); | |
| } | |
| async function ce(e, s = void 0, t = [], n = !1, i = void 0) { | |
| if (Array.isArray(e)) { | |
| let r = []; | |
| return await Promise.all( | |
| e.map(async (o, a) => { | |
| let u = t.slice(); | |
| u.push(String(a)); | |
| const l = await ce( | |
| e[a], | |
| n ? i?.parameters[a]?.component || void 0 : s, | |
| u, | |
| !1, | |
| i | |
| ); | |
| r = r.concat(l); | |
| }) | |
| ), r; | |
| } else { | |
| if (globalThis.Buffer && e instanceof globalThis.Buffer || e instanceof Blob) | |
| return [ | |
| { | |
| path: t, | |
| blob: new Blob([e]), | |
| type: s | |
| } | |
| ]; | |
| if (typeof e == "object" && e !== null) { | |
| let r = []; | |
| for (const o of Object.keys(e)) { | |
| const a = [...t, o], u = e[o]; | |
| r = r.concat( | |
| await ce( | |
| u, | |
| void 0, | |
| a, | |
| !1, | |
| i | |
| ) | |
| ); | |
| } | |
| return r; | |
| } | |
| } | |
| return []; | |
| } | |
| function Ot(e, s) { | |
| let t = s?.dependencies?.find((n) => n.id == e)?.queue; | |
| return t != null ? !t : !s.enable_queue; | |
| } | |
| function Tt(e, s) { | |
| return new Promise((t, n) => { | |
| const i = new MessageChannel(); | |
| i.port1.onmessage = (({ data: r }) => { | |
| i.port1.close(), t(r); | |
| }), window.parent.postMessage(e, s, [i.port2]); | |
| }); | |
| } | |
| function es(e) { | |
| if (typeof e == "string") { | |
| if (e.startsWith("http://") || e.startsWith("https://")) | |
| return { | |
| path: e, | |
| url: e, | |
| orig_name: e.split("/").pop() ?? "unknown", | |
| meta: { _type: "gradio.FileData" } | |
| }; | |
| if (Rt) | |
| return new Le("upload_file", { | |
| path: e, | |
| name: e, | |
| orig_path: e | |
| }); | |
| } else { | |
| if (typeof File < "u" && e instanceof File) | |
| return new Blob([e]); | |
| if (e instanceof Buffer) | |
| return new Blob([e]); | |
| if (e instanceof Blob) | |
| return e; | |
| } | |
| throw new Error( | |
| "Invalid input: must be a URL, File, Blob, or Buffer object." | |
| ); | |
| } | |
| function V(e, s, t, n, i = !1) { | |
| if (n === "input" && !i) | |
| throw new Error("Invalid code path. Cannot skip state inputs for input."); | |
| if (n === "output" && i) | |
| return e; | |
| let r = [], o = 0; | |
| const a = n === "input" ? s.inputs : s.outputs; | |
| for (let u = 0; u < a.length; u++) { | |
| const l = a[u]; | |
| if (t.find((c) => c.id === l)?.type === "state") { | |
| if (i) | |
| if (e.length === a.length) { | |
| const c = e[o]; | |
| r.push(c), o++; | |
| } else | |
| r.push(null); | |
| else { | |
| o++; | |
| continue; | |
| } | |
| continue; | |
| } else { | |
| const c = e[o]; | |
| r.push(c), o++; | |
| } | |
| } | |
| return r; | |
| } | |
| async function At(e, s, t) { | |
| const n = this; | |
| await xt(n, s); | |
| const i = await ce( | |
| s, | |
| void 0, | |
| [], | |
| !0, | |
| t | |
| ); | |
| return (await Promise.all( | |
| i.map(async ({ path: o, blob: a, type: u }) => { | |
| if (!a) return { path: o, type: u }; | |
| const l = await n.upload_files(e, [a]), p = l.files && l.files[0]; | |
| return { | |
| path: o, | |
| file_url: p, | |
| type: u, | |
| name: typeof File < "u" && a instanceof File ? a?.name : void 0 | |
| }; | |
| }) | |
| )).forEach(({ path: o, file_url: a, type: u, name: l }) => { | |
| if (u === "Gallery") | |
| $e(s, a, o); | |
| else if (a) { | |
| const p = new Q({ path: a, orig_name: l }); | |
| $e(s, p, o); | |
| } | |
| }), s; | |
| } | |
| async function xt(e, s) { | |
| if (!(e.config?.root || e.config?.root_url)) | |
| throw new Error(Ce); | |
| await Pe(e, s); | |
| } | |
| async function Pe(e, s, t = []) { | |
| for (const n in s) | |
| s[n] instanceof Le ? await Ct(e, s, n) : typeof s[n] == "object" && s[n] !== null && await Pe(e, s[n], [...t, n]); | |
| } | |
| async function Ct(e, s, t) { | |
| let n = s[t]; | |
| const i = e.config?.root || e.config?.root_url; | |
| if (!i) | |
| throw new Error(Ce); | |
| try { | |
| let r, o; | |
| if (typeof process < "u" && process.versions && process.versions.node) { | |
| const p = await import("./__vite-browser-external-DYxpcVy9.js"); | |
| o = (await import("./__vite-browser-external-DYxpcVy9.js")).resolve(process.cwd(), n.meta.path), r = await p.readFile(o); | |
| } else | |
| throw new Error(ut); | |
| const a = new Blob([r], { | |
| type: "application/octet-stream" | |
| }), u = await e.upload_files(i, [a]), l = u.files && u.files[0]; | |
| if (l) { | |
| const p = new Q({ | |
| path: l, | |
| orig_name: n.meta.name || "" | |
| }); | |
| s[t] = p; | |
| } | |
| } catch (r) { | |
| console.error(lt, r); | |
| } | |
| } | |
| async function Dt(e, s, t) { | |
| const n = { "Content-Type": "application/json" }; | |
| this.options.token && (n.Authorization = `Bearer ${this.options.token}`); | |
| try { | |
| var i = await this.fetch(e, { | |
| method: "POST", | |
| body: JSON.stringify(s), | |
| headers: { ...n, ...t }, | |
| credentials: "include" | |
| }); | |
| } catch { | |
| return [{ error: M }, 500]; | |
| } | |
| let r, o; | |
| try { | |
| r = await i.json(), o = i.status; | |
| } catch (a) { | |
| r = { error: `Could not parse server response: ${a}` }, o = 500; | |
| } | |
| return [r, o]; | |
| } | |
| async function Nt(e, s = {}) { | |
| let t = !1, n = !1; | |
| if (!this.config) | |
| throw new Error("Could not resolve app config"); | |
| if (typeof e == "number") | |
| this.config.dependencies.find((i) => i.id == e); | |
| else { | |
| const i = e.replace(/^\//, ""); | |
| this.config.dependencies.find( | |
| (r) => r.id == this.api_map[i] | |
| ); | |
| } | |
| return new Promise(async (i, r) => { | |
| const o = this.submit(e, s, null, null, !0); | |
| let a; | |
| for await (const u of o) | |
| u.type === "data" && (n && i(a), t = !0, a = u), u.type === "status" && (u.stage === "error" && r(u), u.stage === "complete" && (n = !0, t && i(a))); | |
| }); | |
| } | |
| async function G(e, s, t) { | |
| let n = s === "subdomain" ? `https://huggingface.co/api/spaces/by-subdomain/${e}` : `https://huggingface.co/api/spaces/${e}`, i, r; | |
| try { | |
| if (i = await fetch(n), r = i.status, r !== 200) | |
| throw new Error(); | |
| i = await i.json(); | |
| } catch { | |
| t({ | |
| status: "error", | |
| load_status: "error", | |
| message: it, | |
| detail: "NOT_FOUND" | |
| }); | |
| return; | |
| } | |
| if (!i || r !== 200) return; | |
| const { | |
| runtime: { stage: o }, | |
| id: a | |
| } = i; | |
| switch (o) { | |
| case "STOPPED": | |
| case "SLEEPING": | |
| t({ | |
| status: "sleeping", | |
| load_status: "pending", | |
| message: "Space is asleep. Waking it up...", | |
| detail: o | |
| }), setTimeout(() => { | |
| G(e, s, t); | |
| }, 1e3); | |
| break; | |
| case "PAUSED": | |
| t({ | |
| status: "paused", | |
| load_status: "error", | |
| message: "This space has been paused by the author. If you would like to try this demo, consider duplicating the space.", | |
| detail: o, | |
| discussions_enabled: await ke(a) | |
| }); | |
| break; | |
| case "RUNNING": | |
| case "RUNNING_BUILDING": | |
| t({ | |
| status: "running", | |
| load_status: "complete", | |
| message: "Space is running.", | |
| detail: o | |
| }); | |
| break; | |
| case "BUILDING": | |
| t({ | |
| status: "building", | |
| load_status: "pending", | |
| message: "Space is building...", | |
| detail: o | |
| }), setTimeout(() => { | |
| G(e, s, t); | |
| }, 1e3); | |
| break; | |
| case "APP_STARTING": | |
| t({ | |
| status: "starting", | |
| load_status: "pending", | |
| message: "Space is starting...", | |
| detail: o | |
| }), setTimeout(() => { | |
| G(e, s, t); | |
| }, 1e3); | |
| break; | |
| default: | |
| t({ | |
| status: "space_error", | |
| load_status: "error", | |
| message: "This space is experiencing an issue.", | |
| detail: o, | |
| discussions_enabled: await ke(a) | |
| }); | |
| break; | |
| } | |
| } | |
| const Ie = async (e, s) => { | |
| let t = 0; | |
| const n = 12, i = 5e3; | |
| return new Promise((r) => { | |
| G( | |
| e, | |
| le.test(e) ? "space_name" : "subdomain", | |
| (o) => { | |
| s(o), o.status === "running" || o.status === "error" || o.status === "paused" || o.status === "space_error" ? r() : (o.status === "sleeping" || o.status === "building") && (t < n ? (t++, setTimeout(() => { | |
| Ie(e, s).then(r); | |
| }, i)) : r()); | |
| } | |
| ); | |
| }); | |
| }, Lt = /^(?=[^]*\b[dD]iscussions{0,1}\b)(?=[^]*\b[dD]isabled\b)[^]*$/; | |
| async function ke(e) { | |
| try { | |
| const s = await fetch( | |
| `https://huggingface.co/api/spaces/${e}/discussions`, | |
| { | |
| method: "HEAD" | |
| } | |
| ), t = s.headers.get("x-error-message"); | |
| return !(!s.ok || t && Lt.test(t)); | |
| } catch { | |
| return !1; | |
| } | |
| } | |
| async function Pt(e, s) { | |
| const t = {}; | |
| s && (t.Authorization = `Bearer ${s}`); | |
| try { | |
| const n = await fetch( | |
| `https://huggingface.co/api/spaces/${e}/${Ye}`, | |
| { headers: t } | |
| ); | |
| if (n.status !== 200) | |
| throw new Error("Space hardware could not be obtained."); | |
| const { hardware: i } = await n.json(); | |
| return i.current; | |
| } catch (n) { | |
| throw new Error(n.message); | |
| } | |
| } | |
| async function It(e, s, t) { | |
| const n = {}; | |
| t && (n.Authorization = `Bearer ${t}`); | |
| const i = { | |
| seconds: s | |
| }; | |
| try { | |
| const r = await fetch( | |
| `https://huggingface.co/api/spaces/${e}/${Qe}`, | |
| { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json", ...n }, | |
| body: JSON.stringify(i) | |
| } | |
| ); | |
| if (r.status !== 200) | |
| throw new 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." | |
| ); | |
| return await r.json(); | |
| } catch (r) { | |
| throw new Error(r.message); | |
| } | |
| } | |
| const Re = [ | |
| "cpu-basic", | |
| "cpu-upgrade", | |
| "cpu-xl", | |
| "t4-small", | |
| "t4-medium", | |
| "a10g-small", | |
| "a10g-large", | |
| "a10g-largex2", | |
| "a10g-largex4", | |
| "a100-large", | |
| "zero-a10g", | |
| "h100", | |
| "h100x8" | |
| ]; | |
| async function jt(e, s) { | |
| const { token: t, private: n, hardware: i, timeout: r, auth: o } = s; | |
| if (i && !Re.includes(i)) | |
| throw new Error( | |
| `Invalid hardware type provided. Valid types are: ${Re.map((g) => `"${g}"`).join(",")}.` | |
| ); | |
| const { http_protocol: a, host: u } = await pe(e, t); | |
| let l = null; | |
| if (o) { | |
| const g = await De( | |
| a, | |
| u, | |
| o, | |
| fetch | |
| ); | |
| g && (l = Ne(g)); | |
| } | |
| const p = { | |
| Authorization: `Bearer ${t}`, | |
| "Content-Type": "application/json", | |
| ...l ? { Cookie: l.join("; ") } : {} | |
| }, c = (await (await fetch("https://huggingface.co/api/whoami-v2", { | |
| headers: p | |
| })).json()).name, m = e.split("/")[1], w = { | |
| repository: `${c}/${m}` | |
| }; | |
| n && (w.private = !0); | |
| let N; | |
| try { | |
| i || (N = await Pt(e, t)); | |
| } catch (g) { | |
| throw Error(ue + g.message); | |
| } | |
| const f = i || N || "cpu-basic"; | |
| w.hardware = f; | |
| try { | |
| const g = await fetch( | |
| `https://huggingface.co/api/spaces/${e}/duplicate`, | |
| { | |
| method: "POST", | |
| headers: p, | |
| body: JSON.stringify(w) | |
| } | |
| ); | |
| if (g.status === 409) | |
| try { | |
| return await Y.connect(`${c}/${m}`, s); | |
| } catch (q) { | |
| throw console.error("Failed to connect Client instance:", q), q; | |
| } | |
| else if (g.status !== 200) | |
| throw new Error(g.statusText); | |
| const H = await g.json(); | |
| return await It(`${c}/${m}`, r || 300, t), await Y.connect( | |
| Ut(H.url), | |
| s | |
| ); | |
| } catch (g) { | |
| throw new Error(g); | |
| } | |
| } | |
| function Ut(e) { | |
| const s = /https:\/\/huggingface.co\/spaces\/([^/]+\/[^/]+)/, t = e.match(s); | |
| if (t) | |
| return t[1]; | |
| } | |
| class qt extends TransformStream { | |
| #e = ""; | |
| /** Constructs a new instance. */ | |
| constructor(s = { allowCR: !1 }) { | |
| super({ | |
| transform: (t, n) => { | |
| for (t = this.#e + t; ; ) { | |
| const i = t.indexOf(` | |
| `), r = s.allowCR ? t.indexOf("\r") : -1; | |
| if (r !== -1 && r !== t.length - 1 && (i === -1 || i - 1 > r)) { | |
| n.enqueue(t.slice(0, r)), t = t.slice(r + 1); | |
| continue; | |
| } | |
| if (i === -1) | |
| break; | |
| const o = t[i - 1] === "\r" ? i - 1 : i; | |
| n.enqueue(t.slice(0, o)), t = t.slice(i + 1); | |
| } | |
| this.#e = t; | |
| }, | |
| flush: (t) => { | |
| if (this.#e === "") | |
| return; | |
| const n = s.allowCR && this.#e.endsWith("\r") ? this.#e.slice(0, -1) : this.#e; | |
| t.enqueue(n); | |
| } | |
| }); | |
| } | |
| } | |
| function zt(e) { | |
| let s = new TextDecoderStream(), t = new qt({ allowCR: !0 }); | |
| return e.pipeThrough(s).pipeThrough(t); | |
| } | |
| function Bt(e) { | |
| let t = /[:]\s*/.exec(e), n = t && t.index; | |
| if (n) | |
| return [ | |
| e.substring(0, n), | |
| e.substring(n + t[0].length) | |
| ]; | |
| } | |
| function Oe(e, s, t) { | |
| e.get(s) || e.set(s, t); | |
| } | |
| async function* Ft(e, s) { | |
| if (!e.body) | |
| return; | |
| let t = zt(e.body), n, i = t.getReader(), r; | |
| for (; ; ) { | |
| if (s && s.aborted) | |
| return i.cancel(); | |
| if (n = await i.read(), n.done) | |
| return; | |
| if (!n.value) { | |
| r && (yield r), r = void 0; | |
| continue; | |
| } | |
| let [o, a] = Bt(n.value) || []; | |
| o === "data" ? (r ||= {}, r[o] = r[o] ? r[o] + ` | |
| ` + a : a) : o === "event" ? (r ||= {}, r[o] = a) : o === "id" ? (r ||= {}, r[o] = String(+a) === a ? +a : a) : o === "retry" && (r ||= {}, r[o] = +a || void 0); | |
| } | |
| } | |
| async function Gt(e, s) { | |
| let t = new Request(e, s); | |
| Oe(t.headers, "Accept", "text/event-stream"), Oe(t.headers, "Content-Type", "application/json"); | |
| let n = await fetch(t); | |
| if (!n.ok) | |
| throw n; | |
| return Ft(n, t.signal); | |
| } | |
| async function Mt() { | |
| let { | |
| event_callbacks: e, | |
| unclosed_events: s, | |
| pending_stream_messages: t, | |
| stream_status: n, | |
| config: i, | |
| jwt: r | |
| } = this; | |
| const o = this; | |
| if (!i) | |
| throw new Error("Could not resolve app config"); | |
| n.open = !0; | |
| let a = null, u = new URLSearchParams({ | |
| session_hash: this.session_hash | |
| }).toString(), l = new URL(`${i.root}${this.api_prefix}/${Te}?${u}`); | |
| if (r && l.searchParams.set("__sign", r), a = this.stream(l), !a) { | |
| console.warn("Cannot connect to SSE endpoint: " + l.toString()); | |
| return; | |
| } | |
| a.onmessage = async function(p) { | |
| let c = JSON.parse(p.data); | |
| if (c.msg === "close_stream") { | |
| de(n, o.abort_controller); | |
| return; | |
| } | |
| const m = c.event_id; | |
| if (!m) | |
| await Promise.all( | |
| Object.keys(e).map( | |
| (w) => e[w](c) | |
| ) | |
| ); | |
| else if (e[m] && i) { | |
| c.msg === "process_completed" && ["sse", "sse_v1", "sse_v2", "sse_v2.1", "sse_v3"].includes( | |
| i.protocol | |
| ) && s.delete(m); | |
| let w = e[m]; | |
| typeof window < "u" && typeof document < "u" ? setTimeout(w, 0, c) : w(c); | |
| } else | |
| t[m] || (t[m] = []), t[m].push(c); | |
| }, a.onerror = async function(p) { | |
| console.error(p), await Promise.all( | |
| Object.keys(e).map( | |
| (c) => e[c]({ | |
| msg: "broken_connection", | |
| message: M | |
| }) | |
| ) | |
| ); | |
| }; | |
| } | |
| function de(e, s) { | |
| e && (e.open = !1, s?.abort()); | |
| } | |
| function Ht(e, s, t) { | |
| !e[s] ? (e[s] = [], t.data.forEach((i, r) => { | |
| e[s][r] = i; | |
| })) : t.data.forEach((i, r) => { | |
| let o = Jt(e[s][r], i); | |
| e[s][r] = o, t.data[r] = o; | |
| }); | |
| } | |
| function Jt(e, s) { | |
| return s.forEach(([t, n, i]) => { | |
| e = Wt(e, n, t, i); | |
| }), e; | |
| } | |
| function Wt(e, s, t, n) { | |
| if (s.length === 0) { | |
| if (t === "replace") | |
| return n; | |
| if (t === "append") | |
| return e + n; | |
| throw new Error(`Unsupported action: ${t}`); | |
| } | |
| let i = e; | |
| for (let o = 0; o < s.length - 1; o++) | |
| i = i[s[o]]; | |
| const r = s[s.length - 1]; | |
| switch (t) { | |
| case "replace": | |
| i[r] = n; | |
| break; | |
| case "append": | |
| i[r] += n; | |
| break; | |
| case "add": | |
| Array.isArray(i) ? i.splice(Number(r), 0, n) : i[r] = n; | |
| break; | |
| case "delete": | |
| Array.isArray(i) ? i.splice(Number(r), 1) : delete i[r]; | |
| break; | |
| default: | |
| throw new Error(`Unknown action: ${t}`); | |
| } | |
| return e; | |
| } | |
| function Zt(e, s = {}) { | |
| const t = { | |
| close: () => { | |
| console.warn("Method not implemented."); | |
| }, | |
| onerror: null, | |
| onmessage: null, | |
| onopen: null, | |
| readyState: 0, | |
| url: e.toString(), | |
| withCredentials: !1, | |
| CONNECTING: 0, | |
| OPEN: 1, | |
| CLOSED: 2, | |
| addEventListener: () => { | |
| throw new Error("Method not implemented."); | |
| }, | |
| dispatchEvent: () => { | |
| throw new Error("Method not implemented."); | |
| }, | |
| removeEventListener: () => { | |
| throw new Error("Method not implemented."); | |
| } | |
| }; | |
| return Gt(e, s).then(async (n) => { | |
| t.readyState = t.OPEN; | |
| try { | |
| for await (const i of n) | |
| t.onmessage && t.onmessage(i); | |
| t.readyState = t.CLOSED; | |
| } catch (i) { | |
| t.onerror && t.onerror(i), t.readyState = t.CLOSED; | |
| } | |
| }).catch((n) => { | |
| console.error(n), t.onerror && t.onerror(n), t.readyState = t.CLOSED; | |
| }), t; | |
| } | |
| function Kt(e, s = {}, t, n, i, r) { | |
| try { | |
| let o = function(h) { | |
| (i || ze[h.type]) && p(h); | |
| }, a = function() { | |
| for (Me = !0; F.length > 0; ) | |
| F.shift()({ | |
| value: void 0, | |
| done: !0 | |
| }); | |
| }, u = function(h) { | |
| F.length > 0 ? F.shift()(h) : ie.push(h); | |
| }, l = function(h) { | |
| u(Vt(h)), a(); | |
| }, p = function(h) { | |
| u({ value: h, done: !1 }); | |
| }, c = function() { | |
| return ie.length > 0 ? Promise.resolve(ie.shift()) : new Promise((h) => F.push(h)); | |
| }; | |
| const { token: m } = this.options, { | |
| fetch: w, | |
| app_reference: N, | |
| config: f, | |
| session_hash: g, | |
| api_info: H, | |
| api_map: q, | |
| stream_status: X, | |
| pending_stream_messages: ee, | |
| pending_diff_streams: te, | |
| event_callbacks: se, | |
| unclosed_events: je, | |
| post_data: ne, | |
| options: J, | |
| api_prefix: R | |
| } = this, he = r || { "x-gradio-user": "api" }, Ue = this; | |
| if (!H) throw new Error("No API found"); | |
| if (!f) throw new Error("Could not resolve app config"); | |
| let { fn_index: d, endpoint_info: fe, dependency: z } = Yt( | |
| H, | |
| e, | |
| q, | |
| f | |
| ), qe = wt(s, fe), j, O = f.protocol ?? "ws"; | |
| if (O === "ws") | |
| throw new Error("WebSocket protocol is not supported in this version"); | |
| let U = "", Qt = () => U; | |
| const _ = typeof e == "number" ? "/predict" : e; | |
| let W, b = null, T = !1, _e = {}, B = typeof window < "u" && typeof document < "u" ? new URLSearchParams(window.location.search).toString() : ""; | |
| const ze = J?.events?.reduce( | |
| (h, k) => (h[k] = !0, h), | |
| {} | |
| ) || {}; | |
| async function Be() { | |
| let h = {}, k = {}; | |
| h = { event_id: b }, k = { event_id: b, session_hash: g, fn_index: d }; | |
| try { | |
| if (!f) | |
| throw new Error("Could not resolve app config"); | |
| "event_id" in k && await w(`${f.root}${R}/${st}`, { | |
| headers: { "Content-Type": "application/json" }, | |
| method: "POST", | |
| body: JSON.stringify(k) | |
| }), await w(`${f.root}${R}/${tt}`, { | |
| headers: { "Content-Type": "application/json" }, | |
| method: "POST", | |
| body: JSON.stringify(h) | |
| }); | |
| } catch { | |
| console.warn( | |
| "The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable." | |
| ); | |
| } | |
| } | |
| const Fe = async (h) => { | |
| await this._resolve_heartbeat(h); | |
| }; | |
| async function me(h) { | |
| if (!f) return; | |
| let k = h.render_id; | |
| f.components = [ | |
| ...f.components.filter((v) => v.props.rendered_in !== k), | |
| ...h.components | |
| ], f.dependencies = [ | |
| ...f.dependencies.filter((v) => v.rendered_in !== k), | |
| ...h.dependencies | |
| ]; | |
| const Z = f.components.some((v) => v.type === "state"), y = f.dependencies.some( | |
| (v) => v.targets.some((L) => L[1] === "unload") | |
| ); | |
| f.connect_heartbeat = Z || y, await Fe(f), o({ | |
| type: "render", | |
| data: h, | |
| endpoint: _, | |
| fn_index: d | |
| }); | |
| } | |
| const Ge = this.handle_blob( | |
| f.root, | |
| qe, | |
| fe | |
| ).then(async (h) => { | |
| if (W = { | |
| data: V( | |
| h, | |
| z, | |
| f.components, | |
| "input", | |
| !0 | |
| ) || [], | |
| event_data: t, | |
| fn_index: d, | |
| trigger_id: n | |
| }, Ot(d, f)) | |
| o({ | |
| type: "status", | |
| endpoint: _, | |
| stage: "pending", | |
| queue: !1, | |
| fn_index: d, | |
| time: /* @__PURE__ */ new Date() | |
| }), ne( | |
| `${f.root}${R}/run${_.startsWith("/") ? _ : `/${_}`}${B ? "?" + B : ""}`, | |
| { | |
| ...W, | |
| session_hash: g | |
| }, | |
| he | |
| ).then(async ([y, v]) => { | |
| const L = y.data; | |
| v == 200 ? (o({ | |
| type: "data", | |
| endpoint: _, | |
| fn_index: d, | |
| data: V( | |
| L, | |
| z, | |
| f.components, | |
| "output", | |
| J.with_null_state | |
| ), | |
| time: /* @__PURE__ */ new Date(), | |
| event_data: t, | |
| trigger_id: n | |
| }), y.render_config && await me(y.render_config), o({ | |
| type: "status", | |
| endpoint: _, | |
| fn_index: d, | |
| stage: "complete", | |
| eta: y.average_duration, | |
| queue: !1, | |
| time: /* @__PURE__ */ new Date() | |
| })) : o({ | |
| type: "status", | |
| stage: "error", | |
| endpoint: _, | |
| fn_index: d, | |
| message: y.error, | |
| queue: !1, | |
| time: /* @__PURE__ */ new Date() | |
| }); | |
| }).catch((y) => { | |
| o({ | |
| type: "status", | |
| stage: "error", | |
| message: y.message, | |
| endpoint: _, | |
| fn_index: d, | |
| queue: !1, | |
| time: /* @__PURE__ */ new Date() | |
| }); | |
| }); | |
| else if (O == "sse") { | |
| o({ | |
| type: "status", | |
| stage: "pending", | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d, | |
| time: /* @__PURE__ */ new Date() | |
| }); | |
| var Z = new URLSearchParams({ | |
| fn_index: d.toString(), | |
| session_hash: g | |
| }).toString(); | |
| let y = new URL( | |
| `${f.root}${R}/${Te}?${B ? B + "&" : ""}${Z}` | |
| ); | |
| if (this.jwt && y.searchParams.set("__sign", this.jwt), j = this.stream(y), !j) | |
| return Promise.reject( | |
| new Error("Cannot connect to SSE endpoint: " + y.toString()) | |
| ); | |
| j.onmessage = async function(v) { | |
| const L = JSON.parse(v.data), { type: P, status: A, data: x } = Ee( | |
| L, | |
| _e[d] | |
| ); | |
| if (P === "update" && A && !T) | |
| o({ | |
| type: "status", | |
| endpoint: _, | |
| fn_index: d, | |
| time: /* @__PURE__ */ new Date(), | |
| ...A | |
| }), A.stage === "error" && (j?.close(), a()); | |
| else if (P === "data") { | |
| let [C, I] = await ne( | |
| `${f.root}${R}/queue/data`, | |
| { | |
| ...W, | |
| session_hash: g, | |
| event_id: b | |
| } | |
| ); | |
| I !== 200 && (o({ | |
| type: "status", | |
| stage: "error", | |
| message: M, | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d, | |
| time: /* @__PURE__ */ new Date() | |
| }), j?.close(), a()); | |
| } else P === "complete" ? T = A : P === "log" ? o({ | |
| type: "log", | |
| title: x.title, | |
| log: x.log, | |
| level: x.level, | |
| endpoint: _, | |
| duration: x.duration, | |
| visible: x.visible, | |
| fn_index: d | |
| }) : (P === "generating" || P === "streaming") && o({ | |
| type: "status", | |
| time: /* @__PURE__ */ new Date(), | |
| ...A, | |
| stage: A?.stage, | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d | |
| }); | |
| x && (o({ | |
| type: "data", | |
| time: /* @__PURE__ */ new Date(), | |
| data: V( | |
| x.data, | |
| z, | |
| f.components, | |
| "output", | |
| J.with_null_state | |
| ), | |
| endpoint: _, | |
| fn_index: d, | |
| event_data: t, | |
| trigger_id: n | |
| }), T && (o({ | |
| type: "status", | |
| time: /* @__PURE__ */ new Date(), | |
| ...T, | |
| stage: A?.stage, | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d | |
| }), j?.close(), a())); | |
| }; | |
| } else if (O == "sse_v1" || O == "sse_v2" || O == "sse_v2.1" || O == "sse_v3") { | |
| o({ | |
| type: "status", | |
| stage: "pending", | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d, | |
| time: /* @__PURE__ */ new Date() | |
| }); | |
| let y = ""; | |
| typeof window < "u" && typeof document < "u" && (y = window?.location?.hostname); | |
| const L = y.includes(".dev.") ? `https://moon-${y.split(".")[1]}.dev.spaces.huggingface.tech` : "https://huggingface.co"; | |
| return (typeof window < "u" && typeof document < "u" && window.parent != window && window.supports_zerogpu_headers ? Tt("zerogpu-headers", L) : Promise.resolve(null)).then((C) => { | |
| const I = { ...he, ...C || {} }; | |
| return ne( | |
| `${f.root}${R}/${Ze}?${B}`, | |
| { | |
| ...W, | |
| session_hash: g | |
| }, | |
| I | |
| ); | |
| }).then(async ([C, I]) => { | |
| if (C.event_id && (U = C.event_id), I === 503) | |
| o({ | |
| type: "status", | |
| stage: "error", | |
| message: Ae, | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d, | |
| time: /* @__PURE__ */ new Date(), | |
| visible: !0 | |
| }); | |
| else if (I === 422) | |
| o({ | |
| type: "status", | |
| stage: "error", | |
| message: C.detail, | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d, | |
| code: "validation_error", | |
| time: /* @__PURE__ */ new Date(), | |
| visible: !0 | |
| }), a(); | |
| else if (I !== 200) | |
| o({ | |
| type: "status", | |
| stage: "error", | |
| broken: !1, | |
| message: C.detail, | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d, | |
| time: /* @__PURE__ */ new Date(), | |
| visible: !0 | |
| }); | |
| else { | |
| b = C.event_id, U = b; | |
| let we = async function(oe) { | |
| try { | |
| const { type: E, status: S, data: $, original_msg: He } = Ee( | |
| oe, | |
| _e[d] | |
| ); | |
| if (E == "heartbeat") | |
| return; | |
| if (E === "update" && S && !T) | |
| o({ | |
| type: "status", | |
| endpoint: _, | |
| fn_index: d, | |
| time: /* @__PURE__ */ new Date(), | |
| original_msg: He, | |
| ...S | |
| }); | |
| else if (E === "complete") | |
| T = S; | |
| else if (E == "unexpected_error" || E == "broken_connection") { | |
| console.error("Unexpected error", S?.message); | |
| const Je = E === "broken_connection"; | |
| o({ | |
| type: "status", | |
| stage: "error", | |
| message: S?.message || "An Unexpected Error Occurred!", | |
| queue: !0, | |
| endpoint: _, | |
| broken: Je, | |
| session_not_found: S?.session_not_found, | |
| fn_index: d, | |
| time: /* @__PURE__ */ new Date() | |
| }); | |
| } else if (E === "log") { | |
| o({ | |
| type: "log", | |
| title: $.title, | |
| log: $.log, | |
| level: $.level, | |
| endpoint: _, | |
| duration: $.duration, | |
| visible: $.visible, | |
| fn_index: d | |
| }); | |
| return; | |
| } else (E === "generating" || E === "streaming") && (o({ | |
| type: "status", | |
| time: /* @__PURE__ */ new Date(), | |
| ...S, | |
| stage: S?.stage, | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d | |
| }), $ && z.connection !== "stream" && ["sse_v2", "sse_v2.1", "sse_v3"].includes(O) && Ht(te, b, $)); | |
| $ && (o({ | |
| type: "data", | |
| time: /* @__PURE__ */ new Date(), | |
| data: V( | |
| $.data, | |
| z, | |
| f.components, | |
| "output", | |
| J.with_null_state | |
| ), | |
| endpoint: _, | |
| fn_index: d | |
| }), $.render_config && await me($.render_config), T && (o({ | |
| type: "status", | |
| time: /* @__PURE__ */ new Date(), | |
| ...T, | |
| stage: S?.stage, | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d | |
| }), a())), (S?.stage === "complete" || S?.stage === "error") && (se[b] && delete se[b], b in te && delete te[b]); | |
| } catch (E) { | |
| console.error("Unexpected client exception", E), o({ | |
| type: "status", | |
| stage: "error", | |
| message: "An Unexpected Error Occurred!", | |
| queue: !0, | |
| endpoint: _, | |
| fn_index: d, | |
| time: /* @__PURE__ */ new Date() | |
| }), ["sse_v2", "sse_v2.1", "sse_v3"].includes(O) && (de(X, Ue.abort_controller), X.open = !1, a()); | |
| } | |
| }; | |
| b in ee && (ee[b].forEach((oe) => we(oe)), delete ee[b]), se[b] = we, je.add(b), X.open || await this.open_stream(); | |
| } | |
| }); | |
| } | |
| }); | |
| let Me = !1; | |
| const ie = [], F = [], ge = { | |
| [Symbol.asyncIterator]: () => ge, | |
| next: c, | |
| throw: async (h) => (l(h), c()), | |
| return: async () => (a(), { value: void 0, done: !0 }), | |
| cancel: Be, | |
| send_chunk: (h) => { | |
| this.post_data(`${f.root}${R}/stream/${U}`, { | |
| ...h, | |
| session_hash: this.session_hash | |
| }); | |
| }, | |
| close_stream: () => { | |
| this.post_data( | |
| `${f.root}${R}/stream/${U}/close`, | |
| {} | |
| ), a(); | |
| }, | |
| event_id: () => U, | |
| wait_for_id: async () => (await Ge, b) | |
| }; | |
| return ge; | |
| } catch (o) { | |
| throw console.error("Submit function encountered an error:", o), o; | |
| } | |
| } | |
| function Vt(e) { | |
| return { | |
| then: (s, t) => t(e) | |
| }; | |
| } | |
| function Yt(e, s, t, n) { | |
| let i, r, o; | |
| if (typeof s == "number") | |
| i = s, r = e.unnamed_endpoints[i], o = n.dependencies.find((a) => a.id == s); | |
| else { | |
| const a = s.replace(/^\//, ""); | |
| i = t[a], r = e.named_endpoints[s.trim()], o = n.dependencies.find( | |
| (u) => u.id == t[a] | |
| ); | |
| } | |
| if (typeof i != "number") | |
| throw new Error( | |
| "There is no endpoint matching that name of fn_index matching that number." | |
| ); | |
| return { fn_index: i, endpoint_info: r, dependency: o }; | |
| } | |
| class Y { | |
| app_reference; | |
| options; | |
| deep_link = null; | |
| config; | |
| api_prefix = ""; | |
| api_info; | |
| api_map = {}; | |
| session_hash = Math.random().toString(36).substring(2); | |
| jwt = !1; | |
| last_status = {}; | |
| cookies = null; | |
| // streaming | |
| stream_status = { open: !1 }; | |
| closed = !1; | |
| pending_stream_messages = {}; | |
| pending_diff_streams = {}; | |
| event_callbacks = {}; | |
| unclosed_events = /* @__PURE__ */ new Set(); | |
| heartbeat_event = null; | |
| abort_controller = null; | |
| stream_instance = null; | |
| current_payload; | |
| get_url_config(s = null) { | |
| if (!this.config) | |
| throw new Error(D); | |
| s === null && (s = window.location.href); | |
| const t = (o) => o.replace(/^\/+|\/+$/g, ""); | |
| let n = t(new URL(this.config.root).pathname), i = t(new URL(s).pathname), r; | |
| return i.startsWith(n) ? r = t(i.substring(n.length)) : r = "", this.get_page_config(r); | |
| } | |
| get_page_config(s) { | |
| if (!this.config) | |
| throw new Error(D); | |
| let t = this.config; | |
| return s in t.page || (s = ""), { | |
| ...t, | |
| current_page: s, | |
| layout: t.page[s].layout, | |
| components: t.components.filter( | |
| (n) => t.page[s].components.includes(n.id) | |
| ), | |
| dependencies: this.config.dependencies.filter( | |
| (n) => t.page[s].dependencies.includes(n.id) | |
| ) | |
| }; | |
| } | |
| fetch(s, t) { | |
| const n = new Headers(t?.headers || {}); | |
| return this && this.cookies && n.append("Cookie", this.cookies), this && this.options.headers && new Headers(this.options.headers).forEach((r, o) => { | |
| n.append(o, r); | |
| }), fetch(s, { ...t, headers: n }); | |
| } | |
| stream(s) { | |
| const t = new Headers(); | |
| return this && this.cookies && t.append("Cookie", this.cookies), this && this.options.headers && new Headers(this.options.headers).forEach((i, r) => { | |
| t.append(r, i); | |
| }), this && this.options.token && t.append("Authorization", `Bearer ${this.options.token}`), this.abort_controller = new AbortController(), this.stream_instance = Zt(s.toString(), { | |
| credentials: "include", | |
| headers: t, | |
| signal: this.abort_controller.signal | |
| }), this.stream_instance; | |
| } | |
| view_api; | |
| upload_files; | |
| upload; | |
| handle_blob; | |
| post_data; | |
| submit; | |
| predict; | |
| open_stream; | |
| resolve_config; | |
| resolve_cookies; | |
| constructor(s, t = { events: ["data"] }) { | |
| this.app_reference = s, this.deep_link = t.query_params?.deep_link || null, t.events || (t.events = ["data"]), this.options = t, this.current_payload = {}, t.cookies && (this.cookies = t.cookies), this.view_api = yt.bind(this), this.upload_files = bt.bind(this), this.handle_blob = At.bind(this), this.post_data = Dt.bind(this), this.submit = Kt.bind(this), this.predict = Nt.bind(this), this.open_stream = Mt.bind(this), this.resolve_config = dt.bind(this), this.resolve_cookies = ht.bind(this), this.upload = kt.bind(this), this.fetch = this.fetch.bind(this), this.handle_space_success = this.handle_space_success.bind(this), this.stream = this.stream.bind(this); | |
| } | |
| async init() { | |
| this.options.auth && await this.resolve_cookies(), await this._resolve_config().then( | |
| ({ config: s }) => this._resolve_heartbeat(s) | |
| ), this.api_info = await this.view_api(), this.api_map = pt(this.config?.dependencies || []); | |
| } | |
| async _resolve_heartbeat(s) { | |
| if (s && (this.config = s, this.api_prefix = s.api_prefix || "", this.config && this.config.connect_heartbeat && this.config.space_id && this.options.token && (this.jwt = await be( | |
| this.config.space_id, | |
| this.options.token, | |
| this.cookies | |
| ))), s.space_id && this.options.token && (this.jwt = await be(s.space_id, this.options.token)), this.config && this.config.connect_heartbeat) { | |
| const t = new URL( | |
| `${this.config.root}${this.api_prefix}/${Xe}/${this.session_hash}` | |
| ); | |
| this.jwt && t.searchParams.set("__sign", this.jwt), this.heartbeat_event || (this.heartbeat_event = this.stream(t)); | |
| } | |
| } | |
| static async connect(s, t = { | |
| events: ["data"] | |
| }) { | |
| const n = new this(s, t); | |
| return t.session_hash && (n.session_hash = t.session_hash), await n.init(), n; | |
| } | |
| async reconnect() { | |
| const s = new URL( | |
| `${this.config.root}${this.api_prefix}/${nt}` | |
| ); | |
| let t; | |
| try { | |
| const n = await this.fetch(s); | |
| if (!n.ok) | |
| throw new Error(); | |
| t = (await n.json()).app_id; | |
| } catch { | |
| return "broken"; | |
| } | |
| return t !== this.config.app_id ? "changed" : "connected"; | |
| } | |
| close() { | |
| this.closed = !0, de(this.stream_status, this.abort_controller); | |
| } | |
| set_current_payload(s) { | |
| this.current_payload = s; | |
| } | |
| static async duplicate(s, t = { | |
| events: ["data"] | |
| }) { | |
| return jt(s, t); | |
| } | |
| async _resolve_config() { | |
| const { http_protocol: s, host: t, space_id: n } = await pe( | |
| this.app_reference, | |
| this.options.token | |
| ), { status_callback: i } = this.options; | |
| n && i && await Ie(n, i); | |
| let r; | |
| try { | |
| let o = `${s}//${t}`; | |
| if (r = await this.resolve_config(o), !r) | |
| throw new Error(D); | |
| return this.config_success(r); | |
| } catch (o) { | |
| if (n && i) | |
| G( | |
| n, | |
| le.test(n) ? "space_name" : "subdomain", | |
| this.handle_space_success | |
| ); | |
| else | |
| throw i && i({ | |
| status: "error", | |
| message: "Could not load this space.", | |
| load_status: "error", | |
| detail: "NOT_FOUND" | |
| }), Error(o); | |
| } | |
| } | |
| async config_success(s) { | |
| if (this.config = s, this.api_prefix = s.api_prefix || "", this.config.auth_required) | |
| return this.prepare_return_obj(); | |
| try { | |
| this.api_info = await this.view_api(); | |
| } catch (t) { | |
| console.error(ot + t.message); | |
| } | |
| return this.prepare_return_obj(); | |
| } | |
| async handle_space_success(s) { | |
| if (!this) | |
| throw new Error(D); | |
| const { status_callback: t } = this.options; | |
| if (t && t(s), s.status === "running") | |
| try { | |
| if (this.config = await this._resolve_config(), this.api_prefix = this?.config?.api_prefix || "", !this.config) | |
| throw new Error(D); | |
| return await this.config_success(this.config); | |
| } catch (n) { | |
| throw t && t({ | |
| status: "error", | |
| message: "Could not load this space.", | |
| load_status: "error", | |
| detail: "NOT_FOUND" | |
| }), n; | |
| } | |
| } | |
| async component_server(s, t, n) { | |
| if (!this.config) | |
| throw new Error(D); | |
| const i = {}, { token: r } = this.options, { session_hash: o } = this; | |
| r && (i.Authorization = `Bearer ${this.options.token}`); | |
| let a, u = this.config.components.find( | |
| (p) => p.id === s | |
| ); | |
| u?.props?.root_url ? a = u.props.root_url : a = this.config.root; | |
| let l; | |
| if ("binary" in n) { | |
| l = new FormData(); | |
| for (const p in n.data) | |
| p !== "binary" && l.append(p, n.data[p]); | |
| l.set("component_id", s.toString()), l.set("fn_name", t), l.set("session_hash", o); | |
| } else | |
| l = JSON.stringify({ | |
| data: n, | |
| component_id: s, | |
| fn_name: t, | |
| session_hash: o | |
| }), i["Content-Type"] = "application/json"; | |
| r && (i.Authorization = `Bearer ${r}`); | |
| try { | |
| const p = await this.fetch( | |
| `${a}${this.api_prefix}/${et}/`, | |
| { | |
| method: "POST", | |
| body: l, | |
| headers: i, | |
| credentials: "include" | |
| } | |
| ); | |
| if (!p.ok) | |
| throw new Error( | |
| "Could not connect to component server: " + p.statusText | |
| ); | |
| return await p.json(); | |
| } catch (p) { | |
| console.warn(p); | |
| } | |
| } | |
| set_cookies(s) { | |
| this.cookies = Ne(s).join("; "); | |
| } | |
| prepare_return_obj() { | |
| return { | |
| config: this.config, | |
| predict: this.predict, | |
| submit: this.submit, | |
| view_api: this.view_api, | |
| component_server: this.component_server | |
| }; | |
| } | |
| } | |
| async function ts(e, s = { | |
| events: ["data"] | |
| }) { | |
| return await Y.connect(e, s); | |
| } | |
| async function ss(e, s) { | |
| return await Y.duplicate(e, s); | |
| } | |
| export { | |
| Y as Client, | |
| Q as FileData, | |
| ct as MISSING_CREDENTIALS_MSG, | |
| ts as client, | |
| ss as duplicate, | |
| es as handle_file, | |
| Nt as predict, | |
| Xt as prepare_files, | |
| Kt as submit, | |
| kt as upload, | |
| bt as upload_files | |
| }; | |