ArtVentureX / sd-webui-agent-scheduler

605 stars 61 forks source link

api callback return raw data #193

Open joindn opened 7 months ago

joindn commented 7 months ago

The API callback supports the raw image data format of the Stable Diffusion original API

Xerophayze commented 1 month ago

I was able to get this working with the system I have setup. using webui forge as my image system with your extension installed. modified the code necessary to implement this command. Here is the code i used in my backend to access this api and retrieve the raw image data and convert to jpg before sending it to my front end:

const payload = { prompt: prompt, cfg_scale: cfg, steps: steps, height: height, width: width, seed: seed, override_settings: { sd_model_checkpoint: checkpoint }, alwayson_scripts: alwayson_scripts || {}, enable_hr: enable_hr || false };

try {
    const queueResponse = await axios.post('http://server:port/agent-scheduler/v1/queue/txt2img', payload);
    const taskId = queueResponse.data.task_id;
    console.log('Task queued with ID:', taskId);

    let jobStatus;
    do {
        jobStatus = await checkJobStatus(taskId);
        await new Promise(resolve => setTimeout(resolve, 1000)); // Wait 1 seconds before checking again
    } while (jobStatus !== 'done');

    const imageDataResponse = await axios.get(`http://server:port/agent-scheduler/v1/results/${taskId}`);
    let imageBase64 = imageDataResponse.data.data[0].image; // Retrieve the base64 encoded image

    if (imageBase64.startsWith('data:image/')) {
        imageBase64 = imageBase64.split(',')[1]; // Remove the data URL prefix if present
    }

    const imageBuffer = Buffer.from(imageBase64, 'base64');
    const compressedImageBuffer = await sharp(imageBuffer).jpeg({ quality: 90 }).toBuffer();
    const jpegBase64 = compressedImageBuffer.toString('base64');

    res.json({ imageUrl: `data:image/jpeg;base64,${jpegBase64}` });
} catch (error) {
    console.error('Failed to generate image:', error);
    res.status(500).send('Failed to generate image due to server error');
}

});