File size: 963 Bytes
14afd56
 
 
 
 
0b3de5f
 
 
 
 
 
14afd56
 
 
 
 
 
 
 
 
0b3de5f
14afd56
 
 
 
 
 
 
 
 
 
02d0094
 
 
 
14afd56
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
var express = require('express');
var router = express.Router();
var textToSpeech = require('../helpers/tts');
var callOpenAI = require('../helpers/callOpenAI');  // Import the helper function

function logRequestBody(req, res, next) {
  console.log('Request body:', req.body);
  next();
}

router.post('/talk', logRequestBody, async function(req, res, next) {
  try {
    const ttsResult = await textToSpeech(req.body.text, req.body.language);
    res.json(ttsResult);

  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

router.post('/chat', logRequestBody, async function(req, res, next) {
  try {
    const userContent = req.body.text;
    const openAIResponse = await callOpenAI(userContent);

    res.json({ response: openAIResponse });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

router.get('/', function(req, res, next) {
  res.send("AI avatar backend is running.");
});

module.exports = router;