open-webui/scripts/prepare-pyodide.js

86 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-05-18 21:24:32 +00:00
const packages = [
2024-06-17 21:26:10 +00:00
'micropip',
'packaging',
2024-05-18 21:24:32 +00:00
'requests',
'beautifulsoup4',
'numpy',
'pandas',
'matplotlib',
'scikit-learn',
'scipy',
'regex',
'seaborn'
2024-05-18 21:24:32 +00:00
];
import { loadPyodide } from 'pyodide';
2024-06-17 21:26:10 +00:00
import { writeFile, readFile, copyFile, readdir, rmdir } from 'fs/promises';
async function downloadPackages() {
console.log('Setting up pyodide + micropip');
2024-06-19 02:44:32 +00:00
let pyodide;
try {
pyodide = await loadPyodide({
packageCacheDir: 'static/pyodide'
});
} catch (err) {
console.error('Failed to load Pyodide:', err);
return;
}
2024-06-17 21:26:10 +00:00
const packageJson = JSON.parse(await readFile('package.json'));
2024-06-19 02:44:32 +00:00
const pyodideVersion = packageJson.dependencies.pyodide.replace('^', '');
2024-06-17 21:26:10 +00:00
2024-06-17 21:28:56 +00:00
try {
const pyodidePackageJson = JSON.parse(await readFile('static/pyodide/package.json'));
2024-06-19 02:44:32 +00:00
const pyodidePackageVersion = pyodidePackageJson.version.replace('^', '');
2024-06-17 21:28:56 +00:00
2024-06-19 02:44:32 +00:00
if (pyodideVersion !== pyodidePackageVersion) {
2024-06-17 21:28:56 +00:00
console.log('Pyodide version mismatch, removing static/pyodide directory');
await rmdir('static/pyodide', { recursive: true });
}
} catch (e) {
2024-06-19 02:44:32 +00:00
console.log('Pyodide package not found, proceeding with download.');
2024-06-17 21:26:10 +00:00
}
2024-06-19 02:44:32 +00:00
try {
console.log('Loading micropip package');
await pyodide.loadPackage('micropip');
const micropip = pyodide.pyimport('micropip');
console.log('Downloading Pyodide packages:', packages);
try {
for (const pkg of packages) {
console.log(`Installing package: ${pkg}`);
await micropip.install(pkg);
}
} catch (err) {
console.error('Package installation failed:', err);
return;
}
console.log('Pyodide packages downloaded, freezing into lock file');
try {
const lockFile = await micropip.freeze();
await writeFile('static/pyodide/pyodide-lock.json', lockFile);
} catch (err) {
console.error('Failed to write lock file:', err);
}
} catch (err) {
console.error('Failed to load or install micropip:', err);
}
}
async function copyPyodide() {
console.log('Copying Pyodide files into static directory');
// Copy all files from node_modules/pyodide to static/pyodide
for await (const entry of await readdir('node_modules/pyodide')) {
await copyFile(`node_modules/pyodide/${entry}`, `static/pyodide/${entry}`);
}
}
await downloadPackages();
await copyPyodide();