get image

This commit is contained in:
zjt 2024-05-25 22:31:49 +08:00
parent 6f43190f9d
commit a32007c8fd
3 changed files with 62 additions and 47 deletions

View File

@ -20,7 +20,8 @@ const Txt23DHandler: RequestHandler<any, any> = async (ctx) => {
const { prompt, url } = requestBody; const { prompt, url } = requestBody;
const inputNode = selectNodeFromApiJSONbyID(txt23dApiFormatJSON, "33"); const inputNode = selectNodeFromApiJSONbyID(txt23dApiFormatJSON, "33");
inputNode.inputs.string = prompt; inputNode.inputs.string = prompt;
ctx.body = { try {
ctx.body = {
url: ctx.body = baseUrl + '/view?filename=' + await new Promise((resolve, reject) => { url: ctx.body = baseUrl + '/view?filename=' + await new Promise((resolve, reject) => {
const taskID = Math.random().toFixed(10); const taskID = Math.random().toFixed(10);
const ws = new WebSocket(`${baseWsUrl}/ws?clientId=${taskID}`); const ws = new WebSocket(`${baseWsUrl}/ws?clientId=${taskID}`);
@ -52,5 +53,9 @@ const Txt23DHandler: RequestHandler<any, any> = async (ctx) => {
} }
}) })
} }
} catch (error) {
console.log(error);
}
} }
export default Txt23DHandler; export default Txt23DHandler;

View File

@ -22,58 +22,63 @@ const Txt2ImgHandler: RequestHandler<Txt2ImgRequest, any> = async (ctx, next) =>
const { prompt, loraDetail, loraModel } = requestBody; const { prompt, loraDetail, loraModel } = requestBody;
const inputNode = selectNodeFromApiJSONbyID(txt2imgAPIformatJSON, "33"); const inputNode = selectNodeFromApiJSONbyID(txt2imgAPIformatJSON, "33");
let temp = ""; let temp = "";
let preViewBlobs:any = []; let preViewBlobs: any = [];
inputNode.inputs.string = prompt; inputNode.inputs.string = prompt;
ctx.body = { try {
data: await new Promise((resolve, reject) => { ctx.body = {
const taskID = Math.random().toFixed(10); data: await new Promise((resolve, reject) => {
const ws = new WebSocket(`${baseWsUrl}/ws?clientId=${taskID}`); const taskID = Math.random().toFixed(10);
ws.onopen = () => { const ws = new WebSocket(`${baseWsUrl}/ws?clientId=${taskID}`);
try { ws.onopen = () => {
axios.post("/prompt", { try {
client_id: taskID, axios.post("/prompt", {
prompt: txt2imgAPIformatJSON, client_id: taskID,
// extra_data: txt2imgAPIformatExtraData prompt: txt2imgAPIformatJSON,
}); // extra_data: txt2imgAPIformatExtraData
console.log(`ok`); });
} catch (error) { console.log(`ok`);
console.log(error); } catch (error) {
} console.log(error);
}
ws.onmessage = (event) => { ws.onmessage = (event) => {
if (typeof event.data === "string") { if (typeof event.data === "string") {
const { type, data } = JSON.parse(event.data); const { type, data } = JSON.parse(event.data);
// if (type === "executed") { // if (type === "executed") {
// if (data.node === '94') { // if (data.node === '94') {
// resolve(data.output.images[0].filename) // resolve(data.output.images[0].filename)
// } // }
// } // }
if (type === "executed") { if (type === "executed") {
if (data.node === '21') { if (data.node === '21') {
temp = data.output.text[0]; temp = data.output.text[0];
} }
if (data.node === '94') { if (data.node === '94') {
console.log(data.output); console.log(data.output);
const filePath = './data.json'; const filePath = './data.json';
readJSONFile(filePath) readJSONFile(filePath)
.then((data: any) => { .then((data: any) => {
console.log('读取的JSON数据:', data); console.log('读取的JSON数据:', data);
// 修改对象 // 修改对象
data.push({ prompt: prompt, url: baseUrl + '/view?filename=' + data.output.images[0].filename, like: 0, index: data.length }) data.push({ prompt: prompt, url: baseUrl + '/view?filename=' + data.output.images[0].filename, like: 0, index: data.length })
// 保存回JSON文件 // 保存回JSON文件
saveJSONFile(filePath, data); saveJSONFile(filePath, data);
}) })
resolve({ prompt: temp, url: baseUrl + '/view?filename=' + data.output.images[0].filename }); resolve({ prompt: temp, url: baseUrl + '/view?filename=' + data.output.images[0].filename });
}
} else if (type === "executing") {
} }
}else if (type === "executing") { } else if (event.data instanceof Buffer) {
} }
} else if (event.data instanceof Buffer) {
} }
} }
} })
}) }
} catch (error) {
console.log(error);
} }
} }
export default Txt2ImgHandler; export default Txt2ImgHandler;

View File

@ -21,7 +21,8 @@ const UpscaleHandler: RequestHandler<any ,any> = async (ctx) => {
const { prompt, url } = requestBody; const { prompt, url } = requestBody;
const inputNode = selectNodeFromApiJSONbyID(upScaleAPIFormatJSON, "12"); const inputNode = selectNodeFromApiJSONbyID(upScaleAPIFormatJSON, "12");
inputNode.inputs.image = url; inputNode.inputs.image = url;
ctx.body = { try {
ctx.body = {
url: baseUrl + '/view?filename=' + await new Promise((resolve, reject) => { url: baseUrl + '/view?filename=' + await new Promise((resolve, reject) => {
const taskID = Math.random().toFixed(10); const taskID = Math.random().toFixed(10);
const ws = new WebSocket(`${baseWsUrl}/ws?clientId=${taskID}`); const ws = new WebSocket(`${baseWsUrl}/ws?clientId=${taskID}`);
@ -51,6 +52,10 @@ const UpscaleHandler: RequestHandler<any ,any> = async (ctx) => {
} }
} }
}) })
}
}catch (error) {
console.log(error);
} }
} }
export default UpscaleHandler; export default UpscaleHandler;