-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathindex.ts
More file actions
146 lines (132 loc) · 5.2 KB
/
index.ts
File metadata and controls
146 lines (132 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { IPromptWord, PromptWordType } from "../../parsePrompts"
import { translateZh2En } from "../../../translatePrompts"
import { round } from "lodash"
import {IPromptGroup} from "../../../../Sub/PromptWork";
/** 解析命令 */
export async function stableDiffusionWebUIParse(text: string, options?: { zh2en?: boolean }): Promise<IPromptWord[]> {
// 因为使用较少,暂不支持 Prompt matrix https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#prompt-matrix
// let textListByMatrix = text.split("|")
let words: IPromptWord[] = []
let texts = split(text).filter((t) => t != "")
console.log("[stableDiffusionWebUIParse]", { texts })
if (options?.zh2en) texts = await translateZh2En(texts)
texts.forEach((text, i) => {
let { word } = paresWord(text)
words.push(<IPromptWord>word)
})
return words
}
function split(text: string) {
return text.split(/[,,\n]/).map((word) => word.trim())
}
/** 解析单个字符 */
function paresWord(text: string) {
const REG_Attention_number = /^\(([^:]+?):([0-9\.]+?)\)$/ // (xxx:2)
const REG_Attention_adds = /^(\(+)(.+?)(\)+)$/ // ((((xxx)))
const REG_Attention_subs = /^(\[+)(.+?)(\]+)$/ // [[xxx]]
const REG_ExtraNetworks = /^<(.+?):(.+?):(.*?)>$/ // <lora:filename:multiplier>
const Editing_from_to_when = /^\[(.*?):(.*?):(.*?)\]$/ // [from:to:when]
const Editing_to_when = /^\[(.*?):(.*?)\]$/ // [from:to:when]
let lv, alv, displayText
let word
let re: any
// [from:to:when] https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#prompt-editing
if (match(Editing_from_to_when.test(text))) {
displayText = text
word = {
text: displayText,
type: PromptWordType.Word,
rawText: text,
...Commands.Editing_from_to_when,
}
}
// [from:when]
else if (match(Editing_to_when.test(text))) {
displayText = text
word = {
text: displayText,
type: PromptWordType.Word,
rawText: text,
...Commands.Editing_to_when,
}
}
// (xxx:2)
else if (match(REG_Attention_number.exec(text))) {
displayText = re[1]
lv = re[2]
word = { text: displayText, lv, type: PromptWordType.Word, rawText: text }
}
// ((((xxx)))
else if (match(REG_Attention_adds.exec(text))) {
displayText = re[2]
alv = re[1].length
lv = round(Math.pow(1.1, alv), 2)
word = { text: displayText, lv, alv, type: PromptWordType.Word, rawText: text }
}
// [[[xxx]]]
else if (match(REG_Attention_subs.exec(text))) {
displayText = re[2]
alv = -re[1].length
lv = round(1 / Math.pow(1.1, Math.abs(alv)), 2)
word = { text: displayText, lv, alv, type: PromptWordType.Word, rawText: text }
}
// <lora:filename:multiplier>
else if (match(REG_ExtraNetworks.exec(text))) {
word = {
text: text,
type: PromptWordType.Word,
rawText: text,
langText: `${re[1]}`,
subType: "command",
link: `https://www.google.com/search?q=${encodeURIComponent(re[2])}%20 civitai.com `,
}
} else {
displayText = text
word = { text: displayText, type: PromptWordType.Word, rawText: text }
}
// console.log("[paresWordInfo]", word)
return { displayText, lv, word }
function match(v: any) {
re = v
return v
}
}
const Commands = {
Editing_from_to_when: {
subType: "command",
langText: "剪辑 (Editing) ",
desc: `[起始词:结束词:切换时机] \n在生成采样的过程中,从一个关键词切换到另一个关键词。\n切换时机:大于 1 的整数代表指定步数,如果小于 1 的小数代表百分比时间。\n结束词: 如果为空表示切换时删除起始词`,
link: `https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#prompt-editing`,
},
Editing_to_when: {
subType: "command",
langText: "剪辑 (Editing) ",
desc: `[起始词:切换时机] \n在生成采样的过程中,从一个关键词切换到另一个关键词。\n切换时机:大于 1 的整数代表指定步数,如果小于 1 的小数代表百分比时间。`,
link: `https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#prompt-editing`,
},
}
export function stableDiffusionWebUIStringify(groups: IPromptGroup[] = []) {
let finText = ""
let commands: string[] = []
let i = 0,
len = groups.length
for (let group of groups) {
let chars: string[] = []
for (let list of group.lists) {
for (let item of list.items) {
if (item.data.disabled) continue
if (item.data.word.subType === "command") {
commands.push(item.data.word.rawText!)
} else {
chars.push(item.data.word.rawText!)
}
}
}
finText += chars.filter((x) => x != "").join(", ")
finText += ` `
i++
}
if (commands.length > 0) finText += ` ${commands.join(" ")}`
// console.log("exportPrompts")
return finText.trim()
}