Add dotenv

This commit is contained in:
Hongbo Wu 2022-08-26 16:30:09 +08:00
parent 5a1dbb594e
commit b2d49d1fbf
5 changed files with 52 additions and 21 deletions

View file

@ -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')
}

View file

@ -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<string> => {
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

View file

@ -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"

View file

@ -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<boolean> => {
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<TextToSpeechOutput> => {
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')
}
)

View file

@ -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"