1import * as svelte from './svelte/svelte.js'; 2 3true&&(function polyfill() { 4 const relList = document.createElement('link').relList; 5 if (relList && relList.supports && relList.supports('modulepreload')) { 6 return; 7 } 8 for (const link of document.querySelectorAll('link[rel="modulepreload"]')) { 9 processPreload(link); 10 } 11 new MutationObserver((mutations) => { 12 for (const mutation of mutations) { 13 if (mutation.type !== 'childList') { 14 continue; 15 } 16 for (const node of mutation.addedNodes) { 17 if (node.tagName === 'LINK' && node.rel === 'modulepreload') 18 processPreload(node); 19 } 20 } 21 }).observe(document, { childList: true, subtree: true }); 22 function getFetchOpts(link) { 23 const fetchOpts = {}; 24 if (link.integrity) 25 fetchOpts.integrity = link.integrity; 26 if (link.referrerPolicy) 27 fetchOpts.referrerPolicy = link.referrerPolicy; 28 if (link.crossOrigin === 'use-credentials') 29 fetchOpts.credentials = 'include'; 30 else if (link.crossOrigin === 'anonymous') 31 fetchOpts.credentials = 'omit'; 32 else 33 fetchOpts.credentials = 'same-origin'; 34 return fetchOpts; 35 } 36 function processPreload(link) { 37 if (link.ep) 38 // ep marker = processed 39 return; 40 link.ep = true; 41 // prepopulate the load record 42 const fetchOpts = getFetchOpts(link); 43 fetch(link.href, fetchOpts); 44 } 45}()); 46 47const scriptRel = 'modulepreload';const assetsURL = function(dep, importerUrl) { return new URL(dep, importerUrl).href };const seen = {};const __vitePreload = function preload(baseModule, deps, importerUrl) { 48 // @ts-expect-error true will be replaced with boolean later 49 if (!true || !deps || deps.length === 0) { 50 return baseModule(); 51 } 52 const links = document.getElementsByTagName('link'); 53 return Promise.all(deps.map((dep) => { 54 // @ts-expect-error assetsURL is declared before preload.toString() 55 dep = assetsURL(dep, importerUrl); 56 if (dep in seen) 57 return; 58 seen[dep] = true; 59 const isCss = dep.endsWith('.css'); 60 const cssSelector = isCss ? '[rel="stylesheet"]' : ''; 61 const isBaseRelative = !!importerUrl; 62 // check if the file is already preloaded by SSR markup 63 if (isBaseRelative) { 64 // When isBaseRelative is true then we have `importerUrl` and `dep` is 65 // already converted to an absolute URL by the `assetsURL` function 66 for (let i = links.length - 1; i >= 0; i--) { 67 const link = links[i]; 68 // The `links[i].href` is an absolute URL thanks to browser doing the work 69 // for us. See https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes:idl-domstring-5 70 if (link.href === dep && (!isCss || link.rel === 'stylesheet')) { 71 return; 72 } 73 } 74 } 75 else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) { 76 return; 77 } 78 const link = document.createElement('link'); 79 link.rel = isCss ? 'stylesheet' : scriptRel; 80 if (!isCss) { 81 link.as = 'script'; 82 link.crossOrigin = ''; 83 } 84 link.href = dep; 85 document.head.appendChild(link); 86 if (isCss) { 87 return new Promise((res, rej) => { 88 link.addEventListener('load', res); 89 link.addEventListener('error', () => rej(new Error(`Unable to preload CSS for ${dep}`))); 90 }); 91 } 92 })).then(() => baseModule()); 93}; 94 95const reset = ''; 96 97const global$1 = ''; 98 99const pollen = ''; 100 101const typography = ''; 102 103var fn = new Intl.Collator(0, { numeric: 1 }).compare; 104function semiver(a, b, bool) { 105 a = a.split("."); 106 b = b.split("."); 107 return fn(a[0], b[0]) || fn(a[1], b[1]) || (b[2] = b.slice(2).join("."), bool = /[.-]/.test(a[2] = a.slice(2).join(".")), bool == /[.-]/.test(b[2]) ? fn(a[2], b[2]) : bool ? -1 : 1); 108} 109function resolve_root(base_url, root_path, prioritize_base) { 110 if (root_path.startsWith("http://") || root_path.startsWith("https://")) { 111 return prioritize_base ? base_url : root_path; 112 } 113 return base_url + root_path; 114} 115function determine_protocol(endpoint) { 116 if (endpoint.startsWith("http")) { 117 const { protocol, host } = new URL(endpoint); 118 if (host.endsWith("hf.space")) { 119 return { 120 ws_protocol: "wss", 121 host, 122 http_protocol: protocol 123 }; 124 } 125 return { 126 ws_protocol: protocol === "https:" ? "wss" : "ws", 127 http_protocol: protocol, 128 host 129 }; 130 } else if (endpoint.startsWith("file:")) { 131 return { 132 ws_protocol: "ws", 133 http_protocol: "http:", 134 host: "lite.local" 135 // Special fake hostname only used for this case. This matches the hostname allowed in `is_self_host()` in `js/wasm/network/host.ts`. 136 }; 137 } 138 return { 139 ws_protocol: "wss", 140 http_protocol: "https:", 141 host: endpoint 142 }; 143} 144const RE_SPACE_NAME = /^[^\/]*\/[^\/]*$/; 145const RE_SPACE_DOMAIN = /.*hf\.space\/{0,1}$/; 146async function process_endpoint(app_reference, token) { 147 const headers = {}; 148 if (token) { 149 headers.Authorization = `Bearer ${token}`; 150 } 151 const _app_reference = app_reference.trim(); 152 if (RE_SPACE_NAME.test(_app_reference)) { 153 try { 154 const res = await fetch( 155 `https://huggingface.co/api/spaces/${_app_reference}/host`, 156 { headers } 157 ); 158 if (res.status !== 200) 159 throw new Error("Space metadata could not be loaded."); 160 const _host = (await res.json()).host; 161 return { 162 space_id: app_reference, 163 ...determine_protocol(_host) 164 }; 165 } catch (e) { 166 throw new Error("Space metadata could not be loaded." + e.message); 167 } 168 } 169 if (RE_SPACE_DOMAIN.test(_app_reference)) { 170 const { ws_protocol, http_protocol, host } = determine_protocol(_app_reference); 171 return { 172 space_id: host.replace(".hf.space", ""), 173 ws_protocol, 174 http_protocol, 175 host 176 }; 177 } 178 return { 179 space_id: false, 180 ...determine_protocol(_app_reference) 181 }; 182} 183function map_names_to_ids(fns) { 184 let apis = {}; 185 fns.forEach(({ api_name }, i) => { 186 if (api_name) 187 apis[api_name] = i; 188 }); 189 return apis; 190} 191const RE_DISABLED_DISCUSSION = /^(?=[^]*\b[dD]iscussions{0,1}\b)(?=[^]*\b[dD]isabled\b)[^]*$/; 192async function discussions_enabled(space_id) { 193 try { 194 const r = await fetch( 195 `https://huggingface.co/api/spaces/${space_id}/discussions`, 196 { 197 method: "HEAD" 198 } 199 ); 200 const error = r.headers.get("x-error-message"); 201 if (error && RE_DISABLED_DISCUSSION.test(error)) 202 return false;
203 return true; 204 } catch (e) { 205 return false; 206 } 207} 208function apply_edit(target, path, action, value) { 209 if (path.length === 0) { 210 if (action === "replace") { 211 return value; 212 } else if (action === "append") { 213 return target + value; 214 } 215 throw new Error(`Unsupported action: ${action}`); 216 } 217 let current = target; 218 for (let i = 0; i < path.length - 1; i++) { 219 current = current[path[i]]; 220 } 221 const last_path = path[path.length - 1]; 222 switch (action) { 223 case "replace": 224 current[last_path] = value; 225 break; 226 case "append": 227 current[last_path] += value; 228 break; 229 case "add": 230 if (Array.isArray(current)) { 231 current.splice(Number(last_path), 0, value); 232 } else { 233 current[last_path] = value; 234 } 235 break; 236 case "delete": 237 if (Array.isArray(current)) { 238 current.splice(Number(last_path), 1); 239 } else { 240 delete current[last_path]; 241 } 242 break; 243 default: 244 throw new Error(`Unknown action: ${action}`); 245 } 246 return target; 247} 248function apply_diff(obj, diff) { 249 diff.forEach(([action, path, value]) => { 250 obj = apply_edit(obj, path, action, value); 251 }); 252 return obj; 253} 254async function upload(file_data, root, upload_id, upload_fn = upload_files) { 255 let files = (Array.isArray(file_data) ? file_data : [file_data]).map( 256 (file_data2) => file_data2.blob 257 ); 258 return await Promise.all( 259 await upload_fn(root, files, void 0, upload_id).then( 260 async (response) => { 261 if (response.error) { 262 throw new Error(response.error); 263 } else { 264 if (response.files) { 265 return response.files.map((f, i) => { 266 const file = new FileData({ 267 ...file_data[i], 268 path: f, 269 url: root + "/file=" + f 270 }); 271 return file; 272 }); 273 } 274 return []; 275 } 276 } 277 ) 278 ); 279} 280async function prepare_files(files, is_stream) { 281 return files.map( 282 (f, i) => new FileData({ 283 path: f.name, 284 orig_name: f.name, 285 blob: f, 286 size: f.size, 287 mime_type: f.type, 288 is_stream 289 }) 290 ); 291} 292class FileData { 293 constructor({ 294 path, 295 url, 296 orig_name, 297 size, 298 blob, 299 is_stream, 300 mime_type, 301 alt_text 302 }) { 303 this.path = path; 304 this.url = url; 305 this.orig_name = orig_name; 306 this.size = size; 307 this.blob = url ? void 0 : blob; 308 this.is_stream = is_stream; 309 this.mime_type = mime_type; 310 this.alt_text = alt_text; 311 } 312} 313const QUEUE_FULL_MSG = "This application is too busy. Keep trying!"; 314const BROKEN_CONNECTION_MSG = "Connection errored out."; 315let NodeBlob; 316function api_factory(fetch_implementation, EventSource_factory) { 317 return { post_data: post_data2, upload_files: upload_files2, client: client2, handle_blob: handle_blob2 }; 318 async function post_data2(url, body, token) { 319 const headers = { "Content-Type": "application/json" }; 320 if (token) { 321 headers.Authorization = `Bearer ${token}`; 322 } 323 try { 324 var response = await fetch_implementation(url, { 325 method: "POST", 326 body: JSON.stringify(body), 327 headers 328 }); 329 } catch (e) { 330 return [{ error: BROKEN_CONNECTION_MSG }, 500]; 331 } 332 let output; 333 let status; 334 try { 335 output = await response.json(); 336 status = response.status; 337 } catch (e) { 338 output = { error: `Could not parse server response: ${e}` }; 339 status = 500; 340 } 341 return [output, status]; 342 } 343 async function upload_files2(root, files, token, upload_id) { 344 const headers = {}; 345 if (token) { 346 headers.Authorization = `Bearer ${token}`; 347 } 348 const chunkSize = 1e3; 349 const uploadResponses = []; 350 for (let i = 0; i < files.length; i += chunkSize) { 351 const chunk = files.slice(i, i + chunkSize); 352 const formData = new FormData(); 353 chunk.forEach((file) => { 354 formData.append("files", file); 355 }); 356 try { 357 const upload_url = upload_id ? `${root}/upload?upload_id=${upload_id}` : `${root}/upload`; 358 var response = await fetch_implementation(upload_url, { 359 method: "POST", 360 body: formData, 361 headers 362 }); 363 } catch (e) { 364 return { error: BROKEN_CONNECTION_MSG }; 365 } 366 const output = await response.json(); 367 uploadResponses.push(...output); 368 } 369 return { files: uploadResponses }; 370 } 371 async function client2(app_reference, options = {}) { 372 return new Promise(async (res) => { 373 const { status_callback, hf_token } = options; 374 const return_obj = { 375 predict, 376 submit, 377 view_api, 378 component_server 379 }; 380 if ((typeof window === "undefined" || !("WebSocket" in window)) && !global.Websocket) { 381 const ws = await __vitePreload(() => import('./wrapper-6f348d45-eeeb8bd1.js'),true?["./wrapper-6f348d45-eeeb8bd1.js","./__vite-browser-external-cb19bde2.js"]:void 0,import.meta.url); 382 NodeBlob = (await __vitePreload(() => import('./__vite-browser-external-cb19bde2.js').then(n => n._),true?[]:void 0,import.meta.url)).Blob; 383 global.WebSocket = ws.WebSocket; 384 } 385 const { ws_protocol, http_protocol, host, space_id } = await process_endpoint(app_reference, hf_token); 386 const session_hash = Math.random().toString(36).substring(2); 387 const last_status = {}; 388 let stream_open = false;
389 let pending_stream_messages = {}; 390 let pending_diff_streams = {}; 391 let event_stream = null; 392 const event_callbacks = {}; 393 const unclosed_events = /* @__PURE__ */ new Set(); 394 let config; 395 let api_map = {}; 396 let jwt = false; 397 if (hf_token && space_id) { 398 jwt = await get_jwt(space_id, hf_token); 399 } 400 async function config_success(_config) { 401 config = _config; 402 if (window.location.protocol === "https:") { 403 config.root = config.root.replace("http://", "https://"); 404 } 405 api_map = map_names_to_ids((_config == null ? void 0 : _config.dependencies) || []); 406 if (config.auth_required) { 407 return { 408 config, 409 ...return_obj 410 }; 411 } 412 try { 413 api = await view_api(config); 414 } catch (e) { 415 console.error(`Could not get api details: ${e.message}`); 416 } 417 return { 418 config, 419 ...return_obj 420 }; 421 } 422 let api; 423 async function handle_space_sucess(status) { 424 if (status_callback) 425 status_callback(status); 426 if (status.status === "running") 427 try { 428 config = await resolve_config( 429 fetch_implementation, 430 `${http_protocol}//${host}`, 431 hf_token 432 ); 433 const _config = await config_success(config); 434 res(_config); 435 } catch (e) { 436 console.error(e); 437 if (status_callback) { 438 status_callback({ 439 status: "error", 440 message: "Could not load this space.", 441 load_status: "error", 442 detail: "NOT_FOUND" 443 }); 444 } 445 } 446 } 447 try { 448 config = await resolve_config( 449 fetch_implementation, 450 `${http_protocol}//${host}`, 451 hf_token 452 ); 453 const _config = await config_success(config); 454 res(_config); 455 } catch (e) { 456 console.error(e); 457 if (space_id) { 458 check_space_status( 459 space_id, 460 RE_SPACE_NAME.test(space_id) ? "space_name" : "subdomain", 461 handle_space_sucess 462 ); 463 } else { 464 if (status_callback) 465 status_callback({ 466 status: "error", 467 message: "Could not load this space.", 468 load_status: "error", 469 detail: "NOT_FOUND" 470 }); 471 } 472 } 473 function predict(endpoint, data, event_data) { 474 let data_returned = false; 475 let status_complete = false; 476 let dependency; 477 if (typeof endpoint === "number") { 478 dependency = config.dependencies[endpoint]; 479 } else { 480 const trimmed_endpoint = endpoint.replace(/^\//, ""); 481 dependency = config.dependencies[api_map[trimmed_endpoint]]; 482 } 483 if (dependency.types.continuous) { 484 throw new Error( 485 "Cannot call predict on this function as it may run forever. Use submit instead" 486 ); 487 } 488 return new Promise((res2, rej) => { 489 const app = submit(endpoint, data, event_data); 490 let result; 491 app.on("data", (d) => { 492 if (status_complete) { 493 app.destroy(); 494 res2(d); 495 } 496 data_returned = true; 497 result = d; 498 }).on("status", (status) => { 499 if (status.stage === "error") 500 rej(status); 501 if (status.stage === "complete") { 502 status_complete = true; 503 if (data_returned) { 504 app.destroy(); 505 res2(result); 506 } 507 } 508 }); 509 }); 510 } 511 function submit(endpoint, data, event_data, trigger_id = null) { 512 let fn_index; 513 let api_info; 514 if (typeof endpoint === "number") { 515 fn_index = endpoint; 516 api_info = api.unnamed_endpoints[fn_index]; 517 } else { 518 const trimmed_endpoint = endpoint.replace(/^\//, ""); 519 fn_index = api_map[trimmed_endpoint]; 520 api_info = api.named_endpoints[endpoint.trim()]; 521 } 522 if (typeof fn_index !== "number") { 523 throw new Error( 524 "There is no endpoint matching that name of fn_index matching that number." 525 ); 526 } 527 let websocket; 528 let eventSource; 529 let protocol = config.protocol ?? "ws"; 530 const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint; 531 let payload; 532 let event_id = null; 533 let complete = false;
534 const listener_map = {}; 535 let url_params = ""; 536 if (typeof window !== "undefined") { 537 url_params = new URLSearchParams(window.location.search).toString(); 538 } 539 handle_blob2(`${config.root}`, data, api_info, hf_token).then( 540 (_payload) => { 541 payload = { 542 data: _payload || [], 543 event_data, 544 fn_index, 545 trigger_id 546 }; 547 if (skip_queue(fn_index, config)) { 548 fire_event({ 549 type: "status", 550 endpoint: _endpoint, 551 stage: "pending", 552 queue: false, 553 fn_index, 554 time: /* @__PURE__ */ new Date() 555 }); 556 post_data2( 557 `${config.root}/run${_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`}${url_params ? "?" + url_params : ""}`, 558 { 559 ...payload, 560 session_hash 561 }, 562 hf_token 563 ).then(([output, status_code]) => { 564 const data2 = output.data; 565 if (status_code == 200) { 566 fire_event({ 567 type: "data", 568 endpoint: _endpoint, 569 fn_index, 570 data: data2, 571 time: /* @__PURE__ */ new Date() 572 }); 573 fire_event({ 574 type: "status", 575 endpoint: _endpoint, 576 fn_index, 577 stage: "complete", 578 eta: output.average_duration, 579 queue: false, 580 time: /* @__PURE__ */ new Date() 581 }); 582 } else { 583 fire_event({ 584 type: "status", 585 stage: "error", 586 endpoint: _endpoint, 587 fn_index, 588 message: output.error, 589 queue: false, 590 time: /* @__PURE__ */ new Date() 591 }); 592 } 593 }).catch((e) => { 594 fire_event({ 595 type: "status", 596 stage: "error", 597 message: e.message, 598 endpoint: _endpoint, 599 fn_index, 600 queue: false, 601 time: /* @__PURE__ */ new Date() 602 }); 603 }); 604 } else if (protocol == "ws") { 605 fire_event({ 606 type: "status", 607 stage: "pending", 608 queue: true, 609 endpoint: _endpoint, 610 fn_index, 611 time: /* @__PURE__ */ new Date() 612 }); 613 let url = new URL(`${ws_protocol}://${resolve_root( 614 host, 615 config.path, 616 true 617 )} 618 /queue/join${url_params ? "?" + url_params : ""}`); 619 if (jwt) { 620 url.searchParams.set("__sign", jwt); 621 } 622 websocket = new WebSocket(url); 623 websocket.onclose = (evt) => { 624 if (!evt.wasClean) { 625 fire_event({ 626 type: "status", 627 stage: "error", 628 broken: true, 629 message: BROKEN_CONNECTION_MSG, 630 queue: true, 631 endpoint: _endpoint, 632 fn_index, 633 time: /* @__PURE__ */ new Date() 634 }); 635 } 636 }; 637 websocket.onmessage = function(event) { 638 const _data = JSON.parse(event.data); 639 const { type, status, data: data2 } = handle_message( 640 _data, 641 last_status[fn_index] 642 ); 643 if (type === "update" && status && !complete) { 644 fire_event({ 645 type: "status", 646 endpoint: _endpoint, 647 fn_index, 648 time: /* @__PURE__ */ new Date(), 649 ...status 650 }); 651 if (status.stage === "error") { 652 websocket.close(); 653 } 654 } else if (type === "hash") { 655 websocket.send(JSON.stringify({ fn_index, session_hash })); 656 return; 657 } else if (type === "data") { 658 websocket.send(JSON.stringify({ ...payload, session_hash })); 659 } else if (type === "complete") { 660 complete = status; 661 } else if (type === "log") { 662 fire_event({ 663 type: "log", 664 log: data2.log, 665 level: data2.level, 666 endpoint: _endpoint, 667 fn_index 668 }); 669 } else if (type === "generating") { 670 fire_event({ 671 type: "status", 672 time: /* @__PURE__ */ new Date(), 673 ...status, 674 stage: status == null ? void 0 : status.stage, 675 queue: true, 676 endpoint: _endpoint, 677 fn_index 678 }); 679 } 680 if (data2) { 681 fire_event({ 682 type: "data", 683 time: /* @__PURE__ */ new Date(), 684 data: data2.data, 685 endpoint: _endpoint, 686 fn_index 687 }); 688 if (complete) { 689 fire_event({ 690 type: "status", 691 time: /* @__PURE__ */ new Date(), 692 ...complete, 693 stage: status == null ? void 0 : status.stage, 694 queue: true, 695 endpoint: _endpoint, 696 fn_index 697 }); 698 websocket.close(); 699 } 700 } 701 }; 702 if (semiver(config.version || "2.0.0", "3.6") < 0) { 703 addEventListener( 704 "open", 705 () => websocket.send(JSON.stringify({ hash: session_hash })) 706 ); 707 } 708 } else if (protocol == "sse") { 709 fire_event({ 710 type: "status", 711 stage: "pending", 712 queue: true, 713 endpoint: _endpoint, 714 fn_index, 715 time: /* @__PURE__ */ new Date() 716 }); 717 var params = new URLSearchParams({ 718 fn_index: fn_index.toString(), 719 session_hash 720 }).toString(); 721 let url = new URL( 722 `${config.root}/queue/join?${url_params ? url_params + "&" : ""}${params}` 723 ); 724 eventSource = EventSource_factory(url); 725 eventSource.onmessage = async function(event) { 726 const _data = JSON.parse(event.data); 727 const { type, status, data: data2 } = handle_message( 728 _data, 729 last_status[fn_index] 730 ); 731 if (type === "update" && status && !complete) { 732 fire_event({ 733 type: "status", 734 endpoint: _endpoint, 735 fn_index, 736 time: /* @__PURE__ */ new Date(), 737 ...status 738 }); 739 if (status.stage === "error") { 740 eventSource.close(); 741 } 742 } else if (type === "data") { 743 event_id = _data.event_id; 744 let [_, status2] = await post_data2( 745 `${config.root}/queue/data`, 746 { 747 ...payload, 748 session_hash, 749 event_id 750 }, 751 hf_token 752 ); 753 if (status2 !== 200) { 754 fire_event({ 755 type: "status", 756 stage: "error", 757 message: BROKEN_CONNECTION_MSG, 758 queue: true, 759 endpoint: _endpoint, 760 fn_index, 761 time: /* @__PURE__ */ new Date() 762 }); 763 eventSource.close(); 764 } 765 } else if (type === "complete") { 766 complete = status; 767 } else if (type === "log") { 768 fire_event({ 769 type: "log", 770 log: data2.log, 771 level: data2.level, 772 endpoint: _endpoint, 773 fn_index 774 }); 775 } else if (type === "generating") { 776 fire_event({ 777 type: "status", 778 time: /* @__PURE__ */ new Date(), 779 ...status, 780 stage: status == null ? void 0 : status.stage, 781 queue: true, 782 endpoint: _endpoint, 783 fn_index 784 }); 785 } 786 if (data2) { 787 fire_event({ 788 type: "data", 789 time: /* @__PURE__ */ new Date(), 790 data: data2.data, 791 endpoint: _endpoint, 792 fn_index 793 }); 794 if (complete) { 795 fire_event({ 796 type: "status", 797 time: /* @__PURE__ */ new Date(), 798 ...complete, 799 stage: status == null ? void 0 : status.stage, 800 queue: true, 801 endpoint: _endpoint, 802 fn_index 803 }); 804 eventSource.close(); 805 } 806 } 807 }; 808 } else if (protocol == "sse_v1" || protocol == "sse_v2") { 809 fire_event({ 810 type: "status", 811 stage: "pending", 812 queue: true, 813 endpoint: _endpoint, 814 fn_index, 815 time: /* @__PURE__ */ new Date() 816 }); 817 post_data2( 818 `${config.root}/queue/join?${url_params}`, 819 { 820 ...payload, 821 session_hash 822 }, 823 hf_token 824 ).then(([response, status]) => { 825 if (status === 503) { 826 fire_event({ 827 type: "status", 828 stage: "error", 829 message: QUEUE_FULL_MSG, 830 queue: true, 831 endpoint: _endpoint, 832 fn_index, 833 time: /* @__PURE__ */ new Date() 834 }); 835 } else if (status !== 200) { 836 fire_event({ 837 type: "status", 838 stage: "error", 839 message: BROKEN_CONNECTION_MSG, 840 queue: true, 841 endpoint: _endpoint, 842 fn_index, 843 time: /* @__PURE__ */ new Date() 844 }); 845 } else { 846 event_id = response.event_id; 847 let callback = async function(_data) { 848 try { 849 const { type, status: status2, data: data2 } = handle_message( 850 _data, 851 last_status[fn_index] 852 ); 853 if (type == "heartbeat") { 854 return; 855 } 856 if (type === "update" && status2 && !complete) { 857 fire_event({ 858 type: "status", 859 endpoint: _endpoint, 860 fn_index, 861 time: /* @__PURE__ */ new Date(), 862 ...status2 863 }); 864 } else if (type === "complete") { 865 complete = status2; 866 } else if (type == "unexpected_error") { 867 console.error("Unexpected error", status2 == null ? void 0 : status2.message); 868 fire_event({ 869 type: "status", 870 stage: "error", 871 message: (status2 == null ? void 0 : status2.message) || "An Unexpected Error Occurred!", 872 queue: true, 873 endpoint: _endpoint, 874 fn_index, 875 time: /* @__PURE__ */ new Date() 876 }); 877 } else if (type === "log") { 878 fire_event({ 879 type: "log", 880 log: data2.log, 881 level: data2.level, 882 endpoint: _endpoint, 883 fn_index 884 }); 885 return; 886 } else if (type === "generating") { 887 fire_event({ 888 type: "status", 889 time: /* @__PURE__ */ new Date(), 890 ...status2, 891 stage: status2 == null ? void 0 : status2.stage, 892 queue: true, 893 endpoint: _endpoint, 894 fn_index 895 }); 896 if (data2 && protocol === "sse_v2") { 897 apply_diff_stream(event_id, data2); 898 } 899 } 900 if (data2) { 901 fire_event({ 902 type: "data", 903 time: /* @__PURE__ */ new Date(), 904 data: data2.data, 905 endpoint: _endpoint, 906 fn_index 907 }); 908 if (complete) { 909 fire_event({ 910 type: "status", 911 time: /* @__PURE__ */ new Date(), 912 ...complete, 913 stage: status2 == null ? void 0 : status2.stage, 914 queue: true, 915 endpoint: _endpoint, 916 fn_index 917 }); 918 } 919 } 920 if ((status2 == null ? void 0 : status2.stage) === "complete" || (status2 == null ? void 0 : status2.stage) === "error") { 921 if (event_callbacks[event_id]) { 922 delete event_callbacks[event_id]; 923 } 924 if (event_id in pending_diff_streams) {
925 delete pending_diff_streams[event_id]; 926 } 927 } 928 } catch (e) { 929 console.error("Unexpected client exception", e); 930 fire_event({ 931 type: "status", 932 stage: "error", 933 message: "An Unexpected Error Occurred!", 934 queue: true, 935 endpoint: _endpoint, 936 fn_index, 937 time: /* @__PURE__ */ new Date() 938 }); 939 close_stream(); 940 } 941 }; 942 if (event_id in pending_stream_messages) { 943 pending_stream_messages[event_id].forEach( 944 (msg) => callback(msg) 945 ); 946 delete pending_stream_messages[event_id]; 947 } 948 event_callbacks[event_id] = callback; 949 unclosed_events.add(event_id); 950 if (!stream_open) { 951 open_stream(); 952 } 953 } 954 }); 955 } 956 } 957 ); 958 function apply_diff_stream(event_id2, data2) { 959 let is_first_generation = !pending_diff_streams[event_id2]; 960 if (is_first_generation) { 961 pending_diff_streams[event_id2] = []; 962 data2.data.forEach((value, i) => { 963 pending_diff_streams[event_id2][i] = value; 964 }); 965 } else { 966 data2.data.forEach((value, i) => { 967 let new_data = apply_diff( 968 pending_diff_streams[event_id2][i], 969 value 970 ); 971 pending_diff_streams[event_id2][i] = new_data; 972 data2.data[i] = new_data; 973 }); 974 } 975 } 976 function fire_event(event) { 977 const narrowed_listener_map = listener_map; 978 const listeners = narrowed_listener_map[event.type] || []; 979 listeners == null ? void 0 : listeners.forEach((l) => l(event)); 980 } 981 function on(eventType, listener) { 982 const narrowed_listener_map = listener_map; 983 const listeners = narrowed_listener_map[eventType] || []; 984 narrowed_listener_map[eventType] = listeners; 985 listeners == null ? void 0 : listeners.push(listener); 986 return { on, off, cancel, destroy }; 987 } 988 function off(eventType, listener) { 989 const narrowed_listener_map = listener_map; 990 let listeners = narrowed_listener_map[eventType] || []; 991 listeners = listeners == null ? void 0 : listeners.filter((l) => l !== listener); 992 narrowed_listener_map[eventType] = listeners; 993 return { on, off, cancel, destroy }; 994 } 995 async function cancel() { 996 const _status = { 997 stage: "complete", 998 queue: false, 999 time: /* @__PURE__ */ new Date() 1000 }; 1001 complete = _status; 1002 fire_event({ 1003 ..._status, 1004 type: "status", 1005 endpoint: _endpoint, 1006 fn_index 1007 }); 1008 let cancel_request = {}; 1009 if (protocol === "ws") { 1010 if (websocket && websocket.readyState === 0) { 1011 websocket.addEventListener("open", () => { 1012 websocket.close(); 1013 }); 1014 } else { 1015 websocket.close(); 1016 } 1017 cancel_request = { fn_index, session_hash }; 1018 } else { 1019 eventSource.close(); 1020 cancel_request = { event_id }; 1021 } 1022 try { 1023 await fetch_implementation(`${config.root}/reset`, { 1024 headers: { "Content-Type": "application/json" }, 1025 method: "POST", 1026 body: JSON.stringify(cancel_request) 1027 }); 1028 } catch (e) { 1029 console.warn( 1030 "The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable." 1031 ); 1032 } 1033 } 1034 function destroy() { 1035 for (const event_type in listener_map) { 1036 listener_map[event_type].forEach((fn2) => { 1037 off(event_type, fn2); 1038 }); 1039 } 1040 } 1041 return { 1042 on, 1043 off, 1044 cancel, 1045 destroy 1046 }; 1047 } 1048 function open_stream() { 1049 stream_open = true; 1050 let params = new URLSearchParams({ 1051 session_hash 1052 }).toString(); 1053 let url = new URL(`${config.root}/queue/data?${params}`); 1054 event_stream = EventSource_factory(url); 1055 event_stream.onmessage = async function(event) { 1056 let _data = JSON.parse(event.data); 1057 const event_id = _data.event_id; 1058 if (!event_id) { 1059 await Promise.all( 1060 Object.keys(event_callbacks).map( 1061 (event_id2) => event_callbacks[event_id2](_data) 1062 ) 1063 ); 1064 } else if (event_callbacks[event_id]) { 1065 if (_data.msg === "process_completed") { 1066 unclosed_events.delete(event_id); 1067 if (unclosed_events.size === 0) { 1068 close_stream(); 1069 } 1070 } 1071 let fn2 = event_callbacks[event_id]; 1072 window.setTimeout(fn2, 0, _data); 1073 } else { 1074 if (!pending_stream_messages[event_id]) { 1075 pending_stream_messages[event_id] = []; 1076 } 1077 pending_stream_messages[event_id].push(_data); 1078 } 1079 }; 1080 event_stream.onerror = async function(event) { 1081 await Promise.all( 1082 Object.keys(event_callbacks).map( 1083 (event_id) => event_callbacks[event_id]({ 1084 msg: "unexpected_error", 1085 message: BROKEN_CONNECTION_MSG 1086 }) 1087 ) 1088 ); 1089 close_stream(); 1090 }; 1091 } 1092 function close_stream() { 1093 stream_open = false;
1094 event_stream == null ? void 0 : event_stream.close(); 1095 } 1096 async function component_server(component_id, fn_name, data) { 1097 var _a; 1098 const headers = { "Content-Type": "application/json" }; 1099 if (hf_token) { 1100 headers.Authorization = `Bearer ${hf_token}`; 1101 } 1102 let root_url; 1103 let component = config.components.find( 1104 (comp) => comp.id === component_id 1105 ); 1106 if ((_a = component == null ? void 0 : component.props) == null ? void 0 : _a.root_url) { 1107 root_url = component.props.root_url; 1108 } else { 1109 root_url = config.root; 1110 } 1111 const response = await fetch_implementation( 1112 `${root_url}/component_server/`, 1113 { 1114 method: "POST", 1115 body: JSON.stringify({ 1116 data, 1117 component_id, 1118 fn_name, 1119 session_hash 1120 }), 1121 headers 1122 } 1123 ); 1124 if (!response.ok) { 1125 throw new Error( 1126 "Could not connect to component server: " + response.statusText 1127 ); 1128 } 1129 const output = await response.json(); 1130 return output; 1131 } 1132 async function view_api(config2) { 1133 if (api) 1134 return api; 1135 const headers = { "Content-Type": "application/json" }; 1136 if (hf_token) { 1137 headers.Authorization = `Bearer ${hf_token}`; 1138 } 1139 let response; 1140 if (semiver(config2.version || "2.0.0", "3.30") < 0) { 1141 response = await fetch_implementation( 1142 "https://gradio-space-api-fetcher-v2.hf.space/api", 1143 { 1144 method: "POST", 1145 body: JSON.stringify({ 1146 serialize: false, 1147 config: JSON.stringify(config2) 1148 }), 1149 headers 1150 } 1151 ); 1152 } else { 1153 response = await fetch_implementation(`${config2.root}/info`, { 1154 headers 1155 }); 1156 } 1157 if (!response.ok) { 1158 throw new Error(BROKEN_CONNECTION_MSG); 1159 } 1160 let api_info = await response.json(); 1161 if ("api" in api_info) { 1162 api_info = api_info.api; 1163 } 1164 if (api_info.named_endpoints["/predict"] && !api_info.unnamed_endpoints["0"]) { 1165 api_info.unnamed_endpoints[0] = api_info.named_endpoints["/predict"]; 1166 } 1167 const x = transform_api_info(api_info, config2, api_map); 1168 return x; 1169 } 1170 }); 1171 } 1172 async function handle_blob2(endpoint, data, api_info, token) { 1173 const blob_refs = await walk_and_store_blobs( 1174 data, 1175 void 0, 1176 [], 1177 true, 1178 api_info 1179 ); 1180 return Promise.all( 1181 blob_refs.map(async ({ path, blob, type }) => { 1182 if (blob) { 1183 const file_url = (await upload_files2(endpoint, [blob], token)).files[0]; 1184 return { path, file_url, type, name: blob == null ? void 0 : blob.name }; 1185 } 1186 return { path, type }; 1187 }) 1188 ).then((r) => { 1189 r.forEach(({ path, file_url, type, name }) => { 1190 if (type === "Gallery") { 1191 update_object(data, file_url, path); 1192 } else if (file_url) { 1193 const file = new FileData({ path: file_url, orig_name: name }); 1194 update_object(data, file, path); 1195 } 1196 }); 1197 return data; 1198 }); 1199 } 1200} 1201const { post_data, upload_files, client, handle_blob } = api_factory( 1202 fetch, 1203 (...args) => new EventSource(...args) 1204); 1205function get_type(type, component, serializer, signature_type) { 1206 switch (type.type) { 1207 case "string": 1208 return "string"; 1209 case "boolean": 1210 return "boolean"; 1211 case "number": 1212 return "number"; 1213 } 1214 if (serializer === "JSONSerializable" || serializer === "StringSerializable") { 1215 return "any"; 1216 } else if (serializer === "ListStringSerializable") { 1217 return "string[]"; 1218 } else if (component === "Image") { 1219 return signature_type === "parameter" ? "Blob | File | Buffer" : "string"; 1220 } else if (serializer === "FileSerializable") { 1221 if ((type == null ? void 0 : type.type) === "array") { 1222 return signature_type === "parameter" ? "(Blob | File | Buffer)[]" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}[]`; 1223 } 1224 return signature_type === "parameter" ? "Blob | File | Buffer" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}`; 1225 } else if (serializer === "GallerySerializable") { 1226 return signature_type === "parameter" ? "[(Blob | File | Buffer), (string | null)][]" : `[{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}, (string | null))][]`; 1227 } 1228} 1229function get_description(type, serializer) { 1230 if (serializer === "GallerySerializable") { 1231 return "array of [file, label] tuples"; 1232 } else if (serializer === "ListStringSerializable") {
1233 return "array of strings"; 1234 } else if (serializer === "FileSerializable") { 1235 return "array of files or single file"; 1236 } 1237 return type.description; 1238} 1239function transform_api_info(api_info, config, api_map) { 1240 const new_data = { 1241 named_endpoints: {}, 1242 unnamed_endpoints: {} 1243 }; 1244 for (const key in api_info) { 1245 const cat = api_info[key]; 1246 for (const endpoint in cat) { 1247 const dep_index = config.dependencies[endpoint] ? endpoint : api_map[endpoint.replace("/", "")]; 1248 const info = cat[endpoint]; 1249 new_data[key][endpoint] = {}; 1250 new_data[key][endpoint].parameters = {}; 1251 new_data[key][endpoint].returns = {}; 1252 new_data[key][endpoint].type = config.dependencies[dep_index].types; 1253 new_data[key][endpoint].parameters = info.parameters.map( 1254 ({ label, component, type, serializer }) => ({ 1255 label, 1256 component, 1257 type: get_type(type, component, serializer, "parameter"), 1258 description: get_description(type, serializer) 1259 }) 1260 ); 1261 new_data[key][endpoint].returns = info.returns.map( 1262 ({ label, component, type, serializer }) => ({ 1263 label, 1264 component, 1265 type: get_type(type, component, serializer, "return"), 1266 description: get_description(type, serializer) 1267 }) 1268 ); 1269 } 1270 } 1271 return new_data; 1272} 1273async function get_jwt(space, token) { 1274 try { 1275 const r = await fetch(`https://huggingface.co/api/spaces/${space}/jwt`, { 1276 headers: { 1277 Authorization: `Bearer ${token}` 1278 } 1279 }); 1280 const jwt = (await r.json()).token; 1281 return jwt || false; 1282 } catch (e) { 1283 console.error(e); 1284 return false; 1285 } 1286} 1287function update_object(object, newValue, stack) { 1288 while (stack.length > 1) { 1289 object = object[stack.shift()]; 1290 } 1291 object[stack.shift()] = newValue; 1292} 1293async function walk_and_store_blobs(param, type = void 0, path = [], root = false, api_info = void 0) { 1294 if (Array.isArray(param)) { 1295 let blob_refs = []; 1296 await Promise.all( 1297 param.map(async (v, i) => { 1298 var _a; 1299 let new_path = path.slice(); 1300 new_path.push(i); 1301 const array_refs = await walk_and_store_blobs( 1302 param[i], 1303 root ? ((_a = api_info == null ? void 0 : api_info.parameters[i]) == null ? void 0 : _a.component) || void 0 : type, 1304 new_path, 1305 false, 1306 api_info 1307 ); 1308 blob_refs = blob_refs.concat(array_refs); 1309 }) 1310 ); 1311 return blob_refs; 1312 } else if (globalThis.Buffer && param instanceof globalThis.Buffer) { 1313 const is_image = type === "Image"; 1314 return [ 1315 { 1316 path, 1317 blob: is_image ? false : new NodeBlob([param]), 1318 type 1319 } 1320 ]; 1321 } else if (typeof param === "object") { 1322 let blob_refs = []; 1323 for (let key in param) { 1324 if (param.hasOwnProperty(key)) { 1325 let new_path = path.slice(); 1326 new_path.push(key); 1327 blob_refs = blob_refs.concat( 1328 await walk_and_store_blobs( 1329 param[key], 1330 void 0, 1331 new_path, 1332 false, 1333 api_info 1334 ) 1335 ); 1336 } 1337 } 1338 return blob_refs; 1339 } 1340 return []; 1341} 1342function skip_queue(id, config) { 1343 var _a, _b, _c, _d; 1344 return !(((_b = (_a = config == null ? void 0 : config.dependencies) == null ? void 0 : _a[id]) == null ? void 0 : _b.queue) === null ? config.enable_queue : (_d = (_c = config == null ? void 0 : config.dependencies) == null ? void 0 : _c[id]) == null ? void 0 : _d.queue) || false; 1345} 1346async function resolve_config(fetch_implementation, endpoint, token) { 1347 const headers = {}; 1348 if (token) { 1349 headers.Authorization = `Bearer ${token}`; 1350 } 1351 if (typeof window !== "undefined" && window.gradio_config && location.origin !== "http://localhost:9876" && !window.gradio_config.dev_mode) { 1352 const path = window.gradio_config.root; 1353 const config = window.gradio_config; 1354 config.root = resolve_root(endpoint, config.root, false); 1355 return { ...config, path }; 1356 } else if (endpoint) { 1357 let response = await fetch_implementation(`${endpoint}/config`, { 1358 headers 1359 }); 1360 if (response.status === 200) { 1361 const config = await response.json(); 1362 config.path = config.path ?? ""; 1363 config.root = endpoint; 1364 return config; 1365 } 1366 throw new Error("Could not get config."); 1367 } 1368 throw new Error("No config or app endpoint found"); 1369} 1370async function check_space_status(id, type, status_callback) { 1371 let endpoint = type === "subdomain" ? `https://huggingface.co/api/spaces/by-subdomain/${id}` : `https://huggingface.co/api/spaces/${id}`; 1372 let response; 1373 let _status; 1374 try { 1375 response = await fetch(endpoint); 1376 _status = response.status; 1377 if (_status !== 200) { 1378 throw new Error(); 1379 } 1380 response = await response.json(); 1381 } catch (e) { 1382 status_callback({ 1383 status: "error", 1384 load_status: "error", 1385 message: "Could not get space status", 1386 detail: "NOT_FOUND" 1387 }); 1388 return; 1389 } 1390 if (!response || _status !== 200) 1391 return; 1392 const { 1393 runtime: { stage }, 1394 id: space_name 1395 } = response; 1396 switch (stage) { 1397 case "STOPPED": 1398 case "SLEEPING": 1399 status_callback({ 1400 status: "sleeping", 1401 load_status: "pending", 1402 message: "Space is asleep. Waking it up...", 1403 detail: stage 1404 }); 1405 setTimeout(() => { 1406 check_space_status(id, type, status_callback); 1407 }, 1e3); 1408 break; 1409 case "PAUSED": 1410 status_callback({ 1411 status: "paused", 1412 load_status: "error", 1413 message: "This space has been paused by the author. If you would like to try this demo, consider duplicating the space.", 1414 detail: stage, 1415 discussions_enabled: await discussions_enabled(space_name) 1416 }); 1417 break; 1418 case "RUNNING":
1419 case "RUNNING_BUILDING": 1420 status_callback({ 1421 status: "running", 1422 load_status: "complete", 1423 message: "", 1424 detail: stage 1425 }); 1426 break; 1427 case "BUILDING": 1428 status_callback({ 1429 status: "building", 1430 load_status: "pending", 1431 message: "Space is building...", 1432 detail: stage 1433 }); 1434 setTimeout(() => { 1435 check_space_status(id, type, status_callback); 1436 }, 1e3); 1437 break; 1438 default: 1439 status_callback({ 1440 status: "space_error", 1441 load_status: "error", 1442 message: "This space is experiencing an issue.", 1443 detail: stage, 1444 discussions_enabled: await discussions_enabled(space_name) 1445 }); 1446 break; 1447 } 1448} 1449function handle_message(data, last_status) { 1450 const queue = true; 1451 switch (data.msg) { 1452 case "send_data": 1453 return { type: "data" }; 1454 case "send_hash": 1455 return { type: "hash" }; 1456 case "queue_full": 1457 return { 1458 type: "update", 1459 status: { 1460 queue, 1461 message: QUEUE_FULL_MSG, 1462 stage: "error", 1463 code: data.code, 1464 success: data.success 1465 } 1466 }; 1467 case "heartbeat": 1468 return { 1469 type: "heartbeat" 1470 }; 1471 case "unexpected_error": 1472 return { 1473 type: "unexpected_error", 1474 status: { 1475 queue, 1476 message: data.message, 1477 stage: "error", 1478 success: false 1479 } 1480 }; 1481 case "estimation": 1482 return { 1483 type: "update", 1484 status: { 1485 queue, 1486 stage: last_status || "pending", 1487 code: data.code, 1488 size: data.queue_size, 1489 position: data.rank, 1490 eta: data.rank_eta, 1491 success: data.success 1492 } 1493 }; 1494 case "progress": 1495 return { 1496 type: "update", 1497 status: { 1498 queue, 1499 stage: "pending", 1500 code: data.code, 1501 progress_data: data.progress_data, 1502 success: data.success 1503 } 1504 }; 1505 case "log": 1506 return { type: "log", data }; 1507 case "process_generating": 1508 return { 1509 type: "generating", 1510 status: { 1511 queue, 1512 message: !data.success ? data.output.error : null, 1513 stage: data.success ? "generating" : "error", 1514 code: data.code, 1515 progress_data: data.progress_data, 1516 eta: data.average_duration 1517 }, 1518 data: data.success ? data.output : null 1519 }; 1520 case "process_completed": 1521 if ("error" in data.output) { 1522 return { 1523 type: "update", 1524 status: { 1525 queue, 1526 message: data.output.error, 1527 stage: "error", 1528 code: data.code, 1529 success: data.success 1530 } 1531 }; 1532 } 1533 return { 1534 type: "complete", 1535 status: { 1536 queue, 1537 message: !data.success ? data.output.error : void 0, 1538 stage: data.success ? "complete" : "error", 1539 code: data.code, 1540 progress_data: data.progress_data 1541 }, 1542 data: data.success ? data.output : null 1543 }; 1544 case "process_starts": 1545 return { 1546 type: "update", 1547 status: { 1548 queue, 1549 stage: "pending", 1550 code: data.code, 1551 size: data.rank, 1552 position: 0, 1553 success: data.success, 1554 eta: data.eta 1555 } 1556 }; 1557 } 1558 return { type: "none", status: { stage: "error", queue } }; 1559} 1560 1561function mount_css(url, target) { 1562 const base = new URL(import.meta.url).origin; 1563 const _url = new URL(url, base).href; 1564 const existing_link = document.querySelector(`link[href='${_url}']`); 1565 if (existing_link) 1566 return Promise.resolve(); 1567 const link = document.createElement("link"); 1568 link.rel = "stylesheet"; 1569 link.href = _url; 1570 return new Promise((res, rej) => { 1571 link.addEventListener("load", () => res()); 1572 link.addEventListener("error", () => { 1573 console.error(`Unable to preload CSS for ${_url}`); 1574 res(); 1575 }); 1576 target.appendChild(link); 1577 }); 1578} 1579function prefix_css(string, version, style_element = document.createElement("style")) { 1580 style_element.remove(); 1581 const stylesheet = new CSSStyleSheet(); 1582 stylesheet.replaceSync(string); 1583 let importString = ""; 1584 string = string.replace(/@import\s+url\((.*?)\);\s*/g, (match, url) => { 1585 importString += `@import url(${url}); 1586`; 1587 return ""; 1588 }); 1589 const rules = stylesheet.cssRules; 1590 let css_string = ""; 1591 let gradio_css_infix = `gradio-app .gradio-container.gradio-container-${version} .contain `; 1592 for (let i = 0; i < rules.length; i++) { 1593 const rule = rules[i]; 1594 let is_dark_rule = rule.cssText.includes(".dark"); 1595 if (rule instanceof CSSStyleRule) { 1596 const selector = rule.selectorText; 1597 if (selector) { 1598 const new_selector = selector.replace(".dark", "").split(",").map( 1599 (s) => `${is_dark_rule ? ".dark" : ""} ${gradio_css_infix} ${s.trim()} ` 1600 ).join(","); 1601 css_string += rule.cssText; 1602 css_string += rule.cssText.replace(selector, new_selector); 1603 } 1604 } else if (rule instanceof CSSMediaRule) { 1605 let mediaCssString = `@media ${rule.media.mediaText} {`; 1606 for (let j = 0; j < rule.cssRules.length; j++) { 1607 const innerRule = rule.cssRules[j]; 1608 if (innerRule instanceof CSSStyleRule) { 1609 let is_dark_rule2 = innerRule.cssText.includes(".dark "); 1610 const selector = innerRule.selectorText; 1611 const new_selector = selector.replace(".dark", "").split(",").map( 1612 (s) => `${is_dark_rule2 ? ".dark" : ""} ${gradio_css_infix} ${s.trim()} ` 1613 ).join(","); 1614 mediaCssString += innerRule.cssText.replace(selector, new_selector); 1615 } 1616 } 1617 mediaCssString += "}"; 1618 css_string += mediaCssString; 1619 } else if (rule instanceof CSSKeyframesRule) { 1620 css_string += `@keyframes ${rule.name} {`; 1621 for (let j = 0; j < rule.cssRules.length; j++) { 1622 const innerRule = rule.cssRules[j]; 1623 if (innerRule instanceof CSSKeyframeRule) { 1624 css_string += `${innerRule.keyText} { ${innerRule.style.cssText} }`; 1625 } 1626 } 1627 css_string += "}"; 1628 } else if (rule instanceof CSSFontFaceRule) { 1629 css_string += `@font-face { ${rule.style.cssText} }`; 1630 } 1631 } 1632 css_string = importString + css_string; 1633 style_element.textContent = css_string; 1634 document.head.appendChild(style_element); 1635 return style_element; 1636} 1637 1638const ENTRY_CSS = "./assets/index-34709fe9.css"; 1639let FONTS; 1640FONTS = []; 1641let IndexComponent; 1642let _res; 1643let pending = new Promise((res) => { 1644 _res = res; 1645}); 1646async function get_index() { 1647 IndexComponent = (await __vitePreload(() => import('./Index-9d23d982.js').then(n => n.I),true?["./Index-9d23d982.js","./Index-bca1a2bf.css"]:void 0,import.meta.url)).default; 1648 _res(); 1649}
1650function create_custom_element() { 1651 const o = { 1652 SvelteComponent: svelte.SvelteComponent 1653 }; 1654 for (const key in svelte) { 1655 if (key === "SvelteComponent") 1656 continue; 1657 if (key === "SvelteComponentDev") { 1658 o[key] = o["SvelteComponent"]; 1659 } else { 1660 o[key] = svelte[key]; 1661 } 1662 } 1663 window.__gradio__svelte__internal = o; 1664 class GradioApp extends HTMLElement { 1665 constructor() { 1666 super(); 1667 this.host = this.getAttribute("host"); 1668 this.space = this.getAttribute("space"); 1669 this.src = this.getAttribute("src"); 1670 this.control_page_title = this.getAttribute("control_page_title"); 1671 this.initial_height = this.getAttribute("initial_height") ?? "300px"; 1672 this.is_embed = this.getAttribute("embed") ?? "true"; 1673 this.container = this.getAttribute("container") ?? "true"; 1674 this.info = this.getAttribute("info") ?? true; 1675 this.autoscroll = this.getAttribute("autoscroll"); 1676 this.eager = this.getAttribute("eager"); 1677 this.theme_mode = this.getAttribute("theme_mode"); 1678 this.updating = false; 1679 this.loading = false; 1680 } 1681 async connectedCallback() { 1682 await get_index(); 1683 this.loading = true; 1684 if (this.app) { 1685 this.app.$destroy(); 1686 } 1687 if (typeof FONTS !== "string") { 1688 FONTS.forEach((f) => mount_css(f, document.head)); 1689 } 1690 await mount_css(ENTRY_CSS, document.head); 1691 const event = new CustomEvent("domchange", { 1692 bubbles: true, 1693 cancelable: false, 1694 composed: true 1695 }); 1696 const observer = new MutationObserver((mutations) => { 1697 this.dispatchEvent(event); 1698 }); 1699 observer.observe(this, { childList: true }); 1700 this.app = new IndexComponent({ 1701 target: this, 1702 props: { 1703 // embed source 1704 space: this.space ? this.space.trim() : this.space, 1705 src: this.src ? this.src.trim() : this.src, 1706 host: this.host ? this.host.trim() : this.host, 1707 // embed info 1708 info: this.info === "false" ? false : true, 1709 container: this.container === "false" ? false : true, 1710 is_embed: this.is_embed === "false" ? false : true, 1711 initial_height: this.initial_height, 1712 eager: this.eager === "true" ? true : false, 1713 // gradio meta info 1714 version: "4-20-0", 1715 theme_mode: this.theme_mode, 1716 // misc global behaviour 1717 autoscroll: this.autoscroll === "true" ? true : false, 1718 control_page_title: this.control_page_title === "true" ? true : false, 1719 // injectables 1720 client, 1721 upload_files, 1722 // for gradio docs 1723 // TODO: Remove -- i think this is just for autoscroll behavhiour, app vs embeds 1724 app_mode: window.__gradio_mode__ === "app" 1725 } 1726 }); 1727 if (this.updating) { 1728 this.setAttribute(this.updating.name, this.updating.value); 1729 } 1730 this.loading = false; 1731 } 1732 static get observedAttributes() { 1733 return ["src", "space", "host"]; 1734 } 1735 async attributeChangedCallback(name, old_val, new_val) { 1736 await pending; 1737 if ((name === "host" || name === "space" || name === "src") && new_val !== old_val) { 1738 this.updating = { name, value: new_val }; 1739 if (this.loading) 1740 return; 1741 if (this.app) { 1742 this.app.$destroy(); 1743 } 1744 this.space = null; 1745 this.host = null; 1746 this.src = null; 1747 if (name === "host") { 1748 this.host = new_val; 1749 } else if (name === "space") { 1750 this.space = new_val; 1751 } else if (name === "src") { 1752 this.src = new_val; 1753 } 1754 this.app = new IndexComponent({ 1755 target: this, 1756 props: { 1757 // embed source 1758 space: this.space ? this.space.trim() : this.space, 1759 src: this.src ? this.src.trim() : this.src, 1760 host: this.host ? this.host.trim() : this.host, 1761 // embed info 1762 info: this.info === "false" ? false : true, 1763 container: this.container === "false" ? false : true,
1764 is_embed: this.is_embed === "false" ? false : true, 1765 initial_height: this.initial_height, 1766 eager: this.eager === "true" ? true : false, 1767 // gradio meta info 1768 version: "4-20-0", 1769 theme_mode: this.theme_mode, 1770 // misc global behaviour 1771 autoscroll: this.autoscroll === "true" ? true : false, 1772 control_page_title: this.control_page_title === "true" ? true : false, 1773 // injectables 1774 client, 1775 upload_files, 1776 // for gradio docs 1777 // TODO: Remove -- i think this is just for autoscroll behavhiour, app vs embeds 1778 app_mode: window.__gradio_mode__ === "app" 1779 } 1780 }); 1781 this.updating = false; 1782 } 1783 } 1784 } 1785 if (!customElements.get("gradio-app")) 1786 customElements.define("gradio-app", GradioApp); 1787} 1788create_custom_element(); 1789 1790export { __vitePreload as _, prefix_css as a, mount_css as m, prepare_files as p, upload as u }; 1791//# sourceMappingURL=index-12d4b00b.js.map
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.