Skip to content

Instantly share code, notes, and snippets.

@rakyll
Created August 13, 2024 23:37
Show Gist options
  • Save rakyll/009d0980b17cebe5258a4f19693ecf75 to your computer and use it in GitHub Desktop.
Save rakyll/009d0980b17cebe5258a4f19693ecf75 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
apiKey, ok := os.LookupEnv("GEMINI_API_KEY")
if !ok {
log.Fatal("Environment variable GEMINI_API_KEY not set")
}
client, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
if err != nil {
log.Fatalf("Error creating client: %v", err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.5-pro-exp-0801")
model.SetTemperature(1)
model.SetTopK(64)
model.SetTopP(0.95)
model.SetMaxOutputTokens(8192)
model.ResponseMIMEType = "text/plain"
model.SafetySettings = []*genai.SafetySetting{
{Category: genai.HarmCategoryHarassment, Threshold: genai.HarmBlockNone},
}
session := model.StartChat()
f, err := os.Open("/Users/jbd/Downloads/sample-0.mp3")
if err != nil {
log.Fatalf("Failed to open file: %v", err)
}
content, err := io.ReadAll(f)
if err != nil {
log.Fatalf("Failed to read the contents: %v", err)
}
const bufferSize = 8192
for i := 0; i < len(content); i += bufferSize {
endx := i + bufferSize
if endx > len(content) {
endx = len(content)
}
log.Printf("Transcribing from %d to %d", i, endx)
resp, err := session.SendMessage(ctx, genai.Text("Transcribe the audio."), genai.Blob{
MIMEType: "audio/mp3",
Data: content[i:endx],
})
if err != nil {
log.Fatalf("Error sending message: %v", err)
}
for _, part := range resp.Candidates[0].Content.Parts {
fmt.Printf("%v\n", part)
}
}
}
@rakyll
Copy link
Author

rakyll commented Aug 13, 2024

The output:

2024/08/13 16:35:59 Transcribing from 0 to 8192
He does P-

2024/08/13 16:36:00 Transcribing from 8192 to 16384
Doesn't to be long P-

2024/08/13 16:36:01 Transcribing from 16384 to 24576
to you. A P-

2024/08/13 16:36:02 Transcribing from 24576 to 32768
And I don't see P-
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment