Skip to content

api callback return raw data#193

Open
joindn wants to merge 3 commits into
SipherAGI:mainfrom
joindn:api-callback-raw-data
Open

api callback return raw data#193
joindn wants to merge 3 commits into
SipherAGI:mainfrom
joindn:api-callback-raw-data

Conversation

@joindn

@joindn joindn commented Dec 8, 2023

Copy link
Copy Markdown

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

@Xerophayze

Copy link
Copy Markdown

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');
}

});

@Xerophayze Xerophayze left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether it was chance or destiny, it just so happens that A system that I was implementing required that I'd be able to move the raw image data from the web UI Forge install that I have using the agent scheduler to my backend server for my website. And then from there to the front end. Once I implemented these changes that were shown in this pull request, and with a little bit of troubleshooting on my code, it worked great. If you want to see the demonstration of it, it's part of my Xero online prompt generator XeroGen, which you can watch the intro and tutorial here https://shop.xerophayze.com/xerogen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants