Spaces:
Sleeping
Sleeping
File size: 5,473 Bytes
c42c510 31ac2a2 c42c510 56490ad 31ac2a2 c42c510 055dded c42c510 443d661 c42c510 0bc7d98 c42c510 055dded c42c510 b790a80 055dded b790a80 055dded c42c510 055dded c42c510 cdecc9d 820f5dc cdecc9d c42c510 cdecc9d c42c510 cdecc9d c42c510 11a7ca0 c42c510 cdecc9d c42c510 | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | const express = require('express');
const cors = require('cors'); // Add this line
const app = express();
const sdk = require("microsoft-cognitiveservices-speech-sdk");
const path = require('path');
const fs = require('fs')
let MAX_CHAR=parseInt(process.env.MAX_CHAR)
console.log('MAX_CHAR:',MAX_CHAR);
app.use(cors()); // Use cors middleware here
// Serve static files from the "public" directory
app.use(express.static(path.join(__dirname, 'public')));
app.get('/tts/:text', async (req, res) => {
let startTime=Date.now();
console.log('start /tts ' + startTime);
let text = req?.params?.text || 'test';
let max_characters=MAX_CHAR || 490;
let subtext= text.substring(0, max_characters-50);
console.log('max_characters:',max_characters);
console.log('text that will be convert to audio:', subtext);
try {
let response = await fetch("https://api.ttsopenai.com/api/v1/text-to-speech-stream", {
method: "POST",
headers: {
"accept": "application/json",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache", "content-type": "application/json", "pragma": "no-cache", "sec-ch-ua": "\"Google Chrome\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Linux\"", "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "same-site",
"Referer": "https://ttsopenai.com/",
"Referrer-Policy": "strict-origin-when-cross-origin",
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzM5ODk1ODAsInN1YiI6IjAyZGJjMTMyLWI3OTQtMTFlZi1iNmIwLTk2YjQ3NWYyOWRkZSJ9.uag7hSnnxQzpHwWCFuEdTphjMuC_M9CBcngJLjQeJcg"
},
body: JSON.stringify({ "model": "tts-1", "voice": "alloy", "voice_id":"OA001","speed": 1, "input": subtext +". openai tts,this speech is generated by openai tts" || 'hi this tts from openai, input you text now' })
});
console.log('response:', response.status,Date.now()-startTime);
if (response.status !== 200) {
// console.log(response);
throw Error(response.status + await response.text())
}
let res_json = await response.json();
console.log('get response ',Date.now()-startTime);
response= await fetch('https://api.ttsopenai.com/api/v1/history/'+res_json.uuid,{
headers: {
"accept": "application/json",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache", "content-type": "application/json", "pragma": "no-cache", "sec-ch-ua": "\"Google Chrome\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Linux\"", "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "same-site",
"Referer": "https://ttsopenai.com/",
"Referrer-Policy": "strict-origin-when-cross-origin",
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzM5ODk1ODAsInN1YiI6IjAyZGJjMTMyLWI3OTQtMTFlZi1iNmIwLTk2YjQ3NWYyOWRkZSJ9.uag7hSnnxQzpHwWCFuEdTphjMuC_M9CBcngJLjQeJcg"
}
})
res_json=await response.json()
res.redirect(res_json.media_url)
// const mp3File = fs.createWriteStream('output.mp3');
// mp3File.write(Buffer.from(mp3Buffer));
// mp3File.end();
// console.log('MP3 file saved to output.mp3');
// Send the saved file to the client
//res.set('Content-Type', 'audio/mpeg');
//res.send(Buffer.from(mp3Buffer))
//console.log('sent output.mp3 ',Date.now()-startTime);
return;
} catch (error) {
console.error(error, 'try azure tts');
}
console.log('redirct to auretts');
res.redirect('/atts/'+text);
});
app.get('/atts/:text', (req, res) => {
const text = req?.params?.text || 'test';
const voice = req?.query?.voicename || "de-DE-SeraphinaMultilingualNeural";
let audioFile = "audio.mp3";
let speechConfig = sdk.SpeechConfig.fromSubscription(process.env.SPEECH_KEY, process.env.SPEECH_REGION);
let audioConfig = sdk.AudioConfig.fromAudioFileOutput(audioFile);
speechConfig.speechSynthesisOutputFormat = sdk.SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3;
speechConfig.speechSynthesisVoiceName = voice;
let synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
console.log('/attss',text);
synthesizer.speakTextAsync(text +`.\n speaked by Azure TTS`,
function (result) {
if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) {
console.log("synthesis finished.");
res.sendFile(path.join(__dirname,audioFile)); // This will initiate file download
} else {
console.error("Speech synthesis canceled, " + result.errorDetails +
"\nDid you set the speech resource key and region values?");
}
synthesizer.close();
synthesizer = null;
},
function (err) {
console.trace("err - " + err);
synthesizer.close();
synthesizer = null;
});
console.log("Now synthesizing to: " + audioFile);
});
const port = process.env.PORT || 7860;
app.listen(port, () => console.log(`Server is running on port ${port}`));
|