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

@stable-canvas/sd-webui-a1111-prompt-parser

v1.2.4

Published

sd-webui-a1111-prompt-parser is a Stable Diffusion webUI (A1111) prompt parser for JavaScript. It parses Stable Diffusion model prompts into structured data for easy analysis and manipulation by developers.

Downloads

21

Readme

sd-webui-a1111-prompt-parser

Introduction

sd-webui-a1111-prompt-parser is a Stable Diffusion webUI (A1111) prompt parser for JavaScript. It parses Stable Diffusion model prompts into structured data for easy analysis and manipulation by developers.

Features

  • Parses A1111 format prompts, supporting the following syntax:
    • Plain text
    • Emphasis (parentheses)
    • Weight (brackets)
    • Lora models
    • Hypernetwork models
    • Negative prompts (square brackets)
    • Step Control (scheduling)
  • Converts parsed results into JavaScript objects for easy manipulation and use
  • Supports regenerating A1111 format prompts from JavaScript objects

Installation

pnpm add @stable-canvas/sd-webui-a1111-prompt-parser

Usage

import { PromptParser } from "@stable-canvas/sd-webui-a1111-prompt-parser";

const parser = new PromptParser();

const prompt = `masterpiece, 1girl, blonde hair, <lora:Zelda_v1:0.5>, (chromatic aberration:0.7), sharp focus, hyper detailed, (fog:0.7), <hypernet:sxz-bloom:0.5>, [real photo], [highlight:dark:0.9], (((good anatomy)))`;

const output = parser.parse(prompt);

console.log(output);

Output:

[
  { "type": "plain", "value": "masterpiece" },
  { "type": "plain", "value": "1girl" },
  { "type": "plain", "value": "blonde hair" },
  { "type": "extra_networks", "value": "lora", "args": ["Zelda_v1", "0.5"] },
  { "type": "weighted", "value": 0.7, "args": [{ "type": "plain", "value": "chromatic aberration" }] },
  { "type": "plain", "value": "sharp focus" },
  { "type": "plain", "value": "hyper detailed" },
  { "type": "weighted", "value": 0.7, "args": [{ "type": "plain", "value": "fog" }] },
  { "type": "extra_networks", "value": "hypernet", "args": ["sxz-bloom", "0.5"] },
  { "type": "negative", "value": 1, "args": [{ "type": "plain", "value": "real photo" }] },
  { "type": "scheduled_full", "value": 0.9, "args": [[{ "type": "plain", "value": "highlight" }], [{ "type": "plain", "value": "dark" }]] },
  { "type": "positive", "value": 3, "args": [{ "type": "plain", "value": "good anatomy" }] }
]

API

PromptParser Class

Constructor

new PromptParser(options?: SDPromptParser.ILarkOptions);
  • options: Optional parameters for configuring the Lark parser. For detailed parameter descriptions, refer to the [Lark documentation](https://lark-parser.readthedocs.io/en/latest/classes/ Lark.html#lark.Lark).

parse Method

parse(text: string, options?: ParseOptions): SDPromptParser.PromptNode[];
  • text: The prompt string to be parsed.
  • options: Optional parameters for configuring parsing behavior.
    • force: When set to true, parsing is forced even if there are syntax errors in the prompt. The parsing results may be incomplete. The default is false.
  • Returns: An array of parsed prompt nodes, node type definitions refer to SDPromptParser.PromptNode.

compilation Function

compilation(node: SDPromptParser.IPromptASTNode): SDPromptParser.PromptNode[];
  • node: The root node of the abstract syntax tree (AST) generated by the Lark parser.
  • Returns: An array of parsed prompt nodes, node type definitions refer to SDPromptParser.PromptNode.

generation_token Function

generation_token(nodes: SDPromptParser.PromptNode[], options?: GenerationOptions): string[];
  • nodes: An array of nodes for which to generate prompt strings.
  • options: Optional parameters for configuring generation behavior.
    • remove_1_weighted: When set to true, nodes with a weight of 1 are removed. The default is false.
  • Returns: An array of generated prompt strings.

generation_str Function

generation_str(nodes: SDPromptParser.PromptNode[], options?: GenerationOptions): string;
  • nodes: An array of nodes for which to generate the prompt string.
  • options: Optional parameters for configuring generation behavior.
    • remove_1_weighted: When set to true, nodes with a weight of 1 are removed. The default is false.
  • Returns: The generated prompt string.

Build

1. Build the Parser

1.1 Install Lark.js

pip install lark-js

1.2 Build

pnpm build-lark

2. Build the Package

pnpm build

Test

pnpm test

License

MIT