From d0cec3d4d64b47e4aaf837840c799113bcab0cbd Mon Sep 17 00:00:00 2001 From: Luke Oliff Date: Mon, 16 Oct 2023 22:33:02 +0100 Subject: [PATCH] feat: update demo for new v3 sdk alpha --- package-lock.json | 116 +++++++++++++++++++++++++++++----------------- package.json | 2 +- server.js | 68 +++++++++++++++++++-------- 3 files changed, 123 insertions(+), 63 deletions(-) diff --git a/package-lock.json b/package-lock.json index 427edeb..6735634 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "name": "deepgram-javascript-01", "version": "0.0.1", "dependencies": { - "@deepgram/sdk": "^1.18.1", + "@deepgram/sdk": "^3.0.0-alpha.4", "cors": "^2.8", "express": "^4.18", "multer": "^1.4.5-lts.1", @@ -19,13 +19,13 @@ } }, "node_modules/@deepgram/sdk": { - "version": "1.18.1", - "resolved": "https://registry.npmjs.org/@deepgram/sdk/-/sdk-1.18.1.tgz", - "integrity": "sha512-4oXadgVhv06I9uLYcLrkWS4doucwKm7ufQ6bk1hnDcV3aRhTmAd6Dkeyg8mPCvLVrOET7mI7vJGJhodHmcNKjA==", + "version": "3.0.0-alpha.4", + "resolved": "https://registry.npmjs.org/@deepgram/sdk/-/sdk-3.0.0-alpha.4.tgz", + "integrity": "sha512-Gcjr8F1Gfbm+qjW2bcy023Kfz01Z+cfSL85EEj8bSI6+Oe3itSyuMs8sAQmXmzhGTQhvu2pLltem7BCnss94Tg==", "dependencies": { - "bufferutil": "^4.0.6", - "utf-8-validate": "^5.0.9", - "ws": "^7.5.5" + "cross-fetch": "^3.1.5", + "events": "^3.3.0", + "modern-isomorphic-ws": "^1.0.5" } }, "node_modules/abbrev": { @@ -153,18 +153,6 @@ "version": "1.1.2", "license": "MIT" }, - "node_modules/bufferutil": { - "version": "4.0.7", - "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.7.tgz", - "integrity": "sha512-kukuqc39WOHtdxtw4UScxF/WVnMFVSQVKhtx3AjZJzhd0RGZZldcrfSEbVsWWe6KNH253574cq5F+wpv0G9pJw==", - "hasInstallScript": true, - "dependencies": { - "node-gyp-build": "^4.3.0" - }, - "engines": { - "node": ">=6.14.2" - } - }, "node_modules/busboy": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", @@ -336,6 +324,14 @@ "node": ">= 0.10" } }, + "node_modules/cross-fetch": { + "version": "3.1.8", + "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz", + "integrity": "sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg==", + "dependencies": { + "node-fetch": "^2.6.12" + } + }, "node_modules/cross-spawn": { "version": "6.0.5", "license": "MIT", @@ -494,6 +490,14 @@ "node": ">= 0.6" } }, + "node_modules/events": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz", + "integrity": "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==", + "engines": { + "node": ">=0.8.x" + } + }, "node_modules/express": { "version": "4.18.2", "license": "MIT", @@ -1158,6 +1162,14 @@ "mkdirp": "bin/cmd.js" } }, + "node_modules/modern-isomorphic-ws": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/modern-isomorphic-ws/-/modern-isomorphic-ws-1.0.5.tgz", + "integrity": "sha512-cgJzdkn1//XyYJsFZkjPYKN21CXeG+KC38Q5uFwTnbUwG3pmmsUDS9a5RRJ6lFnjsnEbUKx+rIXjxX/uCUFYvQ==", + "peerDependencies": { + "ws": "^8.11.0" + } + }, "node_modules/ms": { "version": "2.1.2", "dev": true, @@ -1191,14 +1203,23 @@ "version": "1.0.5", "license": "MIT" }, - "node_modules/node-gyp-build": { - "version": "4.6.0", - "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.6.0.tgz", - "integrity": "sha512-NTZVKn9IylLwUzaKjkas1e4u2DLNcV4rdYagA4PWdPwW87Bi7z+BznyKSRwS/761tV/lzCGXplWsiaMjLqP2zQ==", - "bin": { - "node-gyp-build": "bin.js", - "node-gyp-build-optional": "optional.js", - "node-gyp-build-test": "build-test.js" + "node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } } }, "node_modules/nodemon": { @@ -1786,6 +1807,11 @@ "nodetouch": "bin/nodetouch.js" } }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "node_modules/type-is": { "version": "1.6.18", "license": "MIT", @@ -1839,18 +1865,6 @@ "node": ">= 0.8" } }, - "node_modules/utf-8-validate": { - "version": "5.0.10", - "resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-5.0.10.tgz", - "integrity": "sha512-Z6czzLq4u8fPOyx7TU6X3dvUZVvoJmxSQ+IcrlmagKhilxlhZgxPK6C5Jqbkw1IDUmFTM+cz9QDnnLTwDz/2gQ==", - "hasInstallScript": true, - "dependencies": { - "node-gyp-build": "^4.3.0" - }, - "engines": { - "node": ">=6.14.2" - } - }, "node_modules/util-deprecate": { "version": "1.0.2", "license": "MIT" @@ -1877,6 +1891,20 @@ "node": ">= 0.8" } }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/which": { "version": "1.3.1", "license": "ISC", @@ -1920,14 +1948,16 @@ } }, "node_modules/ws": { - "version": "7.5.9", - "license": "MIT", + "version": "8.14.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.14.2.tgz", + "integrity": "sha512-wEBG1ftX4jcglPxgFCMJmZ2PLtSbJ2Peg6TmpJFTbe9GZYOQCDPdMYu/Tm0/bGZkw8paZnJY45J4K2PZrLYq8g==", + "peer": true, "engines": { - "node": ">=8.3.0" + "node": ">=10.0.0" }, "peerDependencies": { "bufferutil": "^4.0.1", - "utf-8-validate": "^5.0.2" + "utf-8-validate": ">=5.0.2" }, "peerDependenciesMeta": { "bufferutil": { diff --git a/package.json b/package.json index 80b5a7b..af5dc1e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "start": "node server.js" }, "dependencies": { - "@deepgram/sdk": "^1.18.1", + "@deepgram/sdk": "^3.0.0-alpha.4", "cors": "^2.8", "express": "^4.18", "multer": "^1.4.5-lts.1", diff --git a/server.js b/server.js index d5ffaee..e3b188f 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,13 @@ -const { Deepgram } = require("@deepgram/sdk"); +const { createClient } = require("@deepgram/sdk"); const config = require("./config.json"); const express = require("express"); const multer = require("multer"); const path = require("path"); const port = process.env.API_PORT || 8080; -const deepgram = new Deepgram(config.dgKey, "api.beta.deepgram.com"); +const deepgram = createClient(config.dgKey, { + global: { url: "api.beta.deepgram.com" }, +}); const storage = multer.memoryStorage(); const upload = multer({ storage: storage }); @@ -13,39 +15,67 @@ const app = express(); app.use(express.static(path.join(__dirname, "static"))); +const transcribeFile = async (source, options) => { + return await deepgram.listen.prerecorded.transcribeFile(source, options); +}; + +const transcribeUrl = async (source, options) => { + return await deepgram.listen.prerecorded.transcribeUrl(source, options); +}; + app.post("/api", upload.single("file"), async (req, res) => { const { body, file } = req; const { url, features, model, version, tier } = body; const dgFeatures = JSON.parse(features); - - let dgRequest = null; + let dgRequest; try { // validate the URL for a URL request + + if (!url && !file) { + throw Error( + "Error: You need to choose a file to transcribe your own audio." + ); + } + + let transcription; + if (url) { dgRequest = { url }; + const { result: transcriptionResult, error: transcriptionError } = + await transcribeUrl(dgRequest, { + ...dgFeatures, + model, + tier, + ...(version ? { version } : null), + ...(model === "whisper" ? null : { tier }), + }); + + if (transcriptionError) { + throw Error(transcriptionError.message); + } + + transcription = transcriptionResult; } - // get file buffer for a file request if (file) { const { mimetype, buffer } = file; dgRequest = { buffer, mimetype }; - } + const { result: transcriptionResult, error: transcriptionError } = + await transcribeFile(dgRequest, { + ...dgFeatures, + model, + tier, + ...(version ? { version } : null), + ...(model === "whisper" ? null : { tier }), + }); - if (!dgRequest) { - throw Error( - "Error: You need to choose a file to transcribe your own audio." - ); - } + if (transcriptionError) { + throw Error(transcriptionError.message); + } - // send request to deepgram - const transcription = await deepgram.transcription.preRecorded(dgRequest, { - ...dgFeatures, - model, - tier, - ...(version ? { version } : null), - ...(model === "whisper" ? null : { tier }), - }); + transcription = transcriptionResult; + } // return results res.send({ model, version, tier, dgRequest, dgFeatures, transcription });