2024-01-05 17:04:46 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/config"
|
2024-02-21 01:21:19 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/schema"
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-04-13 07:45:34 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/concurrency"
|
2024-01-05 17:04:46 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
2024-04-13 07:45:34 +00:00
|
|
|
"github.com/go-skynet/LocalAI/pkg/model"
|
2024-01-05 17:04:46 +00:00
|
|
|
)
|
|
|
|
|
2024-04-13 07:45:34 +00:00
|
|
|
type TranscriptionBackendService struct {
|
|
|
|
ml *model.ModelLoader
|
|
|
|
bcl *config.BackendConfigLoader
|
|
|
|
appConfig *config.ApplicationConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTranscriptionBackendService(ml *model.ModelLoader, bcl *config.BackendConfigLoader, appConfig *config.ApplicationConfig) *TranscriptionBackendService {
|
|
|
|
return &TranscriptionBackendService{
|
|
|
|
ml: ml,
|
|
|
|
bcl: bcl,
|
|
|
|
appConfig: appConfig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tbs *TranscriptionBackendService) Transcribe(request *schema.OpenAIRequest) <-chan concurrency.ErrorOr[*schema.TranscriptionResult] {
|
|
|
|
responseChannel := make(chan concurrency.ErrorOr[*schema.TranscriptionResult])
|
|
|
|
go func(request *schema.OpenAIRequest) {
|
|
|
|
bc, request, err := tbs.bcl.LoadBackendConfigForModelAndOpenAIRequest(request.Model, request, tbs.appConfig)
|
|
|
|
if err != nil {
|
|
|
|
responseChannel <- concurrency.ErrorOr[*schema.TranscriptionResult]{Error: fmt.Errorf("failed reading parameters from request:%w", err)}
|
|
|
|
close(responseChannel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tr, err := modelTranscription(request.File, request.Language, tbs.ml, bc, tbs.appConfig)
|
|
|
|
if err != nil {
|
|
|
|
responseChannel <- concurrency.ErrorOr[*schema.TranscriptionResult]{Error: err}
|
|
|
|
close(responseChannel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
responseChannel <- concurrency.ErrorOr[*schema.TranscriptionResult]{Value: tr}
|
|
|
|
close(responseChannel)
|
|
|
|
}(request)
|
|
|
|
return responseChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
func modelTranscription(audio, language string, ml *model.ModelLoader, backendConfig *config.BackendConfig, appConfig *config.ApplicationConfig) (*schema.TranscriptionResult, error) {
|
2024-01-05 17:04:46 +00:00
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
opts := modelOpts(backendConfig, appConfig, []model.Option{
|
2024-01-05 17:04:46 +00:00
|
|
|
model.WithBackendString(model.WhisperBackend),
|
2024-03-01 15:19:53 +00:00
|
|
|
model.WithModel(backendConfig.Model),
|
|
|
|
model.WithContext(appConfig.Context),
|
2024-03-13 09:05:30 +00:00
|
|
|
model.WithThreads(uint32(*backendConfig.Threads)),
|
2024-03-01 15:19:53 +00:00
|
|
|
model.WithAssetDir(appConfig.AssetsDestination),
|
2024-01-05 17:04:46 +00:00
|
|
|
})
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
whisperModel, err := ml.BackendLoader(opts...)
|
2024-01-05 17:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if whisperModel == nil {
|
|
|
|
return nil, fmt.Errorf("could not load whisper model")
|
|
|
|
}
|
|
|
|
|
|
|
|
return whisperModel.AudioTranscription(context.Background(), &proto.TranscriptRequest{
|
|
|
|
Dst: audio,
|
|
|
|
Language: language,
|
2024-03-13 09:05:30 +00:00
|
|
|
Threads: uint32(*backendConfig.Threads),
|
2024-01-05 17:04:46 +00:00
|
|
|
})
|
|
|
|
}
|