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