| 24 | const adaptationClient = new speech.AdaptationClient(); |
| 25 | |
| 26 | async function modelAdaptation() { |
| 27 | // Create`PhraseSet` and `CustomClasses` to create custom lists of similar |
| 28 | // items that are likely to occur in your input data. |
| 29 | |
| 30 | // The parent resource where the custom class and phrase set will be created. |
| 31 | const parent = `projects/${projectId}/locations/${location}`; |
| 32 | |
| 33 | // Create the custom class |
| 34 | const customClassResponse = await adaptationClient.createCustomClass({ |
| 35 | parent: parent, |
| 36 | customClassId: customClassId, |
| 37 | customClass: { |
| 38 | items: [{value: 'sushido'}, {value: 'altura'}, {value: 'taneda'}], |
| 39 | }, |
| 40 | }); |
| 41 | |
| 42 | // Create the phrase set |
| 43 | const phraseSetResponse = await adaptationClient.createPhraseSet({ |
| 44 | parent: parent, |
| 45 | phraseSetId: phraseSetId, |
| 46 | phraseSet: { |
| 47 | boost: 10, |
| 48 | phrases: [{value: `Visit restaurants like ${customClassId}`}], |
| 49 | }, |
| 50 | }); |
| 51 | |
| 52 | // The next section shows how to use the newly created custom |
| 53 | // class and phrase set to send a transcription request with speech adaptation |
| 54 | |
| 55 | const speechAdaptation = { |
| 56 | phraseSets: [phraseSetResponse[0]], |
| 57 | phraseSetReferences: [], |
| 58 | customClasses: [customClassResponse[0]], |
| 59 | }; |
| 60 | |
| 61 | const audio = { |
| 62 | uri: gcsUri, |
| 63 | }; |
| 64 | |
| 65 | const config = { |
| 66 | encoding: 'FLAC', |
| 67 | sampleRateHertz: 16000, |
| 68 | languageCode: 'en-US', |
| 69 | adaptation: speechAdaptation, |
| 70 | }; |
| 71 | |
| 72 | const request = { |
| 73 | audio: audio, |
| 74 | config: config, |
| 75 | }; |
| 76 | |
| 77 | // Detects speech in the audio file |
| 78 | const [response] = await client.recognize(request); |
| 79 | const transcription = response.results |
| 80 | .map(result => result.alternatives[0].transcript) |
| 81 | .join('\n'); |
| 82 | console.log(`Transcription: ${transcription}`); |
| 83 | } |