Google 有一个页面描述了如何使用他们的Gemini-1.5模型之一来转录音频。他们提供了一个示例脚本(见下文)。该脚本通过Part.from_uri()
命令从Google Storage获取音频文件。
然而,我希望使用本地文件。将URI设置为“file:///…”不起作用。我该怎么做呢?
Google的(可用的,基于云的)代码如下:
import vertexaifrom vertexai.generative_models import GenerativeModel, GenerationConfig, Part# TODO(developer): Update and un-comment below line# PROJECT_ID = "your-project-id"vertexai.init(project=PROJECT_ID, location="us-central1")model = GenerativeModel("gemini-1.5-flash-002")prompt = """Can you transcribe this interview, in the format of timecode, speaker, caption.Use speaker A, speaker B, etc. to identify speakers."""audio_file_uri = "gs://cloud-samples-data/generative-ai/audio/pixel.mp3"audio_file = Part.from_uri(audio_file_uri, mime_type="audio/mpeg")contents = [audio_file, prompt]response = model.generate_content(contents, generation_config=GenerationConfig(audio_timestamp=True))print(response.text)# Example response:# [00:00:00] Speaker A: Your devices are getting better over time...# [00:00:16] Speaker B: Welcome to the Made by Google podcast, ...# [00:01:00] Speaker A: So many features. I am a singer. ...# [00:01:33] Speaker B: Amazing. DeCarlos, same question to you, ...
回答:
尽管人们可能会期望以下base-64编码和解码会相互抵消,但以下代码似乎有效。(该代码是此页面上的一个稍作修改的版本。)
... encoded_audio = base64.b64encode(open(audio_path, "rb").read()).decode("utf-8") mime_type = "audio/mpeg" audio_content = Part.from_data( data=base64.b64decode(encoded_audio), mime_type=mime_type ) contents = [audio_content, prompt]...