Spaces:
Runtime error
Runtime error
File size: 3,004 Bytes
1307964 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
const { readSecret, SECRET_KEYS } = require('./secrets');
const fetch = require('node-fetch').default;
const express = require('express');
const { jsonParser } = require('../express-common');
const router = express.Router();
router.post('/list', jsonParser, async (req, res) => {
try {
const key = readSecret(req.user.directories, SECRET_KEYS.AZURE_TTS);
if (!key) {
console.error('Azure TTS API Key not set');
return res.sendStatus(403);
}
const region = req.body.region;
if (!region) {
console.error('Azure TTS region not set');
return res.sendStatus(400);
}
const url = `https://${region}.tts.speech.microsoft.com/cognitiveservices/voices/list`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Ocp-Apim-Subscription-Key': key,
},
});
if (!response.ok) {
console.error('Azure Request failed', response.status, response.statusText);
return res.sendStatus(500);
}
const voices = await response.json();
return res.json(voices);
} catch (error) {
console.error('Azure Request failed', error);
return res.sendStatus(500);
}
});
router.post('/generate', jsonParser, async (req, res) => {
try {
const key = readSecret(req.user.directories, SECRET_KEYS.AZURE_TTS);
if (!key) {
console.error('Azure TTS API Key not set');
return res.sendStatus(403);
}
const { text, voice, region } = req.body;
if (!text || !voice || !region) {
console.error('Missing required parameters');
return res.sendStatus(400);
}
const url = `https://${region}.tts.speech.microsoft.com/cognitiveservices/v1`;
const lang = String(voice).split('-').slice(0, 2).join('-');
const escapedText = String(text).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
const ssml = `<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='${lang}'><voice xml:lang='${lang}' name='${voice}'>${escapedText}</voice></speak>`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': key,
'Content-Type': 'application/ssml+xml',
'X-Microsoft-OutputFormat': 'ogg-48khz-16bit-mono-opus',
},
body: ssml,
});
if (!response.ok) {
console.error('Azure Request failed', response.status, response.statusText);
return res.sendStatus(500);
}
const audio = await response.buffer();
res.set('Content-Type', 'audio/ogg');
return res.send(audio);
} catch (error) {
console.error('Azure Request failed', error);
return res.sendStatus(500);
}
});
module.exports = {
router,
};
|