|
| 1 | +<h1 align="center" style="border-bottom: none;">Speech to Text Code Pattern 🗣</h1> |
| 2 | +<h3 align="center">Sample React app for playing around with the Watson Speech to Text service.</h3> |
| 3 | +<p align="center"> |
| 4 | + <a href="http://travis-ci.org/watson-developer-cloud/speech-to-text-code-pattern"> |
| 5 | + <img alt="Travis" src="https://travis-ci.org/watson-developer-cloud/speech-to-text-code-pattern.svg?branch=master"> |
| 6 | + </a> |
| 7 | + <a href="#badge"> |
| 8 | + <img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg"> |
| 9 | + </a> |
| 10 | +</p> |
| 11 | +</p> |
| 12 | + |
| 13 | +✨ **Demo:** https://speech-to-text-code-pattern.mybluemix.net/ ✨ |
| 14 | + |
| 15 | +## Flow |
| 16 | + |
| 17 | +<p align="center"> |
| 18 | + <img alt="architecture" width="600" src="./public/architecture.png"> |
| 19 | +</p> |
| 20 | + |
| 21 | +1. User supplies an audio input to the application (running locally, in the IBM Cloud or in IBM Cloud Pak for Data). |
| 22 | +1. The application sends the audio data to the Watson Speech to Text service through a [WebSocket connection](https://cloud.ibm.com/docs/services/speech-to-text?topic=speech-to-text-websockets). |
| 23 | +1. As the data is processed, the Speech to Text service returns information about extracted text and other metadata to the application to display. |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +1. Sign up for an [IBM Cloud account](https://console.bluemix.net/registration/). |
| 28 | +1. Download the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview). |
| 29 | +1. Create an instance of the Speech to Text service and get your credentials: |
| 30 | + - Go to the [Speech to Text](https://console.bluemix.net/catalog/services/speech-to-text) page in the IBM Cloud Catalog. |
| 31 | + - Log in to your IBM Cloud account. |
| 32 | + - Click **Create**. |
| 33 | + - Click **Show** to view the service credentials. |
| 34 | + - Copy the `apikey` value. |
| 35 | + - Copy the `url` value. |
| 36 | + |
| 37 | +### Cloud Pak for Data |
| 38 | + |
| 39 | +To use this code pattern with a Speech to Text instance provisioned on Cloud Pak for Data, you can use your CPD `username` and `password` credentials or an `access_token` to authenticate your requests. You also need the service `url` as described [here](https://cloud.ibm.com/apidocs/speech-to-text/speech-to-text-data#authentication). |
| 40 | + |
| 41 | +Another important thing to note is that this code pattern assumes that you're using a **valid SSL certificate** for your CPD cluster. If not, you'll receive transcription errors. If you'd still like to use the app with an invalid certificate, you'll need to look up your browser-specific way to ignore these certificate errors. Do note that this is very insecure though! |
| 42 | + |
| 43 | +## Configuring the application |
| 44 | + |
| 45 | +Depending on where your service instance is you may have different ways to download the credentials file. |
| 46 | + |
| 47 | +> Need more information? See the [authentication wiki](https://github.com/IBM/node-sdk-core/blob/master/AUTHENTICATION.md). |
| 48 | +
|
| 49 | +### Automatically |
| 50 | + |
| 51 | +Copy the credential file to the application folder. |
| 52 | + |
| 53 | +**Cloud Pak for Data** |
| 54 | + |
| 55 | +<p align="center"> |
| 56 | + <img alt="CPD" width="600" src="https://watson-developer-cloud.github.io/images/credentials-cpd.png"> |
| 57 | +</p> |
| 58 | + |
| 59 | +**Public Cloud** |
| 60 | + |
| 61 | +<p align="center"> |
| 62 | + <img alt="public" width="600" src="https://watson-developer-cloud.github.io/images/credentials-public.png"> |
| 63 | +</p> |
| 64 | + |
| 65 | +### Manually |
| 66 | + |
| 67 | +1. In the application folder, copy the `.env.example` file and create a file called `.env`: |
| 68 | + |
| 69 | + ``` |
| 70 | + cp .env.example .env |
| 71 | + ``` |
| 72 | + |
| 73 | +2. Open the `.env` file and add the service credentials depending on your environment. |
| 74 | + |
| 75 | + Example `.env` file that configures the `apikey` and `url` for a Speech to Text service instance hosted in the US South region: |
| 76 | + |
| 77 | + ``` |
| 78 | + SPEECH_TO_TEXT_APIKEY=12345abcde |
| 79 | + SPEECH_TO_TEXT_URL=https://stream.watsonplatform.net/speech-to-text/api |
| 80 | + ``` |
| 81 | + |
| 82 | + - **CPD using username and password:** If your service instance is running in Cloud Pak for Data and you want to use `username` and `password` credentials, add the following variables to the `.env` file. |
| 83 | + |
| 84 | + ``` |
| 85 | + SPEECH_TO_TEXT_USERNAME=admin |
| 86 | + SPEECH_TO_TEXT_PASSWORD=password |
| 87 | + SPEECH_TO_TEXT_URL=https://{cpd-url}:{cpd-port}/speech-to-text/api |
| 88 | + ``` |
| 89 | +
|
| 90 | + - **CPD using access token:** If your service instance is running in Cloud Pak for Data and you want to use the `access_token` from the service instance detail page, add the following: |
| 91 | +
|
| 92 | + ``` |
| 93 | + SPEECH_TO_TEXT_BEARER_TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.ey... |
| 94 | + SPEECH_TO_TEXT_URL=https://{cpd-url}:{cpd-port}/speech-to-text/api |
| 95 | + ``` |
| 96 | +
|
| 97 | +## Running locally |
| 98 | +
|
| 99 | +1. Install the dependencies |
| 100 | +
|
| 101 | + ``` |
| 102 | + npm install |
| 103 | + ``` |
| 104 | +
|
| 105 | +1. Build the application |
| 106 | +
|
| 107 | + ``` |
| 108 | + npm run build |
| 109 | + ``` |
| 110 | +
|
| 111 | +1. Run the application |
| 112 | +
|
| 113 | + ``` |
| 114 | + npm run dev |
| 115 | + ``` |
| 116 | +
|
| 117 | +1. View the application in a browser at `localhost:3000` |
| 118 | +
|
| 119 | +## Deploying to IBM Cloud as a Cloud Foundry Application |
| 120 | +
|
| 121 | +Click on the button below to deploy this demo to the IBM Cloud. |
| 122 | +
|
| 123 | +[](https://cloud.ibm.com/devops/setup/deploy?repository=https://github.com/watson-developer-cloud/speech-to-text-code-pattern) |
| 124 | +
|
| 125 | +1. Build the application |
| 126 | +
|
| 127 | + ``` |
| 128 | + npm run build |
| 129 | + ``` |
| 130 | +
|
| 131 | +1. Login to IBM Cloud with the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview) |
| 132 | +
|
| 133 | + ``` |
| 134 | + ibmcloud login |
| 135 | + ``` |
| 136 | +
|
| 137 | +1. Target a Cloud Foundry organization and space. |
| 138 | +
|
| 139 | + ``` |
| 140 | + ibmcloud target --cf |
| 141 | + ``` |
| 142 | +
|
| 143 | +1. Edit the `manifest.yml` file. Change the **name** field to something unique. For example: `- name: my-app-name`. |
| 144 | +1. Deploy the application |
| 145 | +
|
| 146 | + ``` |
| 147 | + ibmcloud app push |
| 148 | + ``` |
| 149 | +
|
| 150 | +1. View the application online at the app URL, for example: https://my-app-name.mybluemix.net |
| 151 | +
|
| 152 | +## Tests |
| 153 | +
|
| 154 | +#### Unit tests |
| 155 | +
|
| 156 | +Run unit tests with: |
| 157 | +
|
| 158 | +``` |
| 159 | +npm run test:components |
| 160 | +``` |
| 161 | +
|
| 162 | +See the output for more info. |
| 163 | +
|
| 164 | +#### Integration tests |
| 165 | +
|
| 166 | +First you have to make sure your code is built: |
| 167 | +
|
| 168 | +``` |
| 169 | +npm run build |
| 170 | +``` |
| 171 | +
|
| 172 | +Then run integration tests with: |
| 173 | +
|
| 174 | +``` |
| 175 | +npm run test:integration |
| 176 | +``` |
| 177 | +
|
| 178 | +## Directory structure |
| 179 | +
|
| 180 | +```none |
| 181 | +. |
| 182 | +├── app.js // Express routes |
| 183 | +├── config // Express configuration |
| 184 | +│ ├── error-handler.js |
| 185 | +│ ├── express.js |
| 186 | +│ └── security.js |
| 187 | +├── package.json |
| 188 | +├── public // Static resources |
| 189 | +├── server.js // Entry point |
| 190 | +├── test // Tests |
| 191 | +└── src // React client |
| 192 | + └── index.js // App entry point |
| 193 | +``` |
| 194 | + |
| 195 | +## License |
| 196 | + |
| 197 | +This sample code is licensed under the [MIT License](https://opensource.org/licenses/MIT). |
| 198 | + |
| 199 | +## Open Source @ IBM |
| 200 | + |
| 201 | +Find more open source projects on the [IBM Github Page](http://ibm.github.io/) |
0 commit comments