Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lo/update for v3 node sdk #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 73 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
68 changes: 49 additions & 19 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,81 @@
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 });

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 });
Expand Down