Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api callback return raw data #193

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

joindn
Copy link

@joindn joindn commented Dec 8, 2023

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

@Xerophayze
Copy link

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

});

Copy link

@Xerophayze Xerophayze left a comment

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