/
sdWebuiApi.go
212 lines (195 loc) · 6.36 KB
/
sdWebuiApi.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package setuai
import (
"crypto/rand"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/levigross/grequests"
"strconv"
)
type Sdtxt2imgReq struct {
EnableHr bool `json:"enable_hr"`
//DenoisingStrength int `json:"denoising_strength"`
//FirstphaseWidth int `json:"firstphase_width"`
//FirstphaseHeight int `json:"firstphase_height"`
Prompt string `json:"prompt"`
Styles []string `json:"styles"`
Seed int `json:"seed"`
Subseed int `json:"subseed"`
SubseedStrength int `json:"subseed_strength"`
SeedResizeFromH int `json:"seed_resize_from_h"`
SeedResizeFromW int `json:"seed_resize_from_w"`
BatchSize int `json:"batch_size"`
NIter int `json:"n_iter"`
Steps int `json:"steps"`
CfgScale int `json:"cfg_scale"`
Width int `json:"width"`
Height int `json:"height"`
//RestoreFaces bool `json:"restore_faces"`
//Tiling bool `json:"tiling"`
NegativePrompt string `json:"negative_prompt"`
//Eta int `json:"eta"`
//SChurn int `json:"s_churn"`
//STmax int `json:"s_tmax"`
//STmin int `json:"s_tmin"`
//SNoise int `json:"s_noise"`
//OverrideSettings struct {
//} `json:"override_settings"`
SamplerIndex string `json:"sampler_index"`
}
type Sdtxt2imgRsp struct {
Images []string `json:"images"`
Parameters struct {
EnableHr bool `json:"enable_hr"`
DenoisingStrength float32 `json:"denoising_strength"`
FirstphaseWidth float32 `json:"firstphase_width"`
FirstphaseHeight float32 `json:"firstphase_height"`
Prompt string `json:"prompt"`
Styles []string `json:"styles"`
Seed int `json:"seed"`
Subseed int `json:"subseed"`
SubseedStrength float32 `json:"subseed_strength"`
SeedResizeFromH float32 `json:"seed_resize_from_h"`
SeedResizeFromW float32 `json:"seed_resize_from_w"`
BatchSize int `json:"batch_size"`
NIter int `json:"n_iter"`
Steps int `json:"steps"`
CfgScale float32 `json:"cfg_scale"`
Width int `json:"width"`
Height int `json:"height"`
RestoreFaces bool `json:"restore_faces"`
Tiling bool `json:"tiling"`
NegativePrompt string `json:"negative_prompt"`
Eta float32 `json:"eta"`
SChurn float32 `json:"s_churn"`
STmax float32 `json:"s_tmax"`
STmin float32 `json:"s_tmin"`
SNoise float32 `json:"s_noise"`
OverrideSettings struct {
} `json:"override_settings"`
SamplerIndex string `json:"sampler_index"`
} `json:"parameters"`
Info string `json:"info"`
}
type Sdtxt2imgRspInfo struct {
Prompt string `json:"prompt"`
AllPrompts []string `json:"all_prompts"`
NegativePrompt string `json:"negative_prompt"`
Seed int `json:"seed"`
AllSeeds []int `json:"all_seeds"`
Subseed int64 `json:"subseed"`
AllSubseeds []int64 `json:"all_subseeds"`
SubseedStrength float64 `json:"subseed_strength"`
Width int `json:"width"`
Height int `json:"height"`
SamplerIndex int `json:"sampler_index"`
Sampler string `json:"sampler"`
CfgScale float64 `json:"cfg_scale"`
Steps int `json:"steps"`
BatchSize int `json:"batch_size"`
RestoreFaces bool `json:"restore_faces"`
FaceRestorationModel interface{} `json:"face_restoration_model"`
SdModelHash string `json:"sd_model_hash"`
SeedResizeFromW int `json:"seed_resize_from_w"`
SeedResizeFromH int `json:"seed_resize_from_h"`
DenoisingStrength float64 `json:"denoising_strength"`
ExtraGenerationParams struct {
} `json:"extra_generation_params"`
IndexOfFirstImage int `json:"index_of_first_image"`
Infotexts []string `json:"infotexts"`
Styles []string `json:"styles"`
JobTimestamp string `json:"job_timestamp"`
ClipSkip int `json:"clip_skip"`
}
func SdDecode(data []byte) ([]byte, string){
j := Sdtxt2imgRsp{}
err := json.Unmarshal(data, &j)
if err!= nil{
return nil,""
}
b64 := j.Images[0]
img,err := base64.StdEncoding.DecodeString(b64)
if err!= nil{
return nil,""
}
k := Sdtxt2imgRspInfo{}
err = json.Unmarshal([]byte(j.Info), &k)
if err!= nil{
return img,""
}
return img, fmt.Sprintf("seed:%d\r\n", k.Seed)
}
func SdRequest(url string, prompt, width, height, scale, sampler, steps, seed, uc *string)([]byte,string){
j := Sdtxt2imgReq{
false,
//0,
//0,
//0,
"",
[]string{},
-1,
-1,
0,
0,
0,
1,
1,
20,
11,
512,
768,
//false,
//false,
"lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, lowres, bad anatomy, bad hands, text,error, missing fngers,extra digt ,fewer digits,cropped, wort quality ,low quality,normal quality, jpeg artifacts,signature,watermark, username, blurry, bad feet",
//0,
//0,
//0,
//0,
//1,
//nil,
"Euler a",
}
if prompt != nil{
j.Prompt = *prompt
}
if width!=nil{
j.Width, _ = strconv.Atoi(*width)
}
if height!=nil{
j.Height, _ = strconv.Atoi(*height)
}
if scale!=nil{
j.CfgScale, _ = strconv.Atoi(*scale)
}
if sampler!=nil{
j.SamplerIndex = *sampler
}
if steps!=nil{
j.Steps, _ = strconv.Atoi(*steps)
}
if seed!=nil{
j.Seed, _ = strconv.Atoi(*seed)
} else {
var n uint32
binary.Read(rand.Reader, binary.LittleEndian, &n)
j.Seed = int(n)
print("seed = ", j.Seed, "\r\n")
}
if uc!=nil{
j.NegativePrompt = *uc
}
jsonBytes, err := json.Marshal(j)
if err != nil{
return nil, error.Error(err)
}
opt := grequests.RequestOptions{
Headers: map[string]string{
"content-type":"application/json",
},
JSON: jsonBytes,
}
r,err := grequests.Post(url+"/sdapi/v1/txt2img", &opt)
img,txt := SdDecode(r.Bytes())
return img, txt
}