Skip to content
Snippets Groups Projects
Commit d8058431 authored by svornost's avatar svornost
Browse files

If a single slave has multiple image generation requests queued, satisfy them...

If a single slave has multiple image generation requests queued, satisfy them all with the generated image from the last such request.
parent 26d04d17
No related branches found
No related tags found
No related merge requests found
......@@ -81,7 +81,7 @@ async function fetchWithTimeout(url, timeout, options) {
App.Art.GenAI.StableDiffusionClientQueue = class {
constructor() {
/** @type {Array<{slaveID: number, body: string, resolve: function(Response | PromiseLike<Response>): void, reject: function(string): void}>} */
/** @type {Array<{slaveID: number, body: string, resolve: function(Object): void, reject: function(string): void}>} */
this.queue = [];
this.interrupted = false;
}
......@@ -93,22 +93,30 @@ App.Art.GenAI.StableDiffusionClientQueue = class {
async process() {
while (this.queue.length > 0 && !this.interrupted) {
const top = this.queue.first();
console.log("Fetching image for slave: ", top.slaveID);
// find all the requests for this slave in the queue; we'll satisfy them all at once
// using only the newest data (i.e. the data from the last member)
const satisfied = this.queue.filter(item => item.slaveID === top.slaveID);
console.log(`Fetching image for slave ${top.slaveID}, satisfying ${satisfied.length} requests`);
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: top.body,
body: satisfied.last().body,
};
try {
const results = await fetchWithTimeout(`${V.aiApiUrl}/sdapi/v1/txt2img`, 60000, options);
top.resolve(results);
const response = await fetchWithTimeout(`${V.aiApiUrl}/sdapi/v1/txt2img`, 60000, options);
if (!response.ok) {
throw new Error(`Error fetching Stable Diffusion image - status: ${response.status}`);
}
const obj = await response.json();
satisfied.forEach(item => item.resolve(obj));
} catch (e) {
top.reject(e);
satisfied.forEach(item => item.reject(e));
}
this.queue.shift();
this.queue.delete(...satisfied);
}
}
......@@ -126,7 +134,7 @@ App.Art.GenAI.StableDiffusionClientQueue = class {
* Queue image generation for an entity
* @param {number} slaveID or a unique negative value for non-slave entities
* @param {string} body body of the post request to be sent to txt2img
* @returns {Promise<Response>}
* @returns {Promise<Object>}
*/
async add(slaveID, body) {
if (this.interrupted) {
......@@ -183,22 +191,6 @@ App.Art.GenAI.StableDiffusionClientQueue = class {
App.Art.GenAI.sdQueue = new App.Art.GenAI.StableDiffusionClientQueue();
App.Art.GenAI.StableDiffusionClient = class {
/**
* @param {number} slaveID
* @param {App.Art.GenAI.StableDiffusionSettings} settings
* @returns {Promise<string>} - Base 64 encoded image (could be a jpeg or png)
*/
async fetchImage(slaveID, settings) {
const response = await App.Art.GenAI.sdQueue.add(slaveID, JSON.stringify(settings));
if (!response.ok) {
console.error("Error fetching Stable Diffusion image", response);
throw new Error(`Error fetching Stable Diffusion image - status: ${response.status}`);
}
let parsedRes = await response.json();
return parsedRes.images[0];
}
/**
* @param {FC.SlaveState} slave
* @returns {App.Art.GenAI.StableDiffusionSettings}
......@@ -237,7 +229,8 @@ App.Art.GenAI.StableDiffusionClient = class {
}
});
return this.fetchImage(slave.ID, settings);
const response = await App.Art.GenAI.sdQueue.add(slave.ID, JSON.stringify(settings));
return response.images[0];
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment