diff --git a/package.json b/package.json index f2cd9c2..c61c0b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iimttapi", - "version": "0.0.1", + "version": "1.0.0", "description": "Create a Node.js app for building production-ready RESTful APIs using Express, by running one command", "bin": "bin/createNodejsApp.js", "main": "src/index.js", @@ -24,7 +24,7 @@ "docker:dev": "docker-compose -f docker-compose.yml -f docker-compose.dev.yml up", "docker:test": "docker-compose -f docker-compose.yml -f docker-compose.test.yml up", "prepare": "husky install" - }, + }, "keywords": [ "node", "node.js", diff --git a/src/routes/api/aiTextToSpeech.js b/src/routes/api/aiTextToSpeech.js new file mode 100644 index 0000000..0341085 --- /dev/null +++ b/src/routes/api/aiTextToSpeech.js @@ -0,0 +1,50 @@ +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, + secretAccessKey: process.env.AWS_polly2_SECRET_ACCESS_KEY, + }); + + const polly = new AWS.Polly(); + + const voiceIdMap = { + "Ava": "Ivy", + "Monstero": "Justin", + "Dax": "Kevin", + "Kai": "Daniele", + "Yara": "Sally" + }; + + const { text, voiceId: frontendVoiceId } = req.body; + + if (!text || !frontendVoiceId) { + return res.status(400).json({ error: 'Text and voiceId are required' }); + } + + const pollyVoiceId = voiceIdMap[frontendVoiceId]; + if (!pollyVoiceId) { + return res.status(400).json({ error: 'Invalid voiceId provided' }); + } + + const params = { + Text: text, + OutputFormat: 'mp3', + VoiceId: pollyVoiceId, + }; + + try { + const data = await polly.synthesizeSpeech(params).promise(); + const audioBuffer = data.AudioStream; + + res.json({ + audioContent: audioBuffer.toString('base64'), + }); + } catch (error) { + console.error('Error generating speech:', error); + res.status(500).json({ error: 'Error generating speech' }); + } + +} + +module.exports = aiTextToSpeech; \ No newline at end of file diff --git a/src/routes/v1/api.route.js b/src/routes/v1/api.route.js index fc6d618..7888dcb 100644 --- a/src/routes/v1/api.route.js +++ b/src/routes/v1/api.route.js @@ -28,6 +28,7 @@ const aiFeedbackActionParent = require("../api/aiFeedbackActionParent"); const aiFollowupQuestion = require("../api/aiFollowupQuestion"); const aiMarkDrawing = require("../api/aiMarkDrawing"); const aiFeedbackOnReportWithFollowup = require("../api/aiFeedbackOnReportWithFollowup"); +const aiTextToSpeech = require("../api/aiTextToSpeech"); @@ -172,6 +173,13 @@ router.get("/ping", (req, res) => { router.post("/aiMarkDrawing", (req, res) => { aiMarkDrawing(req, res); }); + + // AI to provide star for drawing. + router.post("/aiTextToSpeech", (req, res) => { + aiTextToSpeech(req, res); + }); + + module.exports = router;