forked from cyclic-software/starter-micro-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (52 loc) · 2.04 KB
/
index.js
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
const express = require('express');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
const savedAudioDirectory = path.join(__dirname, 'saved_audio');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.static('public'));
app.get('/generate', async (req, res) => {
const apiKey = req.query.key;
const prompt = req.query.prompt;
if (!apiKey || !prompt) {
return res.status(400).json({ error: "API key and prompt are required." });
}
if (apiKey === "sudiptoisgay") {
try {
const payload = {
text: prompt,
};
const apiUrl = 'https://api.elevenlabs.io/v1/text-to-speech/zrHiDhphv9ZnVXBqCLjz';
const apiKey = 'e3bc9b24bf7240186872e8285125f85a';
const response = await axios.post(apiUrl, payload, {
headers: {
'Content-Type': 'application/json',
'xi-api-key': apiKey,
},
responseType: 'stream',
});
// Generate a random filename based on the current timestamp
const timestamp = new Date().getTime();
const mp3Filename = `output_${timestamp}.mp3`;
const mp3Path = path.join(savedAudioDirectory, mp3Filename);
response.data.pipe(fs.createWriteStream(mp3Path));
response.data.on('end', () => {
if (fs.existsSync(mp3Path)) {
res.status(200).sendFile(mp3Path);
} else {
res.status(500).json({ error: "Failed to save MP3 file." });
}
});
} catch (error) {
res.status(500).json({ error: `Error processing response: ${error.message}` });
}
} else {
res.status(401).json({ error: "Access denied! Invalid API key." });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});