Google的Gemini处理本地音频文件

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]...

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注