-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
245 lines (207 loc) · 5.15 KB
/
update.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// model.go
//
// Copyright (c) 2023 Junpei Kawamoto
//
// This software is released under the MIT License.
//
// http://opensource.org/licenses/mit-license.php
package main
import (
"context"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"sort"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/cheggaaa/pb/v3"
"github.com/fatih/color"
"github.com/jkawamoto/go-civitai/models"
"github.com/zeebo/blake3"
)
const pbTemplate = `{{with string . "prefix"}}{{.}} {{end}}{{bar . }} {{percent . }}{{with string . "suffix"}} {{.}}{{end}}`
// fileHash returns the BLAKE3 hash of the given named file.
func fileHash(name string) (_ string, err error) {
f, err := os.Open(name)
if err != nil {
return "", err
}
defer func() {
err = errors.Join(err, f.Close())
}()
info, err := f.Stat()
if err != nil {
return "", err
}
bar := pb.New(int(info.Size()))
bar.SetTemplate(pbTemplate)
bar.Set(pb.SIBytesPrefix, true)
bar.Set("prefix", filepath.Base(name)+" ")
bar.Start()
defer bar.Finish()
hash := blake3.New()
_, err = io.Copy(hash, bar.NewProxyReader(f))
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
// Update packs information about new versions for a model.
type Update struct {
ModelName string
CurrentVersion string
Candidates map[string]*models.ModelVersion
}
// findUpdate retrieves the model information of the given model file.
func findUpdate(ctx context.Context, cli Client, name string) (*Update, error) {
hash, err := fileHash(name)
if err != nil {
return nil, err
}
cur, err := cli.GetModelVersion(ctx, hash)
if err != nil {
return nil, err
}
m, err := cli.GetModel(ctx, cur.ModelID)
if err != nil {
return nil, err
}
res := &Update{
ModelName: m.Name,
CurrentVersion: cur.Name,
Candidates: make(map[string]*models.ModelVersion),
}
for _, v := range m.ModelVersions {
if time.Time(v.CreatedAt).After(time.Time(cur.CreatedAt)) {
res.Candidates[v.Name] = v
}
}
return res, nil
}
// modelVersionList is an alias of []*models.ModelVersion that implements sort.Interface.
type modelVersionList []*models.ModelVersion
func (m modelVersionList) Len() int {
return len(m)
}
func (m modelVersionList) Less(i, j int) bool {
return time.Time(m[i].CreatedAt).Before(time.Time(m[j].CreatedAt))
}
func (m modelVersionList) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func findUpdatesFromDir(ctx context.Context, cli Client, dir string) ([]*Update, error) {
ms := make(map[int64]modelVersionList)
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
if !isModelFile(path) {
return nil
}
hash, err := fileHash(path)
if err != nil {
return err
}
v, err := cli.GetModelVersion(ctx, hash)
if err != nil {
if coder, ok := err.(interface {
Code() int
}); ok {
if coder.Code() == http.StatusNotFound {
fmt.Println(color.YellowString("Model information is not found"))
return nil
}
}
return err
}
ms[v.ModelID] = append(ms[v.ModelID], v)
return nil
})
if err != nil {
return nil, err
}
var res []*Update
for modelID, versions := range ms {
model, err := cli.GetModel(ctx, modelID)
if err != nil {
return nil, err
}
sort.Sort(sort.Reverse(versions))
cur := versions[0]
sort.Sort(sort.Reverse(modelVersionList(model.ModelVersions)))
candidates := make(map[string]*models.ModelVersion)
for _, v := range model.ModelVersions {
if time.Time(v.CreatedAt).After(time.Time(cur.CreatedAt)) {
candidates[v.Name] = v
}
}
if len(candidates) != 0 {
res = append(res, &Update{
ModelName: model.Name,
CurrentVersion: cur.Name,
Candidates: candidates,
})
}
}
return res, nil
}
func (u Update) run(ctx context.Context, cli Client, dest string) error {
switch len(u.Candidates) {
case 0:
fmt.Println(u.ModelName, "has no updates")
return nil
case 1:
fmt.Println(color.GreenString("%v has a newer version", u.ModelName))
var ver *models.ModelVersion
for _, v := range u.Candidates {
ver = v
}
var confirm bool
err := survey.AskOne(&survey.Confirm{
Message: fmt.Sprintf("Do you want to update %v \u279c %v", u.CurrentVersion, ver.Name),
}, &confirm)
if err != nil {
return err
}
if !confirm {
fmt.Println(color.YellowString("Skipped downloading the newer model"))
return nil
}
if err = cli.Download(ctx, ver, dest); err != nil {
return err
}
default:
fmt.Println(color.GreenString("%v has multiple newer versions", u.ModelName))
var opts []string
for n := range u.Candidates {
opts = append(opts, n)
}
var selected []string
err := survey.AskOne(&survey.MultiSelect{
Message: fmt.Sprintf("Which versions do you want to download (current: %v)", u.CurrentVersion),
Options: opts,
}, &selected)
if err != nil {
return err
}
if len(selected) == 0 {
fmt.Println(color.YellowString("Skipped downloading any models"))
return nil
}
for _, n := range selected {
ver := u.Candidates[n]
if err = cli.Download(ctx, ver, dest); err != nil {
return err
}
}
}
return nil
}