Add Electron desktop app to portable builds

This commit is contained in:
oobabooga
2026-05-01 23:05:14 -07:00
parent c6a57a4572
commit cfda0991ce
9 changed files with 489 additions and 6 deletions

View File

@@ -150,6 +150,56 @@ jobs:
# 5. Clean up
rm -rf .git cmd* update_wizard* Colab-TextGen-GPU.ipynb docker setup.cfg .github .gitignore requirements/ one_click.py
# 5b. Bundle Electron desktop launcher
ELECTRON_VERSION="41.5.0"
APP_DIR="app"
if [[ "$RUNNER_OS" == "Windows" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-win32-x64.zip"
ELECTRON_BIN="electron/electron.exe"
rm -f start_windows.bat
elif [[ "$RUNNER_OS" == "macOS" ]]; then
if [[ "$OS_TYPE" == "macos-15-intel" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-x64.zip"
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-arm64.zip"
fi
ELECTRON_BIN="electron/Electron.app/Contents/MacOS/Electron"
rm -f start_macos.sh
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-linux-x64.zip"
ELECTRON_BIN="electron/electron"
rm -f start_linux.sh
fi
echo "Downloading Electron ${ELECTRON_VERSION} (${ELECTRON_ZIP})..."
curl -L -o /tmp/electron.zip \
"https://github.com/electron/electron/releases/download/v${ELECTRON_VERSION}/${ELECTRON_ZIP}"
mkdir electron
unzip -q /tmp/electron.zip -d electron
rm /tmp/electron.zip
if [[ "$RUNNER_OS" == "Windows" ]]; then
sed "s|__APP__|${APP_DIR}|g" desktop/textgen.bat > textgen.bat
sed -i 's/$/\r/' textgen.bat
else
sed "s|__APP__|${APP_DIR}|g; s|__ELECTRON__|${ELECTRON_BIN}|g" desktop/textgen.sh > textgen
chmod +x textgen
fi
mv desktop/main.js desktop/package.json .
rm -rf desktop
# 5c. Restructure: textgen-VERSION/{textgen, user_data/, app/<everything else>}
mkdir "${APP_DIR}"
shopt -s dotglob
for item in *; do
case "$item" in
"${APP_DIR}"|user_data|textgen|textgen.bat) ;;
*) mv "$item" "${APP_DIR}/" ;;
esac
done
shopt -u dotglob
# 6. Create archive
cd ..
if [[ "$RUNNER_OS" == "Windows" ]]; then

View File

@@ -143,9 +143,6 @@ jobs:
REQ_FILE="requirements/portable/requirements_ik.txt"
fi
# 4. Inject --ik into start scripts
sed -i 's/--portable/--portable --ik/g' start_linux.sh start_windows.bat 2>/dev/null || true
# 5. Install packages
echo "Installing Python packages from $REQ_FILE..."
$PIP_PATH install --target="./$PACKAGES_PATH" -r "$REQ_FILE"
@@ -153,6 +150,61 @@ jobs:
# 6. Clean up
rm -rf .git cmd* update_wizard* Colab-TextGen-GPU.ipynb docker setup.cfg .github .gitignore requirements/ one_click.py
# 5b. Bundle Electron desktop launcher
ELECTRON_VERSION="41.5.0"
APP_DIR="app"
if [[ "$RUNNER_OS" == "Windows" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-win32-x64.zip"
ELECTRON_BIN="electron/electron.exe"
rm -f start_windows.bat
elif [[ "$RUNNER_OS" == "macOS" ]]; then
if [[ "$OS_TYPE" == "macos-15-intel" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-x64.zip"
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-arm64.zip"
fi
ELECTRON_BIN="electron/Electron.app/Contents/MacOS/Electron"
rm -f start_macos.sh
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-linux-x64.zip"
ELECTRON_BIN="electron/electron"
rm -f start_linux.sh
fi
echo "Downloading Electron ${ELECTRON_VERSION} (${ELECTRON_ZIP})..."
curl -L -o /tmp/electron.zip \
"https://github.com/electron/electron/releases/download/v${ELECTRON_VERSION}/${ELECTRON_ZIP}"
mkdir electron
unzip -q /tmp/electron.zip -d electron
rm /tmp/electron.zip
if [[ "$RUNNER_OS" == "Windows" ]]; then
sed "s|__APP__|${APP_DIR}|g" desktop/textgen.bat > textgen.bat
sed -i 's/$/\r/' textgen.bat
else
sed "s|__APP__|${APP_DIR}|g; s|__ELECTRON__|${ELECTRON_BIN}|g" desktop/textgen.sh > textgen
chmod +x textgen
fi
mv desktop/main.js desktop/package.json .
rm -rf desktop
# 5c. Restructure: textgen-VERSION/{textgen, user_data/, app/<everything else>}
mkdir "${APP_DIR}"
shopt -s dotglob
for item in *; do
case "$item" in
"${APP_DIR}"|user_data|textgen|textgen.bat) ;;
*) mv "$item" "${APP_DIR}/" ;;
esac
done
shopt -u dotglob
# 5d. Inject --ik into spawn args
sed -i 's/"--portable", "--api"/"--portable", "--ik", "--api"/' "${APP_DIR}/main.js"
sed -i 's|--portable --api|--portable --ik --api|g' textgen 2>/dev/null || true
sed -i 's|--portable --api|--portable --ik --api|g' textgen.bat 2>/dev/null || true
# 7. Create archive
cd ..
if [[ "$RUNNER_OS" == "Windows" ]]; then

View File

@@ -138,9 +138,6 @@ jobs:
REQ_FILE="requirements/portable/requirements_ik_cpu_only.txt"
echo "Using requirements file: $REQ_FILE"
# 4. Inject --ik into start scripts
sed -i 's/--portable/--portable --ik/g' start_linux.sh start_windows.bat 2>/dev/null || true
# 5. Install packages
echo "Installing Python packages from $REQ_FILE..."
$PIP_PATH install --target="./$PACKAGES_PATH" -r "$REQ_FILE"
@@ -148,6 +145,61 @@ jobs:
# 6. Clean up
rm -rf .git cmd* update_wizard* Colab-TextGen-GPU.ipynb docker setup.cfg .github .gitignore requirements/ one_click.py
# 5b. Bundle Electron desktop launcher
ELECTRON_VERSION="41.5.0"
APP_DIR="app"
if [[ "$RUNNER_OS" == "Windows" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-win32-x64.zip"
ELECTRON_BIN="electron/electron.exe"
rm -f start_windows.bat
elif [[ "$RUNNER_OS" == "macOS" ]]; then
if [[ "$OS_TYPE" == "macos-15-intel" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-x64.zip"
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-arm64.zip"
fi
ELECTRON_BIN="electron/Electron.app/Contents/MacOS/Electron"
rm -f start_macos.sh
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-linux-x64.zip"
ELECTRON_BIN="electron/electron"
rm -f start_linux.sh
fi
echo "Downloading Electron ${ELECTRON_VERSION} (${ELECTRON_ZIP})..."
curl -L -o /tmp/electron.zip \
"https://github.com/electron/electron/releases/download/v${ELECTRON_VERSION}/${ELECTRON_ZIP}"
mkdir electron
unzip -q /tmp/electron.zip -d electron
rm /tmp/electron.zip
if [[ "$RUNNER_OS" == "Windows" ]]; then
sed "s|__APP__|${APP_DIR}|g" desktop/textgen.bat > textgen.bat
sed -i 's/$/\r/' textgen.bat
else
sed "s|__APP__|${APP_DIR}|g; s|__ELECTRON__|${ELECTRON_BIN}|g" desktop/textgen.sh > textgen
chmod +x textgen
fi
mv desktop/main.js desktop/package.json .
rm -rf desktop
# 5c. Restructure: textgen-VERSION/{textgen, user_data/, app/<everything else>}
mkdir "${APP_DIR}"
shopt -s dotglob
for item in *; do
case "$item" in
"${APP_DIR}"|user_data|textgen|textgen.bat) ;;
*) mv "$item" "${APP_DIR}/" ;;
esac
done
shopt -u dotglob
# 5d. Inject --ik into spawn args
sed -i 's/"--portable", "--api"/"--portable", "--ik", "--api"/' "${APP_DIR}/main.js"
sed -i 's|--portable --api|--portable --ik --api|g' textgen 2>/dev/null || true
sed -i 's|--portable --api|--portable --ik --api|g' textgen.bat 2>/dev/null || true
# 7. Create archive
cd ..
if [[ "$RUNNER_OS" == "Windows" ]]; then

View File

@@ -145,6 +145,56 @@ jobs:
# 5. Clean up
rm -rf .git cmd* update_wizard* Colab-TextGen-GPU.ipynb docker setup.cfg .github .gitignore requirements/ one_click.py
# 5b. Bundle Electron desktop launcher
ELECTRON_VERSION="41.5.0"
APP_DIR="app"
if [[ "$RUNNER_OS" == "Windows" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-win32-x64.zip"
ELECTRON_BIN="electron/electron.exe"
rm -f start_windows.bat
elif [[ "$RUNNER_OS" == "macOS" ]]; then
if [[ "$OS_TYPE" == "macos-15-intel" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-x64.zip"
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-arm64.zip"
fi
ELECTRON_BIN="electron/Electron.app/Contents/MacOS/Electron"
rm -f start_macos.sh
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-linux-x64.zip"
ELECTRON_BIN="electron/electron"
rm -f start_linux.sh
fi
echo "Downloading Electron ${ELECTRON_VERSION} (${ELECTRON_ZIP})..."
curl -L -o /tmp/electron.zip \
"https://github.com/electron/electron/releases/download/v${ELECTRON_VERSION}/${ELECTRON_ZIP}"
mkdir electron
unzip -q /tmp/electron.zip -d electron
rm /tmp/electron.zip
if [[ "$RUNNER_OS" == "Windows" ]]; then
sed "s|__APP__|${APP_DIR}|g" desktop/textgen.bat > textgen.bat
sed -i 's/$/\r/' textgen.bat
else
sed "s|__APP__|${APP_DIR}|g; s|__ELECTRON__|${ELECTRON_BIN}|g" desktop/textgen.sh > textgen
chmod +x textgen
fi
mv desktop/main.js desktop/package.json .
rm -rf desktop
# 5c. Restructure: textgen-VERSION/{textgen, user_data/, app/<everything else>}
mkdir "${APP_DIR}"
shopt -s dotglob
for item in *; do
case "$item" in
"${APP_DIR}"|user_data|textgen|textgen.bat) ;;
*) mv "$item" "${APP_DIR}/" ;;
esac
done
shopt -u dotglob
# 6. Create archive
cd ..
if [[ "$RUNNER_OS" == "Windows" ]]; then

View File

@@ -145,6 +145,56 @@ jobs:
# 5. Clean up
rm -rf .git cmd* update_wizard* Colab-TextGen-GPU.ipynb docker setup.cfg .github .gitignore requirements/ one_click.py
# 5b. Bundle Electron desktop launcher
ELECTRON_VERSION="41.5.0"
APP_DIR="app"
if [[ "$RUNNER_OS" == "Windows" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-win32-x64.zip"
ELECTRON_BIN="electron/electron.exe"
rm -f start_windows.bat
elif [[ "$RUNNER_OS" == "macOS" ]]; then
if [[ "$OS_TYPE" == "macos-15-intel" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-x64.zip"
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-arm64.zip"
fi
ELECTRON_BIN="electron/Electron.app/Contents/MacOS/Electron"
rm -f start_macos.sh
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-linux-x64.zip"
ELECTRON_BIN="electron/electron"
rm -f start_linux.sh
fi
echo "Downloading Electron ${ELECTRON_VERSION} (${ELECTRON_ZIP})..."
curl -L -o /tmp/electron.zip \
"https://github.com/electron/electron/releases/download/v${ELECTRON_VERSION}/${ELECTRON_ZIP}"
mkdir electron
unzip -q /tmp/electron.zip -d electron
rm /tmp/electron.zip
if [[ "$RUNNER_OS" == "Windows" ]]; then
sed "s|__APP__|${APP_DIR}|g" desktop/textgen.bat > textgen.bat
sed -i 's/$/\r/' textgen.bat
else
sed "s|__APP__|${APP_DIR}|g; s|__ELECTRON__|${ELECTRON_BIN}|g" desktop/textgen.sh > textgen
chmod +x textgen
fi
mv desktop/main.js desktop/package.json .
rm -rf desktop
# 5c. Restructure: textgen-VERSION/{textgen, user_data/, app/<everything else>}
mkdir "${APP_DIR}"
shopt -s dotglob
for item in *; do
case "$item" in
"${APP_DIR}"|user_data|textgen|textgen.bat) ;;
*) mv "$item" "${APP_DIR}/" ;;
esac
done
shopt -u dotglob
# 6. Create archive
cd ..
if [[ "$RUNNER_OS" == "Windows" ]]; then

View File

@@ -171,6 +171,56 @@ jobs:
# 5. Clean up
rm -rf .git cmd* update_wizard* Colab-TextGen-GPU.ipynb docker setup.cfg .github .gitignore requirements/ one_click.py
# 5b. Bundle Electron desktop launcher
ELECTRON_VERSION="41.5.0"
APP_DIR="app"
if [[ "$RUNNER_OS" == "Windows" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-win32-x64.zip"
ELECTRON_BIN="electron/electron.exe"
rm -f start_windows.bat
elif [[ "$RUNNER_OS" == "macOS" ]]; then
if [[ "$OS_TYPE" == "macos-15-intel" ]]; then
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-x64.zip"
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-darwin-arm64.zip"
fi
ELECTRON_BIN="electron/Electron.app/Contents/MacOS/Electron"
rm -f start_macos.sh
else
ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-linux-x64.zip"
ELECTRON_BIN="electron/electron"
rm -f start_linux.sh
fi
echo "Downloading Electron ${ELECTRON_VERSION} (${ELECTRON_ZIP})..."
curl -L -o /tmp/electron.zip \
"https://github.com/electron/electron/releases/download/v${ELECTRON_VERSION}/${ELECTRON_ZIP}"
mkdir electron
unzip -q /tmp/electron.zip -d electron
rm /tmp/electron.zip
if [[ "$RUNNER_OS" == "Windows" ]]; then
sed "s|__APP__|${APP_DIR}|g" desktop/textgen.bat > textgen.bat
sed -i 's/$/\r/' textgen.bat
else
sed "s|__APP__|${APP_DIR}|g; s|__ELECTRON__|${ELECTRON_BIN}|g" desktop/textgen.sh > textgen
chmod +x textgen
fi
mv desktop/main.js desktop/package.json .
rm -rf desktop
# 5c. Restructure: textgen-VERSION/{textgen, user_data/, app/<everything else>}
mkdir "${APP_DIR}"
shopt -s dotglob
for item in *; do
case "$item" in
"${APP_DIR}"|user_data|textgen|textgen.bat) ;;
*) mv "$item" "${APP_DIR}/" ;;
esac
done
shopt -u dotglob
# 6. Create archive
cd ..
if [[ "$RUNNER_OS" == "Windows" ]]; then

148
desktop/main.js Normal file
View File

@@ -0,0 +1,148 @@
const { app, BrowserWindow, screen } = require("electron");
const { spawn } = require("child_process");
const path = require("path");
const net = require("net");
const TITLE = "TextGen";
const STARTUP_TIMEOUT_MS = 120000;
const isWin = process.platform === "win32";
const baseDir = app.getAppPath();
const python = path.join(
baseDir,
"portable_env",
isWin ? "python.exe" : path.join("bin", "python3"),
);
// Launcher passes user args after "--" so Chromium's argv parser ignores them.
const argv = process.argv.slice(2);
const dashIdx = argv.indexOf("--");
const userArgs = dashIdx >= 0 ? argv.slice(dashIdx + 1) : argv;
app.setName(TITLE);
let serverProcess = null;
let mainWindow = null;
let portCheckInterval = null;
let portCheckTimeout = null;
function checkPort(port) {
return new Promise((resolve) => {
const sock = new net.Socket();
sock.setTimeout(500);
sock.once("connect", () => { sock.destroy(); resolve(true); });
sock.once("error", () => resolve(false));
sock.once("timeout", () => { sock.destroy(); resolve(false); });
sock.connect(port, "127.0.0.1");
});
}
function clearTimers() {
if (portCheckTimeout) { clearTimeout(portCheckTimeout); portCheckTimeout = null; }
if (portCheckInterval) { clearInterval(portCheckInterval); portCheckInterval = null; }
}
function killServer() {
const proc = serverProcess;
if (!proc) return;
serverProcess = null;
try {
if (isWin) {
spawn("taskkill", ["/pid", String(proc.pid), "/T", "/F"], { stdio: "ignore" });
} else {
process.kill(-proc.pid, "SIGINT");
setTimeout(() => {
try { process.kill(-proc.pid, "SIGKILL"); } catch (_) {}
}, 5000);
}
} catch (_) {
try { proc.kill("SIGINT"); } catch (_) {}
}
}
function createWindow(port) {
const { width: sw, height: sh } = screen.getPrimaryDisplay().workAreaSize;
const width = Math.min(Math.max(Math.floor(sw * 0.9), 1200), 1600);
const height = Math.min(Math.max(Math.floor(sh * 0.9), 800), 1000);
mainWindow = new BrowserWindow({
width,
height,
title: TITLE,
autoHideMenuBar: true,
webPreferences: { nodeIntegration: false, contextIsolation: true },
});
mainWindow.on("page-title-updated", (e) => e.preventDefault());
mainWindow.webContents.on("will-prevent-unload", (e) => e.preventDefault());
mainWindow.on("closed", () => { mainWindow = null; });
mainWindow.loadURL(`http://127.0.0.1:${port}`);
}
async function waitForPortAndOpen(port) {
if (await checkPort(port)) {
createWindow(port);
return;
}
portCheckTimeout = setTimeout(() => {
clearTimers();
console.error(`Server failed to become ready within ${STARTUP_TIMEOUT_MS / 1000}s.`);
app.quit();
}, STARTUP_TIMEOUT_MS);
portCheckInterval = setInterval(async () => {
if (await checkPort(port)) {
clearTimers();
createWindow(port);
}
}, 500);
}
app.whenReady().then(() => {
serverProcess = spawn(python, ["server.py", "--portable", "--api", ...userArgs], {
cwd: baseDir,
detached: !isWin,
env: {
...process.env,
PYTHONNOUSERSITE: "1",
PYTHONPATH: undefined,
PYTHONHOME: undefined,
PYTHONUNBUFFERED: "1",
FORCE_COLOR: "1",
TERM: "xterm-256color",
},
});
if (!isWin) serverProcess.unref();
const passthrough = (data) => process.stdout.write(data);
const onData = (data) => {
const text = data.toString();
process.stdout.write(text);
if (!text.includes("Running on local URL:")) return;
const match = text.match(/http:\/\/127\.0\.0\.1:(\d+)/);
if (!match) return;
serverProcess.stdout.off("data", onData);
serverProcess.stderr.off("data", onData);
serverProcess.stdout.on("data", passthrough);
serverProcess.stderr.on("data", passthrough);
waitForPortAndOpen(parseInt(match[1], 10));
};
serverProcess.stdout.on("data", onData);
serverProcess.stderr.on("data", onData);
serverProcess.on("error", (err) => {
console.error("Failed to spawn server:", err);
clearTimers();
app.quit();
});
serverProcess.on("close", (code) => {
console.log(`Server process exited with code ${code}`);
clearTimers();
serverProcess = null;
if (mainWindow && !mainWindow.isDestroyed()) mainWindow.close();
app.quit();
});
});
app.on("before-quit", killServer);
app.on("window-all-closed", () => app.quit());
process.on("SIGINT", () => { killServer(); process.exit(); });
process.on("SIGTERM", () => { killServer(); process.exit(); });

16
desktop/textgen.bat Normal file
View File

@@ -0,0 +1,16 @@
@echo off
set "APP=%~dp0__APP__"
for %%a in (%*) do (
if /i "%%~a"=="--help" goto :help
if /i "%%~a"=="-h" goto :help
if /i "%%~a"=="--nowebui" goto :server
if /i "%%~a"=="--listen" goto :server
)
start "" "%APP%\electron\electron.exe" "%APP%" -- %*
exit /b 0
:help
"%APP%\portable_env\python.exe" "%APP%\server.py" --help
exit /b %errorlevel%
:server
"%APP%\portable_env\python.exe" "%APP%\server.py" --portable --api %*
exit /b %errorlevel%

15
desktop/textgen.sh Normal file
View File

@@ -0,0 +1,15 @@
#!/bin/bash
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APP="$DIR/__APP__"
PY="$APP/portable_env/bin/python3"
for arg; do
case "$arg" in
--help|-h)
exec "$PY" "$APP/server.py" --help
;;
--nowebui|--listen)
exec "$PY" "$APP/server.py" --portable --api "$@"
;;
esac
done
exec "$APP/__ELECTRON__" "$APP" -- "$@"