JSPM

  • Created
  • Published
  • Downloads 1612
  • Score
    100M100P100Q97909F
  • License ISC

Novita api sdk

Package Exports

  • novita-sdk
  • novita-sdk/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (novita-sdk) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Novita Javascript SDK

this SDK is based on the official API documentation

join our discord server for help

Installation, via npm

npm i novita-sdk

Quick Start

Get api key refer to https://docs.novita.ai/get-started

We offer two ways to use the sdk

1.Called as a function

import { txt2ImgSync, setNovitaKey } from "novita-sdk";

setNovitaKey("your api key");

txt2ImgSync({
  model_name: "",
  prompt: "a girl",
})
  .then((res) => {
    console.log("imgs", res);
  })
  .catch((err) => {
    console.log(err);
  });

2.Use by way of class

import { NovitaSDK } from "novita-sdk";

const sdk = new NovitaSDK("your api key");

sdk
  .txt2ImgSync(params)
  .then((res) => {
    console.log("imgs", res);
  })
  .catch((err) => {
    alert(err);
  });

function list

  • setNovitaKey
  • getModels
  • img2img
  • txt2Img
  • txt2ImgSync
  • img2imgSync
  • upscale
  • upscaleSync

Usage in React

import * as React from 'react';
import { txt2ImgSync } from 'novita-sdk';
import './style.css';

const { useState, useCallback } = React;

export default function App() {
  const [imgs, setImgs] = useState<string[]>([]);
  const [loading, setLoading] = useState(false);
  const generateImg = useCallback(() => {
    setLoading(true);
    txt2ImgSync({
      model_name: '',
      prompt: 'a girl',
    })
      .then((res) => {
        setImgs(res);
        setLoading(false);
      })
      .catch((err) => {
        console.log(err);
        setLoading(false);
      });
  }, []);
  return (
    <div>
      <h1>Text to image</h1>
      <button onClick={generateImg} disabled={loading}>
        {loading ? 'progressing' : 'click to generate image'}
      </button>
      <div
        style={{
          marginTop: '20px',
        }}
      >
        {imgs.map((one) => (
          <img
            src={one}
            crossOrigin="anonymous"
            referrerPolicy="origin-when-cross-origin"
            style={{
              objectFit: 'cover',
            }}
          />
        ))}
      </div>
    </div>
  );
}

When you use this model interface, keep an eye on dependency_status and download_status, which are only considered to be available if they are both 1

We recommend that you keep the interface data in memory, e.g. redux.

getModels().then((res) => {
  console.log(res.models.slice(0, 100));
});

Lora Usage

txt2ImgSync({
  model_name: "majicmixRealistic_v2.safetensors",
  prompt:
    "Best quality, masterpiece, ultra high res, (photorealistic:1.4), raw photo, 1girl, offshoulder, in the dark, deep shadow, low key, cold light",
  negative_prompt:
    "ng_deepnegative_v1_75t, badhandv4 (worst quality:2), (low quality:2), (normal quality:2), lowres, bad anatomy, bad hands, normal quality, ((monochrome)), ((grayscale))",
  sampler_name: "DPM++ 2M Karras",
  lora: [
    {
      sd_name: "film",
      weight: 0.4,
    },
  ],
}).then((res) => {
  console.log(res);
});

ControlNet QRCode

txt2ImgSync({
  prompt:
    "a beautify butterfly in the colorful flowers, best quality, best details, masterpiece",
  model_name: "",
  steps: 30,
  controlnet_units: [
    {
      input_image: imgbase64,
      module: ControlNetPreprocessor.NULL,
      control_mode: ControlNetMode.BALANCED,
      model: "control_v1p_sd15_qrcode_monster_v2",
      weight: 2.0,
    },
  ],
}).then((res) => {
  console.log(res);
});

Upscalse

.upscaleSync({
  image: base64String,
  resize_mode: 0,
  upscaling_resize: 2,
})
.then((res) => {
  if (res) {
    setImg(res[0]);
  }
  setLoading(false);
})
.catch((err) => {
  alert(err);
});