2023-05-18 13:59:03 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-27 07:26:33 +00:00
|
|
|
"encoding/json"
|
2023-05-18 13:59:03 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2023-05-20 07:06:30 +00:00
|
|
|
"net/url"
|
2023-05-27 07:26:33 +00:00
|
|
|
"os"
|
2023-05-20 07:06:30 +00:00
|
|
|
"strings"
|
2023-05-18 13:59:03 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/go-skynet/LocalAI/pkg/gallery"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type galleryOp struct {
|
|
|
|
req ApplyGalleryModelRequest
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
type galleryOpStatus struct {
|
|
|
|
Error error `json:"error"`
|
|
|
|
Processed bool `json:"processed"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type galleryApplier struct {
|
|
|
|
modelPath string
|
|
|
|
sync.Mutex
|
|
|
|
C chan galleryOp
|
|
|
|
statuses map[string]*galleryOpStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGalleryApplier(modelPath string) *galleryApplier {
|
|
|
|
return &galleryApplier{
|
|
|
|
modelPath: modelPath,
|
|
|
|
C: make(chan galleryOp),
|
|
|
|
statuses: make(map[string]*galleryOpStatus),
|
|
|
|
}
|
|
|
|
}
|
2023-05-27 07:26:33 +00:00
|
|
|
|
|
|
|
func applyGallery(modelPath string, req ApplyGalleryModelRequest, cm *ConfigMerger) error {
|
|
|
|
url, err := req.DecodeURL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a GET request to the URL
|
|
|
|
response, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
// Read the response body
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal YAML data into a Config struct
|
|
|
|
var config gallery.Config
|
|
|
|
err = yaml.Unmarshal(body, &config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Files = append(config.Files, req.AdditionalFiles...)
|
|
|
|
|
|
|
|
if err := gallery.Apply(modelPath, req.Name, &config, req.Overrides); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload models
|
|
|
|
return cm.LoadConfigs(modelPath)
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:59:03 +00:00
|
|
|
func (g *galleryApplier) updatestatus(s string, op *galleryOpStatus) {
|
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
g.statuses[s] = op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *galleryApplier) getstatus(s string) *galleryOpStatus {
|
|
|
|
g.Lock()
|
|
|
|
defer g.Unlock()
|
|
|
|
|
|
|
|
return g.statuses[s]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *galleryApplier) start(c context.Context, cm *ConfigMerger) {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.Done():
|
|
|
|
return
|
|
|
|
case op := <-g.C:
|
|
|
|
g.updatestatus(op.id, &galleryOpStatus{Message: "processing"})
|
|
|
|
|
|
|
|
updateError := func(e error) {
|
|
|
|
g.updatestatus(op.id, &galleryOpStatus{Error: e, Processed: true})
|
|
|
|
}
|
2023-05-20 07:06:30 +00:00
|
|
|
|
2023-05-27 07:26:33 +00:00
|
|
|
if err := applyGallery(g.modelPath, op.req, cm); err != nil {
|
2023-05-18 13:59:03 +00:00
|
|
|
updateError(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-27 07:26:33 +00:00
|
|
|
g.updatestatus(op.id, &galleryOpStatus{Processed: true, Message: "completed"})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2023-05-18 13:59:03 +00:00
|
|
|
|
2023-05-27 07:26:33 +00:00
|
|
|
func ApplyGalleryFromFile(modelPath, s string, cm *ConfigMerger) error {
|
|
|
|
dat, err := os.ReadFile(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var requests []ApplyGalleryModelRequest
|
|
|
|
err = json.Unmarshal(dat, &requests)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-19 06:31:11 +00:00
|
|
|
|
2023-05-27 07:26:33 +00:00
|
|
|
for _, r := range requests {
|
|
|
|
if err := applyGallery(modelPath, r, cm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-05-18 13:59:03 +00:00
|
|
|
|
2023-05-27 07:26:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func ApplyGalleryFromString(modelPath, s string, cm *ConfigMerger) error {
|
|
|
|
var requests []ApplyGalleryModelRequest
|
|
|
|
err := json.Unmarshal([]byte(s), &requests)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-18 13:59:03 +00:00
|
|
|
|
2023-05-27 07:26:33 +00:00
|
|
|
for _, r := range requests {
|
|
|
|
if err := applyGallery(modelPath, r, cm); err != nil {
|
|
|
|
return err
|
2023-05-18 13:59:03 +00:00
|
|
|
}
|
2023-05-27 07:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-05-18 13:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// endpoints
|
|
|
|
|
|
|
|
type ApplyGalleryModelRequest struct {
|
2023-05-20 15:03:53 +00:00
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Overrides map[string]interface{} `json:"overrides"`
|
|
|
|
AdditionalFiles []gallery.File `json:"files"`
|
2023-05-18 13:59:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 07:06:30 +00:00
|
|
|
const (
|
|
|
|
githubURI = "github:"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (request ApplyGalleryModelRequest) DecodeURL() (string, error) {
|
|
|
|
input := request.URL
|
|
|
|
var rawURL string
|
|
|
|
|
|
|
|
if strings.HasPrefix(input, githubURI) {
|
|
|
|
parts := strings.Split(input, ":")
|
|
|
|
repoParts := strings.Split(parts[1], "@")
|
|
|
|
branch := "main"
|
|
|
|
|
|
|
|
if len(repoParts) > 1 {
|
|
|
|
branch = repoParts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
repoPath := strings.Split(repoParts[0], "/")
|
|
|
|
org := repoPath[0]
|
|
|
|
project := repoPath[1]
|
|
|
|
projectPath := strings.Join(repoPath[2:], "/")
|
|
|
|
|
|
|
|
rawURL = fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s/%s", org, project, branch, projectPath)
|
|
|
|
} else if strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://") {
|
|
|
|
// Handle regular URLs
|
|
|
|
u, err := url.Parse(input)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("invalid URL: %w", err)
|
|
|
|
}
|
|
|
|
rawURL = u.String()
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("invalid URL format")
|
|
|
|
}
|
|
|
|
|
|
|
|
return rawURL, nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:59:03 +00:00
|
|
|
func getOpStatus(g *galleryApplier) func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
|
2023-05-20 15:03:53 +00:00
|
|
|
status := g.getstatus(c.Params("uuid"))
|
2023-05-18 13:59:03 +00:00
|
|
|
if status == nil {
|
|
|
|
return fmt.Errorf("could not find any status for ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyModelGallery(modelPath string, cm *ConfigMerger, g chan galleryOp) func(c *fiber.Ctx) error {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
input := new(ApplyGalleryModelRequest)
|
|
|
|
// Get input data from the request body
|
|
|
|
if err := c.BodyParser(input); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuid.NewUUID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g <- galleryOp{
|
|
|
|
req: *input,
|
|
|
|
id: uuid.String(),
|
|
|
|
}
|
|
|
|
return c.JSON(struct {
|
2023-05-20 15:03:53 +00:00
|
|
|
ID string `json:"uuid"`
|
2023-05-18 13:59:03 +00:00
|
|
|
StatusURL string `json:"status"`
|
|
|
|
}{ID: uuid.String(), StatusURL: c.BaseURL() + "/models/jobs/" + uuid.String()})
|
|
|
|
}
|
|
|
|
}
|