Merge pull request #566 from jmorganca/mxyng/api-check-model-exists

Use API to check if model exists and pull if necessary
This commit is contained in:
Michael Yang 2023-09-21 10:35:14 -07:00 committed by GitHub
commit 8c83701e9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 35 deletions

View file

@ -11,7 +11,6 @@ import (
"io" "io"
"log" "log"
"net" "net"
"net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -108,35 +107,28 @@ func CreateHandler(cmd *cobra.Command, args []string) error {
} }
func RunHandler(cmd *cobra.Command, args []string) error { func RunHandler(cmd *cobra.Command, args []string) error {
insecure, err := cmd.Flags().GetBool("insecure") client, err := api.FromEnv()
if err != nil { if err != nil {
return err return err
} }
mp := server.ParseModelPath(args[0]) models, err := client.List(context.Background())
if mp.ProtocolScheme == "http" && !insecure {
return fmt.Errorf("insecure protocol http")
}
fp, err := mp.GetManifestPath(false)
if err != nil { if err != nil {
return err return err
} }
_, err = os.Stat(fp) modelName, modelTag, ok := strings.Cut(args[0], ":")
switch { if !ok {
case errors.Is(err, os.ErrNotExist): modelTag = "latest"
if err := pull(args[0], insecure); err != nil {
var apiStatusError api.StatusError
if !errors.As(err, &apiStatusError) {
return err
} }
if apiStatusError.StatusCode != http.StatusBadGateway { for _, model := range models.Models {
return err if model.Name == strings.Join([]string{modelName, modelTag}, ":") {
return RunGenerate(cmd, args)
} }
} }
case err != nil:
if err := PullHandler(cmd, args); err != nil {
return err return err
} }

View file

@ -499,23 +499,25 @@ func CopyModelHandler(c *gin.Context) {
} }
} }
func Serve(ln net.Listener, origins []string) error { var defaultAllowOrigins = []string{
"localhost",
"127.0.0.1",
"0.0.0.0",
}
func Serve(ln net.Listener, allowOrigins []string) error {
config := cors.DefaultConfig() config := cors.DefaultConfig()
config.AllowWildcard = true config.AllowWildcard = true
config.AllowOrigins = append(origins, []string{
"http://localhost", config.AllowOrigins = allowOrigins
"http://localhost:*", for _, allowOrigin := range defaultAllowOrigins {
"https://localhost", config.AllowOrigins = append(config.AllowOrigins,
"https://localhost:*", fmt.Sprintf("http://%s", allowOrigin),
"http://127.0.0.1", fmt.Sprintf("https://%s", allowOrigin),
"http://127.0.0.1:*", fmt.Sprintf("http://%s:*", allowOrigin),
"https://127.0.0.1", fmt.Sprintf("https://%s:*", allowOrigin),
"https://127.0.0.1:*", )
"http://0.0.0.0", }
"http://0.0.0.0:*",
"https://0.0.0.0",
"https://0.0.0.0:*",
}...)
r := gin.Default() r := gin.Default()
r.Use(cors.New(config)) r.Use(cors.New(config))