mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
webp import
This commit is contained in:
146
server.js
146
server.js
@@ -23,6 +23,10 @@ const mime = require('mime-types');
|
||||
const cookieParser = require('cookie-parser');
|
||||
const crypto = require('crypto');
|
||||
const ipaddr = require('ipaddr.js');
|
||||
const json5 = require('json5');
|
||||
|
||||
const ExifReader = require('exifreader');
|
||||
const webp = require('webp-converter');
|
||||
|
||||
const config = require(path.join(process.cwd(), './config.conf'));
|
||||
const server_port = process.env.SILLY_TAVERN_PORT || config.port;
|
||||
@@ -395,7 +399,7 @@ app.post("/getchat", jsonParser, function (request, response) {
|
||||
const lines = data.split('\n');
|
||||
|
||||
// Iterate through the array of strings and parse each line as JSON
|
||||
const jsonData = lines.map(JSON.parse);
|
||||
const jsonData = lines.map(json5.parse);
|
||||
response.send(jsonData);
|
||||
//console.log('read the requested file')
|
||||
|
||||
@@ -441,7 +445,7 @@ app.post("/getstatus", jsonParser, function (request, response_getstatus = respo
|
||||
var response = body.match(/gradio_config[ =]*(\{.*\});/)[1];
|
||||
if (!response)
|
||||
throw "no_connection";
|
||||
let model = JSON.parse(response).components.filter((x) => x.props.label == "Model" && x.type == "dropdown")[0].props.value;
|
||||
let model = json5.parse(response).components.filter((x) => x.props.label == "Model" && x.type == "dropdown")[0].props.value;
|
||||
data = { result: model };
|
||||
if (!data)
|
||||
throw "no_connection";
|
||||
@@ -678,28 +682,44 @@ async function charaWrite(img_url, data, target_img, response = undefined, mes =
|
||||
}
|
||||
}
|
||||
|
||||
async function charaRead(img_url, input_format) {
|
||||
let format;
|
||||
if (input_format === undefined) {
|
||||
if (img_url.indexOf('.webp') !== -1) {
|
||||
format = 'webp';
|
||||
} else {
|
||||
format = 'png';
|
||||
}
|
||||
} else {
|
||||
format = input_format;
|
||||
}
|
||||
|
||||
switch (format) {
|
||||
case 'webp':
|
||||
const exif_data = await ExifReader.load(fs.readFileSync(img_url));
|
||||
const char_data = exif_data['UserComment']['description'];
|
||||
if (char_data === 'Undefined' && exif_data['UserComment'].value && exif_data['UserComment'].value.length === 1) {
|
||||
return exif_data['UserComment'].value[0];
|
||||
}
|
||||
return char_data;
|
||||
case 'png':
|
||||
const buffer = fs.readFileSync(img_url);
|
||||
const chunks = extract(buffer);
|
||||
|
||||
|
||||
|
||||
function charaRead(img_url) {
|
||||
const buffer = fs.readFileSync(img_url);
|
||||
const chunks = extract(buffer);
|
||||
|
||||
const textChunks = chunks.filter(function (chunk) {
|
||||
return chunk.name === 'tEXt';
|
||||
}).map(function (chunk) {
|
||||
//console.log(text.decode(chunk.data));
|
||||
return PNGtext.decode(chunk.data);
|
||||
});
|
||||
var base64DecodedData = Buffer.from(textChunks[0].text, 'base64').toString('utf8');
|
||||
return base64DecodedData;//textChunks[0].text;
|
||||
//console.log(textChunks[0].keyword); // 'hello'
|
||||
//console.log(textChunks[0].text); // 'world'
|
||||
const textChunks = chunks.filter(function (chunk) {
|
||||
return chunk.name === 'tEXt';
|
||||
}).map(function (chunk) {
|
||||
return PNGtext.decode(chunk.data);
|
||||
});
|
||||
var base64DecodedData = Buffer.from(textChunks[0].text, 'base64').toString('utf8');
|
||||
return base64DecodedData;//textChunks[0].text;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
app.post("/getcharacters", jsonParser, function (request, response) {
|
||||
fs.readdir(charactersPath, (err, files) => {
|
||||
fs.readdir(charactersPath, async (err, files) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
@@ -710,11 +730,10 @@ app.post("/getcharacters", jsonParser, function (request, response) {
|
||||
//console.log(pngFiles);
|
||||
characters = {};
|
||||
var i = 0;
|
||||
pngFiles.forEach(item => {
|
||||
//console.log(item);
|
||||
var img_data = charaRead(charactersPath + item);
|
||||
for (const item of pngFiles) {
|
||||
try {
|
||||
let jsonObject = JSON.parse(img_data);
|
||||
var img_data = await charaRead(charactersPath + item);
|
||||
let jsonObject = json5.parse(img_data);
|
||||
jsonObject.avatar = item;
|
||||
//console.log(jsonObject);
|
||||
characters[i] = {};
|
||||
@@ -727,7 +746,7 @@ app.post("/getcharacters", jsonParser, function (request, response) {
|
||||
console.log("An unexpected error occurred: ", error);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
//console.log(characters);
|
||||
response.send(JSON.stringify(characters));
|
||||
});
|
||||
@@ -746,7 +765,7 @@ app.post("/getbackgrounds", jsonParser, function (request, response) {
|
||||
app.post("/iscolab", jsonParser, function (request, response) {
|
||||
let send_data = false;
|
||||
if (is_colab) {
|
||||
send_data = String(process.env.colaburl).trim();
|
||||
send_data = String(process.env.colaburl).trim();
|
||||
}
|
||||
response.send({ colaburl: send_data });
|
||||
|
||||
@@ -1036,7 +1055,7 @@ function readWorldInfoFile(worldInfoName) {
|
||||
}
|
||||
|
||||
const worldInfoText = fs.readFileSync(pathToWorldInfo, 'utf8');
|
||||
const worldInfo = JSON.parse(worldInfoText);
|
||||
const worldInfo = json5.parse(worldInfoText);
|
||||
return worldInfo;
|
||||
}
|
||||
|
||||
@@ -1222,7 +1241,7 @@ app.post("/getallchatsofcharacter", jsonParser, function (request, response) {
|
||||
});
|
||||
rl.on('close', () => {
|
||||
if (lastLine) {
|
||||
let jsonData = JSON.parse(lastLine);
|
||||
let jsonData = json5.parse(lastLine);
|
||||
if (jsonData.name !== undefined) {
|
||||
chatData[i] = {};
|
||||
chatData[i]['file_name'] = file;
|
||||
@@ -1247,6 +1266,7 @@ app.post("/getallchatsofcharacter", jsonParser, function (request, response) {
|
||||
};
|
||||
})
|
||||
});
|
||||
|
||||
function getPngName(file) {
|
||||
let i = 1;
|
||||
let base_name = file;
|
||||
@@ -1256,23 +1276,24 @@ function getPngName(file) {
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
app.post("/importcharacter", urlencodedParser, async function (request, response) {
|
||||
|
||||
if (!request.body) return response.sendStatus(400);
|
||||
|
||||
let png_name = '';
|
||||
let filedata = request.file;
|
||||
//console.log(filedata.filename);
|
||||
let uploadPath = path.join('./uploads', filedata.filename);
|
||||
var format = request.body.file_type;
|
||||
//console.log(format);
|
||||
if (filedata) {
|
||||
if (format == 'json') {
|
||||
fs.readFile('./uploads/' + filedata.filename, 'utf8', async (err, data) => {
|
||||
fs.readFile(uploadPath, 'utf8', async (err, data) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
response.send({ error: true });
|
||||
}
|
||||
const jsonData = JSON.parse(data);
|
||||
const jsonData = json5.parse(data);
|
||||
|
||||
if (jsonData.name !== undefined) {
|
||||
jsonData.name = sanitize(jsonData.name);
|
||||
@@ -1295,43 +1316,29 @@ app.post("/importcharacter", urlencodedParser, async function (request, response
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
|
||||
var img_data = charaRead('./uploads/' + filedata.filename);
|
||||
let jsonData = JSON.parse(img_data);
|
||||
var img_data = await charaRead(uploadPath, format);
|
||||
let jsonData = json5.parse(img_data);
|
||||
jsonData.name = sanitize(jsonData.name);
|
||||
|
||||
if (format == 'webp') {
|
||||
let convertedPath = path.join('./uploads', path.basename(uploadPath, ".webp") + ".png")
|
||||
await webp.dwebp(uploadPath, convertedPath, "-o");
|
||||
uploadPath = convertedPath;
|
||||
}
|
||||
|
||||
png_name = getPngName(jsonData.name);
|
||||
|
||||
if (jsonData.name !== undefined) {
|
||||
let char = { "name": jsonData.name, "description": jsonData.description ?? '', "personality": jsonData.personality ?? '', "first_mes": jsonData.first_mes ?? '', "avatar": 'none', "chat": humanizedISO8601DateTime(), "mes_example": jsonData.mes_example ?? '', "scenario": jsonData.scenario ?? '', "create_date": humanizedISO8601DateTime(), "talkativeness": jsonData.talkativeness ?? 0.5 };
|
||||
char = JSON.stringify(char);
|
||||
await charaWrite('./uploads/' + filedata.filename, char, png_name, response, { file_name: png_name });
|
||||
/*
|
||||
fs.copyFile('./uploads/'+filedata.filename, charactersPath+png_name+'.png', (err) => {
|
||||
if(err) {
|
||||
response.send({error:true});
|
||||
return console.log(err);
|
||||
}else{
|
||||
//console.log(img_file+fileType);
|
||||
response.send({file_name: png_name});
|
||||
}
|
||||
//console.log('The image was copied from temp directory.');
|
||||
});*/
|
||||
await charaWrite(uploadPath, char, png_name, response, { file_name: png_name });
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
response.send({ error: true });
|
||||
}
|
||||
}
|
||||
//charaWrite(img_path+img_file, char, request.body.ch_name, response);
|
||||
}
|
||||
//console.log("The file was saved.");
|
||||
|
||||
|
||||
//console.log(request.body);
|
||||
//response.send(request.body.ch_name);
|
||||
|
||||
//response.redirect("https://metanit.com")
|
||||
});
|
||||
|
||||
app.post("/importchat", urlencodedParser, function (request, response) {
|
||||
@@ -1356,7 +1363,7 @@ app.post("/importchat", urlencodedParser, function (request, response) {
|
||||
response.send({ error: true });
|
||||
}
|
||||
|
||||
const jsonData = JSON.parse(data);
|
||||
const jsonData = json5.parse(data);
|
||||
var new_chat = [];
|
||||
if (jsonData.histories !== undefined) {
|
||||
//console.log('/importchat confirms JSON histories are defined');
|
||||
@@ -1409,7 +1416,7 @@ app.post("/importchat", urlencodedParser, function (request, response) {
|
||||
});
|
||||
|
||||
rl.once('line', (line) => {
|
||||
let jsonData = JSON.parse(line);
|
||||
let jsonData = json5.parse(line);
|
||||
|
||||
if (jsonData.user_name !== undefined) {
|
||||
//console.log(humanizedISO8601DateTime()+':/importchat copying chat as '+ch_name+' - '+humanizedISO8601DateTime()+'.jsonl');
|
||||
@@ -1447,7 +1454,7 @@ app.post('/importworldinfo', urlencodedParser, (request, response) => {
|
||||
const fileContents = fs.readFileSync(pathToUpload, 'utf8');
|
||||
|
||||
try {
|
||||
const worldContent = JSON.parse(fileContents);
|
||||
const worldContent = json5.parse(fileContents);
|
||||
if (!('entries' in worldContent)) {
|
||||
throw new Error('File must contain a world info entries list');
|
||||
}
|
||||
@@ -1519,7 +1526,7 @@ app.post('/getgroups', jsonParser, (_, response) => {
|
||||
const files = fs.readdirSync(directories.groups);
|
||||
files.forEach(function (file) {
|
||||
const fileContents = fs.readFileSync(path.join(directories.groups, file), 'utf8');
|
||||
const group = JSON.parse(fileContents);
|
||||
const group = json5.parse(fileContents);
|
||||
groups.push(group);
|
||||
});
|
||||
|
||||
@@ -1578,7 +1585,7 @@ app.post('/getgroupchat', jsonParser, (request, response) => {
|
||||
const lines = data.split('\n');
|
||||
|
||||
// Iterate through the array of strings and parse each line as JSON
|
||||
const jsonData = lines.map(JSON.parse);
|
||||
const jsonData = lines.map(json5.parse);
|
||||
return response.send(jsonData);
|
||||
} else {
|
||||
return response.send([]);
|
||||
@@ -1641,7 +1648,7 @@ app.post('/status_poe', jsonParser, async (request, response) => {
|
||||
const botNames = client.get_bot_names();
|
||||
client.disconnect_ws();
|
||||
|
||||
return response.send({'bot_names': botNames});
|
||||
return response.send({ 'bot_names': botNames });
|
||||
}
|
||||
catch {
|
||||
return response.sendStatus(401);
|
||||
@@ -1661,8 +1668,8 @@ app.post('/purge_poe', jsonParser, async (request, response) => {
|
||||
const client = await getPoeClient(token);
|
||||
await client.purge_conversation(bot, count);
|
||||
client.disconnect_ws();
|
||||
|
||||
return response.send({"ok" : true});
|
||||
|
||||
return response.send({ "ok": true });
|
||||
}
|
||||
catch {
|
||||
return response.sendStatus(500);
|
||||
@@ -1673,24 +1680,24 @@ app.post('/generate_poe', jsonParser, async (request, response) => {
|
||||
if (!request.body.token || !request.body.prompt) {
|
||||
return response.sendStatus(400);
|
||||
}
|
||||
|
||||
|
||||
const token = request.body.token;
|
||||
const prompt = request.body.prompt;
|
||||
const bot = request.body.bot ?? POE_DEFAULT_BOT;
|
||||
|
||||
|
||||
try {
|
||||
const client = await getPoeClient(token);
|
||||
|
||||
|
||||
let reply;
|
||||
for await (const mes of client.send_message(bot, prompt)) {
|
||||
reply = mes.text;
|
||||
}
|
||||
|
||||
console.log(reply);
|
||||
|
||||
|
||||
client.disconnect_ws();
|
||||
|
||||
return response.send({'reply': reply});
|
||||
|
||||
return response.send({ 'reply': reply });
|
||||
}
|
||||
catch {
|
||||
return response.sendStatus(500);
|
||||
@@ -2034,8 +2041,6 @@ function convertStage1() {
|
||||
getCharacterFile2(directories, 0);
|
||||
}
|
||||
function convertStage2() {
|
||||
//directoriesB = JSON.parse(directoriesB);
|
||||
//console.log(directoriesB);
|
||||
var mes = true;
|
||||
for (const key in directoriesB) {
|
||||
if (mes) {
|
||||
@@ -2044,9 +2049,6 @@ function convertStage2() {
|
||||
console.log('***');
|
||||
mes = false;
|
||||
}
|
||||
//console.log(`${key}: ${directoriesB[key]}`);
|
||||
//console.log(JSON.parse(charactersB[key]));
|
||||
//console.log(directoriesB[key]);
|
||||
|
||||
var char = JSON.parse(charactersB[key]);
|
||||
char.create_date = humanizedISO8601DateTime();
|
||||
|
Reference in New Issue
Block a user