Preload prepare models if they are not local but url or huggingface repositories
(modelPath string)
| 334 | |
| 335 | // Preload prepare models if they are not local but url or huggingface repositories |
| 336 | func (bcl *ModelConfigLoader) Preload(modelPath string) error { |
| 337 | bcl.Lock() |
| 338 | defer bcl.Unlock() |
| 339 | |
| 340 | status := func(fileName, current, total string, percent float64) { |
| 341 | utils.DisplayDownloadFunction(fileName, current, total, percent) |
| 342 | } |
| 343 | |
| 344 | xlog.Info("Preloading models", "path", modelPath) |
| 345 | |
| 346 | renderMode := "dark" |
| 347 | if os.Getenv("COLOR") != "" { |
| 348 | renderMode = os.Getenv("COLOR") |
| 349 | } |
| 350 | |
| 351 | glamText := func(t string) { |
| 352 | out, err := glamour.Render(t, renderMode) |
| 353 | if err == nil && os.Getenv("NO_COLOR") == "" { |
| 354 | fmt.Println(out) |
| 355 | } else { |
| 356 | fmt.Println(t) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | for i, config := range bcl.configs { |
| 361 | |
| 362 | // Download files and verify their SHA |
| 363 | for i, file := range config.DownloadFiles { |
| 364 | xlog.Debug("Checking file exists and matches SHA", "filename", file.Filename) |
| 365 | |
| 366 | if err := utils.VerifyPath(file.Filename, modelPath); err != nil { |
| 367 | return err |
| 368 | } |
| 369 | // Create file path |
| 370 | filePath := filepath.Join(modelPath, file.Filename) |
| 371 | |
| 372 | if err := file.URI.DownloadFile(filePath, file.SHA256, i, len(config.DownloadFiles), status); err != nil { |
| 373 | return err |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // If the model is an URL, expand it, and download the file |
| 378 | if config.IsModelURL() { |
| 379 | modelFileName := config.ModelFileName() |
| 380 | uri := downloader.URI(config.Model) |
| 381 | if uri.ResolveURL() != config.Model { |
| 382 | // check if file exists |
| 383 | if _, err := os.Stat(filepath.Join(modelPath, modelFileName)); errors.Is(err, os.ErrNotExist) { |
| 384 | err := uri.DownloadFile(filepath.Join(modelPath, modelFileName), "", 0, 0, status) |
| 385 | if err != nil { |
| 386 | return err |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | cc := bcl.configs[i] |
| 391 | c := &cc |
| 392 | c.PredictionOptions.Model = modelFileName |
| 393 | bcl.configs[i] = *c |
no test coverage detected