File size: 1,980 Bytes
7def60a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package localai

import (
	"github.com/gofiber/fiber/v2"
	"github.com/mudler/LocalAI/core/config"
	"github.com/mudler/LocalAI/core/gallery"
	"github.com/mudler/LocalAI/core/p2p"
	"github.com/mudler/LocalAI/core/services"
	"github.com/mudler/LocalAI/internal"
	"github.com/mudler/LocalAI/pkg/model"
)

func WelcomeEndpoint(appConfig *config.ApplicationConfig,
	cl *config.BackendConfigLoader, ml *model.ModelLoader, modelStatus func() (map[string]string, map[string]string)) func(*fiber.Ctx) error {
	return func(c *fiber.Ctx) error {
		models, _ := services.ListModels(cl, ml, "", true)
		backendConfigs := cl.GetAllBackendConfigs()

		galleryConfigs := map[string]*gallery.Config{}
		modelsWithBackendConfig := map[string]interface{}{}

		for _, m := range backendConfigs {
			modelsWithBackendConfig[m.Name] = nil

			cfg, err := gallery.GetLocalModelConfiguration(ml.ModelPath, m.Name)
			if err != nil {
				continue
			}
			galleryConfigs[m.Name] = cfg
		}

		// Get model statuses to display in the UI the operation in progress
		processingModels, taskTypes := modelStatus()

		modelsWithoutConfig := []string{}

		for _, m := range models {
			if _, ok := modelsWithBackendConfig[m]; !ok {
				modelsWithoutConfig = append(modelsWithoutConfig, m)
			}
		}

		summary := fiber.Map{
			"Title":             "LocalAI API - " + internal.PrintableVersion(),
			"Version":           internal.PrintableVersion(),
			"Models":            modelsWithoutConfig,
			"ModelsConfig":      backendConfigs,
			"GalleryConfig":     galleryConfigs,
			"IsP2PEnabled":      p2p.IsP2PEnabled(),
			"ApplicationConfig": appConfig,
			"ProcessingModels":  processingModels,
			"TaskTypes":         taskTypes,
		}

		if string(c.Context().Request.Header.ContentType()) == "application/json" || len(c.Accepts("html")) == 0 {
			// The client expects a JSON response
			return c.Status(fiber.StatusOK).JSON(summary)
		} else {
			// Render index
			return c.Render("views/index", summary)
		}
	}
}