generated from p1atdev/deno_template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nai.ts
70 lines (60 loc) · 2.05 KB
/
nai.ts
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
import { decodeChunk, Chunk } from "./deps.ts"
import { Prompt, PromptLoader } from "./prompt.ts"
const NAIExifTag = ["Title", "Description", "Software", "Source", "Comment"] as const
export type NAIExifTagType = typeof NAIExifTag[number]
export type NAISamplingAlgorithm = "k_euler_ancestral" | "k_euler" | "k_lms" | "plms" | "ddim"
export const NAISoftwareName = "NovelAI"
export interface NAIPrompt extends Prompt {
model: typeof NAISoftwareName
strength: number
noise: number
samplingAlgorithm: NAISamplingAlgorithm
}
export interface NAIMetaComment {
steps: number
sampler: string
seed: number
strength: number
noise: number
scale: number
uc: string
}
export interface NAIChunk extends Chunk {
keyword: NAIExifTagType
text: string
}
export class NAIPromptLoader extends PromptLoader {
getPrompt = (): NAIPrompt => {
const decoded = this.chunks
.map((chunk) => decodeChunk(chunk))
.filter((chunk): chunk is NAIChunk => {
return NAIExifTag.some((tag) => tag === chunk.keyword)
})
const positive = decoded.find((chunk) => chunk.keyword === "Description")?.text
if (!positive) {
throw new Error("Description chunk not found")
}
const comment = decoded.find((chunk) => chunk.keyword === "Comment")?.text
if (!comment) {
throw new Error("Comment chunk not found")
}
const meta: NAIMetaComment = JSON.parse(comment)
const metaInfo: NAIPrompt = {
model: NAISoftwareName,
source: this.exif.Source,
positive: positive ?? "",
negative: meta.uc,
size: {
width: this.exif.ImageWidth,
height: this.exif.ImageHeight,
},
seed: meta.seed,
steps: meta.steps,
scale: meta.scale,
strength: meta.strength,
noise: meta.noise,
samplingAlgorithm: meta.sampler as NAISamplingAlgorithm,
}
return metaInfo
}
}