Created
June 22, 2024 19:52
-
-
Save boxabirds/01b5f0091abdd7dc9575c3ccee7ca3fe to your computer and use it in GitHub Desktop.
Whisper.cpp go demo
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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
whisper "github.com/ggerganov/whisper.cpp/bindings/go" | |
) | |
func main() { | |
// Check if an audio file path is provided | |
if len(os.Args) < 2 { | |
fmt.Println("Please provide the path to an audio file.") | |
os.Exit(1) | |
} | |
audioPath := os.Args[1] | |
// Initialize whisper context | |
ctx, err := whisper.New("path/to/whisper/model.bin") | |
if err != nil { | |
log.Fatalf("Failed to create whisper context: %v", err) | |
} | |
defer ctx.Free() | |
// Read audio file | |
audioData, err := ioutil.ReadFile(audioPath) | |
if err != nil { | |
log.Fatalf("Failed to read audio file: %v", err) | |
} | |
// Process audio data | |
if err := ctx.Process(audioData, nil, nil); err != nil { | |
log.Fatalf("Failed to process audio: %v", err) | |
} | |
// Get the number of segments | |
segmentCount := ctx.SegmentCount() | |
// Print transcribed text | |
fmt.Println("Transcription:") | |
for i := 0; i < segmentCount; i++ { | |
segment, err := ctx.GetSegment(i) | |
if err != nil { | |
log.Printf("Failed to get segment %d: %v", i, err) | |
continue | |
} | |
fmt.Printf("[%.2f -> %.2f] %s\n", segment.Start, segment.End, segment.Text) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment