Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #209, #222, #372, and #386 # ENOENT, ENOTEMPTY, TypeError: Cannot read properties of undefined (reading 'split') #373

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 22 additions & 23 deletions bin/web/views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -22,7 +22,7 @@
padding: 10px;
box-sizing: border-box;
}

#input {
white-space: pre-wrap;
border: 1px solid rgba(0,0,0,0.2);
Expand All @@ -33,11 +33,11 @@
box-sizing: border-box;
background: white;
}

#input:focus {
outline: none;
}

button {
padding: 10px;
background: rgba(0, 0, 0, 0.8);
Expand All @@ -46,29 +46,29 @@
outline: none;
box-sizing: border-box;
}

#messages {
list-style-type: none;
margin: 0;
box-sizing: border-box;
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;
Expand All @@ -77,19 +77,19 @@
font-size: 14px;
background: #efefef;
}

.hidden {
display: none !important;
}

.input-container {
background: rgba(0, 0, 0, 0.06);
}
.input-container button {
padding: 30px;
margin-left: 10px;
}

.info {
font-size: 12px;
padding: 0 5px 10px;
Expand All @@ -98,7 +98,7 @@
padding: 5px 10px;
margin-right: 10px;
}

.form-header {
display: flex;
align-items: center;
Expand All @@ -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;
Expand All @@ -124,7 +124,7 @@
.form-header select {
width: 150px;
}

.logo {
color: white;
font-weight: bold;
Expand All @@ -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;
Expand Down Expand Up @@ -230,7 +230,7 @@
repeat_last_n: 64,
repeat_penalty: 1.3,
debug: false,
models: []
models: [],
}
}
const socket = io();
Expand All @@ -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 `<option value="${model}" ${config.model === model ? "selected" : ""}>${model}</option>`
}).join("")
return `<a class='logo' href="/">Dalai</a><div class='stretch'></div>
Expand Down
42 changes: 28 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()

Expand Down Expand Up @@ -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 {
Expand All @@ -306,7 +309,7 @@ class Dalai {
cb(this.htmlencode(msg))
} else {
cb(msg)
}
}
}
}
async uninstall(core, ...models) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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`)
Expand All @@ -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
}
}

Expand Down