From b2d49d1fbf1135b858c7d065c86e3deb3d4d6759 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 26 Aug 2022 16:30:09 +0800 Subject: [PATCH] Add dotenv --- packages/api/src/routers/article_router.ts | 9 ++++- packages/api/src/utils/createTask.ts | 13 +++++-- packages/text-to-speech/package.json | 1 + packages/text-to-speech/src/index.ts | 45 ++++++++++++++-------- yarn.lock | 5 +++ 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/packages/api/src/routers/article_router.ts b/packages/api/src/routers/article_router.ts index 7915abc4f..68e804071 100644 --- a/packages/api/src/routers/article_router.ts +++ b/packages/api/src/routers/article_router.ts @@ -153,7 +153,14 @@ export function articleRouter() { voice: voice || userPersonalization?.speechVoice || 'en-US-JennyNeural', }) // enqueue a task to convert text to speech - const taskName = await enqueueTextToSpeech(uid, speech.id) + const taskName = await enqueueTextToSpeech( + uid, + speech.id, + page.content, + 'ssml', + speech.voice, + env.fileUpload.gcsUploadBucket + ) logger.info('Start Text to speech task', { taskName }) res.status(202).send('Text to speech task started') } diff --git a/packages/api/src/utils/createTask.ts b/packages/api/src/utils/createTask.ts index fc6a858cb..ddc21be2b 100644 --- a/packages/api/src/utils/createTask.ts +++ b/packages/api/src/utils/createTask.ts @@ -330,12 +330,19 @@ export const enqueueSyncWithIntegration = async ( export const enqueueTextToSpeech = async ( userId: string, - speechId: string + speechId: string, + text: string, + textType: 'text' | 'ssml', + voice: string, + bucket: string ): Promise => { const { GOOGLE_CLOUD_PROJECT } = process.env const payload = { - userId, - speechId, + id: speechId, + text, + voice, + bucket, + textType, } // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore diff --git a/packages/text-to-speech/package.json b/packages/text-to-speech/package.json index 3e3901bc4..8743a9d88 100644 --- a/packages/text-to-speech/package.json +++ b/packages/text-to-speech/package.json @@ -28,6 +28,7 @@ "@google-cloud/storage": "^6.4.1", "@sentry/serverless": "^6.16.1", "axios": "^0.27.2", + "dotenv": "^16.0.1", "jsonwebtoken": "^8.5.1", "linkedom": "^0.14.12", "microsoft-cognitiveservices-speech-sdk": "^1.22.0" diff --git a/packages/text-to-speech/src/index.ts b/packages/text-to-speech/src/index.ts index ec3f8403a..73a9e6a58 100644 --- a/packages/text-to-speech/src/index.ts +++ b/packages/text-to-speech/src/index.ts @@ -17,6 +17,8 @@ import { } from 'microsoft-cognitiveservices-speech-sdk' import axios from 'axios' import * as jwt from 'jsonwebtoken' +import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import +dotenv.config() interface TextToSpeechInput { id: string @@ -60,9 +62,10 @@ const createGCSFile = (bucket: string, filename: string): File => { const updateSpeech = async ( speechId: string, - audioFileName: string, - speechMarksFileName: string, - token: string + token: string, + state: 'COMPLETED' | 'FAILED', + audioFileName?: string, + speechMarksFileName?: string ): Promise => { if (!process.env.REST_BACKEND_ENDPOINT) { throw new Error('backend rest api endpoint not exists') @@ -73,6 +76,7 @@ const updateSpeech = async ( speechId, audioFileName, speechMarksFileName, + state, } ) @@ -82,7 +86,7 @@ const updateSpeech = async ( const synthesizeTextToSpeech = async ( input: TextToSpeechInput ): Promise => { - if (!process.env.azureSpeechKey || !process.env.azureSpeechRegion) { + if (!process.env.AZURE_SPEECH_KEY || !process.env.AZURE_SPEECH_REGION) { throw new Error('Azure Speech Key or Region not set') } const audioFileName = `speech/${input.id}.mp3` @@ -91,8 +95,8 @@ const synthesizeTextToSpeech = async ( resumable: true, }) const speechConfig = SpeechConfig.fromSubscription( - process.env.azureSpeechKey, - process.env.azureSpeechRegion + process.env.AZURE_SPEECH_KEY, + process.env.AZURE_SPEECH_REGION ) const textType = input.textType || 'text' if (textType === 'text') { @@ -393,19 +397,26 @@ export const textToSpeechHandler = Sentry.GCPFunction.wrapHttpFunction( return res.status(200).send('UNAUTHENTICATED') } const input = req.body as TextToSpeechInput - const { audioFileName, speechMarksFileName } = await synthesizeTextToSpeech( - input - ) - const updated = await updateSpeech( - input.id, - audioFileName, - speechMarksFileName, - token - ) + try { + const { audioFileName, speechMarksFileName } = + await synthesizeTextToSpeech(input) + const updated = await updateSpeech( + input.id, + token, + 'COMPLETED', + audioFileName, + speechMarksFileName + ) - if (!updated) { - return res.status(500).send('Failed to update speech') + if (!updated) { + return res.status(500).send('Failed to update speech') + } + } catch (e) { + console.error(e) + await updateSpeech(input.id, token, 'FAILED') + return res.status(500).send('Failed to synthesize') } + res.send('OK') } ) diff --git a/yarn.lock b/yarn.lock index b8a67aeb9..6cbf70c34 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12474,6 +12474,11 @@ dotenv@^16.0.0: resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.0.tgz#c619001253be89ebb638d027b609c75c26e47411" integrity sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q== +dotenv@^16.0.1: + version "16.0.1" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.1.tgz#8f8f9d94876c35dac989876a5d3a82a267fdce1d" + integrity sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ== + dotenv@^8.0.0, dotenv@^8.2.0: version "8.6.0" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.6.0.tgz#061af664d19f7f4d8fc6e4ff9b584ce237adcb8b"