@aws-amplify/predictions 6.1.16-unstable.e28d538.0 → 6.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"AmazonAIConvertPredictionsProvider.mjs","sources":["../../../src/providers/AmazonAIConvertPredictionsProvider.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { Buffer } from 'buffer';\nimport { Amplify, ConsoleLogger, fetchAuthSession } from '@aws-amplify/core';\nimport { Category, PredictionsAction, Signer, getAmplifyUserAgentObject, } from '@aws-amplify/core/internals/utils';\nimport { PollyClient, SynthesizeSpeechCommand } from '@aws-sdk/client-polly';\nimport { TranslateClient, TranslateTextCommand, } from '@aws-sdk/client-translate';\nimport { EventStreamCodec, } from '@smithy/eventstream-codec';\nimport { fromUtf8, toUtf8 } from '@smithy/util-utf8';\nimport { PredictionsValidationErrorCode } from '../errors/types/validation';\nimport { assertValidationError } from '../errors/utils/assertValidationError';\nimport { isConvertBytesSource, isTextToSpeechInput, isTranslateTextInput, isValidConvertInput, } from '../types';\nconst logger = new ConsoleLogger('AmazonAIConvertPredictionsProvider');\nconst eventBuilder = new EventStreamCodec(toUtf8, fromUtf8);\nconst LANGUAGES_CODE_IN_8KHZ = ['fr-FR', 'en-AU', 'en-GB', 'fr-CA'];\nexport class AmazonAIConvertPredictionsProvider {\n constructor() {\n this.inputSampleRate = 44100;\n }\n getProviderName() {\n return 'AmazonAIConvertPredictionsProvider';\n }\n convert(input) {\n assertValidationError(isValidConvertInput(input), PredictionsValidationErrorCode.InvalidInput);\n if (isTranslateTextInput(input)) {\n logger.debug('translateText');\n return this.translateText(input);\n }\n else if (isTextToSpeechInput(input)) {\n logger.debug('textToSpeech');\n return this.convertTextToSpeech(input);\n }\n else {\n logger.debug('textToSpeech');\n return this.convertSpeechToText(input);\n }\n }\n async translateText(input) {\n logger.debug('Starting translation');\n const { translateText = {} } = Amplify.getConfig().Predictions?.convert ?? {};\n assertValidationError(!!translateText.region, PredictionsValidationErrorCode.NoRegion);\n const { defaults = {}, region } = translateText;\n const { credentials } = await fetchAuthSession();\n assertValidationError(!!credentials, PredictionsValidationErrorCode.NoCredentials);\n const { sourceLanguage, targetLanguage } = defaults;\n const sourceLanguageCode = input.translateText?.source?.language ?? sourceLanguage;\n const targetLanguageCode = input.translateText?.targetLanguage ?? targetLanguage;\n assertValidationError(!!sourceLanguageCode, PredictionsValidationErrorCode.NoSourceLanguage);\n assertValidationError(!!targetLanguageCode, PredictionsValidationErrorCode.NoTargetLanguage);\n this.translateClient = new TranslateClient({\n region,\n credentials,\n customUserAgent: getAmplifyUserAgentObject({\n category: Category.Predictions,\n action: PredictionsAction.Convert,\n }),\n });\n const translateTextCommand = new TranslateTextCommand({\n SourceLanguageCode: sourceLanguageCode,\n TargetLanguageCode: targetLanguageCode,\n Text: input.translateText?.source?.text,\n });\n const data = await this.translateClient.send(translateTextCommand);\n return {\n text: data.TranslatedText,\n language: data.TargetLanguageCode,\n };\n }\n async convertTextToSpeech(input) {\n const { credentials } = await fetchAuthSession();\n assertValidationError(!!credentials, PredictionsValidationErrorCode.NoCredentials);\n assertValidationError(!!input.textToSpeech?.source, PredictionsValidationErrorCode.NoSource);\n const { speechGenerator } = Amplify.getConfig().Predictions?.convert ?? {};\n assertValidationError(!!speechGenerator?.region, PredictionsValidationErrorCode.NoRegion);\n const { defaults = {}, region } = speechGenerator;\n const { voiceId: defaultVoiceId } = defaults;\n const voiceId = input.textToSpeech?.voiceId ?? defaultVoiceId;\n assertValidationError(!!voiceId, PredictionsValidationErrorCode.NoVoiceId);\n this.pollyClient = new PollyClient({\n region,\n credentials,\n customUserAgent: getAmplifyUserAgentObject({\n category: Category.Predictions,\n action: PredictionsAction.Convert,\n }),\n });\n const synthesizeSpeechCommand = new SynthesizeSpeechCommand({\n OutputFormat: 'mp3',\n Text: input.textToSpeech?.source?.text,\n VoiceId: voiceId,\n TextType: 'text',\n SampleRate: '24000',\n });\n const data = await this.pollyClient.send(synthesizeSpeechCommand);\n const response = new Response(data.AudioStream);\n const arrayBuffer = await response.arrayBuffer();\n const blob = new Blob([arrayBuffer], {\n type: data.ContentType,\n });\n const url = URL.createObjectURL(blob);\n return {\n speech: { url },\n audioStream: arrayBuffer,\n text: input.textToSpeech?.source?.text,\n };\n }\n async convertSpeechToText(input) {\n logger.debug('starting transcription..');\n const { credentials } = await fetchAuthSession();\n assertValidationError(!!credentials, PredictionsValidationErrorCode.NoCredentials);\n const { transcription } = Amplify.getConfig().Predictions?.convert ?? {};\n assertValidationError(!!transcription?.region, PredictionsValidationErrorCode.NoRegion);\n const { defaults, region } = transcription;\n const language = input.transcription?.language ?? defaults?.language;\n assertValidationError(!!language, PredictionsValidationErrorCode.NoLanguage);\n const source = input.transcription?.source;\n assertValidationError(isConvertBytesSource(source), PredictionsValidationErrorCode.InvalidSource);\n const connection = await this.openConnectionWithTranscribe({\n credentials,\n region,\n languageCode: language,\n });\n const fullText = await this.sendDataToTranscribe({\n connection,\n raw: source.bytes,\n languageCode: language,\n });\n return {\n transcription: {\n fullText,\n },\n };\n }\n static serializeDataFromTranscribe(message) {\n let decodedMessage = '';\n const transcribeMessage = eventBuilder.decode(Buffer.from(message.data));\n const transcribeMessageJson = JSON.parse(toUtf8(transcribeMessage.body));\n if (transcribeMessage.headers[':message-type'].value === 'exception') {\n logger.debug('exception', JSON.stringify(transcribeMessageJson.Message, null, 2));\n throw new Error(transcribeMessageJson.Message);\n }\n else if (transcribeMessage.headers[':message-type'].value === 'event') {\n if (transcribeMessageJson.Transcript.Results.length > 0) {\n if (transcribeMessageJson.Transcript.Results[0].Alternatives.length > 0) {\n if (transcribeMessageJson.Transcript.Results[0].Alternatives[0]\n .Transcript.length > 0) {\n if (transcribeMessageJson.Transcript.Results[0].IsPartial === false) {\n decodedMessage =\n transcribeMessageJson.Transcript.Results[0].Alternatives[0]\n .Transcript + '\\n';\n logger.debug({ decodedMessage });\n }\n else {\n logger.debug({\n transcript: transcribeMessageJson.Transcript.Results[0].Alternatives[0],\n });\n }\n }\n }\n }\n }\n return decodedMessage;\n }\n sendDataToTranscribe({ connection, raw, languageCode, }) {\n return new Promise((resolve, reject) => {\n let fullText = '';\n connection.onmessage = message => {\n try {\n const decodedMessage = AmazonAIConvertPredictionsProvider.serializeDataFromTranscribe(message);\n if (decodedMessage) {\n fullText += decodedMessage + ' ';\n }\n }\n catch (err) {\n logger.debug(err);\n reject(err);\n }\n };\n connection.onerror = errorEvent => {\n logger.debug({ errorEvent });\n reject(new Error('failed to transcribe, network error'));\n };\n connection.onclose = closeEvent => {\n logger.debug({ closeEvent });\n resolve(fullText.trim());\n };\n logger.debug({ raw });\n if (Array.isArray(raw)) {\n for (let i = 0; i < raw.length - 1023; i += 1024) {\n const data = raw.slice(i, i + 1024);\n this.sendEncodedDataToTranscribe(connection, data, languageCode);\n }\n }\n else {\n // If Buffer\n this.sendEncodedDataToTranscribe(connection, raw, languageCode);\n }\n // sending end frame\n const endFrameEventMessage = this.getAudioEventMessage(Buffer.from([]));\n const endFrameBinary = eventBuilder.encode(endFrameEventMessage);\n connection.send(endFrameBinary);\n });\n }\n sendEncodedDataToTranscribe(connection, data, languageCode) {\n const downsampledBuffer = this.downsampleBuffer({\n buffer: data,\n outputSampleRate: LANGUAGES_CODE_IN_8KHZ.includes(languageCode)\n ? 8000\n : 16000,\n });\n const pcmEncodedBuffer = this.pcmEncode(downsampledBuffer);\n const audioEventMessage = this.getAudioEventMessage(Buffer.from(pcmEncodedBuffer));\n const binary = eventBuilder.encode(audioEventMessage);\n connection.send(binary);\n }\n getAudioEventMessage(buffer) {\n const audioEventMessage = {\n body: buffer,\n headers: {\n ':message-type': {\n type: 'string',\n value: 'event',\n },\n ':event-type': {\n type: 'string',\n value: 'AudioEvent',\n },\n },\n };\n return audioEventMessage;\n }\n pcmEncode(input) {\n let offset = 0;\n // ArrayBuffer cannot be processed using length property\n if (input instanceof ArrayBuffer) {\n return input;\n }\n const buffer = new ArrayBuffer(input.length * 2);\n const view = new DataView(buffer);\n for (let i = 0; i < input.length; i++, offset += 2) {\n const s = Math.max(-1, Math.min(1, input[i]));\n view.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true);\n }\n return buffer;\n }\n downsampleBuffer({ buffer, outputSampleRate = 16000, }) {\n // Cannot process ArrayBuffer using length property\n if (outputSampleRate === this.inputSampleRate ||\n buffer instanceof ArrayBuffer) {\n return buffer;\n }\n const sampleRateRatio = this.inputSampleRate / outputSampleRate;\n const newLength = Math.round(buffer.length / sampleRateRatio);\n const result = new Float32Array(newLength);\n let offsetResult = 0;\n let offsetBuffer = 0;\n while (offsetResult < result.length) {\n const nextOffsetBuffer = Math.round((offsetResult + 1) * sampleRateRatio);\n let accum = 0;\n let count = 0;\n for (let i = offsetBuffer; i < nextOffsetBuffer && i < buffer.length; i++) {\n accum += buffer[i];\n count++;\n }\n result[offsetResult] = accum / count;\n offsetResult++;\n offsetBuffer = nextOffsetBuffer;\n }\n return result;\n }\n openConnectionWithTranscribe({ credentials, region, languageCode, }) {\n return new Promise((resolve, _reject) => {\n const signedUrl = this.generateTranscribeUrl({\n credentials,\n region,\n languageCode,\n });\n logger.debug('connecting...');\n const connection = new WebSocket(signedUrl);\n connection.binaryType = 'arraybuffer';\n connection.onopen = () => {\n logger.debug('connected');\n resolve(connection);\n };\n });\n }\n generateTranscribeUrl({ credentials: { accessKeyId, secretAccessKey, sessionToken }, region, languageCode, }) {\n const credentials = {\n access_key: accessKeyId,\n secret_key: secretAccessKey,\n session_token: sessionToken,\n };\n const url = [\n `wss://transcribestreaming.${region}.amazonaws.com:8443`,\n '/stream-transcription-websocket?',\n `media-encoding=pcm&`,\n `sample-rate=${LANGUAGES_CODE_IN_8KHZ.includes(languageCode) ? '8000' : '16000'}&`,\n `language-code=${languageCode}`,\n ].join('');\n const signedUrl = Signer.signUrl(url, credentials, { region, service: 'transcribe' }, 300);\n return signedU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C,YAAY,UAAU,EAAE,eAAe;AACvC,YAAY,aAAa,EAAE,YAAY;AACvC,SAAS,CAAC;AACV,QAAQ,MAAM,GAAG,GAAG;AACpB,YAAY,CAAC,0BAA0B,EAAE,MAAM,CAAC,mBAAmB,CAAC;AACpE,YAAY,kCAAkC;AAC9C,YAAY,CAAC,mBAAmB,CAAC;AACjC,YAAY,CAAC,YAAY,EAAE,sBAAsB,CAAC,QAAQ,CAAC,YAAY,CAAC,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;AAC9F,YAAY,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;AAC3C,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACnB,QAAQ,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,WAAW,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,EAAE,GAAG,CAAC,CAAC;AACnG,QAAQ,OAAO,SAAS,CAAC;AACzB,KAAK;AACL;;;;"}
1
+ {"version":3,"file":"AmazonAIConvertPredictionsProvider.mjs","sources":["../../../src/providers/AmazonAIConvertPredictionsProvider.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { Buffer } from 'buffer';\nimport { Amplify, ConsoleLogger, fetchAuthSession } from '@aws-amplify/core';\nimport { Category, PredictionsAction, Signer, getAmplifyUserAgentObject, } from '@aws-amplify/core/internals/utils';\nimport { PollyClient, SynthesizeSpeechCommand, } from '@aws-sdk/client-polly';\nimport { TranslateClient, TranslateTextCommand, } from '@aws-sdk/client-translate';\nimport { EventStreamCodec, } from '@smithy/eventstream-codec';\nimport { fromUtf8, toUtf8 } from '@smithy/util-utf8';\nimport { PredictionsValidationErrorCode } from '../errors/types/validation';\nimport { assertValidationError } from '../errors/utils/assertValidationError';\nimport { isConvertBytesSource, isTextToSpeechInput, isTranslateTextInput, isValidConvertInput, } from '../types';\nconst logger = new ConsoleLogger('AmazonAIConvertPredictionsProvider');\nconst eventBuilder = new EventStreamCodec(toUtf8, fromUtf8);\nconst LANGUAGES_CODE_IN_8KHZ = ['fr-FR', 'en-AU', 'en-GB', 'fr-CA'];\nexport class AmazonAIConvertPredictionsProvider {\n constructor() {\n this.inputSampleRate = 44100;\n }\n getProviderName() {\n return 'AmazonAIConvertPredictionsProvider';\n }\n convert(input) {\n assertValidationError(isValidConvertInput(input), PredictionsValidationErrorCode.InvalidInput);\n if (isTranslateTextInput(input)) {\n logger.debug('translateText');\n return this.translateText(input);\n }\n else if (isTextToSpeechInput(input)) {\n logger.debug('textToSpeech');\n return this.convertTextToSpeech(input);\n }\n else {\n logger.debug('textToSpeech');\n return this.convertSpeechToText(input);\n }\n }\n async translateText(input) {\n logger.debug('Starting translation');\n const { translateText = {} } = Amplify.getConfig().Predictions?.convert ?? {};\n assertValidationError(!!translateText.region, PredictionsValidationErrorCode.NoRegion);\n const { defaults = {}, region } = translateText;\n const { credentials } = await fetchAuthSession();\n assertValidationError(!!credentials, PredictionsValidationErrorCode.NoCredentials);\n const { sourceLanguage, targetLanguage } = defaults;\n const sourceLanguageCode = input.translateText?.source?.language ?? sourceLanguage;\n const targetLanguageCode = input.translateText?.targetLanguage ?? targetLanguage;\n assertValidationError(!!sourceLanguageCode, PredictionsValidationErrorCode.NoSourceLanguage);\n assertValidationError(!!targetLanguageCode, PredictionsValidationErrorCode.NoTargetLanguage);\n this.translateClient = new TranslateClient({\n region,\n credentials,\n customUserAgent: getAmplifyUserAgentObject({\n category: Category.Predictions,\n action: PredictionsAction.Convert,\n }),\n });\n const translateTextCommand = new TranslateTextCommand({\n SourceLanguageCode: sourceLanguageCode,\n TargetLanguageCode: targetLanguageCode,\n Text: input.translateText?.source?.text,\n });\n const data = await this.translateClient.send(translateTextCommand);\n return {\n text: data.TranslatedText,\n language: data.TargetLanguageCode,\n };\n }\n async convertTextToSpeech(input) {\n const { credentials } = await fetchAuthSession();\n assertValidationError(!!credentials, PredictionsValidationErrorCode.NoCredentials);\n assertValidationError(!!input.textToSpeech?.source, PredictionsValidationErrorCode.NoSource);\n const { speechGenerator } = Amplify.getConfig().Predictions?.convert ?? {};\n assertValidationError(!!speechGenerator?.region, PredictionsValidationErrorCode.NoRegion);\n const { defaults = {}, region } = speechGenerator;\n const { voiceId: defaultVoiceId } = defaults;\n const voiceId = input.textToSpeech?.voiceId ?? defaultVoiceId;\n assertValidationError(!!voiceId, PredictionsValidationErrorCode.NoVoiceId);\n this.pollyClient = new PollyClient({\n region,\n credentials,\n customUserAgent: getAmplifyUserAgentObject({\n category: Category.Predictions,\n action: PredictionsAction.Convert,\n }),\n });\n const synthesizeSpeechCommand = new SynthesizeSpeechCommand({\n OutputFormat: 'mp3',\n Text: input.textToSpeech?.source?.text,\n VoiceId: voiceId,\n TextType: 'text',\n SampleRate: '24000',\n });\n const data = await this.pollyClient.send(synthesizeSpeechCommand);\n const response = new Response(data.AudioStream);\n const arrayBuffer = await response.arrayBuffer();\n const blob = new Blob([arrayBuffer], {\n type: data.ContentType,\n });\n const url = URL.createObjectURL(blob);\n return {\n speech: { url },\n audioStream: arrayBuffer,\n text: input.textToSpeech?.source?.text,\n };\n }\n async convertSpeechToText(input) {\n logger.debug('starting transcription..');\n const { credentials } = await fetchAuthSession();\n assertValidationError(!!credentials, PredictionsValidationErrorCode.NoCredentials);\n const { transcription } = Amplify.getConfig().Predictions?.convert ?? {};\n assertValidationError(!!transcription?.region, PredictionsValidationErrorCode.NoRegion);\n const { defaults, region } = transcription;\n const language = input.transcription?.language ?? defaults?.language;\n assertValidationError(!!language, PredictionsValidationErrorCode.NoLanguage);\n const source = input.transcription?.source;\n assertValidationError(isConvertBytesSource(source), PredictionsValidationErrorCode.InvalidSource);\n const connection = await this.openConnectionWithTranscribe({\n credentials,\n region,\n languageCode: language,\n });\n const fullText = await this.sendDataToTranscribe({\n connection,\n raw: source.bytes,\n languageCode: language,\n });\n return {\n transcription: {\n fullText,\n },\n };\n }\n static serializeDataFromTranscribe(message) {\n let decodedMessage = '';\n const transcribeMessage = eventBuilder.decode(Buffer.from(message.data));\n const transcribeMessageJson = JSON.parse(toUtf8(transcribeMessage.body));\n if (transcribeMessage.headers[':message-type'].value === 'exception') {\n logger.debug('exception', JSON.stringify(transcribeMessageJson.Message, null, 2));\n throw new Error(transcribeMessageJson.Message);\n }\n else if (transcribeMessage.headers[':message-type'].value === 'event') {\n if (transcribeMessageJson.Transcript.Results.length > 0) {\n if (transcribeMessageJson.Transcript.Results[0].Alternatives.length > 0) {\n if (transcribeMessageJson.Transcript.Results[0].Alternatives[0]\n .Transcript.length > 0) {\n if (transcribeMessageJson.Transcript.Results[0].IsPartial === false) {\n decodedMessage =\n transcribeMessageJson.Transcript.Results[0].Alternatives[0]\n .Transcript + '\\n';\n logger.debug({ decodedMessage });\n }\n else {\n logger.debug({\n transcript: transcribeMessageJson.Transcript.Results[0].Alternatives[0],\n });\n }\n }\n }\n }\n }\n return decodedMessage;\n }\n sendDataToTranscribe({ connection, raw, languageCode, }) {\n return new Promise((resolve, reject) => {\n let fullText = '';\n connection.onmessage = message => {\n try {\n const decodedMessage = AmazonAIConvertPredictionsProvider.serializeDataFromTranscribe(message);\n if (decodedMessage) {\n fullText += decodedMessage + ' ';\n }\n }\n catch (err) {\n logger.debug(err);\n reject(err);\n }\n };\n connection.onerror = errorEvent => {\n logger.debug({ errorEvent });\n reject(new Error('failed to transcribe, network error'));\n };\n connection.onclose = closeEvent => {\n logger.debug({ closeEvent });\n resolve(fullText.trim());\n };\n logger.debug({ raw });\n if (Array.isArray(raw)) {\n for (let i = 0; i < raw.length - 1023; i += 1024) {\n const data = raw.slice(i, i + 1024);\n this.sendEncodedDataToTranscribe(connection, data, languageCode);\n }\n }\n else {\n // If Buffer\n this.sendEncodedDataToTranscribe(connection, raw, languageCode);\n }\n // sending end frame\n const endFrameEventMessage = this.getAudioEventMessage(Buffer.from([]));\n const endFrameBinary = eventBuilder.encode(endFrameEventMessage);\n connection.send(endFrameBinary);\n });\n }\n sendEncodedDataToTranscribe(connection, data, languageCode) {\n const downsampledBuffer = this.downsampleBuffer({\n buffer: data,\n outputSampleRate: LANGUAGES_CODE_IN_8KHZ.includes(languageCode)\n ? 8000\n : 16000,\n });\n const pcmEncodedBuffer = this.pcmEncode(downsampledBuffer);\n const audioEventMessage = this.getAudioEventMessage(Buffer.from(pcmEncodedBuffer));\n const binary = eventBuilder.encode(audioEventMessage);\n connection.send(binary);\n }\n getAudioEventMessage(buffer) {\n const audioEventMessage = {\n body: buffer,\n headers: {\n ':message-type': {\n type: 'string',\n value: 'event',\n },\n ':event-type': {\n type: 'string',\n value: 'AudioEvent',\n },\n },\n };\n return audioEventMessage;\n }\n pcmEncode(input) {\n let offset = 0;\n // ArrayBuffer cannot be processed using length property\n if (input instanceof ArrayBuffer) {\n return input;\n }\n const buffer = new ArrayBuffer(input.length * 2);\n const view = new DataView(buffer);\n for (let i = 0; i < input.length; i++, offset += 2) {\n const s = Math.max(-1, Math.min(1, input[i]));\n view.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true);\n }\n return buffer;\n }\n downsampleBuffer({ buffer, outputSampleRate = 16000, }) {\n // Cannot process ArrayBuffer using length property\n if (outputSampleRate === this.inputSampleRate ||\n buffer instanceof ArrayBuffer) {\n return buffer;\n }\n const sampleRateRatio = this.inputSampleRate / outputSampleRate;\n const newLength = Math.round(buffer.length / sampleRateRatio);\n const result = new Float32Array(newLength);\n let offsetResult = 0;\n let offsetBuffer = 0;\n while (offsetResult < result.length) {\n const nextOffsetBuffer = Math.round((offsetResult + 1) * sampleRateRatio);\n let accum = 0;\n let count = 0;\n for (let i = offsetBuffer; i < nextOffsetBuffer && i < buffer.length; i++) {\n accum += buffer[i];\n count++;\n }\n result[offsetResult] = accum / count;\n offsetResult++;\n offsetBuffer = nextOffsetBuffer;\n }\n return result;\n }\n openConnectionWithTranscribe({ credentials, region, languageCode, }) {\n return new Promise((resolve, _reject) => {\n const signedUrl = this.generateTranscribeUrl({\n credentials,\n region,\n languageCode,\n });\n logger.debug('connecting...');\n const connection = new WebSocket(signedUrl);\n connection.binaryType = 'arraybuffer';\n connection.onopen = () => {\n logger.debug('connected');\n resolve(connection);\n };\n });\n }\n generateTranscribeUrl({ credentials: { accessKeyId, secretAccessKey, sessionToken }, region, languageCode, }) {\n const credentials = {\n access_key: accessKeyId,\n secret_key: secretAccessKey,\n session_token: sessionToken,\n };\n const url = [\n `wss://transcribestreaming.${region}.amazonaws.com:8443`,\n '/stream-transcription-websocket?',\n `media-encoding=pcm&`,\n `sample-rate=${LANGUAGES_CODE_IN_8KHZ.includes(languageCode) ? '8000' : '16000'}&`,\n `language-code=${languageCode}`,\n ].join('');\n const signedUrl = Signer.signUrl(url, credentials, { region, service: 'transcribe' }, 300);\n return signedUrl;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;AAAA;AACA;AAWA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,oCAAoC,CAAC,CAAC;AACvE,MAAM,YAAY,GAAG,IAAI,gBAAgB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AAC5D,MAAM,sBAAsB,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;AAC7D,MAAM,kCAAkC,CAAC;AAChD,IAAI,WAAW,GAAG;AAClB,QAAQ,IAAI,CAAC,eAAe,GAAG,KAAK,CAAC;AACrC,KAAK;AACL,IAAI,eAAe,GAAG;AACtB,QAAQ,OAAO,oCAAoC,CAAC;AACpD,KAAK;AACL,IAAI,OAAO,CAAC,KAAK,EAAE;AACnB,QAAQ,qBAAqB,CAAC,mBAAmB,CAAC,KAAK,CAAC,EAAE,8BAA8B,CAAC,YAAY,CAAC,CAAC;AACvG,QAAQ,IAAI,oBAAoB,CAAC,KAAK,CAAC,EAAE;AACzC,YAAY,MAAM,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;AAC1C,YAAY,OAAO,IAAI,CAAC,aAAa,CAAC,KAAK,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learning category of aws-amplify",
5
- "main": "./dist/cjs/index.js",
6
- "module": "./dist/esm/index.mjs",
7
- "typings": "./dist/esm/index.d.ts",
8
- "react-native": "./src/index.ts",
9
- "sideEffects": [
10
- "./dist/cjs/Predictions.js",
11
- "./dist/esm/Predictions.mjs"
12
- ],
13
- "scripts": {
14
- "test": "npm run lint && jest -w 1 --coverage --logHeapUsage",
15
- "test:size": "size-limit",
16
- "build-with-test": "npm run clean && npm run build",
17
- "build:umd": "webpack && webpack --config ./webpack.config.dev.js",
18
- "build:esm-cjs": "rollup --forceExit -c rollup.config.mjs",
19
- "build:watch": "npm run build:esm-cjs -- --watch",
20
- "build": "npm run clean && npm run build:esm-cjs && npm run build:umd",
21
- "clean": "npm run clean:size && rimraf dist lib lib-esm",
22
- "clean:size": "rimraf dual-publish-tmp tmp*",
23
- "format": "echo \"Not implemented\"",
24
- "lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage",
25
- "lint:fix": "eslint '**/*.{ts,tsx}' --fix",
26
- "generate-docs-local": "typedoc --out docs src",
27
- "generate-docs-root": "typedoc --out ../../docs src",
28
- "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 87.84"
29
- },
30
- "repository": {
31
- "type": "git",
32
- "url": "https://github.com/aws-amplify/amplify-js.git"
33
- },
34
- "author": "Amazon Web Services",
35
- "license": "Apache-2.0",
36
- "bugs": {
37
- "url": "https://github.com/aws/aws-amplify/issues"
38
- },
39
- "homepage": "https://aws-amplify.github.io/",
40
- "files": [
41
- "dist/cjs",
42
- "dist/esm",
43
- "src"
44
- ],
45
- "dependencies": {
46
- "@aws-amplify/storage": "6.5.4-unstable.e28d538.0+e28d538",
47
- "@aws-sdk/client-comprehend": "3.398.0",
48
- "@aws-sdk/client-polly": "3.398.0",
49
- "@aws-sdk/client-rekognition": "3.398.0",
50
- "@aws-sdk/client-textract": "3.398.0",
51
- "@aws-sdk/client-translate": "3.398.0",
52
- "@smithy/eventstream-codec": "2.0.9",
53
- "@smithy/util-utf8": "2.0.0",
54
- "buffer": "4.9.2",
55
- "tslib": "^2.5.0",
56
- "uuid": "^9.0.0"
57
- },
58
- "peerDependencies": {
59
- "@aws-amplify/core": "6.3.8-unstable.e28d538.0+e28d538"
60
- },
61
- "devDependencies": {
62
- "@aws-amplify/core": "6.3.8-unstable.e28d538.0+e28d538",
63
- "typescript": "5.0.2"
64
- },
65
- "size-limit": [
66
- {
67
- "name": "Predictions",
68
- "path": "./dist/esm/index.mjs",
69
- "import": "{ Predictions }",
70
- "limit": "69.8 kB"
71
- }
72
- ],
73
- "gitHead": "e28d53819793b281493d79a2851740ddaf383d9f"
2
+ "name": "@aws-amplify/predictions",
3
+ "version": "6.1.16",
4
+ "description": "Machine learning category of aws-amplify",
5
+ "main": "./dist/cjs/index.js",
6
+ "module": "./dist/esm/index.mjs",
7
+ "typings": "./dist/esm/index.d.ts",
8
+ "react-native": "./src/index.ts",
9
+ "sideEffects": [
10
+ "./dist/cjs/Predictions.js",
11
+ "./dist/esm/Predictions.mjs"
12
+ ],
13
+ "scripts": {
14
+ "test": "npm run lint && jest -w 1 --coverage --logHeapUsage",
15
+ "test:size": "size-limit",
16
+ "build-with-test": "npm run clean && npm run build",
17
+ "build:umd": "webpack && webpack --config ./webpack.config.dev.js",
18
+ "build:esm-cjs": "rollup --forceExit -c rollup.config.mjs",
19
+ "build:watch": "npm run build:esm-cjs -- --watch",
20
+ "build": "npm run clean && npm run build:esm-cjs && npm run build:umd",
21
+ "clean": "npm run clean:size && rimraf dist lib lib-esm",
22
+ "clean:size": "rimraf dual-publish-tmp tmp*",
23
+ "format": "echo \"Not implemented\"",
24
+ "lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage",
25
+ "lint:fix": "eslint '**/*.{ts,tsx}' --fix",
26
+ "generate-docs-local": "typedoc --out docs src",
27
+ "generate-docs-root": "typedoc --out ../../docs src",
28
+ "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 87.84"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/aws-amplify/amplify-js.git"
33
+ },
34
+ "author": "Amazon Web Services",
35
+ "license": "Apache-2.0",
36
+ "bugs": {
37
+ "url": "https://github.com/aws/aws-amplify/issues"
38
+ },
39
+ "homepage": "https://aws-amplify.github.io/",
40
+ "files": [
41
+ "dist/cjs",
42
+ "dist/esm",
43
+ "src"
44
+ ],
45
+ "dependencies": {
46
+ "@aws-amplify/storage": "6.5.4",
47
+ "@aws-sdk/client-comprehend": "3.621.0",
48
+ "@aws-sdk/client-polly": "3.621.0",
49
+ "@aws-sdk/client-rekognition": "3.621.0",
50
+ "@aws-sdk/client-textract": "3.621.0",
51
+ "@aws-sdk/client-translate": "3.621.0",
52
+ "@smithy/eventstream-codec": "2.0.9",
53
+ "@smithy/util-utf8": "2.0.0",
54
+ "buffer": "4.9.2",
55
+ "tslib": "^2.5.0",
56
+ "uuid": "^9.0.0"
57
+ },
58
+ "peerDependencies": {
59
+ "@aws-amplify/core": "^6.1.0"
60
+ },
61
+ "devDependencies": {
62
+ "@aws-amplify/core": "6.3.8",
63
+ "typescript": "5.0.2"
64
+ },
65
+ "size-limit": [
66
+ {
67
+ "name": "Predictions",
68
+ "path": "./dist/esm/index.mjs",
69
+ "import": "{ Predictions }",
70
+ "limit": "69.8 kB"
71
+ }
72
+ ],
73
+ "gitHead": "3624556dd68af6b538430c5db76d883e656188dc"
74
74
  }
@@ -10,7 +10,11 @@ import {
10
10
  Signer,
11
11
  getAmplifyUserAgentObject,
12
12
  } from '@aws-amplify/core/internals/utils';
13
- import { PollyClient, SynthesizeSpeechCommand } from '@aws-sdk/client-polly';
13
+ import {
14
+ PollyClient,
15
+ SynthesizeSpeechCommand,
16
+ SynthesizeSpeechInput,
17
+ } from '@aws-sdk/client-polly';
14
18
  import {
15
19
  TranslateClient,
16
20
  TranslateTextCommand,
@@ -164,7 +168,7 @@ export class AmazonAIConvertPredictionsProvider {
164
168
  const synthesizeSpeechCommand = new SynthesizeSpeechCommand({
165
169
  OutputFormat: 'mp3',
166
170
  Text: input.textToSpeech?.source?.text,
167
- VoiceId: voiceId,
171
+ VoiceId: voiceId as SynthesizeSpeechInput['VoiceId'],
168
172
  TextType: 'text',
169
173
  SampleRate: '24000',
170
174
  });
@@ -9,6 +9,7 @@ import {
9
9
  import { getUrl } from '@aws-amplify/storage';
10
10
  import {
11
11
  DetectFacesCommand,
12
+ DetectFacesCommandInput,
12
13
  DetectLabelsCommand,
13
14
  DetectLabelsCommandInput,
14
15
  DetectModerationLabelsCommand,
@@ -238,7 +239,8 @@ export class AmazonAIIdentifyPredictionsProvider {
238
239
  } else {
239
240
  const param: AnalyzeDocumentCommandInput = {
240
241
  Document: inputDocument,
241
- FeatureTypes: featureTypes,
242
+ FeatureTypes:
243
+ featureTypes as AnalyzeDocumentCommandInput['FeatureTypes'],
242
244
  };
243
245
 
244
246
  const analyzeDocumentCommand = new AnalyzeDocumentCommand(param);
@@ -448,7 +450,9 @@ export class AmazonAIIdentifyPredictionsProvider {
448
450
 
449
451
  return { entities: faces };
450
452
  } else {
451
- const detectFacesCommand = new DetectFacesCommand(param);
453
+ const detectFacesCommand = new DetectFacesCommand(
454
+ param as DetectFacesCommandInput,
455
+ );
452
456
  const data = await this.rekognitionClient.send(detectFacesCommand);
453
457
  const faces =
454
458
  data.FaceDetails?.map(detail => {
@@ -10,9 +10,13 @@ import {
10
10
  ComprehendClient,
11
11
  DetectDominantLanguageCommand,
12
12
  DetectEntitiesCommand,
13
+ DetectEntitiesCommandInput,
13
14
  DetectKeyPhrasesCommand,
15
+ DetectKeyPhrasesCommandInput,
14
16
  DetectSentimentCommand,
17
+ DetectSentimentCommandInput,
15
18
  DetectSyntaxCommand,
19
+ DetectSyntaxCommandInput,
16
20
  Entity,
17
21
  SyntaxToken,
18
22
  } from '@aws-sdk/client-comprehend';
@@ -159,7 +163,9 @@ export class AmazonAIInterpretPredictionsProvider {
159
163
 
160
164
  private async detectKeyPhrases(params: DetectParams): Promise<KeyPhrases> {
161
165
  try {
162
- const detectKeyPhrasesCommand = new DetectKeyPhrasesCommand(params);
166
+ const detectKeyPhrasesCommand = new DetectKeyPhrasesCommand(
167
+ params as DetectKeyPhrasesCommandInput,
168
+ );
163
169
  const data = await this.comprehendClient!.send(detectKeyPhrasesCommand);
164
170
  const { KeyPhrases: keyPhrases = [] } = data || {};
165
171
 
@@ -180,7 +186,9 @@ export class AmazonAIInterpretPredictionsProvider {
180
186
 
181
187
  private async detectSyntax(params: DetectParams): Promise<TextSyntax[]> {
182
188
  try {
183
- const detectSyntaxCommand = new DetectSyntaxCommand(params);
189
+ const detectSyntaxCommand = new DetectSyntaxCommand(
190
+ params as DetectSyntaxCommandInput,
191
+ );
184
192
  const data = await this.comprehendClient!.send(detectSyntaxCommand);
185
193
  const { SyntaxTokens = [] } = data || {};
186
194
 
@@ -212,7 +220,9 @@ export class AmazonAIInterpretPredictionsProvider {
212
220
 
213
221
  private async detectSentiment(params: DetectParams): Promise<TextSentiment> {
214
222
  try {
215
- const detectSentimentCommand = new DetectSentimentCommand(params);
223
+ const detectSentimentCommand = new DetectSentimentCommand(
224
+ params as DetectSentimentCommandInput,
225
+ );
216
226
  const data = await this.comprehendClient!.send(detectSentimentCommand);
217
227
  const {
218
228
  Sentiment: predominant = '',
@@ -239,7 +249,9 @@ export class AmazonAIInterpretPredictionsProvider {
239
249
 
240
250
  private async detectEntities(params: DetectParams): Promise<TextEntities[]> {
241
251
  try {
242
- const detectEntitiesCommand = new DetectEntitiesCommand(params);
252
+ const detectEntitiesCommand = new DetectEntitiesCommand(
253
+ params as DetectEntitiesCommandInput,
254
+ );
243
255
  const data = await this.comprehendClient!.send(detectEntitiesCommand);
244
256
  const { Entities = [] } = data || {};
245
257