aiTextToSpeech file upload
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
const axios = require('axios');
|
||||
const AWS = require('aws-sdk');
|
||||
|
||||
const aiTextToSpeech = async (req, res) => {
|
||||
const AWS = require('aws-sdk');
|
||||
AWS.config.update({
|
||||
region: 'ap-south-1',
|
||||
accessKeyId: process.env.AWS_polly2_ACCESS_KEY_ID,
|
||||
@@ -7,8 +9,6 @@ const aiTextToSpeech = async (req, res) => {
|
||||
});
|
||||
|
||||
const polly = new AWS.Polly();
|
||||
const s3 = new AWS.S3();
|
||||
const BUCKET_NAME = 'polly-bs';
|
||||
const voiceIdMap = {
|
||||
"Ava": "Ivy",
|
||||
"Monstero": "Justin",
|
||||
@@ -19,7 +19,7 @@ const aiTextToSpeech = async (req, res) => {
|
||||
|
||||
const { text, voiceId: frontendVoiceId } = req.body;
|
||||
|
||||
if (!text || !frontendVoiceId) {
|
||||
if (!text || !frontendVoiceId) {
|
||||
return res.status(400).json({ error: 'Text and voiceId are required' });
|
||||
}
|
||||
|
||||
@@ -31,68 +31,59 @@ const aiTextToSpeech = async (req, res) => {
|
||||
const params = {
|
||||
Text: text,
|
||||
OutputFormat: 'mp3',
|
||||
Engine:'neural',
|
||||
Engine: 'neural',
|
||||
VoiceId: pollyVoiceId,
|
||||
};
|
||||
|
||||
try {
|
||||
// Generate a folder name based on current timestamp and random digits
|
||||
function generateTimestampWithRandomDigits() {
|
||||
// Get the current date and time
|
||||
const now = new Date();
|
||||
|
||||
// Format the current date as YYYY-MM-DD_HH-mm-ss
|
||||
const formattedDate = now.toISOString().replace('T', '_').replace(/\..+/, '').replace(/:/g, '-');
|
||||
|
||||
// Generate 4 random digits
|
||||
const randomDigits = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
|
||||
|
||||
// Concatenate the formatted date with the random digits
|
||||
return `${formattedDate}_${randomDigits}`;
|
||||
}
|
||||
|
||||
// Example usage
|
||||
const folder = generateTimestampWithRandomDigits();
|
||||
}
|
||||
|
||||
// Generate timestamp for folder structure
|
||||
// const timestamp = moment().format('YYYY-MM-DD_HH-mm-ss');
|
||||
const folder = generateTimestampWithRandomDigits();
|
||||
const folderName = `audio_files/${folder}`;
|
||||
|
||||
// Request Polly to synthesize speech
|
||||
const data = await polly.synthesizeSpeech(params).promise();
|
||||
const audioBuffer = data.AudioStream;
|
||||
|
||||
// Define S3 paths
|
||||
const audioFilePath = `${folderName}/speech.mp3`;
|
||||
const textFilePath = `${folderName}/text.txt`;
|
||||
// Prepare the text file as a buffer
|
||||
const textBuffer = Buffer.from(text, 'utf-8');
|
||||
|
||||
// Upload audio file to S3
|
||||
await s3.upload({
|
||||
Bucket: BUCKET_NAME,
|
||||
Key: audioFilePath,
|
||||
Body: audioBuffer,
|
||||
ContentType: 'audio/mpeg'
|
||||
}).promise();
|
||||
// Prepare the form data to send via POST request
|
||||
const formData = new FormData();
|
||||
formData.append('file1', audioBuffer, 'speech.mp3');
|
||||
formData.append('file2', textBuffer, 'text.txt');
|
||||
formData.append('folder', folderName); // Send folder name as form data
|
||||
formData.append('bucket', 'beanstalkedu-test'); // Adjust bucket if needed
|
||||
|
||||
// Upload text file to S3
|
||||
await s3.upload({
|
||||
Bucket: BUCKET_NAME,
|
||||
Key: textFilePath,
|
||||
Body: text,
|
||||
ContentType: 'text/plain'
|
||||
}).promise();
|
||||
// Send the POST request to upload the files
|
||||
const response = await axios.post(
|
||||
'https://preschool-curriculum.in/api/one/v1/file/upload',
|
||||
formData,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${process.env.BEARER_TOKEN}`,
|
||||
...formData.getHeaders() // Ensure FormData headers are included
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Return success response with S3 paths
|
||||
// Return success response with URLs from the response
|
||||
res.json({
|
||||
message: 'Speech generated and saved to S3 successfully',
|
||||
audioFileUrl: `https://${BUCKET_NAME}.s3.amazonaws.com/${audioFilePath}`,
|
||||
textFileUrl: `https://${BUCKET_NAME}.s3.amazonaws.com/${textFilePath}`
|
||||
message: 'Speech generated and saved successfully',
|
||||
fileUrls: response.data.urls,
|
||||
fileKeys: response.data.data
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error generating speech or saving to S3:', error);
|
||||
res.status(500).json({ error: 'Error generating speech or saving to S3' });
|
||||
console.error('Error generating speech or uploading files:', error);
|
||||
res.status(500).json({ error: 'Error generating speech or uploading files' });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports = aiTextToSpeech;
|
||||
module.exports = aiTextToSpeech;
|
||||
|
||||
Reference in New Issue
Block a user