npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

stability-ai

v0.7.0

Published

Node SDK for Stability AI REST API

Downloads

203

Readme

Stability AI Node SDK

A TypeScript library to easily access the Stability AI REST API.

Installation

Yarn

yarn add stability-ai

NPM

npm i stability-ai

General Usage

import StabilityAI from 'stability-ai';

const stability = new StabilityAI(process.env.STABILITY_AI_API_KEY);

All images passed to this library must be in the format of a local filepath or a public URL.

Table of Contents

User (v1)

Engines (v1)

Generation (v1)

3D (v2beta)

Stable Video (v2beta)

Stable Image (v2beta)

User (v1)

Account

const { email, id, organizations, profile_picture } = await stability.v1.user.account()

console.log('User email:', email);
console.log('User id:', id);
console.log('User organizations:', organizations);
if (profile_picture) console.log('User profile picture:', profile_picture);

Balance

const { credits } = await stability.v1.user.balance()

console.log('User credits balance:', credits);

Engines (v1)

List

const engines = await stability.v1.engines.list()

console.log('Engine list:', engines);

Generation (v1)

Text to Image

const results = await stability.v1.generation.textToImage(
  'stable-diffusion-xl-1024-v1-0', 
  [
    { text: 'a man on a horse', weight: 0.5 }
  ]
)

for (const result of results) {
  console.log('Text to image result filepath:', result.filepath);
}

Image to Image

const results = await stability.v1.generation.imageToImage(
  'stable-diffusion-xl-1024-v1-0', 
  [
    { text: 'crazy techincolor surprise', weight: 0.5 }
  ],
  'https://www.example.com/images/your-image.jpg'
)

for (const result of results) {
  console.log('Image to image result filepath:', result.filepath);
}

Image to Image - Masking

const results = await stability.v1.generation.imageToImageMasking(
  'stable-diffusion-xl-1024-v1-0', 
  [
    { text: 'a beautiful ocean', weight: 0.5 }
  ],
  'https://www.example.com/images/your-image-with-alpha-channel.png',
  {
    mask_source: 'INIT_IMAGE_ALPHA'
  }
)

for (const result of results) {
  console.log('Image to image masking result filepath:', result.filepath);
}

3D (v2beta)

Stable Fast 3D

  const result = await stability.v2beta.stable3D.stableFast3D(
    'https://www.example.com/images/photo-you-want-to-move.png'
  );

  console.log('Stable 3D Stable Fast 3D result filepath:', result.filepath);

Stable Video (v2beta)

Image to Video

const result = await stability.v2beta.stableVideo.imageToVideo(
  'https://www.example.com/images/photo-you-want-to-move.png'
);

let filepath: string | undefined = undefined;

while (!filepath) {
  const videoResult = await stability.v2beta.stableVideo.imageToVideoResult(
    result.id,
  );

  if ('filepath' in videoResult) {
    filepath = videoResult.filepath;
  } else if (
    'status' in videoResult &&
    videoResult.status === 'in-progress'
  ) {
    await new Promise((resolve) => setTimeout(resolve, 2500));
  }
}

console.log('Stable Video Image to Video result filepath:', filepath);

Stable Image (v2beta)

Generate - Ultra

const result = await stability.v2beta.stableImage.generate.ultra('a beautiful mountain');

console.log('Stable Image Generate Ultra result filepath:', result.filepath);

Generate - Core

const result = await stability.v2beta.stableImage.generate.core('a beautiful ocean');

console.log('Stable Image Generate Core result filepath:', result.filepath);

Generate - SD3

const result = await stability.v2beta.stableImage.generate.sd3('a very beautiful ocean');

console.log('Stable Image Generate SD3 result filepath:', result.filepath);

Upscale - Conservative

const result = await stability.v2beta.stableImage.upscale.conservative(
  'https://www.example.com/images/photo-you-to-4k-upscale.png',
  'UHD 4k',
);

console.log('Stable Image Upscale Conservative result filepath:', result.filepath);

Upscale - Creative

const result = await stability.v2beta.stableImage.upscale.startCreative(
  'https://www.example.com/images/photo-you-to-4k-upscale.png',
  'UHD 4k',
);

let filepath: string | undefined = undefined;

while (!filepath) {
  const upscaleResult = await stability.v2beta.stableImage.upscale.fetchCreativeResult(
    result.id,
    result.outputFormat,
  );

  if ('filepath' in upscaleResult) {
    filepath = upscaleResult.filepath;
  } else if (
    'status' in upscaleResult &&
    upscaleResult.status === 'in-progress'
  ) {
    await new Promise((resolve) => setTimeout(resolve, 2500));
  }
}

console.log('Stable Image Upscale Creative result filepath:', filepath);

Edit - Erase

const result = await stability.v2beta.stableImage.edit.erase(
  'https://www.example.com/images/your-image-of-the-earth.png'
);

console.log('Stable Image Edit Erase result filepath:', result.filepath);

Edit - Inpaint

const result = await stability.v2beta.stableImage.edit.inpaint(
  'https://www.example.com/images/your-image-of-the-earth.png',
  'disco ball',
);

console.log('Stable Image Edit Inpaint result filepath:', result.filepath);

Edit - Outpaint

const result = await stability.v2beta.stableImage.edit.outpaint(
  'https://www.example.com/images/your-image-of-the-earth.png',
  {
    prompt: 'outer space',
    left: 100
  }
);

console.log('Stable Image Edit Outpaint result filepath:', result.filepath);

Edit - Search and Replace

const result = await stability.v2beta.stableImage.edit.searchAndReplace(
  'https://www.example.com/images/your-image-of-the-earth.png',
  'a disco ball',
  'the earth'
);

console.log('Stable Image Edit Search And Replace result filepath:', result.filepath);

Edit - Remove Background

const result = await stability.v2beta.stableImage.edit.removeBackground(
  'https://www.example.com/images/your-image-of-the-earth.png',
);

console.log('Stable Image Edit Remove Background result filepath:', result.filepath);

Edit - Replace Background and Relight

const result = await stability.v2beta.stableImage.edit.replaceBackgroundAndRelight(
  LOCAL_TEST_FILES.bird,
  {
    backgroundPrompt: 'a beautiful sunset',
    foregroundPrompt: 'enhance lighting',
    lightSourceDirection: 'above',
    lightSourceStrength: 0.7,
  },
);


let filepath: string | undefined = undefined;

while (!filepath) {
  const relightResult =
    await stability.v2beta.stableImage.results.fetchAsyncGenerationResult(
      result.id,
    );

  if ('filepath' in relightResult) {
    filepath = relightResult.filepath;
  } else if (
    'status' in relightResult &&
    relightResult.status === 'in-progress'
  ) {
    await new Promise((resolve) => setTimeout(resolve, 2500));
  }
}

console.log(
  'Stable Image Edit Replace Background and Relight result filepath:',
  filepath,
);

Control - Sketch

const result = await stability.v2beta.stableImage.control.sketch(
  'https://www.example.com/images/your-image-of-the-earth.png',
  'a disco ball'
);

console.log('Stable Image Control Sketch result filepath:', result.filepath);

Control - Structure

const result = await stability.v2beta.stableImage.control.structure(
  'https://www.example.com/images/your-image-of-the-earth.png',
  'a disco ball'
);

console.log('Stable Image Control Structure result filepath:', result.filepath);

Control - Style

const result = await stability.v2beta.stableImage.control.style(
  'https://www.example.com/images/your-image-of-the-earth.png',
  'a disco ball'
);

console.log('Stable Image Control Style result filepath:', result.filepath);

Development and testing

Built in TypeScript, tested with Jest.

$ yarn install
$ yarn test

Road Map

- Add input validation for filetypes, dimensions, etc (Zod integration as candidate for this).
- Support output to S3/GCS bucket
- Wrap job/result methods into one async task w/ internal polling