引言
在现代技术的推动下,语音识别已经成为提高生产力和便利性的关键工具。Google Cloud 的 Speech-to-Text API 提供了一种强大且灵活的方法,将音频文件转录为文本。通过本文,我们将深入探讨如何使用 Python 库将音频转化为文本,并加载到文档中。
主要内容
安装与设置
在开始之前,你需要确保安装了 google-cloud-speech Python 包,并且在 Google Cloud 项目中启用了 Speech-to-Text API。以下是安装步骤:
%pip install --upgrade --quiet langchain-google-community[speech]
有关详细信息,请参阅 Speech-to-Text 客户端库页面 并按照 快速入门指南。
使用示例
GoogleSpeechToTextLoader 需要 project_id 和 file_path 参数。音频文件可以是 Google Cloud Storage URI (如 gs://...) 或本地文件路径。请注意,目前只支持同步请求,并且音频文件有 60 秒或 10MB 的限制。
from langchain_google_community import GoogleSpeechToTextLoader
project_id = "<PROJECT_ID>"
file_path = "gs://cloud-samples-data/speech/audio.flac"
# 或者使用本地文件路径: file_path = "./audio.wav"
# 使用API代理服务提高访问稳定性
loader = GoogleSpeechToTextLoader(project_id=project_id, file_path=file_path)
docs = loader.load()
# 获取转录后的文本
print(docs[0].page_content)
识别配置
通过指定 config 参数,可以使用不同的语音识别模型和启用特定功能。更多信息可以参考 Speech-to-Text 识别器文档 和 RecognizeRequest API 参考。
from google.cloud.speech_v2 import (
AutoDetectDecodingConfig,
RecognitionConfig,
RecognitionFeatures,
)
from langchain_google_community import GoogleSpeechToTextLoader
project_id = "<PROJECT_ID>"
location = "global"
recognizer_id = "<RECOGNIZER_ID>"
file_path = "./audio.wav"
config = RecognitionConfig(
auto_decoding_config=AutoDetectDecodingConfig(),
language_codes=["en-US"],
model="long",
features=RecognitionFeatures(
enable_automatic_punctuation=False,
profanity_filter=True,
enable_spoken_punctuation=True,
enable_spoken_emojis=True,
),
)
loader = GoogleSpeechToTextLoader(
project_id=project_id,
location=location,
recognizer_id=recognizer_id,
file_path=file_path,
config=config,
)
docs = loader.load()
print(docs[0].page_content)
常见问题和解决方案
-
访问限制:由于网络限制,某些地区可能需要使用 API 代理服务来提高访问稳定性。
-
同步请求限制:请确保音频文件不超过 60 秒或 10MB,否则会导致请求失败。
总结和进一步学习资源
通过 Google Cloud Speech-to-Text API,开发者可以将音频内容高效地转录为文本,进而应用于各种场景。建议进一步阅读以下资源以深化理解:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---