-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
128 lines (100 loc) · 3.72 KB
/
index.ts
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
123
124
125
126
127
128
const { OpenAIApi, Configuration } = require("openai");
import * as path from 'path';
import * as fs from 'fs';
import * as express from 'express';
import * as multer from 'multer';
import * as gTTS from "gtts"
import { OpenAI } from "langchain/llms/openai";
const openai = new OpenAIApi(new Configuration({
apiKey: process.env.OPENAI_API_KEY,
}))
let tokens = ""
const model = new OpenAI({
streaming: true,
maxTokens: 3000,
callbacks: [{
handleLLMError(err: Error) {
console.log(err.message)
},
handleLLMNewToken(token: string) {
console.log(token);
tokens += token;
io.emit('message', token);
},
handleLLMEnd() {
io.emit('generating');
const gtts = new gTTS(tokens, 'en');
let files = fs.readdirSync(path.join(__dirname, 'uploads'));
const lastFile = files[files.length - 1];
gtts.save(path.join(__dirname, 'uploads', lastFile.replace(".mp3", "-gen.mp3")), function (err, result){
if(err) { throw new Error(err); }
tokens = ""
console.log("Text to speech converted!");
io.emit('converted');
});
}
}]
});
const storage = multer.diskStorage({
destination(req, file, cb) {
cb(null, 'uploads/');
},
filename(req, file, cb) {
const fileNameArr = file.originalname.split('.');
cb(null, `${Date.now()}.${fileNameArr[fileNameArr.length - 1]}`);
},
});
const upload = multer({ storage });
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;
io.on('connection', (socket) => {
console.log('user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
app.use(express.static('public/assets'));
app.use(express.static('uploads'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
app.post('/record', upload.single('audio'), async (req, res) => {
let files = fs.readdirSync(path.join(__dirname, 'uploads'));
const lastFile = files[files.length - 1];
const transcription = await transcribeAudio(lastFile);
console.log('Transcription:', transcription);
await model.call(transcription);
const txtPath = path.join(__dirname, 'uploads', req.file.filename.replace(".mp3", "-prompt.txt"))
fs.writeFileSync(txtPath, transcription);
const responsePath = path.join(__dirname, 'uploads', req.file.filename.replace(".mp3", "-gen.txt"))
fs.writeFileSync(responsePath, tokens);
return res.json({ success: true })
})
app.get('/recordings', async (req, res) => {
let filesTexts = fs.readdirSync(path.join(__dirname, 'uploads'));
const files = filesTexts.filter((file) => {
const fileNameArr = file.split('-');
return fileNameArr[fileNameArr.length - 1] === 'gen.mp3';
}).map((file) => `/${file}`);
const prompts = filesTexts.filter((file) => {
const fileNameArr = file.split('-');
return fileNameArr[fileNameArr.length - 1] === 'prompt.txt';
}).map((txt) => fs.readFileSync(path.join(__dirname, 'uploads', txt), 'utf8'));
const responses = filesTexts.filter((file) => {
const fileNameArr = file.split('-');
return fileNameArr[fileNameArr.length - 1] === 'gen.txt';
}).map((txt) => fs.readFileSync(path.join(__dirname, 'uploads', txt), 'utf8'));
return res.json({ success: true, files, prompts, responses });
});
async function transcribeAudio(filename) {
const transcript = await openai.createTranscription(
fs.createReadStream(path.join(__dirname, 'uploads', filename)),
"whisper-1"
);
return transcript.data.text;
}
http.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});