-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (77 loc) · 3.13 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const express = require('express');
const app = express();
const router = express.Router();
require('dotenv').config();
const morgan = require('morgan');
const cors = require('cors');
const deepl = require('deepl-node');
const bodyParser = require('body-parser');
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_KEY,
});
const openai = new OpenAIApi(configuration);
const authKey = process.env.DEEPL_KEY; // Replace with your key
const translator = new deepl.Translator(authKey);
app.use(cors());
app.use(express.json());
app.use(morgan('combined'));
app.use(express.static('build'));
const port = process.env.PORT;
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
});
app.use(router);
router.use(bodyParser.json());
router.post('/transcribe', async(req, res, next) => {
try {
const whisperInitRawRes = await fetch("https://api.replicate.com/v1/predictions", {
headers: {
'Authorization': `Token ${process.env.WHISPER_TOKEN}`,
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify({
input: { audio: req.body.blobAsB64, language: 'fi', model: 'medium' },
version: "30414ee7c4fffc37e260fcab7842b5be470b9b840f2b608f5baa9bbef9a259ed",
})
});
const whisperRes = await whisperInitRawRes.json();
if (whisperRes.urls && whisperRes.urls.get) {
res.send({ checkIfTranscribingFinishedUrl: whisperRes.urls.get });
} else throw Error("Initiating transcribing failed.");
} catch (err) {
res.status(500).send({ message: 'Error.' });
next(err);
}
});
router.get('/image', async(req, res, next) => {
try {
let whisperTranscribeRawRes = await fetch(req.query.checkIfTranscribingFinishedUrl, {
headers: {
'Authorization': `Token ${process.env.WHISPER_TOKEN}`,
'Content-Type': 'application/json'
},
method: 'GET',
});
const whisperTranscribeRes = await whisperTranscribeRawRes.json();
// Replicate Whisper API gives us an response immediately but the transcribing might not be ready yet.
// We check if it is ready and return the response accordingly. Front end keeps polling until it gets succeeded status.
if (whisperTranscribeRes.status === 'succeeded') {
const translatedRes = await translator.translateText(whisperTranscribeRes.output.transcription, 'fi', 'en-US');
const imageRes = await openai.createImage({
prompt: translatedRes.text,
n: 1,
size: "512x512",
});
const imageUrl = imageRes.data.data[0].url;
res.send({ status: 'succeeded', url: imageUrl });
return;
};
if (whisperTranscribeRes.status === 'failed') throw Error("Whisper status request failed.");
res.send({ status: 'processing' });
} catch (err) {
res.status(500).send({ message: 'Error.' });
next(err);
}
});