2023-07-14 23:19:43 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
config "github.com/go-skynet/LocalAI/api/config"
|
|
|
|
"github.com/go-skynet/LocalAI/api/options"
|
2023-07-14 23:19:43 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
2023-07-14 23:19:43 +00:00
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ImageGeneration(height, width, mode, step, seed int, positive_prompt, negative_prompt, dst string, loader *model.ModelLoader, c config.Config, o *options.Option) (func() error, error) {
|
|
|
|
|
2023-07-20 20:10:12 +00:00
|
|
|
opts := []model.Option{
|
2023-07-14 23:19:43 +00:00
|
|
|
model.WithBackendString(c.Backend),
|
|
|
|
model.WithAssetDir(o.AssetsDestination),
|
|
|
|
model.WithThreads(uint32(c.Threads)),
|
2023-07-14 23:19:43 +00:00
|
|
|
model.WithContext(o.Context),
|
2023-08-09 06:38:51 +00:00
|
|
|
model.WithModel(c.Model),
|
|
|
|
model.WithLoadGRPCLoadModelOpts(&proto.ModelOptions{
|
|
|
|
CUDA: c.Diffusers.CUDA,
|
|
|
|
SchedulerType: c.Diffusers.SchedulerType,
|
|
|
|
PipelineType: c.Diffusers.PipelineType,
|
|
|
|
}),
|
2023-07-20 20:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range o.ExternalGRPCBackends {
|
|
|
|
opts = append(opts, model.WithExternalBackend(k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
inferenceModel, err := loader.BackendLoader(
|
|
|
|
opts...,
|
2023-07-14 23:19:43 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
fn := func() error {
|
|
|
|
_, err := inferenceModel.GenerateImage(
|
|
|
|
o.Context,
|
|
|
|
&proto.GenerateImageRequest{
|
2023-08-14 21:12:00 +00:00
|
|
|
Height: int32(height),
|
|
|
|
Width: int32(width),
|
|
|
|
Mode: int32(mode),
|
|
|
|
Step: int32(step),
|
|
|
|
Seed: int32(seed),
|
|
|
|
PositivePrompt: positive_prompt,
|
|
|
|
NegativePrompt: negative_prompt,
|
|
|
|
Dst: dst,
|
|
|
|
EnableParameters: c.Diffusers.EnableParameters,
|
2023-07-14 23:19:43 +00:00
|
|
|
})
|
|
|
|
return err
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return func() error {
|
|
|
|
// This is still needed, see: https://github.com/ggerganov/llama.cpp/discussions/784
|
|
|
|
mutexMap.Lock()
|
|
|
|
l, ok := mutexes[c.Backend]
|
|
|
|
if !ok {
|
|
|
|
m := &sync.Mutex{}
|
|
|
|
mutexes[c.Backend] = m
|
|
|
|
l = m
|
|
|
|
}
|
|
|
|
mutexMap.Unlock()
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
return fn()
|
|
|
|
}, nil
|
|
|
|
}
|