|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +// [START googlegenaisdk_textgen_transcript_with_gcs_audio] |
| 18 | +const {GoogleGenAI} = require('@google/genai'); |
| 19 | + |
| 20 | +const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; |
| 21 | +const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global'; |
| 22 | + |
| 23 | +async function generateText( |
| 24 | + projectId = GOOGLE_CLOUD_PROJECT, |
| 25 | + location = GOOGLE_CLOUD_LOCATION |
| 26 | +) { |
| 27 | + const client = new GoogleGenAI({ |
| 28 | + vertexai: true, |
| 29 | + project: projectId, |
| 30 | + location: location, |
| 31 | + }); |
| 32 | + |
| 33 | + const prompt = `Transcribe the interview, in the format of timecode, speaker, caption. |
| 34 | + Use speaker A, speaker B, etc. to identify speakers.`; |
| 35 | + |
| 36 | + const response = await client.models.generateContent({ |
| 37 | + model: 'gemini-2.5-flash', |
| 38 | + contents: [ |
| 39 | + {text: prompt}, |
| 40 | + { |
| 41 | + fileData: { |
| 42 | + fileUri: 'gs://cloud-samples-data/generative-ai/audio/pixel.mp3', |
| 43 | + mimeType: 'audio/mpeg', |
| 44 | + }, |
| 45 | + }, |
| 46 | + ], |
| 47 | + // Required to enable timestamp understanding for audio-only files |
| 48 | + config: { |
| 49 | + audioTimestamp: true, |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + console.log(response.text); |
| 54 | + |
| 55 | + // Example response: |
| 56 | + // [00:00:00] **Speaker A:** your devices are getting better over time. And so ... |
| 57 | + // [00:00:14] **Speaker B:** Welcome to the Made by Google podcast where we meet ... |
| 58 | + // [00:00:20] **Speaker B:** Here's your host, Rasheed Finch. |
| 59 | + // [00:00:23] **Speaker C:** Today we're talking to Aisha Sharif and DeCarlos Love. ... |
| 60 | + // ... |
| 61 | + |
| 62 | + return response.text; |
| 63 | +} |
| 64 | + |
| 65 | +// [END googlegenaisdk_textgen_transcript_with_gcs_audio] |
| 66 | + |
| 67 | +module.exports = { |
| 68 | + generateText, |
| 69 | +}; |
0 commit comments