diff --git a/bin/web/views/index.ejs b/bin/web/views/index.ejs index ffbddee..e0a0b20 100644 --- a/bin/web/views/index.ejs +++ b/bin/web/views/index.ejs @@ -13,7 +13,7 @@ color: rgba(0, 0, 0, 0.8); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; } - + .input-field { /* border: 2px solid rgba(0,0,0,0.2); @@ -22,7 +22,7 @@ padding: 10px; box-sizing: border-box; } - + #input { white-space: pre-wrap; border: 1px solid rgba(0,0,0,0.2); @@ -33,11 +33,11 @@ box-sizing: border-box; background: white; } - + #input:focus { outline: none; } - + button { padding: 10px; background: rgba(0, 0, 0, 0.8); @@ -46,7 +46,7 @@ outline: none; box-sizing: border-box; } - + #messages { list-style-type: none; margin: 0; @@ -54,21 +54,21 @@ font-size: 14px; padding: 0; } - + #messages>li { padding: 10px; font-size: 14px; box-sizing: border-box; } - + #messages>li:nth-child(odd) { background: #efefef; } - + li { white-space: pre-wrap; } - + .loading { margin-bottom: 10px; padding: 10px; @@ -77,11 +77,11 @@ font-size: 14px; background: #efefef; } - + .hidden { display: none !important; } - + .input-container { background: rgba(0, 0, 0, 0.06); } @@ -89,7 +89,7 @@ padding: 30px; margin-left: 10px; } - + .info { font-size: 12px; padding: 0 5px 10px; @@ -98,7 +98,7 @@ padding: 5px 10px; margin-right: 10px; } - + .form-header { display: flex; align-items: center; @@ -107,11 +107,11 @@ background: rgba(0,0,0,0.9); padding: 10px 20px; } - + .stretch { flex-grow: 1; } - + .form-header input[type=text], .form-header select { border: none; @@ -124,7 +124,7 @@ .form-header select { width: 150px; } - + .logo { color: white; font-weight: bold; @@ -135,18 +135,18 @@ font-weight: bold; font-family: georgia; } - + .config-container { display: flex; flex-wrap: wrap; } - + .kv { display: block; font-size: 14px; margin-left: 10px; } - + .kv label { text-align: right; display: block; @@ -230,7 +230,7 @@ repeat_last_n: 64, repeat_penalty: 1.3, debug: false, - models: [] + models: [], } } const socket = io(); @@ -257,9 +257,8 @@ } }).join("") - - if (!str) config.model = config.models[0] - const models = config.models.map((model, i) => { + if (!config.model) config.model = config.models[0] + const models = config.models.map(model => { return `` }).join("") return `
diff --git a/index.js b/index.js index ea1cf3a..11c918b 100644 --- a/index.js +++ b/index.js @@ -178,7 +178,7 @@ class Dalai { // this.progressBar.update(1); // await new Promise((resolve, reject) => { // _7z.unpack(path.resolve(this.home, "x86_64-12.2.0-release-win32-seh-msvcrt-rt_v10-rev2.7z"), this.home, (err) => { -// if (err) { +// if (err) { // reject(err) // } else { // resolve() @@ -189,7 +189,7 @@ class Dalai { // await fs.promises.rm(path.resolve(this.home, "x86_64-12.2.0-release-win32-seh-msvcrt-rt_v10-rev2.7z")) // } async query(req, cb) { - + console.log(`> query:`, req) if (req.method === "installed") { let models = await this.installed() @@ -215,7 +215,10 @@ class Dalai { if (!req.prompt) { return } - + if (!req.model) { + cb("Please specify a model") + return + } let [Core, Model] = req.model.split(".") Model = Model.toUpperCase() @@ -286,7 +289,7 @@ class Dalai { // otherwise flush this.queue.push(msg[i]) let queueContent = this.queue.join("") - + if (!this.bufferStarted && ["\n", "\b", "\f", "\r", "\t"].includes(queueContent)) { // if the buffer hasn't started and incoming tokens are whitespaces, ignore } else { @@ -306,7 +309,7 @@ class Dalai { cb(this.htmlencode(msg)) } else { cb(msg) - } + } } } async uninstall(core, ...models) { @@ -337,15 +340,26 @@ class Dalai { let models_path = path.resolve(engine.home, "models") let temp_path = path.resolve(this.home, "tmp") let temp_models_path = path.resolve(temp_path, "models") - await fs.promises.mkdir(temp_path, { recursive: true }).catch((e) => { console.log("1", e) }) - // 1. move the models folder to ../tmp + // 1. make sure the models_path and temp_path exist and temp_models_path doesn't exist + if (!fs.existsSync(models_path)) { + await fs.promises.mkdir(models_path, { recursive: true }).catch((e) => { console.log("1", e)}) + } + if (!fs.existsSync(temp_path)) { + await fs.promises.mkdir(temp_path, { recursive: true }).catch((e) => { console.log("1", e) }) + } + if (fs.existsSync(temp_models_path)) { + await fs.promises.rm(temp_models_path, { recursive: true }).catch((e) => { console.log("1", e) }) + } + // 2. move the models folder to ../tmp await fs.promises.rename(models_path, temp_models_path).catch((e) => { console.log("2", e) }) - // 2. wipe out the folder - await fs.promises.rm(engine.home, { recursive: true }).catch((e) => { console.log("3", e) }) // 3. install engine await this.add(core) - // 4. move back the files inside /tmp - await fs.promises.rename(temp_models_path, models_path).catch((e) => { console.log("4", e) }) + // 4. wipe out the folder + if (fs.existsSync(models_path)) { + await fs.promises.rm(models_path, { recursive: true }).catch((e) => { console.log("4", e) }) + } + // 5. move back the files inside /tmp + await fs.promises.rename(temp_models_path, models_path).catch((e) => { console.log("5", e) }) // next add the models let res = await this.cores[core].add(...models) @@ -438,7 +452,7 @@ class Dalai { // 3.1. Python: Windows doesn't ship with python, so install a dedicated self-contained python if (platform === "win32") { - await this.python() + await this.python() } const root_python_paths = (platform === "win32" ? ["python3", "python", path.resolve(this.home, "python", "python.exe")] : ["python3", "python"]) const root_pip_paths = (platform === "win32" ? ["pip3", "pip", path.resolve(this.home, "python", "python -m pip")] : ["pip3", "pip"]) @@ -499,7 +513,7 @@ class Dalai { success = await this.exec(`${pip_path} install --user --upgrade pip setuptools wheel`) if (!success) { throw new Error("pip setuptools wheel upgrade failed") - return + return } } success = await this.exec(`${pip_path} install torch torchvision torchaudio sentencepiece numpy`) @@ -508,7 +522,7 @@ class Dalai { success = await this.exec(`${pip_path} install --user torch torchvision torchaudio sentencepiece numpy`) if (!success) { throw new Error("dependency installation failed") - return + return } }