gradio-pr-bot commited on
Commit
2745f4e
·
verified ·
1 Parent(s): 0df5c6f

Upload folder using huggingface_hub

Browse files
6.9.1/preview/package.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/preview",
3
+ "version": "0.16.0",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "author": "",
8
+ "license": "ISC",
9
+ "private": false,
10
+ "scripts": {
11
+ "build": "vite build --ssr"
12
+ },
13
+ "dependencies": {
14
+ "@sveltejs/vite-plugin-svelte": "^6.2.1",
15
+ "@types/which": "^3.0.4",
16
+ "rollup": "^4.59.0",
17
+ "vite": "^7.1.9",
18
+ "svelte-preprocess": "^6.0.3",
19
+ "typescript": "^5.9.3"
20
+ },
21
+ "exports": {
22
+ ".": {
23
+ "default": "./dist/index.js",
24
+ "import": "./dist/index.js",
25
+ "gradio": "./src/index.ts",
26
+ "svelte": "./dist/src/index.js",
27
+ "types": "./dist/index.d.ts"
28
+ },
29
+ "./package.json": "./package.json"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/gradio-app/gradio.git",
34
+ "directory": "js/preview"
35
+ }
36
+ }
6.9.1/preview/src/_deepmerge_internal.ts ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // this is a copy of the deepemerge source code but with an esm interface rather than commonjs
2
+
3
+ export const deepmerge = `
4
+ function isMergeableObject(value) {
5
+ return isNonNullObject(value)
6
+ && !isSpecial(value)
7
+ }
8
+
9
+ function isNonNullObject(value) {
10
+ return !!value && typeof value === 'object'
11
+ }
12
+
13
+ function isSpecial(value) {
14
+ var stringValue = Object.prototype.toString.call(value)
15
+
16
+ return stringValue === '[object RegExp]'
17
+ || stringValue === '[object Date]'
18
+ || isReactElement(value)
19
+ }
20
+
21
+ var canUseSymbol = typeof Symbol === 'function' && Symbol.for
22
+ var REACT_ELEMENT_TYPE = canUseSymbol ? Symbol.for('react.element') : 0xeac7
23
+
24
+ function isReactElement(value) {
25
+ return value.$$typeof === REACT_ELEMENT_TYPE
26
+ }
27
+
28
+ var defaultIsMergeableObject = isMergeableObject;
29
+
30
+ function emptyTarget(val) {
31
+ return Array.isArray(val) ? [] : {}
32
+ }
33
+
34
+ function cloneUnlessOtherwiseSpecified(value, options) {
35
+ return (options.clone !== false && options.isMergeableObject(value))
36
+ ? deepmerge(emptyTarget(value), value, options)
37
+ : value
38
+ }
39
+
40
+ function defaultArrayMerge(target, source, options) {
41
+ return target.concat(source).map(function(element) {
42
+ return cloneUnlessOtherwiseSpecified(element, options)
43
+ })
44
+ }
45
+
46
+ function getMergeFunction(key, options) {
47
+ if (!options.customMerge) {
48
+ return deepmerge
49
+ }
50
+ var customMerge = options.customMerge(key)
51
+ return typeof customMerge === 'function' ? customMerge : deepmerge
52
+ }
53
+
54
+ function getEnumerableOwnPropertySymbols(target) {
55
+ return Object.getOwnPropertySymbols
56
+ ? Object.getOwnPropertySymbols(target).filter(function(symbol) {
57
+ return Object.propertyIsEnumerable.call(target, symbol)
58
+ })
59
+ : []
60
+ }
61
+
62
+ function getKeys(target) {
63
+ return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target))
64
+ }
65
+
66
+ function propertyIsOnObject(object, property) {
67
+ try {
68
+ return property in object
69
+ } catch(_) {
70
+ return false
71
+ }
72
+ }
73
+
74
+ // Protects from prototype poisoning and unexpected merging up the prototype chain.
75
+ function propertyIsUnsafe(target, key) {
76
+ return propertyIsOnObject(target, key) // Properties are safe to merge if they don't exist in the target yet,
77
+ && !(Object.hasOwnProperty.call(target, key) // unsafe if they exist up the prototype chain,
78
+ && Object.propertyIsEnumerable.call(target, key)) // and also unsafe if they're nonenumerable.
79
+ }
80
+
81
+ function mergeObject(target, source, options) {
82
+ var destination = {}
83
+ if (options.isMergeableObject(target)) {
84
+ getKeys(target).forEach(function(key) {
85
+ destination[key] = cloneUnlessOtherwiseSpecified(target[key], options)
86
+ })
87
+ }
88
+ getKeys(source).forEach(function(key) {
89
+ if (propertyIsUnsafe(target, key)) {
90
+ return
91
+ }
92
+
93
+ if (propertyIsOnObject(target, key) && options.isMergeableObject(source[key])) {
94
+ destination[key] = getMergeFunction(key, options)(target[key], source[key], options)
95
+ } else {
96
+ destination[key] = cloneUnlessOtherwiseSpecified(source[key], options)
97
+ }
98
+ })
99
+ return destination
100
+ }
101
+
102
+ function deepmerge(target, source, options) {
103
+ options = options || {}
104
+ options.arrayMerge = options.arrayMerge || defaultArrayMerge
105
+ options.isMergeableObject = options.isMergeableObject || defaultIsMergeableObject
106
+
107
+ options.cloneUnlessOtherwiseSpecified = cloneUnlessOtherwiseSpecified
108
+
109
+ var sourceIsArray = Array.isArray(source)
110
+ var targetIsArray = Array.isArray(target)
111
+ var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray
112
+
113
+ if (!sourceAndTargetTypesMatch) {
114
+ return cloneUnlessOtherwiseSpecified(source, options)
115
+ } else if (sourceIsArray) {
116
+ return options.arrayMerge(target, source, options)
117
+ } else {
118
+ return mergeObject(target, source, options)
119
+ }
120
+ }
121
+
122
+ deepmerge.all = function deepmergeAll(array, options) {
123
+ if (!Array.isArray(array)) {
124
+ throw new Error('first argument should be an array')
125
+ }
126
+
127
+ return array.reduce(function(prev, next) {
128
+ return deepmerge(prev, next, options)
129
+ }, {})
130
+ }
131
+
132
+ export default deepmerge;
133
+ `;
6.9.1/preview/src/build.ts ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import * as fs from "fs";
2
+ import { join, dirname } from "path";
3
+ import { fileURLToPath } from "url";
4
+
5
+ import { build } from "vite";
6
+ import type { PreRenderedChunk } from "rollup";
7
+
8
+ import { plugins, make_gradio_plugin } from "./plugins";
9
+ import { examine_module } from "./index";
10
+
11
+ interface BuildOptions {
12
+ component_dir: string;
13
+ root_dir: string;
14
+ python_path: string;
15
+ }
16
+
17
+ const __dirname = dirname(fileURLToPath(import.meta.url));
18
+
19
+ export async function make_build({
20
+ component_dir,
21
+ root_dir,
22
+ python_path
23
+ }: BuildOptions): Promise<void> {
24
+ process.env.gradio_mode = "dev";
25
+ const svelte_dir = join(root_dir, "assets", "svelte");
26
+
27
+ const module_meta = examine_module(
28
+ component_dir,
29
+ root_dir,
30
+ python_path,
31
+ "build"
32
+ );
33
+ try {
34
+ for (const comp of module_meta) {
35
+ const template_dir = comp.template_dir;
36
+ const source_dir = comp.frontend_dir;
37
+
38
+ const pkg = JSON.parse(
39
+ fs.readFileSync(join(source_dir, "package.json"), "utf-8")
40
+ );
41
+ let component_config = {
42
+ plugins: [],
43
+ svelte: {
44
+ preprocess: []
45
+ },
46
+ build: {
47
+ target: []
48
+ },
49
+ optimizeDeps: {
50
+ exclude: ["svelte", "svelte/*"]
51
+ }
52
+ };
53
+
54
+ if (
55
+ comp.frontend_dir &&
56
+ fs.existsSync(join(comp.frontend_dir, "gradio.config.js"))
57
+ ) {
58
+ const m = await import(
59
+ join("file://" + comp.frontend_dir, "gradio.config.js")
60
+ );
61
+
62
+ component_config.plugins = m.default.plugins || [];
63
+ component_config.svelte.preprocess = m.default.svelte?.preprocess || [];
64
+ component_config.build.target = m.default.build?.target || "modules";
65
+ component_config.optimizeDeps =
66
+ m.default.optimizeDeps || component_config.optimizeDeps;
67
+ }
68
+
69
+ const exports: (string | any)[][] = [
70
+ [
71
+ join(template_dir, "component"),
72
+ [
73
+ join(__dirname, "svelte_runtime_entry.js"),
74
+ join(source_dir, pkg.exports["."].gradio)
75
+ ]
76
+ ],
77
+ [
78
+ join(template_dir, "example"),
79
+ [
80
+ join(__dirname, "svelte_runtime_entry.js"),
81
+ join(source_dir, pkg.exports["./example"].gradio)
82
+ ]
83
+ ]
84
+ ].filter(([_, path]) => !!path);
85
+
86
+ for (const [out_path, entry_path] of exports) {
87
+ try {
88
+ const x = await build({
89
+ root: source_dir,
90
+ configFile: false,
91
+ plugins: [
92
+ ...plugins(component_config),
93
+ make_gradio_plugin({ svelte_dir, component_dir })
94
+ ],
95
+ build: {
96
+ emptyOutDir: true,
97
+ outDir: out_path,
98
+ lib: {
99
+ entry: entry_path,
100
+ fileName: "index.js",
101
+ formats: ["es"]
102
+ },
103
+ minify: true,
104
+ rollupOptions: {
105
+ output: {
106
+ assetFileNames: (chunkInfo) => {
107
+ if (chunkInfo.names[0].endsWith(".css")) {
108
+ return `style.css`;
109
+ }
110
+
111
+ return chunkInfo.names[0];
112
+ },
113
+ entryFileNames: (chunkInfo: PreRenderedChunk) => {
114
+ if (chunkInfo.isEntry) {
115
+ return chunkInfo.name.toLocaleLowerCase() + ".js";
116
+ }
117
+ return `${chunkInfo.name.toLocaleLowerCase()}.js`;
118
+ }
119
+ }
120
+ }
121
+ }
122
+ });
123
+ } catch (e) {
124
+ throw e;
125
+ }
126
+ }
127
+ }
128
+ } catch (e) {
129
+ throw e;
130
+ }
131
+ }
6.9.1/preview/src/dev.ts ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { join } from "path";
2
+ import * as fs from "fs";
3
+
4
+ import { createServer, createLogger } from "vite";
5
+ import type { PreprocessorGroup } from "svelte/compiler";
6
+
7
+ import { plugins, make_gradio_plugin } from "./plugins";
8
+ import { examine_module } from "./index";
9
+
10
+ const vite_messages_to_ignore = [
11
+ "Default and named imports from CSS files are deprecated.",
12
+ "The above dynamic import cannot be analyzed by Vite."
13
+ ];
14
+
15
+ const logger = createLogger();
16
+ const originalWarning = logger.warn;
17
+ logger.warn = (msg, options) => {
18
+ if (vite_messages_to_ignore.some((m) => msg.includes(m))) return;
19
+
20
+ originalWarning(msg, options);
21
+ };
22
+
23
+ const originalError = logger.error;
24
+
25
+ logger.error = (msg, options) => {
26
+ if (msg && msg.includes("Pre-transform error")) return;
27
+ originalError(msg, options);
28
+ };
29
+
30
+ interface ServerOptions {
31
+ component_dir: string;
32
+ root_dir: string;
33
+ frontend_port: number;
34
+ backend_port: number;
35
+ host: string;
36
+ python_path: string;
37
+ }
38
+
39
+ export async function create_server({
40
+ component_dir,
41
+ root_dir,
42
+ frontend_port,
43
+ backend_port,
44
+ host,
45
+ python_path
46
+ }: ServerOptions): Promise<void> {
47
+ process.env.gradio_mode = "dev";
48
+ const [imports, config, runtimes] = await generate_imports(
49
+ component_dir,
50
+ root_dir,
51
+ python_path
52
+ );
53
+
54
+ const svelte_dir = join(root_dir, "assets", "svelte");
55
+
56
+ try {
57
+ const server = await createServer({
58
+ customLogger: logger,
59
+ mode: "development",
60
+ configFile: false,
61
+ root: root_dir,
62
+ server: {
63
+ port: frontend_port,
64
+ host: host,
65
+ fs: {
66
+ allow: [root_dir, component_dir]
67
+ }
68
+ },
69
+ optimizeDeps: config.optimizeDeps,
70
+ cacheDir: join(component_dir, "frontend", "node_modules", ".vite"),
71
+ plugins: [
72
+ ...plugins(config),
73
+ make_gradio_plugin({
74
+ backend_port,
75
+ svelte_dir,
76
+ component_dir,
77
+ imports,
78
+ runtimes
79
+ })
80
+ ]
81
+ });
82
+
83
+ await server.listen();
84
+
85
+ console.info(
86
+ `[orange3]Frontend Server[/] (Go here): ${server.resolvedUrls?.local}`
87
+ );
88
+ } catch (e) {
89
+ console.error(e);
90
+ }
91
+ }
92
+
93
+ function find_frontend_folders(start_path: string): string[] {
94
+ if (!fs.existsSync(start_path)) {
95
+ console.warn("No directory found at:", start_path);
96
+ return [];
97
+ }
98
+
99
+ if (fs.existsSync(join(start_path, "pyproject.toml"))) return [start_path];
100
+
101
+ const results: string[] = [];
102
+ const dir = fs.readdirSync(start_path);
103
+ dir.forEach((dir) => {
104
+ const filepath = join(start_path, dir);
105
+ if (fs.existsSync(filepath)) {
106
+ if (fs.existsSync(join(filepath, "pyproject.toml")))
107
+ results.push(filepath);
108
+ }
109
+ });
110
+
111
+ return results;
112
+ }
113
+
114
+ function to_posix(_path: string): string {
115
+ const isExtendedLengthPath = /^\\\\\?\\/.test(_path);
116
+ const hasNonAscii = /[^\u0000-\u0080]+/.test(_path);
117
+
118
+ if (isExtendedLengthPath || hasNonAscii) {
119
+ return _path;
120
+ }
121
+
122
+ return _path.replace(/\\/g, "/");
123
+ }
124
+
125
+ export interface ComponentConfig {
126
+ plugins: any[];
127
+ svelte: {
128
+ preprocess: PreprocessorGroup[];
129
+ extensions?: string[];
130
+ };
131
+ build: {
132
+ target: string | string[];
133
+ };
134
+ optimizeDeps: object;
135
+ }
136
+
137
+ async function generate_imports(
138
+ component_dir: string,
139
+ root: string,
140
+ python_path: string
141
+ ): Promise<[string, ComponentConfig, string]> {
142
+ const components = find_frontend_folders(component_dir);
143
+
144
+ const component_entries = components.flatMap((component) => {
145
+ return examine_module(component, root, python_path, "dev");
146
+ });
147
+ if (component_entries.length === 0) {
148
+ console.info(
149
+ `No custom components were found in ${component_dir}. It is likely that dev mode does not work properly. Please pass the --gradio-path and --python-path CLI arguments so that gradio uses the right executables.`
150
+ );
151
+ }
152
+
153
+ let component_config: ComponentConfig = {
154
+ plugins: [],
155
+ svelte: {
156
+ preprocess: []
157
+ },
158
+ build: {
159
+ target: []
160
+ },
161
+ optimizeDeps: {
162
+ exclude: ["svelte", "svelte/*"]
163
+ }
164
+ };
165
+
166
+ await Promise.all(
167
+ component_entries.map(async (component) => {
168
+ if (
169
+ component.frontend_dir &&
170
+ fs.existsSync(join(component.frontend_dir, "gradio.config.js"))
171
+ ) {
172
+ const m = await import(
173
+ join("file://" + component.frontend_dir, "gradio.config.js")
174
+ );
175
+
176
+ component_config.plugins = m.default.plugins || [];
177
+ component_config.svelte.preprocess = m.default.svelte?.preprocess || [];
178
+ component_config.build.target = m.default.build?.target || "modules";
179
+ component_config.optimizeDeps =
180
+ m.default.optimizeDeps || component_config.optimizeDeps;
181
+ } else {
182
+ }
183
+ })
184
+ );
185
+
186
+ const imports = component_entries.reduce((acc, component) => {
187
+ const pkg = JSON.parse(
188
+ fs.readFileSync(join(component.frontend_dir, "package.json"), "utf-8")
189
+ );
190
+
191
+ const exports: Record<string, any | undefined> = {
192
+ component: pkg.exports["."],
193
+ example: pkg.exports["./example"]
194
+ };
195
+
196
+ if (!exports.component)
197
+ throw new Error(
198
+ "Could not find component entry point. Please check the exports field of your package.json."
199
+ );
200
+
201
+ const example = exports.example
202
+ ? `example: () => import("/@fs/${to_posix(
203
+ join(component.frontend_dir, exports.example.gradio)
204
+ )}"),\n`
205
+ : "";
206
+ return `${acc}"${component.component_class_id}": {
207
+ ${example}
208
+ component: () => import("/@fs/${to_posix(
209
+ join(component.frontend_dir, exports.component.gradio)
210
+ )}"),
211
+
212
+ },\n`;
213
+ }, "");
214
+
215
+ const runtimes = component_entries.reduce((acc, component) => {
216
+ return `${acc}"${component.component_class_id}": import("svelte"),\n`;
217
+ }, "");
218
+
219
+ return [`{${imports}}`, component_config, `{${runtimes}}`];
220
+ }
6.9.1/preview/src/index.ts ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { type ChildProcess, spawn, spawnSync } from "node:child_process";
2
+ import * as net from "net";
3
+
4
+ import { create_server, type ComponentConfig } from "./dev";
5
+ import { make_build } from "./build";
6
+ import { join, dirname } from "path";
7
+ import { fileURLToPath } from "url";
8
+
9
+ const __dirname = dirname(fileURLToPath(import.meta.url));
10
+
11
+ export interface ComponentMeta {
12
+ name: string;
13
+ template_dir: string;
14
+ frontend_dir: string;
15
+ component_class_id: string;
16
+ }
17
+
18
+ const args = process.argv.slice(2);
19
+ // get individual args as `--arg value` or `value`
20
+
21
+ function parse_args(args: string[]): Record<string, string> {
22
+ const arg_map: Record<string, string> = {};
23
+ for (let i = 0; i < args.length; i++) {
24
+ const arg = args[i];
25
+ if (arg.startsWith("--")) {
26
+ const name = arg.slice(2);
27
+ const value = args[i + 1];
28
+ arg_map[name] = value;
29
+ i++;
30
+ }
31
+ }
32
+ return arg_map;
33
+ }
34
+
35
+ const parsed_args = parse_args(args);
36
+
37
+ async function run(): Promise<void> {
38
+ if (parsed_args.mode === "build") {
39
+ await make_build({
40
+ component_dir: parsed_args["component-directory"],
41
+ root_dir: parsed_args.root,
42
+ python_path: parsed_args["python-path"]
43
+ });
44
+ } else {
45
+ const [backend_port, frontend_port] = await find_free_ports(7860, 8860);
46
+ const options = {
47
+ component_dir: parsed_args["component-directory"],
48
+ root_dir: parsed_args.root,
49
+ frontend_port,
50
+ backend_port,
51
+ host: parsed_args.host,
52
+ ...parsed_args
53
+ };
54
+ process.env.GRADIO_BACKEND_PORT = backend_port.toString();
55
+ const _process = spawn(
56
+ parsed_args["gradio-path"],
57
+ [parsed_args.app, "--watch-dirs", options.component_dir],
58
+ {
59
+ shell: false,
60
+ stdio: "pipe",
61
+ cwd: process.cwd(),
62
+ env: {
63
+ ...process.env,
64
+ GRADIO_SERVER_PORT: backend_port.toString(),
65
+ PYTHONUNBUFFERED: "true"
66
+ }
67
+ }
68
+ );
69
+
70
+ _process.stdout.setEncoding("utf8");
71
+ _process.stderr.setEncoding("utf8");
72
+
73
+ function std_out(mode: "stdout" | "stderr") {
74
+ return function (data: Buffer): void {
75
+ const _data = data.toString();
76
+
77
+ if (_data.includes("Running on")) {
78
+ create_server({
79
+ component_dir: options.component_dir,
80
+ root_dir: options.root_dir,
81
+ frontend_port,
82
+ backend_port,
83
+ host: options.host,
84
+ python_path: parsed_args["python-path"]
85
+ });
86
+ }
87
+
88
+ process[mode].write(_data);
89
+ };
90
+ }
91
+
92
+ _process.stdout.on("data", std_out("stdout"));
93
+ _process.stderr.on("data", std_out("stderr"));
94
+ _process.on("exit", () => kill_process(_process));
95
+ _process.on("close", () => kill_process(_process));
96
+ _process.on("disconnect", () => kill_process(_process));
97
+ }
98
+ }
99
+
100
+ function kill_process(process: ChildProcess): void {
101
+ process.kill("SIGKILL");
102
+ }
103
+
104
+ export { create_server };
105
+
106
+ run();
107
+
108
+ export async function find_free_ports(
109
+ start_port: number,
110
+ end_port: number
111
+ ): Promise<[number, number]> {
112
+ let found_ports: number[] = [];
113
+
114
+ for (let port = start_port; port < end_port; port++) {
115
+ if (await is_free_port(port)) {
116
+ found_ports.push(port);
117
+ if (found_ports.length === 2) {
118
+ return [found_ports[0], found_ports[1]];
119
+ }
120
+ }
121
+ }
122
+
123
+ throw new Error(
124
+ `Could not find free ports: there were not enough ports available.`
125
+ );
126
+ }
127
+
128
+ export function is_free_port(port: number): Promise<boolean> {
129
+ return new Promise((accept, reject) => {
130
+ const sock = net.createConnection(port, "127.0.0.1");
131
+ setTimeout(() => {
132
+ sock.destroy();
133
+ reject(
134
+ new Error(`Timeout while detecting free port with 127.0.0.1:${port} `)
135
+ );
136
+ }, 3000);
137
+ sock.once("connect", () => {
138
+ sock.end();
139
+ accept(false);
140
+ });
141
+ sock.once("error", (e) => {
142
+ sock.destroy();
143
+ //@ts-ignore
144
+ if (e.code === "ECONNREFUSED") {
145
+ accept(true);
146
+ } else {
147
+ reject(e);
148
+ }
149
+ });
150
+ });
151
+ }
152
+
153
+ function is_truthy<T>(value: T | null | undefined | false): value is T {
154
+ return value !== null && value !== undefined && value !== false;
155
+ }
156
+
157
+ export function examine_module(
158
+ component_dir: string,
159
+ root: string,
160
+ python_path: string,
161
+ mode: "build" | "dev"
162
+ ): ComponentMeta[] {
163
+ const _process = spawnSync(
164
+ python_path,
165
+ [join(__dirname, "examine.py"), "-m", mode],
166
+ {
167
+ cwd: join(component_dir, "backend"),
168
+ stdio: "pipe"
169
+ }
170
+ );
171
+ const exceptions: string[] = [];
172
+
173
+ const components = _process.stdout
174
+ .toString()
175
+ .trim()
176
+ .split("\n")
177
+ .map((line) => {
178
+ if (line.startsWith("|EXCEPTION|")) {
179
+ exceptions.push(line.slice("|EXCEPTION|:".length));
180
+ }
181
+ const [name, template_dir, frontend_dir, component_class_id] =
182
+ line.split("~|~|~|~");
183
+ if (name && template_dir && frontend_dir && component_class_id) {
184
+ return {
185
+ name: name.trim(),
186
+ template_dir: template_dir.trim(),
187
+ frontend_dir: frontend_dir.trim(),
188
+ component_class_id: component_class_id.trim()
189
+ };
190
+ }
191
+ return false;
192
+ })
193
+ .filter(is_truthy);
194
+ if (exceptions.length > 0) {
195
+ console.info(
196
+ `While searching for gradio custom component source directories in ${component_dir}, the following exceptions were raised. If dev mode does not work properly please pass the --gradio-path and --python-path CLI arguments so that gradio uses the right executables: ${exceptions.join(
197
+ "\n"
198
+ )}`
199
+ );
200
+ }
201
+ return components;
202
+ }
6.9.1/preview/src/placeholder.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export default {};
6.9.1/preview/src/plugins.ts ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Plugin, PluginOption } from "vite";
2
+ import { svelte } from "@sveltejs/vite-plugin-svelte";
3
+ import { join, dirname } from "path";
4
+ import { createRequire } from "module";
5
+ import { readFileSync } from "fs";
6
+ import { type ComponentConfig } from "./dev";
7
+ import type { PreprocessorGroup } from "svelte/compiler";
8
+ import { sveltePreprocess } from "svelte-preprocess";
9
+
10
+ const svelte_codes_to_ignore: Record<string, string> = {
11
+ "reactive-component": "Icon"
12
+ };
13
+
14
+ export function plugins(config: ComponentConfig): PluginOption[] {
15
+ const _additional_plugins = config.plugins || [];
16
+ const _additional_svelte_preprocess = config.svelte?.preprocess || [];
17
+ const _svelte_extensions = (config.svelte?.extensions || [".svelte"]).map(
18
+ (ext) => {
19
+ if (ext.trim().startsWith(".")) {
20
+ return ext;
21
+ }
22
+ return `.${ext.trim()}`;
23
+ }
24
+ );
25
+
26
+ if (!_svelte_extensions.includes(".svelte")) {
27
+ _svelte_extensions.push(".svelte");
28
+ }
29
+
30
+ return [
31
+ svelte({
32
+ inspector: false,
33
+ onwarn(warning, handler) {
34
+ if (
35
+ svelte_codes_to_ignore.hasOwnProperty(warning.code) &&
36
+ svelte_codes_to_ignore[warning.code] &&
37
+ warning.message.includes(svelte_codes_to_ignore[warning.code])
38
+ ) {
39
+ return;
40
+ }
41
+ handler!(warning);
42
+ },
43
+ prebundleSvelteLibraries: false,
44
+ compilerOptions: {
45
+ discloseVersion: false,
46
+ hmr: true
47
+ },
48
+ extensions: _svelte_extensions,
49
+ preprocess: [
50
+ sveltePreprocess({
51
+ typescript: {
52
+ compilerOptions: {
53
+ declaration: false,
54
+ declarationMap: false
55
+ }
56
+ }
57
+ }),
58
+ ...(_additional_svelte_preprocess as PreprocessorGroup[])
59
+ ]
60
+ }),
61
+ ..._additional_plugins
62
+ ];
63
+ }
64
+
65
+ function resolve_svelte_entry(id: string, base_dir: string): string | null {
66
+ const require_fn = createRequire(join(base_dir, "frontend", "_"));
67
+ try {
68
+ const svelte_pkg_path = require_fn.resolve("svelte/package.json");
69
+ const svelte_dir = dirname(svelte_pkg_path);
70
+ const pkg = JSON.parse(readFileSync(svelte_pkg_path, "utf-8"));
71
+
72
+ const subpath = id === "svelte" ? "." : "./" + id.slice("svelte/".length);
73
+
74
+ if (pkg.exports && pkg.exports[subpath]) {
75
+ const entry = pkg.exports[subpath];
76
+ const resolved =
77
+ typeof entry === "string" ? entry : entry.browser || entry.default;
78
+ if (resolved) {
79
+ return join(svelte_dir, resolved);
80
+ }
81
+ }
82
+ } catch {
83
+ return null;
84
+ }
85
+ return null;
86
+ }
87
+
88
+ interface GradioPluginOptions {
89
+ svelte_dir: string;
90
+ component_dir: string;
91
+ backend_port?: number;
92
+ imports?: string;
93
+ runtimes?: string;
94
+ }
95
+
96
+ export function make_gradio_plugin({
97
+ backend_port,
98
+ component_dir,
99
+ imports,
100
+ runtimes
101
+ }: GradioPluginOptions): Plugin {
102
+ const v_id = "virtual:component-loader";
103
+ const v_id_2 = "virtual:cc-init";
104
+ const resolved_v_id = "\0" + v_id;
105
+ const resolved_v_id_2 = "\0" + v_id_2;
106
+ return {
107
+ name: "gradio",
108
+ enforce: "pre",
109
+ resolveId(id) {
110
+ if (id === v_id) {
111
+ return resolved_v_id;
112
+ }
113
+ if (id === v_id_2) {
114
+ return resolved_v_id_2;
115
+ }
116
+
117
+ if (id.startsWith("svelte")) {
118
+ const resolved = resolve_svelte_entry(id, component_dir);
119
+ if (resolved) {
120
+ return resolved;
121
+ }
122
+ }
123
+ },
124
+ load(id) {
125
+ if (id === resolved_v_id) {
126
+ return `export default {};`;
127
+ }
128
+
129
+ if (id === resolved_v_id_2) {
130
+ return `window.__GRADIO_DEV__ = "dev";
131
+ window.__GRADIO__SERVER_PORT__ = ${backend_port};
132
+ window.__GRADIO__CC__ = ${imports};
133
+ window.__GRADIO__CC__RUNTIMES__ = ${runtimes};`;
134
+ }
135
+ },
136
+ transform(code, id) {
137
+ return code.replace('"_NORMAL_"', '"_CC_"');
138
+ }
139
+ };
140
+ }
141
+
142
+ // export const deepmerge_plugin: Plugin = {
143
+ // name: "deepmerge",
144
+ // enforce: "pre",
145
+ // resolveId(id) {
146
+ // if (id === "deepmerge") {
147
+ // return "deepmerge_internal";
148
+ // }
149
+ // },
150
+ // load(id) {
151
+ // if (id === "deepmerge_internal") {
152
+ // return deepmerge;
153
+ // }
154
+ // },
155
+ // };
6.9.1/preview/test/test/frontend/Example.svelte ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let value: string;
3
+ export let type: "gallery" | "table";
4
+ export let selected = false;
5
+ </script>
6
+
7
+ <div
8
+ class:table={type === "table"}
9
+ class:gallery={type === "gallery"}
10
+ class:selected
11
+ >
12
+ {value}
13
+ </div>
14
+
15
+ <style>
16
+ .gallery {
17
+ padding: var(--size-1) var(--size-2);
18
+ }
19
+ </style>
6.9.1/preview/test/test/frontend/Index.svelte ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import "./main.css";
3
+ import { JsonView } from "@zerodevx/svelte-json-view";
4
+
5
+ import type { Gradio } from "@gradio/utils";
6
+ import { Block, Info } from "@gradio/atoms";
7
+ import { StatusTracker } from "@gradio/statustracker";
8
+ import type { LoadingStatus } from "@gradio/statustracker";
9
+ import type { SelectData } from "@gradio/utils";
10
+
11
+ export let elem_id = "";
12
+ export let elem_classes: string[] = [];
13
+ export let visible: boolean | "hidden" = true;
14
+ export let value = false;
15
+ // export let value_is_output = false;
16
+ // export let label = "Checkbox";
17
+ // export let info: string | undefined = undefined;
18
+ export let container = true;
19
+ export let scale: number | null = null;
20
+ export let min_width: number | undefined = undefined;
21
+ export let loading_status: LoadingStatus;
22
+ export let gradio: Gradio<{
23
+ change: never;
24
+ select: SelectData;
25
+ input: never;
26
+ }>;
27
+ </script>
28
+
29
+ <div class="relative flex min-h-screen flex-col justify-center overflow-hidden">
30
+ <div
31
+ class="relative bg-white px-6 pt-10 pb-8 shadow-xl ring-1 ring-gray-900/5 sm:mx-auto sm:max-w-lg sm:rounded-lg sm:px-10"
32
+ >
33
+ <div class="mx-auto max-w-md">
34
+ <h1 class="text-xl! font-bold! text-gray-900">
35
+ <span class="text-blue-500">Tailwind</span> in Gradio
36
+ </h1>
37
+ <h2><em>(i hope you're happy now)</em></h2>
38
+ <div class="divide-y divide-gray-300/50">
39
+ <div class="space-y-6 py-8 text-base leading-7 text-gray-600">
40
+ <p>
41
+ An advanced online playground for Tailwind CSS, including support
42
+ for things like:
43
+ </p>
44
+ <ul class="space-y-4 my-4!">
45
+ <li class="flex items-center">
46
+ <svg
47
+ class="h-6 w-6 flex-none fill-sky-100 stroke-sky-500 stroke-2 mr-4"
48
+ stroke-linecap="round"
49
+ stroke-linejoin="round"
50
+ >
51
+ <circle cx="12" cy="12" r="11" />
52
+ <path
53
+ d="m8 13 2.165 2.165a1 1 0 0 0 1.521-.126L16 9"
54
+ fill="none"
55
+ />
56
+ </svg>
57
+ <p class="ml-4">
58
+ Customizing your
59
+ <code class="text-sm font-bold text-gray-900"
60
+ >tailwind.config.js</code
61
+ > file
62
+ </p>
63
+ </li>
64
+ <li class="flex items-center">
65
+ <svg
66
+ class="h-6 w-6 flex-none fill-sky-100 stroke-sky-500 stroke-2 mr-4"
67
+ stroke-linecap="round"
68
+ stroke-linejoin="round"
69
+ >
70
+ <circle cx="12" cy="12" r="11" />
71
+ <path
72
+ d="m8 13 2.165 2.165a1 1 0 0 0 1.521-.126L16 9"
73
+ fill="none"
74
+ />
75
+ </svg>
76
+ <p class="ml-4">
77
+ Extracting classes with
78
+ <code class="text-sm font-bold text-gray-900">@apply</code>
79
+ </p>
80
+ </li>
81
+ <li class="flex items-center">
82
+ <svg
83
+ class="h-6 w-6 flex-none fill-sky-100 stroke-sky-500 stroke-2 mr-4"
84
+ stroke-linecap="round"
85
+ stroke-linejoin="round"
86
+ >
87
+ <circle cx="12" cy="12" r="11" />
88
+ <path
89
+ d="m8 13 2.165 2.165a1 1 0 0 0 1.521-.126L16 9"
90
+ fill="none"
91
+ />
92
+ </svg>
93
+ <p class="ml-4">Code completion with instant preview</p>
94
+ </li>
95
+ </ul>
96
+ <p>
97
+ Perfect for learning how the framework works, prototyping a new
98
+ idea, or creating a demo to share online.
99
+ </p>
100
+ </div>
101
+ <div class="pt-8 text-base font-semibold leading-7">
102
+ <p class="text-gray-900">Want to dig deeper into Tailwind?</p>
103
+ <p>
104
+ <a
105
+ href="https://tailwindcss.com/docs"
106
+ class="text-sky-500 hover:text-sky-600">Read the docs &rarr;</a
107
+ >
108
+ </p>
109
+ </div>
110
+ </div>
111
+ </div>
112
+ </div>
113
+ </div>
6.9.1/preview/test/test/frontend/package.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "gradio_test",
3
+ "version": "0.5.2",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": true,
9
+ "main_changeset": true,
10
+ "exports": {
11
+ ".": "./Index.svelte",
12
+ "./example": "./Example.svelte",
13
+ "./package.json": "./package.json"
14
+ },
15
+ "dependencies": {},
16
+ "devDependencies": {},
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/gradio-app/gradio.git",
20
+ "directory": "js/preview/test/test/frontend"
21
+ }
22
+ }
6.9.1/preview/vite.config.ts ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { defineConfig } from "vite";
2
+ import { cpSync, write } from "fs";
3
+ import { join } from "node:path";
4
+ import { createRequire } from "node:module";
5
+
6
+ const require = createRequire(import.meta.url);
7
+ const dir = require.resolve("./package.json");
8
+
9
+ const template_dir = join(dir, "..", "..", "..", "gradio", "templates");
10
+
11
+ export default defineConfig({
12
+ build: {
13
+ lib: {
14
+ entry: "./src/index.ts",
15
+ formats: ["es"]
16
+ },
17
+ outDir: "dist",
18
+ rollupOptions: {
19
+ external: ["fsevents", "vite", "@sveltejs/vite-plugin-svelte"]
20
+ }
21
+ },
22
+ plugins: [copy_files()]
23
+ });
24
+
25
+ export function copy_files() {
26
+ return {
27
+ name: "copy_files",
28
+ writeBundle() {
29
+ cpSync("./src/examine.py", "dist/examine.py");
30
+ cpSync("./src/svelte_runtime_entry.js", "dist/svelte_runtime_entry.js");
31
+ cpSync("./src/register.mjs", join(template_dir, "register.mjs"));
32
+ cpSync("./src/hooks.mjs", join(template_dir, "hooks.mjs"));
33
+ }
34
+ };
35
+ }