-
Notifications
You must be signed in to change notification settings - Fork 2
/
params.go
182 lines (164 loc) · 4.71 KB
/
params.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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"golang.org/x/exp/slices"
)
type paramsType struct {
BotToken string
StableDiffusionPath string
StableDiffusionWebUIPath string
AllowedUserIDs []int64
AdminUserIDs []int64
AllowedGroupIDs []int64
SDStart bool
DelayedSDStart bool
DefaultModel string
DefaultSampler string
DefaultWidth int
DefaultHeight int
DefaultWidthSDXL int
DefaultHeightSDXL int
}
var params paramsType
func (p *paramsType) Init() error {
flag.StringVar(&p.BotToken, "bot-token", "", "telegram bot token")
flag.StringVar(&p.StableDiffusionPath, "sd-path", "", "path of the stable diffusion directory")
flag.StringVar(&p.StableDiffusionWebUIPath, "sd-webui-path", "", "path of the stable diffusion webui start script")
var allowedUserIDs string
flag.StringVar(&allowedUserIDs, "allowed-user-ids", "", "allowed telegram user ids")
var adminUserIDs string
flag.StringVar(&adminUserIDs, "admin-user-ids", "", "admin telegram user ids")
var allowedGroupIDs string
flag.StringVar(&allowedGroupIDs, "allowed-group-ids", "", "allowed telegram group ids")
flag.BoolVar(&p.SDStart, "sd-start", true, "start stable diffusion if needed")
flag.BoolVar(&p.DelayedSDStart, "delayed-sd-start", false, "start stable diffusion only when the first prompt arrives")
flag.StringVar(&p.DefaultModel, "default-model", "", "default model name")
flag.StringVar(&p.DefaultSampler, "default-sampler", "", "default sampler name")
flag.IntVar(&p.DefaultWidth, "default-width", 512, "default image width")
flag.IntVar(&p.DefaultHeight, "default-height", 512, "default image height")
flag.IntVar(&p.DefaultWidthSDXL, "default-width-sdxl", 1024, "default image width for SDXL models")
flag.IntVar(&p.DefaultHeightSDXL, "default-height-sdxl", 1024, "default image height for SDXL models")
flag.Parse()
if p.BotToken == "" {
p.BotToken = os.Getenv("BOT_TOKEN")
}
if p.BotToken == "" {
return fmt.Errorf("bot token not set")
}
if p.StableDiffusionPath == "" {
p.StableDiffusionPath = os.Getenv("STABLE_DIFFUSION_PATH")
}
if p.StableDiffusionPath == "" {
return fmt.Errorf("stable diffusion path not set")
}
if p.StableDiffusionWebUIPath == "" {
p.StableDiffusionWebUIPath = os.Getenv("STABLE_DIFFUSION_WEBUI_PATH")
}
if p.StableDiffusionWebUIPath == "" {
return fmt.Errorf("stable diffusion webui path not set")
}
if allowedUserIDs == "" {
allowedUserIDs = os.Getenv("ALLOWED_USERIDS")
}
sa := strings.Split(allowedUserIDs, ",")
for _, idStr := range sa {
if idStr == "" {
continue
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("allowed user ids contains invalid user ID: " + idStr)
}
p.AllowedUserIDs = append(p.AllowedUserIDs, id)
}
if adminUserIDs == "" {
adminUserIDs = os.Getenv("ADMIN_USERIDS")
}
sa = strings.Split(adminUserIDs, ",")
for _, idStr := range sa {
if idStr == "" {
continue
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("admin ids contains invalid user ID: " + idStr)
}
p.AdminUserIDs = append(p.AdminUserIDs, id)
if !slices.Contains(p.AllowedUserIDs, id) {
p.AllowedUserIDs = append(p.AllowedUserIDs, id)
}
}
if allowedGroupIDs == "" {
allowedGroupIDs = os.Getenv("ALLOWED_GROUPIDS")
}
sa = strings.Split(allowedGroupIDs, ",")
for _, idStr := range sa {
if idStr == "" {
continue
}
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
return fmt.Errorf("allowed group ids contains invalid group ID: " + idStr)
}
p.AllowedGroupIDs = append(p.AllowedGroupIDs, id)
}
s := os.Getenv("SD_START")
if s != "" {
if s == "0" {
p.SDStart = false
} else {
p.SDStart = true
}
}
s = os.Getenv("DELAYED_SD_START")
if s != "" {
if s == "0" {
p.DelayedSDStart = false
} else {
p.DelayedSDStart = true
}
}
if p.DefaultModel == "" {
p.DefaultModel = os.Getenv("DEFAULT_MODEL")
}
if p.DefaultSampler == "" {
p.DefaultSampler = os.Getenv("DEFAULT_SAMPLER")
}
s = os.Getenv("DEFAULT_WIDTH")
if s != "" {
val, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("invalid default width")
}
p.DefaultWidth = val
}
s = os.Getenv("DEFAULT_HEIGHT")
if s != "" {
val, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("invalid default height")
}
p.DefaultHeight = val
}
s = os.Getenv("DEFAULT_WIDTH_SDXL")
if s != "" {
val, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("invalid default width for SDXL")
}
p.DefaultWidthSDXL = val
}
s = os.Getenv("DEFAULT_HEIGHT_SDXL")
if s != "" {
val, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("invalid default height for SDXL")
}
p.DefaultHeightSDXL = val
}
return nil
}