llama vision | start a bunch of llm rpc services | mask image | Search

The code requires modules for file operations and HTTP requests, defines a constant for the output directory path, and includes an asynchronous function doStableRequest to generate an image based on a given prompt. This function makes a POST request to the stable diffusion API, processes the response, and returns an object containing the seed, image buffer, image path, and prompt.

Run example

npm run import -- "stable diffusion request"

stable diffusion request

const fs = require('fs')
const path = require('path')
const {request} = require('gaxios')

const OUTPUT_PATH = path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE, 'stable-diffusion-webui/outputs')

async function doStableRequest(prompt) {
  let width = 1024
  if(prompt.includes('View360')) {
    width = 2048
  }
  let height = 1024
  let specificHeight = prompt.match(/--height=(\d+)/)
  let specificWidth = prompt.match(/--width=(\d+)/)
  if(specificHeight) {
    height = parseInt(specificHeight)
    prompt = prompt.replace(/--height=(\d+)/, '')
  }
  if(specificWidth) {
    width = parseInt(specificWidth)
    prompt = prompt.replace(/--width=(\d+)/, '')
  }
  try {
    let result = await request({
      url: 'http://127.0.0.1:7860/sdapi/v1/txt2img',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      data: JSON.stringify({
        prompt: prompt,
        negative_prompt: 'bad hands, bad feet, bad faces, bad eyes, bad anatomy, extra limbs, missing limbs, tattoo, statue, picture frame, anime, cartoon, signature, abstract',
        save_images: true,
        "width": width,
        "height": height,
        "steps": 30,
        tiling: false,
      })
    })
    let seed = JSON.parse(result.data.info).seed
    let buff = Buffer.from(result.data.images[0], 'base64');
    let now = new Date()
    let folderName = now.getFullYear() + '-' + String(now.getMonth() + 1).padStart(2, '0') + '-' + String(now.getDate()).padStart(2, '0')
    let stablePath = path.join(OUTPUT_PATH, 'txt2img-images', folderName)
    let imagePath
    if(fs.existsSync(stablePath)) {
      let images = fs.readdirSync(stablePath)
      for(let i = 0; i < images.length; i++) {
        if(images[i].match('-' + seed + '-')) {
          imagePath = path.join('txt2img-images', folderName, images[i])
          break
        }
      }
    }
    return {seed, image: buff, imagePath, prompt}
  } catch (e) {
    console.error(e)
  }
}

module.exports = {
  doStableRequest,
  OUTPUT_PATH
}

What the code could have been:

const fs = require('fs');
const path = require('path');
const { request } = require('gaxios');

/**
 * Path to output directory
 */
const OUTPUT_PATH = path.join(
  process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE,
 'stable-diffusion-webui/outputs'
);

/**
 * Make a request to the stable diffusion API
 * @param {string} prompt - The prompt to use for the image generation
 * @returns {object} An object containing the seed, image buffer, image path, and prompt
 */
async function doStableRequest(prompt) {
  // Define default image dimensions
  let width = 1024;
  let height = 1024;

  // Check for special flags in the prompt
  if (prompt.includes('View360')) {
    width = 2048;
  }

  // Check for specific height and width parameters in the prompt
  const specificHeightMatch = prompt.match(/--height=(\d+)/);
  const specificWidthMatch = prompt.match(/--width=(\d+)/);
  if (specificHeightMatch) {
    height = parseInt(specificHeightMatch[1]);
    prompt = prompt.replace(/--height=(\d+)/, '');
  }
  if (specificWidthMatch) {
    width = parseInt(specificWidthMatch[1]);
    prompt = prompt.replace(/--width=(\d+)/, '');
  }

  try {
    // Make the request to the API
    const result = await request({
      url: 'http://127.0.0.1:7860/sdapi/v1/txt2img',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      data: JSON.stringify({
        prompt,
        negative_prompt: 'bad hands, bad feet, bad faces, bad eyes, bad anatomy, extra limbs, missing limbs, tattoo, statue, picture frame, anime, cartoon, signature, abstract',
        save_images: true,
        width,
        height,
        steps: 30,
        tiling: false,
      }),
    });

    // Extract the seed and image buffer from the response
    const seed = JSON.parse(result.data.info).seed;
    const imageBuffer = Buffer.from(result.data.images[0], 'base64');

    // Get the current date and time
    const now = new Date();

    // Create the directory path for the output images
    const folderName = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
    const stablePath = path.join(OUTPUT_PATH, 'txt2img-images', folderName);

    // Check if the output directory exists, and if so, find the image with the matching seed
    let imagePath;
    if (fs.existsSync(stablePath)) {
      const images = fs.readdirSync(stablePath);
      for (const image of images) {
        if (image.match(`-${seed}-`)) {
          imagePath = path.join('txt2img-images', folderName, image);
          break;
        }
      }
    }

    // Return the seed, image buffer, image path, and prompt
    return { seed, image: imageBuffer, imagePath, prompt };
  } catch (error) {
    // Log the error, but don't break the program
    console.error(error);
  }
}

module.exports = {
  doStableRequest,
  OUTPUT_PATH,
};

Code Breakdown

Requirements and Constants

The code starts by requiring the following modules:

It then defines a constant OUTPUT_PATH which is the path to the output directory where images will be saved. This path is constructed using the process.env object to get the home directory path.

doStableRequest Function

The doStableRequest function is an asynchronous function that takes a prompt string as input. It appears to be a stable diffusion API request function, which generates an image based on the given prompt.

Processing the Prompt

The function first checks if the prompt includes the string "View360" and sets the width to 2048 if it does. It also checks for specific height and width values in the prompt and updates the corresponding variables if found.

Making the API Request

The function then makes a POST request to the stable diffusion API using the request function from gaxios. The request includes the prompt, negative prompt, save images flag, width, height, steps, and tiling settings.

Processing the Response

After making the request, the function parses the response data and extracts the seed, image buffer, and other information. It then constructs a folder name based on the current date and looks for an existing image in the output directory with the same seed. If an image is found, it returns the seed, image buffer, image path, and prompt. If no image is found, it returns an empty object.

Error Handling

The function catches any errors that occur during the request or processing and logs them to the console.

Module Exports

Finally, the code exports the doStableRequest function and the OUTPUT_PATH constant.

Function Signature

doStableRequest

Parameters

Returns

Throws