gradio-pr-bot commited on
Commit
52efa54
·
verified ·
1 Parent(s): 5370626

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. 6.0.2/build/package.json +27 -0
  2. 6.0.2/build/src/index.ts +467 -0
6.0.2/build/package.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@self/build",
3
+ "version": "0.5.0",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "main": "out/index.js",
7
+ "private": "true",
8
+ "author": "",
9
+ "license": "ISC",
10
+ "scripts": {
11
+ "build": "esbuild src/index.ts --platform=node --format=esm --target=node18 --bundle --packages=external --outfile=out/index.js && cp src/component_loader.js out/"
12
+ },
13
+ "dependencies": {
14
+ "@gradio/theme": "workspace:^",
15
+ "esbuild": "^0.25.10",
16
+ "svelte-i18n": "^4.0.1"
17
+ },
18
+ "peerDependencies": {
19
+ "svelte": "^5.43.4"
20
+ },
21
+ "main_changeset": true,
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/gradio-app/gradio.git",
25
+ "directory": "js/build"
26
+ }
27
+ }
6.0.2/build/src/index.ts ADDED
@@ -0,0 +1,467 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Plugin } from "vite";
2
+ import { parse, HTMLElement } from "node-html-parser";
3
+
4
+ import { join } from "path";
5
+ import { writeFileSync } from "fs";
6
+
7
+ export function inject_ejs(): Plugin {
8
+ return {
9
+ name: "inject-ejs",
10
+ enforce: "post",
11
+ transformIndexHtml: (html) => {
12
+ const replace_gradio_info_info_html = html.replace(
13
+ /%gradio_api_info%/,
14
+ `<script>window.gradio_api_info = {{ gradio_api_info | toorjson }};</script>`
15
+ );
16
+ return replace_gradio_info_info_html.replace(
17
+ /%gradio_config%/,
18
+ `<script>window.gradio_config = {{ config | toorjson }};</script>`
19
+ );
20
+ }
21
+ };
22
+ }
23
+
24
+ export function generate_cdn_entry({
25
+ version,
26
+ cdn_base
27
+ }: {
28
+ version: string;
29
+ cdn_base: string;
30
+ }): Plugin {
31
+ return {
32
+ name: "generate-cdn-entry",
33
+ enforce: "post",
34
+ writeBundle(config, bundle) {
35
+ if (
36
+ !config.dir ||
37
+ !bundle["index.html"] ||
38
+ bundle["index.html"].type !== "asset"
39
+ )
40
+ return;
41
+
42
+ const source = bundle["index.html"].source as string;
43
+ const tree = parse(source);
44
+
45
+ const script = Array.from(
46
+ tree.querySelectorAll("script[type=module]")
47
+ ).find((node) => node.attributes.src?.includes("assets"));
48
+
49
+ const output_location = join(config.dir, "gradio.js");
50
+
51
+ writeFileSync(output_location, make_entry(script?.attributes.src || ""));
52
+
53
+ if (!script) return;
54
+
55
+ const transformed_html =
56
+ (bundle["index.html"].source as string).substring(0, script?.range[0]) +
57
+ `<script type="module" crossorigin src="${cdn_base}/${version}/gradio.js"></script>` +
58
+ (bundle["index.html"].source as string).substring(
59
+ script?.range[1],
60
+ source.length
61
+ );
62
+
63
+ const share_html_location = join(config.dir, "share.html");
64
+ writeFileSync(share_html_location, transformed_html);
65
+ }
66
+ };
67
+ }
68
+
69
+ const RE_SVELTE_IMPORT =
70
+ /import\s+([\w*{},\s]+)\s+from\s+['"](svelte|svelte\/internal)['"]/g;
71
+
72
+ export function generate_dev_entry({ enable }: { enable: boolean }): Plugin {
73
+ return {
74
+ name: "generate-dev-entry",
75
+ transform(code, id) {
76
+ if (!enable) return;
77
+
78
+ const new_code = code.replace(RE_SVELTE_IMPORT, (str, $1, $2) => {
79
+ return `const ${$1
80
+ .replace(/\* as /, "")
81
+ .replace(/ as /g, ": ")} = window.__gradio__svelte__internal;`;
82
+ });
83
+
84
+ return {
85
+ code: new_code,
86
+ map: null
87
+ };
88
+ }
89
+ };
90
+ }
91
+
92
+ function make_entry(script: string): string {
93
+ return `import("${script}");
94
+ `;
95
+ }
96
+
97
+ export function handle_ce_css(): Plugin {
98
+ return {};
99
+ return {
100
+ enforce: "post",
101
+ name: "custom-element-css",
102
+
103
+ writeBundle(config, bundle) {
104
+ let file_to_insert = {
105
+ filename: "",
106
+ source: ""
107
+ };
108
+
109
+ if (
110
+ !config.dir ||
111
+ !bundle["index.html"] ||
112
+ bundle["index.html"].type !== "asset"
113
+ )
114
+ return;
115
+
116
+ for (const key in bundle) {
117
+ const chunk = bundle[key];
118
+ if (chunk.type === "chunk") {
119
+ const _chunk = chunk;
120
+
121
+ const found = _chunk.code?.indexOf("ENTRY_CSS");
122
+
123
+ if (found > -1)
124
+ file_to_insert = {
125
+ filename: join(config.dir, key),
126
+ source: _chunk.code
127
+ };
128
+ }
129
+ }
130
+
131
+ const tree = parse(bundle["index.html"].source as string);
132
+
133
+ const { style, fonts } = Array.from(
134
+ tree.querySelectorAll("link[rel=stylesheet]")
135
+ ).reduce(
136
+ (acc, next) => {
137
+ if (/.*\/index(.*?)\.css/.test(next.attributes.href)) {
138
+ return { ...acc, style: next };
139
+ }
140
+ return { ...acc, fonts: [...acc.fonts, next.attributes.href] };
141
+ },
142
+ { fonts: [], style: undefined } as {
143
+ fonts: string[];
144
+ style: HTMLElement | undefined;
145
+ }
146
+ );
147
+
148
+ writeFileSync(
149
+ file_to_insert.filename,
150
+ file_to_insert.source
151
+ .replace("__ENTRY_CSS__", style!.attributes.href)
152
+ .replace(
153
+ '"__FONTS_CSS__"',
154
+ `[${fonts.map((f) => `"${f}"`).join(",")}]`
155
+ )
156
+ );
157
+
158
+ const share_html_location = join(config.dir, "share.html");
159
+ const share_html = readFileSync(share_html_location, "utf8");
160
+ const share_tree = parse(share_html);
161
+ const node = Array.from(
162
+ share_tree.querySelectorAll("link[rel=stylesheet]")
163
+ ).find((node) => /.*\/index(.*?)\.css/.test(node.attributes.href));
164
+
165
+ if (!node) return;
166
+ const transformed_html =
167
+ share_html.substring(0, node.range[0]) +
168
+ share_html.substring(node.range[1], share_html.length);
169
+
170
+ writeFileSync(share_html_location, transformed_html);
171
+ }
172
+ };
173
+ }
174
+
175
+ // generate component importsy
176
+
177
+ import * as url from "url";
178
+ const __filename = url.fileURLToPath(import.meta.url);
179
+ const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
180
+
181
+ import { readdirSync, existsSync, readFileSync, statSync } from "fs";
182
+
183
+ function get_export_path(
184
+ path: string,
185
+ root: string,
186
+ pkg_json: Record<string, any>
187
+ ): boolean {
188
+ if (!pkg_json.exports) return false;
189
+ if ( typeof pkg_json.exports[`${path}`] === "object") return true;
190
+ const _path = join(root, "..", `${pkg_json.exports[`${path}`]}`);
191
+
192
+ return existsSync(_path);
193
+ }
194
+
195
+ const ignore_list = [
196
+ "tootils",
197
+ "_cdn-test",
198
+ "_spaces-test",
199
+ "_website",
200
+ "app",
201
+ "atoms",
202
+ "fallback",
203
+ "icons",
204
+ "preview",
205
+ "simpledropdown",
206
+ "simpleimage",
207
+ "simpletextbox",
208
+ "storybook",
209
+ "theme",
210
+ "timeseries",
211
+ "tooltip",
212
+ "upload",
213
+ "utils",
214
+ "sanitize",
215
+ "markdown-code"
216
+ ];
217
+ function generate_component_imports(): string {
218
+ const exports = readdirSync(join(__dirname, "..", ".."))
219
+ .map((dir) => {
220
+ if (ignore_list.includes(dir)) return undefined;
221
+ if (!statSync(join(__dirname, "..","..", dir)).isDirectory()) return undefined;
222
+
223
+ const package_json_path = join(__dirname, "..","..", dir, "package.json");
224
+ if (existsSync(package_json_path)) {
225
+ const package_json = JSON.parse(
226
+ readFileSync(package_json_path, "utf8")
227
+ );
228
+
229
+ const component = get_export_path(".", package_json_path, package_json);
230
+ const example = get_export_path(
231
+ "./example",
232
+ package_json_path,
233
+ package_json
234
+ );
235
+
236
+ const base = get_export_path("./base", package_json_path, package_json);
237
+
238
+ if (!component && !example) return undefined;
239
+
240
+ return {
241
+ name: package_json.name,
242
+ component,
243
+ example,
244
+ base
245
+ };
246
+ }
247
+ return undefined;
248
+ })
249
+ .filter((x) => x !== undefined);
250
+
251
+
252
+ const imports = exports.reduce((acc, _export) => {
253
+ if (!_export) return acc;
254
+
255
+ const example = _export.example
256
+ ? `example: () => import("${_export.name}/example"),\n`
257
+ : "";
258
+ const base = _export.base
259
+ ? `base: () => import("${_export.name}/base"),\n`
260
+ : "";
261
+ return `${acc}"${_export.name.replace("@gradio/", "")}": {
262
+ ${base}
263
+ ${example}
264
+ component: () => import("${_export.name}")
265
+ },\n`;
266
+ }, "");
267
+
268
+ return imports;
269
+ }
270
+
271
+ function load_virtual_component_loader(mode: string): string {
272
+ const loader_path = join(__dirname, "component_loader.js");
273
+ let component_map = "";
274
+
275
+ if (mode === "test") {
276
+ component_map = `
277
+ const component_map = {
278
+ "test-component-one": {
279
+ component: () => import("@gradio-test/test-one"),
280
+ example: () => import("@gradio-test/test-one/example")
281
+ },
282
+ "dataset": {
283
+ component: () => import("@gradio-test/test-two"),
284
+ example: () => import("@gradio-test/test-two/example")
285
+ },
286
+ "image": {
287
+ component: () => import("@gradio/image"),
288
+ example: () => import("@gradio/image/example"),
289
+ base: () => import("@gradio/image/base")
290
+ },
291
+ "audio": {
292
+ component: () => import("@gradio/audio"),
293
+ example: () => import("@gradio/audio/example"),
294
+ base: () => import("@gradio/audio/base")
295
+ },
296
+ "video": {
297
+ component: () => import("@gradio/video"),
298
+ example: () => import("@gradio/video/example"),
299
+ base: () => import("@gradio/video/base")
300
+ },
301
+ // "test-component-one": {
302
+ // component: () => import("@gradio-test/test-one"),
303
+ // example: () => import("@gradio-test/test-one/example")
304
+ // },
305
+ };
306
+ `;
307
+ } else {
308
+ component_map = `
309
+ const component_map = {
310
+ ${generate_component_imports()}
311
+ };
312
+ `;
313
+ }
314
+
315
+ return `${component_map}\n\n${readFileSync(loader_path, "utf8")}`;
316
+ }
317
+
318
+ export function inject_component_loader({ mode }: { mode: string }): Plugin {
319
+ const v_id = "virtual:component-loader";
320
+ const resolved_v_id = "\0" + v_id;
321
+
322
+ return {
323
+ name: "inject-component-loader",
324
+ enforce: "pre",
325
+ resolveId(id: string) {
326
+ if (id === v_id) return resolved_v_id;
327
+ },
328
+ load(id: string) {
329
+ this.addWatchFile(join(__dirname, "component_loader.js"));
330
+ if (id === resolved_v_id) {
331
+ return load_virtual_component_loader(mode);
332
+ }
333
+ }
334
+ };
335
+ }
336
+
337
+ export function inject_svelte_init_code({ mode }: { mode: string }): Plugin {
338
+ const v_id = "virtual:svelte-init";
339
+ const resolved_v_id = "\0" + v_id;
340
+
341
+
342
+ return {
343
+ name: "inject-component-loader",
344
+ enforce: "pre",
345
+ resolveId(id: string) {
346
+ if (id === v_id) return resolved_v_id;
347
+ },
348
+ load(id: string) {
349
+
350
+ if (id === resolved_v_id) {
351
+ return load_virtual_component_loader(mode);
352
+ }
353
+ }
354
+ }
355
+ }
356
+
357
+ import { createRequire } from "module";
358
+ const require = createRequire(import.meta.url);
359
+ const svelte = require("svelte/package.json");
360
+ export const svelte_exports = Object.keys(svelte.exports).map(
361
+ (entry) => "svelte" + entry.replace(/^\./, "svelte")
362
+ );
363
+ export const svelte_exports_transformed = Object.keys(svelte.exports).map(
364
+ (entry) => entry.replace(/^\./, "svelte").split("/").join("_") + ".js"
365
+ );
366
+
367
+
368
+
369
+ function make_init_code(): string {
370
+ const import_strings = svelte_exports
371
+ .map(
372
+ (entry: string) =>
373
+ `import * as ${entry
374
+ .replace(/\.js$/, "")
375
+ .replace(/-/g, "_")
376
+ .replace(/\//g, "_")} from "svelte/${entry}";`
377
+ )
378
+ .join("\n");
379
+ return `${import_strings}
380
+
381
+ const is_browser = typeof window !== "undefined";
382
+ if (is_browser) {
383
+ const o = {
384
+ SvelteComponent: svelte.SvelteComponent
385
+ };
386
+ for (const key in svelte) {
387
+ // if (key === "SvelteComponent") continue;
388
+ // if (key === "SvelteComponentDev") {
389
+ // //@ts-ignore
390
+ // o[key] = o["SvelteComponent"];
391
+ // } else {
392
+ // @ts-ignore
393
+ o[key] = svelte[key];
394
+ // }
395
+ }
396
+ window.__gradio__svelte__internal = o;
397
+ window.__gradio__svelte__internal["globals"] = {};
398
+ window.globals = window;
399
+ }
400
+ `;
401
+ }
402
+
403
+ export function resolve_svelte(enable: boolean): Plugin {
404
+ return {
405
+ enforce: "pre",
406
+ name: "resolve-svelte",
407
+ async resolveId(id: string) {
408
+ if (!enable) return;
409
+
410
+ if (
411
+ id === "./svelte/svelte.js" ||
412
+ id === "svelte" ||
413
+ id === "svelte/internal"
414
+ ) {
415
+ const mod = join(
416
+ __dirname,
417
+ "..",
418
+ "..",
419
+ "..",
420
+ "gradio",
421
+ "templates",
422
+ "frontend",
423
+ "assets",
424
+ "svelte",
425
+ "svelte.js"
426
+ );
427
+ return { id: mod, external: "absolute" };
428
+ }
429
+ }
430
+ };
431
+ }
432
+
433
+ export function mock_modules(): Plugin {
434
+ const v_id_1 = "@gradio-test/test-one";
435
+ const v_id_2 = "@gradio-test/test-two";
436
+ const v_id_1_example = "@gradio-test/test-one/example";
437
+ const v_id_2_example = "@gradio-test/test-two/example";
438
+ const resolved_v_id = "\0" + v_id_1;
439
+ const resolved_v_id_2 = "\0" + v_id_2;
440
+ const resolved_v_id_1_example = "\0" + v_id_1_example;
441
+ const resolved_v_id_2_example = "\0" + v_id_2_example;
442
+ const fallback_example = "@gradio/fallback/example";
443
+ const resolved_fallback_example = "\0" + fallback_example;
444
+
445
+ return {
446
+ name: "mock-modules",
447
+ enforce: "pre",
448
+ resolveId(id: string) {
449
+ if (id === v_id_1) return resolved_v_id;
450
+ if (id === v_id_2) return resolved_v_id_2;
451
+ if (id === v_id_1_example) return resolved_v_id_1_example;
452
+ if (id === v_id_2_example) return resolved_v_id_2_example;
453
+ if (id === fallback_example) return resolved_fallback_example;
454
+ },
455
+ load(id: string) {
456
+ if (
457
+ id === resolved_v_id ||
458
+ id === resolved_v_id_2 ||
459
+ id === resolved_v_id_1_example ||
460
+ id === resolved_v_id_2_example ||
461
+ id === resolved_fallback_example
462
+ ) {
463
+ return `export default {}`;
464
+ }
465
+ }
466
+ };
467
+ }