|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Google Speech-to-Text Audio Transcripts\n", |
| 8 | + "\n", |
| 9 | + "The `GoogleSpeechToTextLoader` allows to transcribe audio files with the [Google Cloud Speech-to-Text API](https://cloud.google.com/speech-to-text) and loads the transcribed text into documents.\n", |
| 10 | + "\n", |
| 11 | + "To use it, you should have the `google-cloud-speech` python package installed, and a Google Cloud project with the [Speech-to-Text API enabled](https://cloud.google.com/speech-to-text/v2/docs/transcribe-client-libraries#before_you_begin).\n", |
| 12 | + "\n", |
| 13 | + "- [Bringing the power of large models to Google Cloud’s Speech API](https://cloud.google.com/blog/products/ai-machine-learning/bringing-power-large-models-google-clouds-speech-api)" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "markdown", |
| 18 | + "metadata": {}, |
| 19 | + "source": [ |
| 20 | + "## Installation & setup\n", |
| 21 | + "\n", |
| 22 | + "First, you need to install the `google-cloud-speech` python package.\n", |
| 23 | + "\n", |
| 24 | + "You can find more info about it on the [Speech-to-Text client libraries](https://cloud.google.com/speech-to-text/v2/docs/libraries) page.\n", |
| 25 | + "\n", |
| 26 | + "Follow the [quickstart guide](https://cloud.google.com/speech-to-text/v2/docs/sync-recognize) in the Google Cloud documentation to create a project and enable the API." |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": null, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "%pip install google-cloud-speech\n" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "## Example\n", |
| 43 | + "\n", |
| 44 | + "The `GoogleSpeechToTextLoader` must include the `project_id` and `file_path` arguments. Audio files can be specified as a Google Cloud Storage URI (`gs://...`) or a local file path.\n", |
| 45 | + "\n", |
| 46 | + "Only synchronous requests are supported by the loader, which has a [limit of 60 seconds or 10MB](https://cloud.google.com/speech-to-text/v2/docs/sync-recognize#:~:text=60%20seconds%20and/or%2010%20MB) per audio file." |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 2, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [], |
| 54 | + "source": [ |
| 55 | + "from langchain.document_loaders import GoogleSpeechToTextLoader\n", |
| 56 | + "\n", |
| 57 | + "project_id = \"<PROJECT_ID>\"\n", |
| 58 | + "file_path = \"gs://cloud-samples-data/speech/audio.flac\"\n", |
| 59 | + "# or a local file path: file_path = \"./audio.wav\"\n", |
| 60 | + "\n", |
| 61 | + "loader = GoogleSpeechToTextLoader(project_id=project_id, file_path=file_path)\n", |
| 62 | + "\n", |
| 63 | + "docs = loader.load()\n" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "markdown", |
| 68 | + "metadata": {}, |
| 69 | + "source": [ |
| 70 | + "Note: Calling `loader.load()` blocks until the transcription is finished." |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "markdown", |
| 75 | + "metadata": {}, |
| 76 | + "source": [ |
| 77 | + "The transcribed text is available in the `page_content`:" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": null, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "docs[0].page_content\n" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "```\n", |
| 94 | + "\"How old is the Brooklyn Bridge?\"\n", |
| 95 | + "```" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "The `metadata` contains the full JSON response with more meta information:" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": null, |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [], |
| 110 | + "source": [ |
| 111 | + "docs[0].metadata\n" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "markdown", |
| 116 | + "metadata": {}, |
| 117 | + "source": [ |
| 118 | + "```json\n", |
| 119 | + "{\n", |
| 120 | + " 'language_code': 'en-US',\n", |
| 121 | + " 'result_end_offset': datetime.timedelta(seconds=1)\n", |
| 122 | + "}\n", |
| 123 | + "```" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "metadata": {}, |
| 129 | + "source": [ |
| 130 | + "## Recognition Config\n", |
| 131 | + "\n", |
| 132 | + "You can specify the `config` argument to use different speech recognition models and enable specific features.\n", |
| 133 | + "\n", |
| 134 | + "Refer to the [Speech-to-Text recognizers documentation](https://cloud.google.com/speech-to-text/v2/docs/recognizers) and the [`RecognizeRequest`](https://cloud.google.com/python/docs/reference/speech/latest/google.cloud.speech_v2.types.RecognizeRequest) API reference for information on how to set a custom configuation.\n", |
| 135 | + "\n", |
| 136 | + "If you don't specify a `config`, the following options will be selected automatically:\n", |
| 137 | + "\n", |
| 138 | + "- Model: [Chirp Universal Speech Model](https://cloud.google.com/speech-to-text/v2/docs/chirp-model)\n", |
| 139 | + "- Language: `en-US`\n", |
| 140 | + "- Audio Encoding: Automatically Detected\n", |
| 141 | + "- Automatic Punctuation: Enabled" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": 6, |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [], |
| 149 | + "source": [ |
| 150 | + "from google.cloud.speech_v2 import AutoDetectDecodingConfig, RecognitionConfig, RecognitionFeatures\n", |
| 151 | + "from langchain.document_loaders import GoogleSpeechToTextLoader\n", |
| 152 | + "\n", |
| 153 | + "project_id = \"<PROJECT_ID>\"\n", |
| 154 | + "location = \"global\"\n", |
| 155 | + "recognizer_id = \"<RECOGNIZER_ID>\"\n", |
| 156 | + "file_path = \"./audio.wav\"\n", |
| 157 | + "\n", |
| 158 | + "config = RecognitionConfig(\n", |
| 159 | + " auto_decoding_config=AutoDetectDecodingConfig(),\n", |
| 160 | + " language_codes=[\"en-US\"],\n", |
| 161 | + " model=\"long\",\n", |
| 162 | + " features=RecognitionFeatures(\n", |
| 163 | + " enable_automatic_punctuation=False,\n", |
| 164 | + " profanity_filter=True,\n", |
| 165 | + " enable_spoken_punctuation=True,\n", |
| 166 | + " enable_spoken_emojis=True\n", |
| 167 | + " ),\n", |
| 168 | + " )\n", |
| 169 | + "\n", |
| 170 | + "loader = GoogleSpeechToTextLoader(\n", |
| 171 | + " project_id=project_id,\n", |
| 172 | + " location=location,\n", |
| 173 | + " recognizer_id=recognizer_id,\n", |
| 174 | + " file_path=file_path,\n", |
| 175 | + " config=config\n", |
| 176 | + ")\n" |
| 177 | + ] |
| 178 | + } |
| 179 | + ], |
| 180 | + "metadata": { |
| 181 | + "kernelspec": { |
| 182 | + "display_name": ".venv", |
| 183 | + "language": "python", |
| 184 | + "name": "python3" |
| 185 | + }, |
| 186 | + "language_info": { |
| 187 | + "codemirror_mode": { |
| 188 | + "name": "ipython", |
| 189 | + "version": 3 |
| 190 | + }, |
| 191 | + "file_extension": ".py", |
| 192 | + "mimetype": "text/x-python", |
| 193 | + "name": "python", |
| 194 | + "nbconvert_exporter": "python", |
| 195 | + "pygments_lexer": "ipython3", |
| 196 | + "version": "3.11.0" |
| 197 | + }, |
| 198 | + "orig_nbformat": 4 |
| 199 | + }, |
| 200 | + "nbformat": 4, |
| 201 | + "nbformat_minor": 2 |
| 202 | +} |
0 commit comments