mirror of
https://github.com/mudler/LocalAI.git
synced 2026-07-03 12:57:02 -04:00
Importing a model from a local directory (e.g. a HuggingFace checkout or an LM Studio store) via a file:// URI produced a config whose model field kept the scheme verbatim, e.g. model: file:///Users/u/.../Qwen3-4bit. The mlx and vllm backends treat that field as a HuggingFace repo id or local path and reject the file:// form with "Repo id must be in the form 'repo_name' or 'namespace/repo_name'", so the model imported fine but failed to load (issue #7461). Add a shared LocalModelPath helper that reduces a file:// URI to the bare filesystem path it points at and leaves HuggingFace/HTTP URIs untouched, and route the mlx, vllm, transformers and diffusers importers (all of which pass details.URI straight into the model field for from_pretrained-style loading) through it. Cover the helper directly plus end-to-end file:// import specs for the mlx and vllm importers. Assisted-by: Claude:claude-opus-4-8 [Claude Code] Signed-off-by: Adira Denis Muhando <dennisadira@gmail.com>
126 lines
2.9 KiB
Go
126 lines
2.9 KiB
Go
package importers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
"github.com/mudler/LocalAI/core/gallery"
|
|
"github.com/mudler/LocalAI/core/schema"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
var _ Importer = &DiffuserImporter{}
|
|
|
|
type DiffuserImporter struct{}
|
|
|
|
func (i *DiffuserImporter) Name() string { return "diffusers" }
|
|
func (i *DiffuserImporter) Modality() string { return "image" }
|
|
func (i *DiffuserImporter) AutoDetects() bool { return true }
|
|
|
|
func (i *DiffuserImporter) Match(details Details) bool {
|
|
preferences, err := details.Preferences.MarshalJSON()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
preferencesMap := make(map[string]any)
|
|
err = json.Unmarshal(preferences, &preferencesMap)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
b, ok := preferencesMap["backend"].(string)
|
|
if ok && b == "diffusers" {
|
|
return true
|
|
}
|
|
|
|
if details.HuggingFace != nil {
|
|
for _, file := range details.HuggingFace.Files {
|
|
if strings.Contains(file.Path, "model_index.json") ||
|
|
strings.Contains(file.Path, "scheduler/scheduler_config.json") {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (i *DiffuserImporter) Import(details Details) (gallery.ModelConfig, error) {
|
|
preferences, err := details.Preferences.MarshalJSON()
|
|
if err != nil {
|
|
return gallery.ModelConfig{}, err
|
|
}
|
|
preferencesMap := make(map[string]any)
|
|
err = json.Unmarshal(preferences, &preferencesMap)
|
|
if err != nil {
|
|
return gallery.ModelConfig{}, err
|
|
}
|
|
|
|
name, ok := preferencesMap["name"].(string)
|
|
if !ok {
|
|
name = filepath.Base(details.URI)
|
|
}
|
|
|
|
description, ok := preferencesMap["description"].(string)
|
|
if !ok {
|
|
description = "Imported from " + details.URI
|
|
}
|
|
|
|
backend := "diffusers"
|
|
b, ok := preferencesMap["backend"].(string)
|
|
if ok {
|
|
backend = b
|
|
}
|
|
|
|
pipelineType, ok := preferencesMap["pipeline_type"].(string)
|
|
if !ok {
|
|
pipelineType = "StableDiffusionPipeline"
|
|
}
|
|
|
|
schedulerType, ok := preferencesMap["scheduler_type"].(string)
|
|
if !ok {
|
|
schedulerType = ""
|
|
}
|
|
|
|
enableParameters, ok := preferencesMap["enable_parameters"].(string)
|
|
if !ok {
|
|
enableParameters = "negative_prompt,num_inference_steps"
|
|
}
|
|
|
|
cuda := false
|
|
if cudaVal, ok := preferencesMap["cuda"].(bool); ok {
|
|
cuda = cudaVal
|
|
}
|
|
|
|
modelConfig := config.ModelConfig{
|
|
Name: name,
|
|
Description: description,
|
|
KnownUsecaseStrings: []string{config.UsecaseImage},
|
|
Backend: backend,
|
|
PredictionOptions: schema.PredictionOptions{
|
|
BasicModelRequest: schema.BasicModelRequest{
|
|
Model: LocalModelPath(details.URI),
|
|
},
|
|
},
|
|
Diffusers: config.Diffusers{
|
|
PipelineType: pipelineType,
|
|
SchedulerType: schedulerType,
|
|
EnableParameters: enableParameters,
|
|
CUDA: cuda,
|
|
},
|
|
}
|
|
|
|
data, err := yaml.Marshal(modelConfig)
|
|
if err != nil {
|
|
return gallery.ModelConfig{}, err
|
|
}
|
|
|
|
return gallery.ModelConfig{
|
|
Name: name,
|
|
Description: description,
|
|
ConfigFile: string(data),
|
|
}, nil
|
|
}
|