-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (58 loc) · 1.69 KB
/
server.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 cors = require("cors");
const multer = require("multer");
const upload = multer();
require("dotenv").config();
const { Deepgram } = require("@deepgram/sdk");
const deepgram = new Deepgram(process.env.DG_API);
const app = express();
const port = 3000;
app.use(express.static("static"));
app.use(cors());
app.use((req, res, next) => {
const allowedOrigins = [
"http://localhost:5173",
"https://deepgram-tokenizer.netlify.app/", // Add any other allowed origins here
];
const origin = req.headers.origin;
if (allowedOrigins.includes(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin);
}
next();
});
app.get("/", (req, res) => res.send("Deployed! 🚀"));
app.get("/deepgram-token", async (req, res) => {
try {
const newKey = await deepgram.keys.create(
process.env.DEEPGRAM_PROJECT_ID,
"Temporary key - works for 10 seconds",
["usage:write"],
{ timeToLive: 10 }
);
res.send(newKey);
} catch (error) {
console.error("Error while creating Deepgram key:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/dg-transcription", upload.single("file"), async (req, res) => {
console.log(req.body);
try {
const dgResponse = await deepgram.transcription.preRecorded(
{
buffer: req.file.buffer,
mimetype: req.file.mimetype,
},
{
smart_format: true,
tag: ["dx-team"],
model: "nova-2-ea",
}
);
console.dir(dgResponse.results, { depth: 4 });
res.send({ apiCall: dgResponse });
} catch (e) {
console.log("error", e);
}
});
app.listen(port, () => console.log(`Listening on port ${port}!`));