Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
machineuser
commited on
Commit
·
ab9170f
1
Parent(s):
76bddde
Sync widgets demo
Browse files
packages/tasks/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.python_generated
|
packages/tasks/package.json
CHANGED
|
@@ -44,6 +44,6 @@
|
|
| 44 |
"license": "MIT",
|
| 45 |
"devDependencies": {
|
| 46 |
"@types/node": "^20.11.5",
|
| 47 |
-
"quicktype-core": "https://github.com/huggingface/quicktype/raw/pack-18.0.
|
| 48 |
}
|
| 49 |
}
|
|
|
|
| 44 |
"license": "MIT",
|
| 45 |
"devDependencies": {
|
| 46 |
"@types/node": "^20.11.5",
|
| 47 |
+
"quicktype-core": "https://github.com/huggingface/quicktype/raw/pack-18.0.17/packages/quicktype-core/quicktype-core-18.0.17.tgz"
|
| 48 |
}
|
| 49 |
}
|
packages/tasks/pnpm-lock.yaml
CHANGED
|
@@ -9,8 +9,8 @@ devDependencies:
|
|
| 9 |
specifier: ^20.11.5
|
| 10 |
version: 20.11.5
|
| 11 |
quicktype-core:
|
| 12 |
-
specifier: https://github.com/huggingface/quicktype/raw/pack-18.0.
|
| 13 |
-
version: '@github.com/huggingface/quicktype/raw/pack-18.0.
|
| 14 |
|
| 15 |
packages:
|
| 16 |
|
|
@@ -189,10 +189,10 @@ packages:
|
|
| 189 |
engines: {node: '>= 14'}
|
| 190 |
dev: true
|
| 191 |
|
| 192 |
-
'@github.com/huggingface/quicktype/raw/pack-18.0.
|
| 193 |
-
resolution: {tarball: https://github.com/huggingface/quicktype/raw/pack-18.0.
|
| 194 |
name: quicktype-core
|
| 195 |
-
version: 18.0.
|
| 196 |
dependencies:
|
| 197 |
'@glideapps/ts-necessities': 2.1.3
|
| 198 |
'@types/urijs': 1.19.25
|
|
|
|
| 9 |
specifier: ^20.11.5
|
| 10 |
version: 20.11.5
|
| 11 |
quicktype-core:
|
| 12 |
+
specifier: https://github.com/huggingface/quicktype/raw/pack-18.0.17/packages/quicktype-core/quicktype-core-18.0.17.tgz
|
| 13 |
+
version: '@github.com/huggingface/quicktype/raw/pack-18.0.17/packages/quicktype-core/quicktype-core-18.0.17.tgz'
|
| 14 |
|
| 15 |
packages:
|
| 16 |
|
|
|
|
| 189 |
engines: {node: '>= 14'}
|
| 190 |
dev: true
|
| 191 |
|
| 192 |
+
'@github.com/huggingface/quicktype/raw/pack-18.0.17/packages/quicktype-core/quicktype-core-18.0.17.tgz':
|
| 193 |
+
resolution: {tarball: https://github.com/huggingface/quicktype/raw/pack-18.0.17/packages/quicktype-core/quicktype-core-18.0.17.tgz}
|
| 194 |
name: quicktype-core
|
| 195 |
+
version: 18.0.17
|
| 196 |
dependencies:
|
| 197 |
'@glideapps/ts-necessities': 2.1.3
|
| 198 |
'@types/urijs': 1.19.25
|
packages/tasks/scripts/inference-codegen.ts
CHANGED
|
@@ -14,6 +14,16 @@ const TYPESCRIPT_HEADER_FILE = `
|
|
| 14 |
|
| 15 |
`;
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
const rootDirFinder = function (): string {
|
| 18 |
let currentPath = path.normalize(import.meta.url);
|
| 19 |
|
|
@@ -62,9 +72,24 @@ async function generateTypescript(inputData: InputData): Promise<SerializedRende
|
|
| 62 |
"prefer-const-values": true,
|
| 63 |
"prefer-unknown": true,
|
| 64 |
"explicit-unions": true,
|
|
|
|
| 65 |
},
|
| 66 |
});
|
| 67 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
/**
|
| 69 |
* quicktype is unable to generate "top-level array types" that are defined in the output spec: https://github.com/glideapps/quicktype/issues/2481
|
| 70 |
* We have to use the TypeScript API to generate those types when required.
|
|
@@ -180,5 +205,17 @@ for (const { task, dirPath } of allTasks) {
|
|
| 180 |
|
| 181 |
console.log(" 🩹 Post-processing the generated code");
|
| 182 |
await postProcessOutput(`${dirPath}/inference.ts`, outputSpec);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
}
|
| 184 |
console.debug("✅ All done!");
|
|
|
|
| 14 |
|
| 15 |
`;
|
| 16 |
|
| 17 |
+
const PYTHON_HEADER_FILE = `
|
| 18 |
+
# Inference code generated from the JSON schema spec in @huggingface/tasks.
|
| 19 |
+
#
|
| 20 |
+
# See:
|
| 21 |
+
# - script: https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/scripts/inference-codegen.ts
|
| 22 |
+
# - specs: https://github.com/huggingface/huggingface.js/tree/main/packages/tasks/src/tasks.
|
| 23 |
+
`;
|
| 24 |
+
|
| 25 |
+
const PYTHON_DIR = "./.python_generated";
|
| 26 |
+
|
| 27 |
const rootDirFinder = function (): string {
|
| 28 |
let currentPath = path.normalize(import.meta.url);
|
| 29 |
|
|
|
|
| 72 |
"prefer-const-values": true,
|
| 73 |
"prefer-unknown": true,
|
| 74 |
"explicit-unions": true,
|
| 75 |
+
"runtime-typecheck": false,
|
| 76 |
},
|
| 77 |
});
|
| 78 |
}
|
| 79 |
+
|
| 80 |
+
async function generatePython(inputData: InputData): Promise<SerializedRenderResult> {
|
| 81 |
+
return await quicktype({
|
| 82 |
+
inputData,
|
| 83 |
+
lang: "python",
|
| 84 |
+
alphabetizeProperties: true,
|
| 85 |
+
rendererOptions: {
|
| 86 |
+
"just-types": true,
|
| 87 |
+
"nice-property-names": true,
|
| 88 |
+
"python-version": "3.7",
|
| 89 |
+
},
|
| 90 |
+
});
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
/**
|
| 94 |
* quicktype is unable to generate "top-level array types" that are defined in the output spec: https://github.com/glideapps/quicktype/issues/2481
|
| 95 |
* We have to use the TypeScript API to generate those types when required.
|
|
|
|
| 205 |
|
| 206 |
console.log(" 🩹 Post-processing the generated code");
|
| 207 |
await postProcessOutput(`${dirPath}/inference.ts`, outputSpec);
|
| 208 |
+
|
| 209 |
+
console.debug(" 🏭 Generating Python code");
|
| 210 |
+
{
|
| 211 |
+
const { lines } = await generatePython(inputData);
|
| 212 |
+
const pythonFilename = `${task}.py`.replace(/-/g, "_");
|
| 213 |
+
const pythonPath = `${PYTHON_DIR}/${pythonFilename}`;
|
| 214 |
+
await fs.mkdir(PYTHON_DIR, { recursive: true });
|
| 215 |
+
await fs.writeFile(pythonPath, [PYTHON_HEADER_FILE, ...lines].join(`\n`), {
|
| 216 |
+
flag: "w+",
|
| 217 |
+
encoding: "utf-8",
|
| 218 |
+
});
|
| 219 |
+
}
|
| 220 |
}
|
| 221 |
console.debug("✅ All done!");
|