-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (51 loc) · 1.93 KB
/
app.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
64
65
66
const axios = require("axios")
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const audioURL = "https://bit.ly/3yxKEIY"
const APIKey = "eb17252418ea43edb448e21843152693"
app.use(bodyParser.json());
app.use((req,res,next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content, Accept, Content-Type, Authorization');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
next();
});
const assembly = axios.create({
baseURL: "https://api.assemblyai.com/v2",
headers: {
authorization: APIKey,
"content-type": "application/json",
},
})
/*assembly
.post("/transcript", {
audio_url: audioURL
})
.then((res) => console.log(res.data))
.catch((err) => console.error(err));*/
app.post('/api/transcript', (req, res, next) => {
const refreshInterval = 5000;
const getTranscript = async () => {
// Sends the audio file to AssemblyAI for transcription
const response = await assembly.post("/transcript", {
audio_url: req.body.audioURL,
})
// Interval for checking transcript completion
const checkCompletionInterval = setInterval(async () => {
const transcript = await assembly.get(`/transcript/${response.data.id}`)
const transcriptStatus = transcript.data.status
if (transcriptStatus !== "completed") {
console.log(`Transcript Status: ${transcriptStatus}`)
} else if (transcriptStatus === "completed") {
console.log("\n-------------Transcription completed!-----------------\n")
let transcriptText = transcript.data.text;
res.status(200).json({text: transcriptText});
//console.log(`Your transcribed text:\n\n${transcriptText}`)
clearInterval(checkCompletionInterval)
}
}, refreshInterval)
}
getTranscript()
});
module.exports = app;