Created
August 13, 2024 23:37
-
-
Save rakyll/009d0980b17cebe5258a4f19693ecf75 to your computer and use it in GitHub Desktop.
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 ( | |
"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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output: