转录音频文件。
模块 whisper_local
LocalWhisperTranscriber
在本地机器上使用 OpenAI 的 Whisper 模型转录音频文件。
有关支持的音频格式、语言和其他参数,请参阅 Whisper API 文档 和官方 Whisper GitHub 存储库。
使用示例
from haystack.components.audio import LocalWhisperTranscriber
whisper = LocalWhisperTranscriber(model="small")
whisper.warm_up()
transcription = whisper.run(sources=["path/to/audio/file"])
LocalWhisperTranscriber.__init__
def __init__(model: WhisperLocalModel = "large",
device: Optional[ComponentDevice] = None,
whisper_params: Optional[dict[str, Any]] = None)
创建 LocalWhisperTranscriber 组件的实例。
参数:
model:要使用的模型名称。设置为以下模型之一:“tiny”、“base”、“small”、“medium”、“large”(默认)。有关模型及其修改的详细信息,请参阅 Whisper 文档。device:加载模型的设备。如果None,则自动选择默认设备。
LocalWhisperTranscriber.warm_up
def warm_up() -> None
将模型加载到内存中。
LocalWhisperTranscriber.to_dict
def to_dict() -> dict[str, Any]
将组件序列化为字典。
返回值:
包含序列化数据的字典。
LocalWhisperTranscriber.from_dict
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "LocalWhisperTranscriber"
从字典反序列化组件。
参数:
data: 要反序列化的字典。
返回值:
反序列化后的组件。
LocalWhisperTranscriber.run
@component.output_types(documents=list[Document])
def run(sources: list[Union[str, Path, ByteStream]],
whisper_params: Optional[dict[str, Any]] = None)
将音频文件列表转录为文档列表。
参数:
sources:要转录的路径或二进制流的列表。whisper_params:有关支持的音频格式、语言和其他参数,请参阅 Whisper API 文档 和官方 Whisper GitHub 仓库。
返回值:
包含以下键的字典
documents:文档列表,每个文档都是一个转录的音频文件。文档的内容是转录文本,文档的元数据包含 Whisper 模型返回的值,例如对齐数据和用于转录的音频文件的路径。
LocalWhisperTranscriber.transcribe
def transcribe(sources: list[Union[str, Path, ByteStream]],
**kwargs) -> list[Document]
将音频文件转录为文档列表,每个输入文件对应一个文档。
有关支持的音频格式、语言和其他参数,请参阅 Whisper API 文档 和官方 Whisper GitHub 仓库。
参数:
sources:要转录的路径或二进制流的列表。
返回值:
文档列表,每个文件对应一个文档。
模块 whisper_remote
RemoteWhisperTranscriber
使用 OpenAI 的 Whisper API 转录音频文件。
该组件需要 OpenAI API 密钥,有关更多详细信息,请参阅 OpenAI 文档。有关支持的音频格式、语言和其他参数,请参阅 Whisper API 文档。
使用示例
from haystack.components.audio import RemoteWhisperTranscriber
whisper = RemoteWhisperTranscriber(api_key=Secret.from_token("<your-api-key>"), model="tiny")
transcription = whisper.run(sources=["path/to/audio/file"])
RemoteWhisperTranscriber.__init__
def __init__(api_key: Secret = Secret.from_env_var("OPENAI_API_KEY"),
model: str = "whisper-1",
api_base_url: Optional[str] = None,
organization: Optional[str] = None,
http_client_kwargs: Optional[dict[str, Any]] = None,
**kwargs)
创建 RemoteWhisperTranscriber 组件的实例。
参数:
api_key:OpenAI API 密钥。您可以使用环境变量设置它OPENAI_API_KEY,或者在初始化时通过此参数传递。model:要使用的模型名称。目前仅接受whisper-1.organization:您的 OpenAI 组织 ID。请参阅 OpenAI 关于 设置您的组织 的文档。api_base:可用于 API 的可选 URL。有关详细信息,请参阅 OpenAI 文档。http_client_kwargs:用于配置自定义的关键字参数字典httpx.Client或httpx.AsyncClient。有关更多信息,请参阅 HTTPX 文档。kwargs:模型的其他可选参数。这些参数将直接发送到 OpenAI 端点。有关更多详细信息,请参阅 OpenAI 文档。一些支持的参数是language:输入音频的语言。提供 ISO-639-1 格式的输入语言以提高转录准确性和延迟。prompt:可选文本,用于指导模型的风格或继续之前的音频片段。提示应与音频语言匹配。response_format: the format of the transcript output. This component only supportsjson.temperature:采样温度,在 0 和 1 之间。较高的值(如 0.8)使输出更随机,而较低的值(如 0.2)使其更集中和确定。如果设置为 0,模型将使用对数概率自动提高温度,直到达到某些阈值。
RemoteWhisperTranscriber.to_dict
def to_dict() -> dict[str, Any]
将组件序列化为字典。
返回值:
包含序列化数据的字典。
RemoteWhisperTranscriber.from_dict
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "RemoteWhisperTranscriber"
从字典反序列化组件。
参数:
data: 要反序列化的字典。
返回值:
反序列化后的组件。
RemoteWhisperTranscriber.run
@component.output_types(documents=list[Document])
def run(sources: list[Union[str, Path, ByteStream]])
将音频文件列表转录为文档列表。
参数:
sources:要转录的音频文件的文件路径或ByteStream对象列表。
返回值:
包含以下键的字典
documents:文档列表,每个文件对应一个文档。每个文档的内容是转录的文本。
