|
import "@gradio/theme/src/reset.css"; |
|
import "@gradio/theme/src/global.css"; |
|
import "@gradio/theme/src/pollen.css"; |
|
import "@gradio/theme/src/typography.css"; |
|
import type { SvelteComponent } from "svelte"; |
|
import { WorkerProxy, type WorkerProxyOptions } from "@gradio/wasm"; |
|
import { api_factory } from "@gradio/client"; |
|
import { wasm_proxied_fetch } from "./fetch"; |
|
import { wasm_proxied_EventSource_factory } from "./sse"; |
|
import { wasm_proxied_mount_css, mount_prebuilt_css } from "./css"; |
|
import type { mount_css } from "../css"; |
|
import Index from "../Index.svelte"; |
|
import ErrorDisplay from "./ErrorDisplay.svelte"; |
|
import type { ThemeMode } from "../types"; |
|
import { bootstrap_custom_element } from "./custom-element"; |
|
|
|
|
|
import gradioWheel from "gradio.whl"; |
|
import gradioClientWheel from "gradio_client.whl"; |
|
|
|
declare let GRADIO_VERSION: string; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface GradioAppController { |
|
run_code: (code: string) => Promise<void>; |
|
run_file: (path: string) => Promise<void>; |
|
write: ( |
|
path: string, |
|
data: string | ArrayBufferView, |
|
opts: any |
|
) => Promise<void>; |
|
rename: (old_path: string, new_path: string) => Promise<void>; |
|
unlink: (path: string) => Promise<void>; |
|
install: (requirements: string[]) => Promise<void>; |
|
unmount: () => void; |
|
} |
|
|
|
export interface Options { |
|
target: HTMLElement; |
|
files?: WorkerProxyOptions["files"]; |
|
requirements?: WorkerProxyOptions["requirements"]; |
|
code?: string; |
|
entrypoint?: string; |
|
sharedWorkerMode?: boolean; |
|
info: boolean; |
|
container: boolean; |
|
isEmbed: boolean; |
|
initialHeight?: string; |
|
eager: boolean; |
|
themeMode: ThemeMode | null; |
|
autoScroll: boolean; |
|
controlPageTitle: boolean; |
|
appMode: boolean; |
|
} |
|
export function create(options: Options): GradioAppController { |
|
|
|
|
|
const observer = new MutationObserver(() => { |
|
document.body.style.padding = "0"; |
|
}); |
|
|
|
observer.observe(options.target, { childList: true }); |
|
|
|
const worker_proxy = new WorkerProxy({ |
|
gradioWheelUrl: new URL(gradioWheel, import.meta.url).href, |
|
gradioClientWheelUrl: new URL(gradioClientWheel, import.meta.url).href, |
|
files: options.files ?? {}, |
|
requirements: options.requirements ?? [], |
|
sharedWorkerMode: options.sharedWorkerMode ?? false |
|
}); |
|
|
|
worker_proxy.addEventListener("initialization-error", (event) => { |
|
showError((event as CustomEvent).detail); |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (options.code != null) { |
|
worker_proxy.runPythonCode(options.code).catch(showError); |
|
} else if (options.entrypoint != null) { |
|
worker_proxy.runPythonFile(options.entrypoint).catch(showError); |
|
} else { |
|
throw new Error("Either code or entrypoint must be provided."); |
|
} |
|
|
|
mount_prebuilt_css(document.head); |
|
|
|
const overridden_fetch: typeof fetch = (input, init?) => { |
|
return wasm_proxied_fetch(worker_proxy, input, init); |
|
}; |
|
const EventSource_factory = (url: URL): EventSource => { |
|
return wasm_proxied_EventSource_factory(worker_proxy, url); |
|
}; |
|
const { client, upload_files } = api_factory( |
|
overridden_fetch, |
|
EventSource_factory |
|
); |
|
const overridden_mount_css: typeof mount_css = async (url, target) => { |
|
return wasm_proxied_mount_css(worker_proxy, url, target); |
|
}; |
|
|
|
let app: SvelteComponent; |
|
function showError(error: Error): void { |
|
if (app != null) { |
|
app.$destroy(); |
|
} |
|
|
|
app = new ErrorDisplay({ |
|
target: options.target, |
|
props: { |
|
is_embed: !options.isEmbed, |
|
error |
|
} |
|
}); |
|
} |
|
function launchNewApp(): void { |
|
if (app != null) { |
|
app.$destroy(); |
|
} |
|
|
|
app = new Index({ |
|
target: options.target, |
|
props: { |
|
|
|
space: null, |
|
src: null, |
|
host: null, |
|
|
|
info: options.info, |
|
container: options.container, |
|
is_embed: options.isEmbed, |
|
initial_height: options.initialHeight ?? "300px", |
|
eager: options.eager, |
|
|
|
version: GRADIO_VERSION, |
|
theme_mode: options.themeMode, |
|
|
|
autoscroll: options.autoScroll, |
|
control_page_title: options.controlPageTitle, |
|
|
|
|
|
app_mode: options.appMode, |
|
|
|
worker_proxy, |
|
client, |
|
upload_files, |
|
mount_css: overridden_mount_css, |
|
fetch_implementation: overridden_fetch, |
|
EventSource_factory |
|
} |
|
}); |
|
} |
|
|
|
launchNewApp(); |
|
|
|
return { |
|
run_code: (code: string) => { |
|
return worker_proxy |
|
.runPythonCode(code) |
|
.then(launchNewApp) |
|
.catch((e) => { |
|
showError(e); |
|
throw e; |
|
}); |
|
}, |
|
run_file: (path: string) => { |
|
return worker_proxy |
|
.runPythonFile(path) |
|
.then(launchNewApp) |
|
.catch((e) => { |
|
showError(e); |
|
throw e; |
|
}); |
|
}, |
|
write: (path, data, opts) => { |
|
return worker_proxy |
|
.writeFile(path, data, opts) |
|
.then(launchNewApp) |
|
.catch((e) => { |
|
showError(e); |
|
throw e; |
|
}); |
|
}, |
|
rename: (old_path, new_path) => { |
|
return worker_proxy |
|
.renameFile(old_path, new_path) |
|
.then(launchNewApp) |
|
.catch((e) => { |
|
showError(e); |
|
throw e; |
|
}); |
|
}, |
|
unlink: (path) => { |
|
return worker_proxy |
|
.unlink(path) |
|
.then(launchNewApp) |
|
.catch((e) => { |
|
showError(e); |
|
throw e; |
|
}); |
|
}, |
|
install: (requirements) => { |
|
return worker_proxy |
|
.install(requirements) |
|
.then(launchNewApp) |
|
.catch((e) => { |
|
showError(e); |
|
throw e; |
|
}); |
|
}, |
|
unmount() { |
|
app.$destroy(); |
|
worker_proxy.terminate(); |
|
} |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
globalThis.createGradioApp = create; |
|
|
|
bootstrap_custom_element(); |
|
|
|
declare let BUILD_MODE: string; |
|
if (BUILD_MODE === "dev") { |
|
(async function () { |
|
const DevApp = (await import("./dev/App.svelte")).default; |
|
|
|
const app = new DevApp({ |
|
target: document.getElementById("dev-app")! |
|
}); |
|
})(); |
|
} |
|
|