|
| 1 | +--- |
| 2 | +title: "Whisper" |
| 3 | +id: integrations-whisper |
| 4 | +description: "Whisper integration for Haystack" |
| 5 | +slug: "/integrations-whisper" |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +## haystack_integrations.components.audio.whisper.whisper_local |
| 10 | + |
| 11 | +### LocalWhisperTranscriber |
| 12 | + |
| 13 | +Transcribes audio files using OpenAI's Whisper model on your local machine. |
| 14 | + |
| 15 | +For the supported audio formats, languages, and other parameters, see the |
| 16 | +[Whisper API documentation](https://platform.openai.com/docs/guides/speech-to-text) and the official Whisper |
| 17 | +[GitHub repository](https://github.com/openai/whisper). |
| 18 | + |
| 19 | +### Usage example |
| 20 | + |
| 21 | +```python |
| 22 | +from haystack_integrations.components.audio.whisper import LocalWhisperTranscriber |
| 23 | + |
| 24 | +whisper = LocalWhisperTranscriber(model="small") |
| 25 | +whisper.warm_up() |
| 26 | +transcription = whisper.run(sources=["path/to/audio/file"]) |
| 27 | +``` |
| 28 | + |
| 29 | +#### __init__ |
| 30 | + |
| 31 | +```python |
| 32 | +__init__( |
| 33 | + model: WhisperLocalModel = "large", |
| 34 | + device: ComponentDevice | None = None, |
| 35 | + whisper_params: dict[str, Any] | None = None, |
| 36 | +) -> None |
| 37 | +``` |
| 38 | + |
| 39 | +Creates an instance of the LocalWhisperTranscriber component. |
| 40 | + |
| 41 | +**Parameters:** |
| 42 | + |
| 43 | +- **model** (<code>WhisperLocalModel</code>) – The name of the model to use. Set to one of the following models: |
| 44 | + "tiny", "base", "small", "medium", "large" (default). |
| 45 | + For details on the models and their modifications, see the |
| 46 | + [Whisper documentation](https://github.com/openai/whisper?tab=readme-ov-file#available-models-and-languages). |
| 47 | +- **device** (<code>ComponentDevice | None</code>) – The device for loading the model. If `None`, automatically selects the default device. |
| 48 | + |
| 49 | +#### warm_up |
| 50 | + |
| 51 | +```python |
| 52 | +warm_up() -> None |
| 53 | +``` |
| 54 | + |
| 55 | +Loads the model in memory. |
| 56 | + |
| 57 | +#### to_dict |
| 58 | + |
| 59 | +```python |
| 60 | +to_dict() -> dict[str, Any] |
| 61 | +``` |
| 62 | + |
| 63 | +Serializes the component to a dictionary. |
| 64 | + |
| 65 | +**Returns:** |
| 66 | + |
| 67 | +- <code>dict\[str, Any\]</code> – Dictionary with serialized data. |
| 68 | + |
| 69 | +#### from_dict |
| 70 | + |
| 71 | +```python |
| 72 | +from_dict(data: dict[str, Any]) -> LocalWhisperTranscriber |
| 73 | +``` |
| 74 | + |
| 75 | +Deserializes the component from a dictionary. |
| 76 | + |
| 77 | +**Parameters:** |
| 78 | + |
| 79 | +- **data** (<code>dict\[str, Any\]</code>) – The dictionary to deserialize from. |
| 80 | + |
| 81 | +**Returns:** |
| 82 | + |
| 83 | +- <code>LocalWhisperTranscriber</code> – The deserialized component. |
| 84 | + |
| 85 | +#### run |
| 86 | + |
| 87 | +```python |
| 88 | +run( |
| 89 | + sources: list[str | Path | ByteStream], |
| 90 | + whisper_params: dict[str, Any] | None = None, |
| 91 | +) -> dict[str, Any] |
| 92 | +``` |
| 93 | + |
| 94 | +Transcribes a list of audio files into a list of documents. |
| 95 | + |
| 96 | +**Parameters:** |
| 97 | + |
| 98 | +- **sources** (<code>list\[str | Path | ByteStream\]</code>) – A list of paths or binary streams to transcribe. |
| 99 | +- **whisper_params** (<code>dict\[str, Any\] | None</code>) – For the supported audio formats, languages, and other parameters, see the |
| 100 | + [Whisper API documentation](https://platform.openai.com/docs/guides/speech-to-text) and the official Whisper |
| 101 | + [GitHup repo](https://github.com/openai/whisper). |
| 102 | + |
| 103 | +**Returns:** |
| 104 | + |
| 105 | +- <code>dict\[str, Any\]</code> – A dictionary with the following keys: |
| 106 | +- `documents`: A list of documents where each document is a transcribed audio file. The content of |
| 107 | + the document is the transcription text, and the document's metadata contains the values returned by |
| 108 | + the Whisper model, such as the alignment data and the path to the audio file used |
| 109 | + for the transcription. |
| 110 | + |
| 111 | +## haystack_integrations.components.audio.whisper.whisper_remote |
| 112 | + |
| 113 | +### RemoteWhisperTranscriber |
| 114 | + |
| 115 | +Transcribes audio files using the OpenAI's Whisper API. |
| 116 | + |
| 117 | +The component requires an OpenAI API key, see the |
| 118 | +[OpenAI documentation](https://platform.openai.com/docs/api-reference/authentication) for more details. |
| 119 | +For the supported audio formats, languages, and other parameters, see the |
| 120 | +[Whisper API documentation](https://platform.openai.com/docs/guides/speech-to-text). |
| 121 | + |
| 122 | +### Usage example |
| 123 | + |
| 124 | +```python |
| 125 | +from haystack_integrations.components.audio.whisper import RemoteWhisperTranscriber |
| 126 | + |
| 127 | +whisper = RemoteWhisperTranscriber(model="whisper-1") |
| 128 | +transcription = whisper.run(sources=["path/to/audio/file"]) |
| 129 | +``` |
| 130 | + |
| 131 | +#### __init__ |
| 132 | + |
| 133 | +```python |
| 134 | +__init__( |
| 135 | + api_key: Secret = Secret.from_env_var("OPENAI_API_KEY"), |
| 136 | + model: str = "whisper-1", |
| 137 | + api_base_url: str | None = None, |
| 138 | + organization: str | None = None, |
| 139 | + http_client_kwargs: dict[str, Any] | None = None, |
| 140 | + **kwargs: Any |
| 141 | +) -> None |
| 142 | +``` |
| 143 | + |
| 144 | +Creates an instance of the RemoteWhisperTranscriber component. |
| 145 | + |
| 146 | +**Parameters:** |
| 147 | + |
| 148 | +- **api_key** (<code>Secret</code>) – OpenAI API key. |
| 149 | + You can set it with an environment variable `OPENAI_API_KEY`, or pass with this parameter |
| 150 | + during initialization. |
| 151 | +- **model** (<code>str</code>) – Name of the model to use. Currently accepts only `whisper-1`. |
| 152 | +- **organization** (<code>str | None</code>) – Your OpenAI organization ID. See OpenAI's documentation on |
| 153 | + [Setting Up Your Organization](https://platform.openai.com/docs/guides/production-best-practices/setting-up-your-organization). |
| 154 | +- **api_base_url** (<code>str | None</code>) – An optional URL to use as the API base. For details, see the |
| 155 | + OpenAI [documentation](https://platform.openai.com/docs/api-reference/audio). |
| 156 | +- **http_client_kwargs** (<code>dict\[str, Any\] | None</code>) – A dictionary of keyword arguments to configure a custom `httpx.Client`or `httpx.AsyncClient`. |
| 157 | + For more information, see the [HTTPX documentation](https://www.python-httpx.org/api/#client). |
| 158 | +- **kwargs** (<code>Any</code>) – Other optional parameters for the model. These are sent directly to the OpenAI |
| 159 | + endpoint. See OpenAI [documentation](https://platform.openai.com/docs/api-reference/audio) for more details. |
| 160 | + Some of the supported parameters are: |
| 161 | +- `language`: The language of the input audio. |
| 162 | + Provide the input language in ISO-639-1 format |
| 163 | + to improve transcription accuracy and latency. |
| 164 | +- `prompt`: An optional text to guide the model's |
| 165 | + style or continue a previous audio segment. |
| 166 | + The prompt should match the audio language. |
| 167 | +- `response_format`: The format of the transcript |
| 168 | + output. This component only supports `json`. |
| 169 | +- `temperature`: The sampling temperature, between 0 |
| 170 | + and 1. Higher values like 0.8 make the output more |
| 171 | + random, while lower values like 0.2 make it more |
| 172 | + focused and deterministic. If set to 0, the model |
| 173 | + uses log probability to automatically increase the |
| 174 | + temperature until certain thresholds are hit. |
| 175 | + |
| 176 | +#### to_dict |
| 177 | + |
| 178 | +```python |
| 179 | +to_dict() -> dict[str, Any] |
| 180 | +``` |
| 181 | + |
| 182 | +Serializes the component to a dictionary. |
| 183 | + |
| 184 | +**Returns:** |
| 185 | + |
| 186 | +- <code>dict\[str, Any\]</code> – Dictionary with serialized data. |
| 187 | + |
| 188 | +#### from_dict |
| 189 | + |
| 190 | +```python |
| 191 | +from_dict(data: dict[str, Any]) -> RemoteWhisperTranscriber |
| 192 | +``` |
| 193 | + |
| 194 | +Deserializes the component from a dictionary. |
| 195 | + |
| 196 | +**Parameters:** |
| 197 | + |
| 198 | +- **data** (<code>dict\[str, Any\]</code>) – The dictionary to deserialize from. |
| 199 | + |
| 200 | +**Returns:** |
| 201 | + |
| 202 | +- <code>RemoteWhisperTranscriber</code> – The deserialized component. |
| 203 | + |
| 204 | +#### run |
| 205 | + |
| 206 | +```python |
| 207 | +run(sources: list[str | Path | ByteStream]) -> dict[str, Any] |
| 208 | +``` |
| 209 | + |
| 210 | +Transcribes the list of audio files into a list of documents. |
| 211 | + |
| 212 | +**Parameters:** |
| 213 | + |
| 214 | +- **sources** (<code>list\[str | Path | ByteStream\]</code>) – A list of file paths or `ByteStream` objects containing the audio files to transcribe. |
| 215 | + |
| 216 | +**Returns:** |
| 217 | + |
| 218 | +- <code>dict\[str, Any\]</code> – A dictionary with the following keys: |
| 219 | +- `documents`: A list of documents, one document for each file. |
| 220 | + The content of each document is the transcribed text. |
| 221 | + |
| 222 | +#### run_async |
| 223 | + |
| 224 | +```python |
| 225 | +run_async(sources: list[str | Path | ByteStream]) -> dict[str, Any] |
| 226 | +``` |
| 227 | + |
| 228 | +Asynchronously transcribes the list of audio files into a list of documents. |
| 229 | + |
| 230 | +This is the asynchronous version of the `run` method. It has the same parameters and return values |
| 231 | +but can be used with `await` in an async code. |
| 232 | + |
| 233 | +**Parameters:** |
| 234 | + |
| 235 | +- **sources** (<code>list\[str | Path | ByteStream\]</code>) – A list of file paths or `ByteStream` objects containing the audio files to transcribe. |
| 236 | + |
| 237 | +**Returns:** |
| 238 | + |
| 239 | +- <code>dict\[str, Any\]</code> – A dictionary with the following keys: |
| 240 | +- `documents`: A list of documents, one document for each file. |
| 241 | + The content of each document is the transcribed text. |
0 commit comments