-
Notifications
You must be signed in to change notification settings - Fork 5
/
auto1111.ts
154 lines (136 loc) · 4.16 KB
/
auto1111.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
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
147
148
149
150
151
152
153
154
import { log } from "./log.ts"
import { SizeNumber } from "./types/mod.ts"
import {
Sampler,
SDModel,
SDAPIOptions,
defaultTxt2ImgOptions,
Txt2ImgOptions,
Txt2ImgRes,
Progress,
PromptStyle,
} from "./types/mod.ts"
export interface AUTO1111Options {
host: string
}
export interface ImagineOptions extends Record<string, any> {
prompt: string
negativePrompt: string
steps: number
scale: number
seed: number
sampler: string
width: SizeNumber
height: SizeNumber
highresFix: boolean
clipSkip: number
count: number
}
export class AUTO1111 {
private host: string
constructor({ host }: AUTO1111Options) {
this.host = host
}
sdModels = async () => {
const url = new URL("/sdapi/v1/sd-models", this.host)
const res = await fetch(url)
const data: SDModel[] = await res.json()
return data
}
samplers = async () => {
const url = new URL("/sdapi/v1/samplers", this.host)
const res = await fetch(url)
const data: Sampler[] = await res.json()
return data
}
promptStyles = async () => {
const url = new URL("/sdapi/v1/prompt-styles", this.host)
const res = await fetch(url)
const data: PromptStyle[] = await res.json()
return data
}
options = async () => {
const url = new URL("/sdapi/v1/options", this.host)
const res = await fetch(url)
const data: SDAPIOptions = await res.json()
return data
}
switchModel = async (modelName: string) => {
const url = new URL("/sdapi/v1/options", this.host)
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
sd_model_checkpoint: modelName,
}),
})
const data: SDAPIOptions = await res.json()
return data
}
refreshCheckpoints = async () => {
const url = new URL("/sdapi/v1/refresh-checkpoints", this.host)
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
})
const _data = await res.text()
if (res.status === 200) {
return true
} else {
return false
}
}
imagine = async ({
prompt,
negativePrompt,
steps,
scale,
seed,
sampler,
width,
height,
highresFix,
clipSkip,
count,
}: Partial<ImagineOptions>): Promise<Txt2ImgRes> => {
const reqBody: Partial<Txt2ImgOptions> = structuredClone(defaultTxt2ImgOptions)
if (prompt !== undefined) reqBody.prompt = prompt
if (negativePrompt !== undefined) reqBody.negative_prompt = negativePrompt
if (steps !== undefined) reqBody.steps = steps
if (scale !== undefined) reqBody.cfg_scale = scale
if (seed !== undefined) reqBody.seed = seed
if (sampler !== undefined) reqBody.sampler_name = sampler
if (width !== undefined) reqBody.width = width
if (height !== undefined) reqBody.height = height
if (count !== undefined) reqBody.n_iter = count
if (highresFix !== undefined) {
reqBody.enable_hr = highresFix
}
reqBody.override_settings = {}
reqBody.override_settings_restore_afterwards = true
if (clipSkip !== undefined) {
reqBody.override_settings.CLIP_stop_at_last_layers = clipSkip
}
// log.info("Imagine parameters:", reqBody)
const url = new URL("/sdapi/v1/txt2img", this.host)
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(reqBody),
})
const data: Txt2ImgRes = await res.json()
return data
}
progress = async () => {
const url = new URL("/sdapi/v1/progress", this.host)
const res = await fetch(url)
const data: Progress = await res.json()
return data
}
}