-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: untangle pkg/grpc and core/schema for Transcription #3419
Merged
dave-gray101
merged 5 commits into
mudler:master
from
dave-gray101:fix-pb-transcriptresult
Sep 2, 2024
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d156124
untangle pkg/grpc and core/schema in Transcribe
dave-gray101 a69bf03
cleanup
dave-gray101 0ad7782
Merge branch 'master' into fix-pb-transcriptresult
dave-gray101 593d0a3
Merge branch 'master' into fix-pb-transcriptresult
dave-gray101 ecffafd
Merge branch 'master' into fix-pb-transcriptresult
dave-gray101 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package main | ||
|
||
// This is a wrapper to statisfy the GRPC service interface | ||
// It is meant to be used by the main executable that is the server for the specific backend type (falcon, gpt3, etc) | ||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper" | ||
"github.com/go-audio/wav" | ||
"github.com/mudler/LocalAI/pkg/grpc/base" | ||
pb "github.com/mudler/LocalAI/pkg/grpc/proto" | ||
"github.com/mudler/LocalAI/pkg/utils" | ||
) | ||
|
||
type Whisper struct { | ||
base.SingleThread | ||
whisper whisper.Model | ||
} | ||
|
||
func (sd *Whisper) Load(opts *pb.ModelOptions) error { | ||
// Note: the Model here is a path to a directory containing the model files | ||
w, err := whisper.New(opts.ModelFile) | ||
sd.whisper = w | ||
return err | ||
} | ||
|
||
func (sd *Whisper) AudioTranscription(opts *pb.TranscriptRequest) (pb.TranscriptResult, error) { | ||
|
||
dir, err := os.MkdirTemp("", "whisper") | ||
if err != nil { | ||
return pb.TranscriptResult{}, err | ||
} | ||
defer os.RemoveAll(dir) | ||
|
||
convertedPath := filepath.Join(dir, "converted.wav") | ||
|
||
if err := utils.AudioToWav(opts.Dst, convertedPath); err != nil { | ||
return pb.TranscriptResult{}, err | ||
} | ||
|
||
// Open samples | ||
fh, err := os.Open(convertedPath) | ||
if err != nil { | ||
return pb.TranscriptResult{}, err | ||
} | ||
defer fh.Close() | ||
|
||
// Read samples | ||
d := wav.NewDecoder(fh) | ||
buf, err := d.FullPCMBuffer() | ||
if err != nil { | ||
return pb.TranscriptResult{}, err | ||
} | ||
|
||
data := buf.AsFloat32Buffer().Data | ||
|
||
// Process samples | ||
context, err := sd.whisper.NewContext() | ||
if err != nil { | ||
return pb.TranscriptResult{}, err | ||
|
||
} | ||
|
||
context.SetThreads(uint(opts.Threads)) | ||
|
||
if opts.Language != "" { | ||
context.SetLanguage(opts.Language) | ||
} else { | ||
context.SetLanguage("auto") | ||
} | ||
|
||
if opts.Translate { | ||
context.SetTranslate(true) | ||
} | ||
|
||
if err := context.Process(data, nil, nil); err != nil { | ||
return pb.TranscriptResult{}, err | ||
} | ||
|
||
segments := []*pb.TranscriptSegment{} | ||
text := "" | ||
for { | ||
s, err := context.NextSegment() | ||
if err != nil { | ||
break | ||
} | ||
|
||
var tokens []int32 | ||
for _, t := range s.Tokens { | ||
tokens = append(tokens, int32(t.Id)) | ||
} | ||
|
||
segment := &pb.TranscriptSegment{Id: int32(s.Num), Text: s.Text, Start: int64(s.Start), End: int64(s.End), Tokens: tokens} | ||
segments = append(segments, segment) | ||
|
||
text += s.Text | ||
} | ||
|
||
return pb.TranscriptResult{ | ||
Segments: segments, | ||
Text: text, | ||
}, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a rename of
backend/go/transcribe/whisper.go
- it's been modified enough that git considers it a new file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transcript.go was also merged into this file to avoid copying locks - it was very whisper specific already