Create speech while requesting

This commit is contained in:
Hongbo Wu
2022-08-17 10:51:39 +08:00
parent 301746d494
commit b0bf4fc5ce
4 changed files with 45 additions and 8 deletions

View File

@ -17,6 +17,10 @@ import { env } from '../env'
import { Claims } from '../resolvers/types'
import { getRepository } from '../entity/utils'
import { Speech } from '../entity/speech'
import { getPageById } from '../elastic/pages'
import { parseHTML } from 'linkedom'
import { synthesizeTextToSpeech } from '../utils/textToSpeech'
import { UserPersonalization } from '../entity/user_personalization'
const logger = buildLogger('app.dispatch')
@ -87,15 +91,40 @@ export function articleRouter() {
},
})
const speech = await getRepository(Speech).findOneBy({
elasticPageId: id,
logger.debug('Text to speech request', { articleId: id })
const userPersonalization = await getRepository(
UserPersonalization
).findOneBy({
user: { id: uid },
})
if (!speech) {
return res.status(404).send({ errorCode: 'NOT_FOUND' })
if (!userPersonalization) {
return res.status(200).send('userPersonalization not found')
}
const page = await getPageById(id)
if (!page) {
return res.status(200).send('Page not found')
}
const text = parseHTML(page.content).document.documentElement.textContent
if (!text) {
return res.status(200).send('Page has no text')
}
const speech = await synthesizeTextToSpeech({
id,
text,
languageCode: page.language,
voice: userPersonalization.speechVoice,
})
await getRepository(Speech).save({
elasticPageId: id,
audioUrl: speech.audioUrl,
speechMarks: JSON.stringify(speech.speechMarks),
user: { id: uid },
})
logger.info('Found speech mp3', { audioUrl: speech.audioUrl })
res.redirect(speech.audioUrl)
}

View File

@ -36,7 +36,7 @@ export function speechServiceRouter() {
return res.status(200).send('Page not found')
}
const text = parseHTML(page.content).document.textContent
const text = parseHTML(page.content).document.documentElement.textContent
if (!text) {
return res.status(200).send('Page has no text')
}

View File

@ -120,7 +120,7 @@ export const createApp = (): {
app.use('/svc/pubsub/integrations', integrationsServiceRouter())
app.use('/svc/reminders', remindersServiceRouter())
app.use('/svc/pdf-attachments', pdfAttachmentsRouter())
app.use('/svc/speech', speechServiceRouter())
app.use('/svc/text-to-speech', speechServiceRouter())
if (env.dev.isLocal) {
app.use('/local/debug', localDebugRouter())

View File

@ -337,7 +337,15 @@ export const enqueueTextToSpeech = async (
// If there is no Google Cloud Project Id exposed, it means that we are in local environment
if (env.dev.isLocal || !GOOGLE_CLOUD_PROJECT) {
return nanoid()
// Calling the handler function directly.
setTimeout(() => {
axios
.post(env.queue.textToSpeechTaskHandlerUrl, payload)
.catch((error) => {
logger.error(error)
})
}, 0)
return ''
}
const createdTasks = await createHttpTaskWithToken({