Create speech while requesting
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
||||
@ -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')
|
||||
}
|
||||
|
||||
@ -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())
|
||||
|
||||
@ -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({
|
||||
|
||||
Reference in New Issue
Block a user