-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (121 loc) · 3 KB
/
main.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
// main.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"
"errors"
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/fatih/color"
)
const (
SafetensorFormat = "safetensor"
PickleFormat = "pickle"
)
// ErrUnknownFormat returns if the given format is neither safetensor nor pickle.
var ErrUnknownFormat = fmt.Errorf("unknown format is specified")
var defaultTargets = []string{
filepath.Join("models", "hypernetworks"),
filepath.Join("models", "Lora"),
filepath.Join("models", "Stable-diffusion"),
filepath.Join("models", "VAE"),
"embeddings",
}
var modelFileExtensions = []string{".safetensors", ".ckpt", ".pt"}
// isModelFile returns true if the given name represents a model file.
func isModelFile(name string) bool {
ext := filepath.Ext(name)
for _, e := range modelFileExtensions {
if ext == e {
return true
}
}
return false
}
func run(ctx context.Context) error {
preferredFormat := SafetensorFormat
flag.Func(
"format",
fmt.Sprintf("prefered file format: %v or %v (default %v)", SafetensorFormat, PickleFormat, SafetensorFormat),
func(s string) error {
if s != SafetensorFormat && s != PickleFormat {
return ErrUnknownFormat
}
preferredFormat = s
return nil
},
)
flag.Parse()
targets := flag.Args()
if len(targets) == 0 {
wd, err := os.Getwd()
if err != nil {
return err
}
for _, t := range defaultTargets {
targets = append(targets, filepath.Join(wd, t))
}
}
cli := NewClient(preferredFormat)
for _, name := range targets {
stat, err := os.Stat(name)
if err != nil {
return err
}
if !stat.IsDir() {
update, err := findUpdate(ctx, cli, name)
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"))
continue
}
}
fmt.Println(color.RedString("Failed to find updates to %v: %v", filepath.Base(name), err))
continue
}
err = update.run(ctx, cli, filepath.Dir(name))
if err != nil {
if errors.Is(err, terminal.InterruptErr) {
return err
}
fmt.Println(color.RedString("Failed to update %v: %v", filepath.Base(name), err))
}
} else {
fmt.Println("Retrieving models in", name)
updates, err := findUpdatesFromDir(ctx, cli, name)
if err != nil {
fmt.Println(color.RedString("Failed to find updates to models in %v: %v", name, err))
continue
}
for _, u := range updates {
err = u.run(ctx, cli, name)
if err != nil {
if errors.Is(err, terminal.InterruptErr) {
return err
}
fmt.Println(color.RedString("Failed to update %v: %v", u.ModelName, err))
}
}
}
}
return nil
}
func main() {
err := run(context.Background())
if err != nil {
fmt.Println(color.RedString("Failed to check for updates: %v", err))
os.Exit(1)
}
}