Clean up server code. Fix oobabooga multigen with streaming. Pass Gradio fn_index as a request header

This commit is contained in:
SillyLossy
2023-04-13 21:50:20 +03:00
parent 58ed03dfe0
commit f7e89b345d
5 changed files with 52 additions and 103 deletions

105
server.js
View File

@@ -218,18 +218,15 @@ app.use('/characters', (req, res) => {
res.status(404).send('File not found');
return;
}
//res.contentType('image/jpeg');
res.send(data);
});
});
app.use(multer({ dest: "uploads" }).single("avatar"));
app.get("/", function (request, response) {
response.sendFile(__dirname + "/public/index.html");
//response.send("<h1>Главная страница</h1>");
});
app.get("/notes/*", function (request, response) {
response.sendFile(__dirname + "/public" + request.url + ".html");
//response.send("<h1>Главная страница</h1>");
});
//**************Kobold api
@@ -319,16 +316,16 @@ function randomHash() {
return result;
}
function textGenProcessStartedHandler(websocket, content, session, prompt, SEND_PROMPT_GRADIO_FN) {
function textGenProcessStartedHandler(websocket, content, session, prompt, fn_index) {
switch (content.msg) {
case "send_hash":
const send_hash = JSON.stringify({ "session_hash": session, "fn_index": SEND_PROMPT_GRADIO_FN });
const send_hash = JSON.stringify({ "session_hash": session, "fn_index": fn_index });
websocket.send(send_hash);
break;
case "estimation":
break;
case "send_data":
const send_data = JSON.stringify({ "session_hash": session, "fn_index": SEND_PROMPT_GRADIO_FN, "data": prompt.data });
const send_data = JSON.stringify({ "session_hash": session, "fn_index": fn_index, "data": prompt.data });
console.log(send_data);
websocket.send(send_data);
break;
@@ -350,7 +347,7 @@ app.post("/generate_textgenerationwebui", jsonParser, async function (request, r
console.log(request.body);
if (!!request.header('X-Response-Streaming')) {
const SEND_PARAMS_GRADIO_FN = 29;
const fn_index = Number(request.header('X-Gradio-Streaming-Function'));
response_generate.writeHead(200, {
'Content-Type': 'text/plain;charset=utf-8',
@@ -381,7 +378,7 @@ app.post("/generate_textgenerationwebui", jsonParser, async function (request, r
websocket.on('message', async (message) => {
const content = json5.parse(message);
console.log(content);
text = textGenProcessStartedHandler(websocket, content, session, request.body, SEND_PARAMS_GRADIO_FN);
text = textGenProcessStartedHandler(websocket, content, session, request.body, fn_index);
});
while (true) {
@@ -450,38 +447,20 @@ app.post("/generate_textgenerationwebui", jsonParser, async function (request, r
app.post("/savechat", jsonParser, function (request, response) {
//console.log(humanizedISO8601DateTime()+':/savechat/ entered');
//console.log(request.data);
//console.log(request.body.bg);
//const data = request.body;
//console.log(request);
//console.log(request.body.chat);
//var bg = "body {background-image: linear-gradient(rgba(19,21,44,0.75), rgba(19,21,44,0.75)), url(../backgrounds/"+request.body.bg+");}";
var dir_name = String(request.body.avatar_url).replace('.png', '');
//console.log(humanizedISO8601DateTime()+':/savechat sees '+dir_name+' as the character name (derived from avatar PNG filename)');
let chat_data = request.body.chat;
let jsonlData = chat_data.map(JSON.stringify).join('\n');
//console.log(humanizedISO8601DateTime()+':/savechat saving a chat named '+request.body.file_name+'.jsonl');
fs.writeFile(chatsPath + dir_name + "/" + request.body.file_name + '.jsonl', jsonlData, 'utf8', function (err) {
if (err) {
response.send(err);
return console.log(err);
//response.send(err);
} else {
//response.redirect("/");
response.send({ result: "ok" });
}
});
});
app.post("/getchat", jsonParser, function (request, response) {
//console.log(request.data);
//console.log(request.body.bg);
//const data = request.body;
//console.log(request);
//console.log(request.body.chat);
//var bg = "body {background-image: linear-gradient(rgba(19,21,44,0.75), rgba(19,21,44,0.75)), url(../backgrounds/"+request.body.bg+");}";
//console.log(humanizedISO8601DateTime()+':/getchat entered');
var dir_name = String(request.body.avatar_url).replace('.png', '');
fs.stat(chatsPath + dir_name, function (err, stat) {
@@ -498,7 +477,6 @@ app.post("/getchat", jsonParser, function (request, response) {
fs.stat(chatsPath + dir_name + "/" + request.body.file_name + ".jsonl", function (err, stat) {
if (err === null) { //if no error (the file exists), read the file
//console.log(humanizedISO8601DateTime()+':/getchat tries to access: '+chatsPath+dir_name+'/'+request.body.file_name+'.jsonl');
if (stat !== undefined) {
fs.readFile(chatsPath + dir_name + "/" + request.body.file_name + ".jsonl", 'utf8', (err, data) => {
if (err) {
@@ -574,10 +552,6 @@ app.post("/getstatus", jsonParser, function (request, response_getstatus = respo
data.result = "no_connection";
}
response_getstatus.send(data);
//console.log(response.statusCode);
//console.log(data);
//response_getstatus.send(data);
//data.results[0].text
}).on('error', function (err) {
//console.log(url);
//console.log('something went wrong on the request', err.request.options);
@@ -629,20 +603,15 @@ app.post("/setsoftprompt", jsonParser, async function (request, response) {
});
function checkServer() {
//console.log('Check run###################################################');
api_server = 'http://127.0.0.1:5000';
var args = {
headers: { "Content-Type": "application/json" }
};
client.get(api_server + "/v1/model", args, function (data, response) {
console.log(data.result);
//console.log('###################################################');
console.log(data);
}).on('error', function (err) {
console.log(err);
//console.log('');
//console.log('something went wrong on the request', err.request.options);
//console.log('errorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr');
});
}
@@ -688,10 +657,6 @@ app.post("/createcharacter", urlencodedParser, function (request, response) {
response.send("Error: A character with that name already exists.");
//response.send({error: true});
}
//console.log(request.body);
//response.send(request.body.ch_name);
//response.redirect("https://metanit.com")
});
@@ -892,11 +857,6 @@ app.post("/getcharacters", jsonParser, function (request, response) {
//console.log(characters);
response.send(JSON.stringify(characters));
});
//var directories = getDirectories("public/characters");
//console.log(directories[0]);
//characters = {};
//character_i = 0;
//getCharacterFile(directories, response,0);
});
app.post("/getbackgrounds", jsonParser, function (request, response) {
@@ -918,11 +878,6 @@ app.post("/getuseravatars", jsonParser, function (request, response) {
});
app.post("/setbackground", jsonParser, function (request, response) {
//console.log(request.data);
//console.log(request.body.bg);
//const data = request.body;
//console.log(request);
//console.log(1);
var bg = "#bg1 {background-image: url(../backgrounds/" + request.body.bg + ");}";
fs.writeFile('public/css/bg_load.css', bg, 'utf8', function (err) {
if (err) {
@@ -1016,8 +971,6 @@ app.post("/downloadbackground", urlencodedParser, function (request, response) {
});
app.post("/savesettings", jsonParser, function (request, response) {
fs.writeFile('public/settings.json', JSON.stringify(request.body), 'utf8', function (err) {
if (err) {
response.send(err);
@@ -1028,8 +981,8 @@ app.post("/savesettings", jsonParser, function (request, response) {
response.send({ result: "ok" });
}
});
});
app.post('/getsettings', jsonParser, (request, response) => { //Wintermute's code
const koboldai_settings = [];
const koboldai_setting_names = [];
@@ -1238,34 +1191,6 @@ function readWorldInfoFile(worldInfoName) {
return worldInfo;
}
function getCharacterFile(directories, response, i) { //old need del
if (directories.length > i) {
fs.stat(charactersPath + directories[i] + '/' + directories[i] + ".json", function (err, stat) {
if (err == null) {
fs.readFile(charactersPath + directories[i] + '/' + directories[i] + ".json", 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
//console.log(data);
characters[character_i] = {};
characters[character_i] = data;
i++;
character_i++;
getCharacterFile(directories, response, i);
});
} else {
i++;
getCharacterFile(directories, response, i);
}
});
} else {
response.send(JSON.stringify(characters));
}
}
function getImages(path) {
return fs
@@ -1277,17 +1202,6 @@ function getImages(path) {
.sort(Intl.Collator().compare);
}
function getKoboldSettingFiles(path) {
return fs.readdirSync(path).sort(function (a, b) {
return new Date(fs.statSync(path + '/' + a).mtime) - new Date(fs.statSync(path + '/' + b).mtime);
}).reverse();
}
function getDirectories(path) {
return fs.readdirSync(path).sort(function (a, b) {
return new Date(fs.statSync(path + '/' + a).mtime) - new Date(fs.statSync(path + '/' + b).mtime);
}).reverse();
}
//***********Novel.ai API
app.post("/getstatus_novelai", jsonParser, function (request, response_getstatus_novel = response) {
@@ -1320,8 +1234,6 @@ app.post("/getstatus_novelai", jsonParser, function (request, response_getstatus
});
});
app.post("/generate_novelai", jsonParser, function (request, response_generate_novel = response) {
if (!request.body) return response_generate_novel.sendStatus(400);
@@ -1581,17 +1493,12 @@ app.post("/exportcharacter", jsonParser, async function (request, response) {
app.post("/importchat", urlencodedParser, function (request, response) {
//console.log(humanizedISO8601DateTime()+':/importchat begun');
if (!request.body) return response.sendStatus(400);
var format = request.body.file_type;
let filedata = request.file;
let avatar_url = (request.body.avatar_url).replace('.png', '');
let ch_name = request.body.character_name;
//console.log(filedata.filename);
//var format = request.body.file_type;
//console.log(format);
//console.log(1);
if (filedata) {
if (format === 'json') {