-
Notifications
You must be signed in to change notification settings - Fork 0
/
sd_api.js
144 lines (124 loc) · 4.57 KB
/
sd_api.js
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
import fetch_sync from 'sync-fetch';
import fetch_async from 'node-fetch';
function GetInfo(url, keyName)
{
let json = fetch_sync(url).json();
let info = [];
json.forEach(element => {
info.push({
name: element[keyName],
value: element[keyName]
});
});
return info;
}
export function GetStyles()
{
return GetInfo("http://127.0.0.1:7860/sdapi/v1/prompt-styles", "name");
}
export function GetSamplers()
{
return GetInfo("http://127.0.0.1:7860/sdapi/v1/samplers", "name");
}
export function GetModels()
{
return GetInfo("http://127.0.0.1:7860/sdapi/v1/sd-models", "title");
}
export async function SetModel(model)
{
return fetch_async("http://127.0.0.1:7860/sdapi/v1/options", {
method: 'post',
body: JSON.stringify({ sd_model_checkpoint: model }),
headers: { 'Content-Type': 'application/json' }
});
}
export async function GetProgress()
{
return fetch_async("http://127.0.0.1:7860/sdapi/v1/progress").then(res => res.json());
}
export async function GetCurrentModel()
{
return fetch_async("http://127.0.0.1:7860/sdapi/v1/options")
.then(res => res.json())
.then(json => json["sd_model_checkpoint"]);
}
export async function Text2Img({prompt, neg_prompt, style, aspect_ratio, seed, sampler, steps, cfg_scale})
{
const {width, height} = await GetWidthHeight(aspect_ratio);
const json = {
prompt: prompt,
negative_prompt: neg_prompt === undefined ? "nsfw" : neg_prompt + ", nsfw",
styles: [style === undefined ? "None" : style],
seed: seed === undefined ? -1 : seed,
sampler_name: sampler === undefined ? "Euler a" : sampler,
steps: steps === undefined ? 80 : steps,
cfg_scale: cfg_scale === undefined ? 7 : cfg_scale,
width: width,
height: height
};
return fetch_async("http://127.0.0.1:7860/sdapi/v1/txt2img", {
method: 'post',
body: JSON.stringify(json),
headers: { 'Content-Type': 'application/json' }
}).then(res => { return res.json() });
}
export async function Img2Img({prompt, url, neg_prompt, denoising_strength, style, aspect_ratio, seed, sampler, steps, cfg_scale})
{
const {width, height} = await GetWidthHeight(aspect_ratio);
const image = Buffer.from(await (await fetch(url)).arrayBuffer()).toString("base64");
const json = {
init_images: [image],
prompt: prompt,
denoising_strength: denoising_strength === undefined ? 0.65 : denoising_strength,
negative_prompt: neg_prompt === undefined ? "nsfw" : neg_prompt + ", nsfw",
styles: [style === undefined ? "None" : style],
seed: seed === undefined ? -1 : seed,
sampler_name: sampler === undefined ? "Euler a" : sampler,
steps: steps === undefined ? 80 : steps,
cfg_scale: cfg_scale === undefined ? 7 : cfg_scale,
width: width,
height: height
};
return fetch_async("http://127.0.0.1:7860/sdapi/v1/img2img", {
method: 'post',
body: JSON.stringify(json),
headers: { 'Content-Type': 'application/json' }
}).then(res => { return res.json() });
}
export async function Upscale(url)
{
const image = Buffer.from(await (await fetch(url)).arrayBuffer()).toString("base64");
const json = {
upscaling_resize: 2,
upscaler_1: "SwinIR_4x", // for now this will just always be SwinIR, there could be an option to change in the future
image: image
}
return fetch_async("http://127.0.0.1:7860/sdapi/v1/extra-single-image", {
method: 'post',
body: JSON.stringify(json),
headers: { 'Content-Type': 'application/json' }
}).then(res => { return res.json() });
}
export async function SendGenInterrupt()
{
return fetch_async("http://127.0.0.1:7860/sdapi/v1/interrupt", {
method: 'post',
});
}
//this function will only work if the 768 in the model name is surrounded by dashes or spaces
async function GetOutputSize()
{
const regex = /(?<!\()\b768\b(?![\w\s]*[\)])/g; // i dunno how this fuckin works lol
let model = await GetCurrentModel();
return regex.test(model) ? 768 : 512;
}
async function GetWidthHeight(aspect_ratio)
{
const size = await GetOutputSize();
var width = size;
var height = size;
if (aspect_ratio === "7:4") {
width *= 1.75;
}
return {width, height}
}