Skip to content

Commit 99c352d

Browse files
committed
feat: Allow to specify url in model config files
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent f869bd8 commit 99c352d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

api/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func Startup(opts ...options.AppOption) (*options.Option, *config.ConfigLoader,
4747
}
4848
}
4949

50+
if err := cl.Preload(options.Loader.ModelPath); err != nil {
51+
log.Error().Msgf("error downloading models: %s", err.Error())
52+
}
53+
5054
if options.Debug {
5155
for _, v := range cl.ListConfigs() {
5256
cfg, _ := cl.GetConfig(v)

api/config/config.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"sync"
1010

11+
"github.com/go-skynet/LocalAI/pkg/utils"
1112
"gopkg.in/yaml.v3"
1213
)
1314

@@ -264,6 +265,32 @@ func (cm *ConfigLoader) ListConfigs() []string {
264265
return res
265266
}
266267

268+
func (cm *ConfigLoader) Preload(modelPath string) error {
269+
cm.Lock()
270+
defer cm.Unlock()
271+
272+
for i, config := range cm.configs {
273+
if strings.HasPrefix(config.PredictionOptions.Model, "http://") || strings.HasPrefix(config.PredictionOptions.Model, "https://") {
274+
// md5 of model name
275+
md5Name := utils.MD5(config.PredictionOptions.Model)
276+
277+
// check if file exists
278+
if _, err := os.Stat(filepath.Join(modelPath, md5Name)); err == os.ErrNotExist {
279+
err := utils.DownloadFile(config.PredictionOptions.Model, filepath.Join(modelPath, md5Name))
280+
if err != nil {
281+
return err
282+
}
283+
}
284+
285+
cc := cm.configs[i]
286+
c := &cc
287+
c.PredictionOptions.Model = md5Name
288+
cm.configs[i] = *c
289+
}
290+
}
291+
return nil
292+
}
293+
267294
func (cm *ConfigLoader) LoadConfigs(path string) error {
268295
cm.Lock()
269296
defer cm.Unlock()

pkg/utils/uri.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"crypto/md5"
45
"fmt"
56
"io"
67
"net/http"
@@ -64,3 +65,29 @@ func GetURI(url string, f func(url string, i []byte) error) error {
6465
// Unmarshal YAML data into a struct
6566
return f(url, body)
6667
}
68+
69+
func DownloadFile(url string, filepath string) error {
70+
// Create the file
71+
out, err := os.Create(filepath)
72+
if err != nil {
73+
return err
74+
}
75+
defer out.Close()
76+
77+
// Get the data
78+
resp, err := http.Get(url)
79+
if err != nil {
80+
return err
81+
}
82+
defer resp.Body.Close()
83+
84+
// Write the body to file
85+
_, err = io.Copy(out, resp.Body)
86+
87+
return err
88+
}
89+
90+
// MD5 of a string
91+
func MD5(s string) string {
92+
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
93+
}

0 commit comments

Comments
 (0)