Upload folder using huggingface_hub
Browse files- 6.0.2/preview/package.json +57 -0
- 6.0.2/preview/src/_deepmerge_internal.ts +133 -0
- 6.0.2/preview/src/build.ts +115 -0
- 6.0.2/preview/src/compiler.ts +2 -0
- 6.0.2/preview/src/dev.ts +213 -0
- 6.0.2/preview/src/index.ts +202 -0
- 6.0.2/preview/src/placeholder.ts +1 -0
- 6.0.2/preview/src/plugins.ts +157 -0
- 6.0.2/preview/src/svelte-disclose.ts +2 -0
- 6.0.2/preview/src/svelte-internal.ts +2 -0
- 6.0.2/preview/src/svelte-submodules.ts +5 -0
- 6.0.2/preview/src/svelte.ts +17 -0
- 6.0.2/preview/test/test/frontend/Example.svelte +19 -0
- 6.0.2/preview/test/test/frontend/Index.svelte +113 -0
- 6.0.2/preview/test/test/frontend/package.json +22 -0
- 6.0.2/preview/vite.config.ts +39 -0
6.0.2/preview/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/preview",
|
| 3 |
+
"version": "0.15.1",
|
| 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:rollup": "rollup -c",
|
| 12 |
+
"build:vite": "vite build --ssr",
|
| 13 |
+
"build": "pnpm build:rollup && pnpm build:vite"
|
| 14 |
+
},
|
| 15 |
+
"devDependencies": {
|
| 16 |
+
"@rollup/plugin-commonjs": "^28.0.6",
|
| 17 |
+
"@rollup/plugin-json": "^6.1.0",
|
| 18 |
+
"@rollup/plugin-node-resolve": "^16.0.2",
|
| 19 |
+
"@rollup/plugin-typescript": "^12.1.4",
|
| 20 |
+
"rollup": "^4.52.4",
|
| 21 |
+
"svelte": "^5.43.4"
|
| 22 |
+
},
|
| 23 |
+
"dependencies": {
|
| 24 |
+
"@originjs/vite-plugin-commonjs": "^1.0.3",
|
| 25 |
+
"@rollup/plugin-sucrase": "^5.0.2",
|
| 26 |
+
"@rollup/plugin-terser": "^0.4.4",
|
| 27 |
+
"@sveltejs/vite-plugin-svelte": "^6.2.1",
|
| 28 |
+
"@types/which": "^3.0.4",
|
| 29 |
+
"coffeescript": "^2.7.0",
|
| 30 |
+
"lightningcss": "^1.30.2",
|
| 31 |
+
"pug": "^3.0.3",
|
| 32 |
+
"sass": "^1.93.2",
|
| 33 |
+
"stylus": "^0.64.0",
|
| 34 |
+
"sucrase": "^3.35.0",
|
| 35 |
+
"sugarss": "^5.0.1",
|
| 36 |
+
"svelte-preprocess": "^6.0.3",
|
| 37 |
+
"typescript": "^5.9.3",
|
| 38 |
+
"vite": "^7.1.9",
|
| 39 |
+
"which": "5.0.0",
|
| 40 |
+
"yootils": "^0.3.1"
|
| 41 |
+
},
|
| 42 |
+
"exports": {
|
| 43 |
+
".": {
|
| 44 |
+
"default": "./dist/index.js",
|
| 45 |
+
"import": "./dist/index.js",
|
| 46 |
+
"gradio": "./src/index.ts",
|
| 47 |
+
"svelte": "./dist/src/index.js",
|
| 48 |
+
"types": "./dist/index.d.ts"
|
| 49 |
+
},
|
| 50 |
+
"./package.json": "./package.json"
|
| 51 |
+
},
|
| 52 |
+
"repository": {
|
| 53 |
+
"type": "git",
|
| 54 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
| 55 |
+
"directory": "js/preview"
|
| 56 |
+
}
|
| 57 |
+
}
|
6.0.2/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.0.2/preview/src/build.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import * as fs from "fs";
|
| 2 |
+
import { join } from "path";
|
| 3 |
+
import { build } from "vite";
|
| 4 |
+
import { plugins, make_gradio_plugin, deepmerge_plugin } from "./plugins";
|
| 5 |
+
import type { PreRenderedChunk } from "rollup";
|
| 6 |
+
import { examine_module } from "./index";
|
| 7 |
+
|
| 8 |
+
interface BuildOptions {
|
| 9 |
+
component_dir: string;
|
| 10 |
+
root_dir: string;
|
| 11 |
+
python_path: string;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
export async function make_build({
|
| 15 |
+
component_dir,
|
| 16 |
+
root_dir,
|
| 17 |
+
python_path
|
| 18 |
+
}: BuildOptions): Promise<void> {
|
| 19 |
+
process.env.gradio_mode = "dev";
|
| 20 |
+
const svelte_dir = join(root_dir, "assets", "svelte");
|
| 21 |
+
|
| 22 |
+
const module_meta = examine_module(
|
| 23 |
+
component_dir,
|
| 24 |
+
root_dir,
|
| 25 |
+
python_path,
|
| 26 |
+
"build"
|
| 27 |
+
);
|
| 28 |
+
try {
|
| 29 |
+
for (const comp of module_meta) {
|
| 30 |
+
const template_dir = comp.template_dir;
|
| 31 |
+
const source_dir = comp.frontend_dir;
|
| 32 |
+
|
| 33 |
+
const pkg = JSON.parse(
|
| 34 |
+
fs.readFileSync(join(source_dir, "package.json"), "utf-8")
|
| 35 |
+
);
|
| 36 |
+
let component_config = {
|
| 37 |
+
plugins: [],
|
| 38 |
+
svelte: {
|
| 39 |
+
preprocess: []
|
| 40 |
+
},
|
| 41 |
+
build: {
|
| 42 |
+
target: []
|
| 43 |
+
},
|
| 44 |
+
optimizeDeps: {
|
| 45 |
+
exclude: ["svelte", "svelte/*"]
|
| 46 |
+
}
|
| 47 |
+
};
|
| 48 |
+
|
| 49 |
+
if (
|
| 50 |
+
comp.frontend_dir &&
|
| 51 |
+
fs.existsSync(join(comp.frontend_dir, "gradio.config.js"))
|
| 52 |
+
) {
|
| 53 |
+
const m = await import(
|
| 54 |
+
join("file://" + comp.frontend_dir, "gradio.config.js")
|
| 55 |
+
);
|
| 56 |
+
|
| 57 |
+
component_config.plugins = m.default.plugins || [];
|
| 58 |
+
component_config.svelte.preprocess = m.default.svelte?.preprocess || [];
|
| 59 |
+
component_config.build.target = m.default.build?.target || "modules";
|
| 60 |
+
component_config.optimizeDeps =
|
| 61 |
+
m.default.optimizeDeps || component_config.optimizeDeps;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
const exports: (string | any)[][] = [
|
| 65 |
+
["component", pkg.exports["."] as object],
|
| 66 |
+
["example", pkg.exports["./example"] as object]
|
| 67 |
+
].filter(([_, path]) => !!path);
|
| 68 |
+
|
| 69 |
+
for (const [entry, path] of exports) {
|
| 70 |
+
try {
|
| 71 |
+
const x = await build({
|
| 72 |
+
root: source_dir,
|
| 73 |
+
configFile: false,
|
| 74 |
+
plugins: [
|
| 75 |
+
...plugins(component_config),
|
| 76 |
+
make_gradio_plugin({ mode: "build", svelte_dir }),
|
| 77 |
+
deepmerge_plugin
|
| 78 |
+
],
|
| 79 |
+
build: {
|
| 80 |
+
emptyOutDir: true,
|
| 81 |
+
outDir: join(template_dir, entry as string),
|
| 82 |
+
lib: {
|
| 83 |
+
entry: join(source_dir, (path as any).gradio),
|
| 84 |
+
fileName: "index.js",
|
| 85 |
+
formats: ["es"]
|
| 86 |
+
},
|
| 87 |
+
minify: true,
|
| 88 |
+
rollupOptions: {
|
| 89 |
+
output: {
|
| 90 |
+
assetFileNames: (chunkInfo) => {
|
| 91 |
+
if (chunkInfo.names[0].endsWith(".css")) {
|
| 92 |
+
return `style.css`;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
return chunkInfo.names[0];
|
| 96 |
+
},
|
| 97 |
+
entryFileNames: (chunkInfo: PreRenderedChunk) => {
|
| 98 |
+
if (chunkInfo.isEntry) {
|
| 99 |
+
return "index.js";
|
| 100 |
+
}
|
| 101 |
+
return `${chunkInfo.name.toLocaleLowerCase()}.js`;
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
});
|
| 107 |
+
} catch (e) {
|
| 108 |
+
throw e;
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
} catch (e) {
|
| 113 |
+
throw e;
|
| 114 |
+
}
|
| 115 |
+
}
|
6.0.2/preview/src/compiler.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export * from "svelte/compiler";
|
| 2 |
+
export * as default from "svelte/compiler";
|
6.0.2/preview/src/dev.ts
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { join } from "path";
|
| 2 |
+
import * as fs from "fs";
|
| 3 |
+
import { createServer, createLogger } from "vite";
|
| 4 |
+
import { plugins, make_gradio_plugin, deepmerge_plugin } from "./plugins";
|
| 5 |
+
import { examine_module } from "./index";
|
| 6 |
+
import type { PreprocessorGroup } from "svelte/compiler";
|
| 7 |
+
|
| 8 |
+
const vite_messages_to_ignore = [
|
| 9 |
+
"Default and named imports from CSS files are deprecated.",
|
| 10 |
+
"The above dynamic import cannot be analyzed by Vite."
|
| 11 |
+
];
|
| 12 |
+
|
| 13 |
+
const logger = createLogger();
|
| 14 |
+
const originalWarning = logger.warn;
|
| 15 |
+
logger.warn = (msg, options) => {
|
| 16 |
+
if (vite_messages_to_ignore.some((m) => msg.includes(m))) return;
|
| 17 |
+
|
| 18 |
+
originalWarning(msg, options);
|
| 19 |
+
};
|
| 20 |
+
|
| 21 |
+
const originalError = logger.error;
|
| 22 |
+
|
| 23 |
+
logger.error = (msg, options) => {
|
| 24 |
+
if (msg && msg.includes("Pre-transform error")) return;
|
| 25 |
+
originalError(msg, options);
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
interface ServerOptions {
|
| 29 |
+
component_dir: string;
|
| 30 |
+
root_dir: string;
|
| 31 |
+
frontend_port: number;
|
| 32 |
+
backend_port: number;
|
| 33 |
+
host: string;
|
| 34 |
+
python_path: string;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
export async function create_server({
|
| 38 |
+
component_dir,
|
| 39 |
+
root_dir,
|
| 40 |
+
frontend_port,
|
| 41 |
+
backend_port,
|
| 42 |
+
host,
|
| 43 |
+
python_path
|
| 44 |
+
}: ServerOptions): Promise<void> {
|
| 45 |
+
process.env.gradio_mode = "dev";
|
| 46 |
+
const [imports, config] = await generate_imports(
|
| 47 |
+
component_dir,
|
| 48 |
+
root_dir,
|
| 49 |
+
python_path
|
| 50 |
+
);
|
| 51 |
+
|
| 52 |
+
const svelte_dir = join(root_dir, "assets", "svelte");
|
| 53 |
+
|
| 54 |
+
try {
|
| 55 |
+
const server = await createServer({
|
| 56 |
+
customLogger: logger,
|
| 57 |
+
mode: "development",
|
| 58 |
+
configFile: false,
|
| 59 |
+
root: root_dir,
|
| 60 |
+
server: {
|
| 61 |
+
port: frontend_port,
|
| 62 |
+
host: host,
|
| 63 |
+
fs: {
|
| 64 |
+
allow: [root_dir, component_dir]
|
| 65 |
+
}
|
| 66 |
+
},
|
| 67 |
+
optimizeDeps: config.optimizeDeps,
|
| 68 |
+
cacheDir: join(component_dir, "frontend", "node_modules", ".vite"),
|
| 69 |
+
plugins: [
|
| 70 |
+
...plugins(config),
|
| 71 |
+
make_gradio_plugin({
|
| 72 |
+
mode: "dev",
|
| 73 |
+
backend_port,
|
| 74 |
+
svelte_dir,
|
| 75 |
+
imports
|
| 76 |
+
}),
|
| 77 |
+
deepmerge_plugin
|
| 78 |
+
]
|
| 79 |
+
});
|
| 80 |
+
|
| 81 |
+
await server.listen();
|
| 82 |
+
|
| 83 |
+
console.info(
|
| 84 |
+
`[orange3]Frontend Server[/] (Go here): ${server.resolvedUrls?.local}`
|
| 85 |
+
);
|
| 86 |
+
} catch (e) {
|
| 87 |
+
console.error(e);
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
function find_frontend_folders(start_path: string): string[] {
|
| 92 |
+
if (!fs.existsSync(start_path)) {
|
| 93 |
+
console.warn("No directory found at:", start_path);
|
| 94 |
+
return [];
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
if (fs.existsSync(join(start_path, "pyproject.toml"))) return [start_path];
|
| 98 |
+
|
| 99 |
+
const results: string[] = [];
|
| 100 |
+
const dir = fs.readdirSync(start_path);
|
| 101 |
+
dir.forEach((dir) => {
|
| 102 |
+
const filepath = join(start_path, dir);
|
| 103 |
+
if (fs.existsSync(filepath)) {
|
| 104 |
+
if (fs.existsSync(join(filepath, "pyproject.toml")))
|
| 105 |
+
results.push(filepath);
|
| 106 |
+
}
|
| 107 |
+
});
|
| 108 |
+
|
| 109 |
+
return results;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
function to_posix(_path: string): string {
|
| 113 |
+
const isExtendedLengthPath = /^\\\\\?\\/.test(_path);
|
| 114 |
+
const hasNonAscii = /[^\u0000-\u0080]+/.test(_path);
|
| 115 |
+
|
| 116 |
+
if (isExtendedLengthPath || hasNonAscii) {
|
| 117 |
+
return _path;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
return _path.replace(/\\/g, "/");
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
export interface ComponentConfig {
|
| 124 |
+
plugins: any[];
|
| 125 |
+
svelte: {
|
| 126 |
+
preprocess: PreprocessorGroup[];
|
| 127 |
+
extensions?: string[];
|
| 128 |
+
};
|
| 129 |
+
build: {
|
| 130 |
+
target: string | string[];
|
| 131 |
+
};
|
| 132 |
+
optimizeDeps: object;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
async function generate_imports(
|
| 136 |
+
component_dir: string,
|
| 137 |
+
root: string,
|
| 138 |
+
python_path: string
|
| 139 |
+
): Promise<[string, ComponentConfig]> {
|
| 140 |
+
const components = find_frontend_folders(component_dir);
|
| 141 |
+
|
| 142 |
+
const component_entries = components.flatMap((component) => {
|
| 143 |
+
return examine_module(component, root, python_path, "dev");
|
| 144 |
+
});
|
| 145 |
+
if (component_entries.length === 0) {
|
| 146 |
+
console.info(
|
| 147 |
+
`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.`
|
| 148 |
+
);
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
let component_config: ComponentConfig = {
|
| 152 |
+
plugins: [],
|
| 153 |
+
svelte: {
|
| 154 |
+
preprocess: []
|
| 155 |
+
},
|
| 156 |
+
build: {
|
| 157 |
+
target: []
|
| 158 |
+
},
|
| 159 |
+
optimizeDeps: {
|
| 160 |
+
exclude: ["svelte", "svelte/*"]
|
| 161 |
+
}
|
| 162 |
+
};
|
| 163 |
+
|
| 164 |
+
await Promise.all(
|
| 165 |
+
component_entries.map(async (component) => {
|
| 166 |
+
if (
|
| 167 |
+
component.frontend_dir &&
|
| 168 |
+
fs.existsSync(join(component.frontend_dir, "gradio.config.js"))
|
| 169 |
+
) {
|
| 170 |
+
const m = await import(
|
| 171 |
+
join("file://" + component.frontend_dir, "gradio.config.js")
|
| 172 |
+
);
|
| 173 |
+
|
| 174 |
+
component_config.plugins = m.default.plugins || [];
|
| 175 |
+
component_config.svelte.preprocess = m.default.svelte?.preprocess || [];
|
| 176 |
+
component_config.build.target = m.default.build?.target || "modules";
|
| 177 |
+
component_config.optimizeDeps =
|
| 178 |
+
m.default.optimizeDeps || component_config.optimizeDeps;
|
| 179 |
+
} else {
|
| 180 |
+
}
|
| 181 |
+
})
|
| 182 |
+
);
|
| 183 |
+
|
| 184 |
+
const imports = component_entries.reduce((acc, component) => {
|
| 185 |
+
const pkg = JSON.parse(
|
| 186 |
+
fs.readFileSync(join(component.frontend_dir, "package.json"), "utf-8")
|
| 187 |
+
);
|
| 188 |
+
|
| 189 |
+
const exports: Record<string, any | undefined> = {
|
| 190 |
+
component: pkg.exports["."],
|
| 191 |
+
example: pkg.exports["./example"]
|
| 192 |
+
};
|
| 193 |
+
|
| 194 |
+
if (!exports.component)
|
| 195 |
+
throw new Error(
|
| 196 |
+
"Could not find component entry point. Please check the exports field of your package.json."
|
| 197 |
+
);
|
| 198 |
+
|
| 199 |
+
const example = exports.example
|
| 200 |
+
? `example: () => import("/@fs/${to_posix(
|
| 201 |
+
join(component.frontend_dir, exports.example.gradio)
|
| 202 |
+
)}"),\n`
|
| 203 |
+
: "";
|
| 204 |
+
return `${acc}"${component.component_class_id}": {
|
| 205 |
+
${example}
|
| 206 |
+
component: () => import("/@fs/${to_posix(
|
| 207 |
+
join(component.frontend_dir, exports.component.gradio)
|
| 208 |
+
)}")
|
| 209 |
+
},\n`;
|
| 210 |
+
}, "");
|
| 211 |
+
|
| 212 |
+
return [`{${imports}}`, component_config];
|
| 213 |
+
}
|
6.0.2/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.0.2/preview/src/placeholder.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
export default {};
|
6.0.2/preview/src/plugins.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { Plugin, PluginOption } from "vite";
|
| 2 |
+
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
| 3 |
+
import preprocess from "svelte-preprocess";
|
| 4 |
+
import { join } from "path";
|
| 5 |
+
import { type ComponentConfig } from "./dev";
|
| 6 |
+
import type { Preprocessor, PreprocessorGroup } from "svelte/compiler";
|
| 7 |
+
import { deepmerge } from "./_deepmerge_internal";
|
| 8 |
+
|
| 9 |
+
const svelte_codes_to_ignore: Record<string, string> = {
|
| 10 |
+
"reactive-component": "Icon"
|
| 11 |
+
};
|
| 12 |
+
|
| 13 |
+
const RE_SVELTE_IMPORT =
|
| 14 |
+
/import\s+(?:([ -~]*)\s+from\s+){0,1}['"](svelte(?:\/[ -~]+){0,3})['"]/g;
|
| 15 |
+
// const RE_BARE_SVELTE_IMPORT = /import ("|')svelte(\/\w+)*("|')(;)*/g;
|
| 16 |
+
export function plugins(config: ComponentConfig): PluginOption[] {
|
| 17 |
+
const _additional_plugins = config.plugins || [];
|
| 18 |
+
const _additional_svelte_preprocess = config.svelte?.preprocess || [];
|
| 19 |
+
const _svelte_extensions = (config.svelte?.extensions || [".svelte"]).map(
|
| 20 |
+
(ext) => {
|
| 21 |
+
if (ext.trim().startsWith(".")) {
|
| 22 |
+
return ext;
|
| 23 |
+
}
|
| 24 |
+
return `.${ext.trim()}`;
|
| 25 |
+
}
|
| 26 |
+
);
|
| 27 |
+
|
| 28 |
+
if (!_svelte_extensions.includes(".svelte")) {
|
| 29 |
+
_svelte_extensions.push(".svelte");
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
return [
|
| 33 |
+
svelte({
|
| 34 |
+
inspector: false,
|
| 35 |
+
onwarn(warning, handler) {
|
| 36 |
+
if (
|
| 37 |
+
svelte_codes_to_ignore.hasOwnProperty(warning.code) &&
|
| 38 |
+
svelte_codes_to_ignore[warning.code] &&
|
| 39 |
+
warning.message.includes(svelte_codes_to_ignore[warning.code])
|
| 40 |
+
) {
|
| 41 |
+
return;
|
| 42 |
+
}
|
| 43 |
+
handler!(warning);
|
| 44 |
+
},
|
| 45 |
+
prebundleSvelteLibraries: false,
|
| 46 |
+
compilerOptions: {
|
| 47 |
+
discloseVersion: false,
|
| 48 |
+
hmr: true
|
| 49 |
+
},
|
| 50 |
+
extensions: _svelte_extensions,
|
| 51 |
+
preprocess: [
|
| 52 |
+
preprocess({
|
| 53 |
+
typescript: {
|
| 54 |
+
compilerOptions: {
|
| 55 |
+
declaration: false,
|
| 56 |
+
declarationMap: false
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
}),
|
| 60 |
+
...(_additional_svelte_preprocess as PreprocessorGroup[])
|
| 61 |
+
]
|
| 62 |
+
}),
|
| 63 |
+
..._additional_plugins
|
| 64 |
+
];
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
interface GradioPluginOptions {
|
| 68 |
+
mode: "dev" | "build";
|
| 69 |
+
svelte_dir: string;
|
| 70 |
+
backend_port?: number;
|
| 71 |
+
imports?: string;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
export function make_gradio_plugin({
|
| 75 |
+
mode,
|
| 76 |
+
svelte_dir,
|
| 77 |
+
backend_port,
|
| 78 |
+
imports
|
| 79 |
+
}: GradioPluginOptions): Plugin {
|
| 80 |
+
const v_id = "virtual:component-loader";
|
| 81 |
+
const resolved_v_id = "\0" + v_id;
|
| 82 |
+
return {
|
| 83 |
+
name: "gradio",
|
| 84 |
+
enforce: "pre",
|
| 85 |
+
resolveId(id) {
|
| 86 |
+
if (id === v_id) {
|
| 87 |
+
return resolved_v_id;
|
| 88 |
+
}
|
| 89 |
+
if (id === "svelte") {
|
| 90 |
+
return {
|
| 91 |
+
id: `../../../../../assets/svelte/svelte_svelte.js`,
|
| 92 |
+
external: true
|
| 93 |
+
};
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
if (id.startsWith("svelte/")) {
|
| 97 |
+
const subpath = id.slice("svelte/".length);
|
| 98 |
+
|
| 99 |
+
return {
|
| 100 |
+
id: `../../../../../assets/svelte/svelte_${subpath.replace(/\//g, "_")}.js`,
|
| 101 |
+
external: true
|
| 102 |
+
};
|
| 103 |
+
}
|
| 104 |
+
},
|
| 105 |
+
load(id) {
|
| 106 |
+
if (id === resolved_v_id) {
|
| 107 |
+
return `export default {};`;
|
| 108 |
+
}
|
| 109 |
+
},
|
| 110 |
+
transformIndexHtml(html) {
|
| 111 |
+
return mode === "dev"
|
| 112 |
+
? [
|
| 113 |
+
{
|
| 114 |
+
tag: "script",
|
| 115 |
+
children: `window.__GRADIO_DEV__ = "dev";
|
| 116 |
+
window.__GRADIO__SERVER_PORT__ = ${backend_port};
|
| 117 |
+
window.__GRADIO__CC__ = ${imports};`
|
| 118 |
+
}
|
| 119 |
+
]
|
| 120 |
+
: undefined;
|
| 121 |
+
}
|
| 122 |
+
};
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
export const deepmerge_plugin: Plugin = {
|
| 126 |
+
name: "deepmerge",
|
| 127 |
+
enforce: "pre",
|
| 128 |
+
resolveId(id) {
|
| 129 |
+
if (id === "deepmerge") {
|
| 130 |
+
return "deepmerge_internal";
|
| 131 |
+
}
|
| 132 |
+
},
|
| 133 |
+
load(id) {
|
| 134 |
+
if (id === "deepmerge_internal") {
|
| 135 |
+
return deepmerge;
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
};
|
| 139 |
+
|
| 140 |
+
function extract_types(str: string): string[] {
|
| 141 |
+
const regex = /type (\w+\b)/g;
|
| 142 |
+
let m;
|
| 143 |
+
const out = [];
|
| 144 |
+
while ((m = regex.exec(str))) out.push(m[1]);
|
| 145 |
+
|
| 146 |
+
return out;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
function remove_types(input: string): string {
|
| 150 |
+
const inner = input.slice(1, -1); // remove { }
|
| 151 |
+
const parts = inner
|
| 152 |
+
.split(",")
|
| 153 |
+
.map((s) => s.trim())
|
| 154 |
+
.filter((s) => s && !/^type\s+\w+\b$/.test(s));
|
| 155 |
+
|
| 156 |
+
return `{ ${parts.join(", ")} }`;
|
| 157 |
+
}
|
6.0.2/preview/src/svelte-disclose.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// @ts-ignore
|
| 2 |
+
export * from "svelte/internal/disclose-version";
|
6.0.2/preview/src/svelte-internal.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//@ts-ignore
|
| 2 |
+
export * from "svelte/internal";
|
6.0.2/preview/src/svelte-submodules.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export * from "svelte/transition";
|
| 2 |
+
export { spring, tweened } from "svelte/motion";
|
| 3 |
+
export * from "svelte/store";
|
| 4 |
+
export * from "svelte/easing";
|
| 5 |
+
export * from "svelte/animate";
|
6.0.2/preview/src/svelte.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export {
|
| 2 |
+
// this proxy is very important, to ensure that we always refer to the same base Component class which is critical for our components to work
|
| 3 |
+
SvelteComponent as SvelteComponentDev,
|
| 4 |
+
SvelteComponent,
|
| 5 |
+
onMount,
|
| 6 |
+
onDestroy,
|
| 7 |
+
beforeUpdate,
|
| 8 |
+
afterUpdate,
|
| 9 |
+
setContext,
|
| 10 |
+
getContext,
|
| 11 |
+
getAllContexts,
|
| 12 |
+
hasContext,
|
| 13 |
+
tick,
|
| 14 |
+
createEventDispatcher,
|
| 15 |
+
SvelteComponentTyped
|
| 16 |
+
// @ts-ignore
|
| 17 |
+
} from "svelte/internal";
|
6.0.2/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.0.2/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 →</a
|
| 107 |
+
>
|
| 108 |
+
</p>
|
| 109 |
+
</div>
|
| 110 |
+
</div>
|
| 111 |
+
</div>
|
| 112 |
+
</div>
|
| 113 |
+
</div>
|
6.0.2/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.0.2/preview/vite.config.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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/register.mjs", join(template_dir, "register.mjs"));
|
| 31 |
+
cpSync("./src/hooks.mjs", join(template_dir, "hooks.mjs"));
|
| 32 |
+
cpSync(
|
| 33 |
+
join(template_dir, "frontend", "assets", "svelte"),
|
| 34 |
+
join(template_dir, "node", "build", "client", "_app"),
|
| 35 |
+
{ recursive: true }
|
| 36 |
+
);
|
| 37 |
+
}
|
| 38 |
+
};
|
| 39 |
+
}
|