From 18706ff27d24dd6b30887697cc47e955b8767607 Mon Sep 17 00:00:00 2001 From: Sandra Rodgers <45321563+SandraRodgers@users.noreply.github.com> Date: Mon, 28 Aug 2023 09:12:05 -0500 Subject: [PATCH] chore: examples for local testing (#159) * adds examples for testing * remove sample folder --- examples/README.md | 67 +++++++++++++++++++++++++++++++ examples/prerecorded/index.js | 56 ++++++++++++++++++++++++++ examples/prerecorded/package.json | 14 +++++++ examples/streaming/index.js | 53 ++++++++++++++++++++++++ examples/streaming/package.json | 15 +++++++ sample/index.js | 37 ----------------- 6 files changed, 205 insertions(+), 37 deletions(-) create mode 100644 examples/README.md create mode 100644 examples/prerecorded/index.js create mode 100644 examples/prerecorded/package.json create mode 100644 examples/streaming/index.js create mode 100644 examples/streaming/package.json delete mode 100644 sample/index.js diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..c1102246 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,67 @@ +# Examples for Testing Features Locally + +The example projects are meant to be used to test features locally by contributors working on this SDK. In the `examples/precorded/package.json` file, you can see that the deepgram package used is the local version: + +```json +"dependencies": { + "@deepgram/sdk": "file:../../" + } +``` +This tells the example prerecorded project to use the local files within this project, not an installed package from npm. + +## Steps to Test Your Code + +If you are contributing changes to this SDK, you can test those changes by using the `prerecorded` or `streaming` projects in the `examples` folder. Here are the steps to follow: + +### Add Your Code +Make your changes to the SDK (be sure you are on a branch you have created to do this work). + +### Install dependencies for the Deepgram project + +In the **root** folder of this SDK, install the dependencies: + +``` +npm i +``` + +### Build the Deepgram project + +In the **root** folder of this SDK, build the project: + +``` +npm run build +``` + +### Change directories + +Move into the `examples` project directory for either the `prerecorded` or the `streaming` example projects: + +``` +cd examples/prerecorded +``` + +### Install dependencies for the example project + +Run the following command to install the example project dependencies: + +``` +npm i +``` + +### Edit the API key, the file, and the mimetype (as needed) + +Replace the API key where it says 'YOUR_DEEPGRAM_API_KEY' + +```js +const deepgramApiKey = "YOUR_DEEPGRAM_API_KEY" +const file = "YOUR_FILE_LOCATION" +const mimetype = "YOUR_FILE_MIME_TYPE"; +``` + +### Run the project + +Make sure you're in the directory with the `index.js` file and run the project with the following command. + +``` +npm run test +``` \ No newline at end of file diff --git a/examples/prerecorded/index.js b/examples/prerecorded/index.js new file mode 100644 index 00000000..e7142b6c --- /dev/null +++ b/examples/prerecorded/index.js @@ -0,0 +1,56 @@ +// Example filename: index.js + +const fs = require("fs"); +const { Deepgram } = require("@deepgram/sdk"); + +// Your Deepgram API Key +const deepgramApiKey = "YOUR_DEEPGRAM_API_KEY"; + +// Location of the file you want to transcribe. Should include filename and extension. +// Example of a local file: ../../Audio/life-moves-pretty-fast.wav +// Example of a remote file: https://static.deepgram.com/examples/interview_speech-analytics.wav +const file = "YOUR_FILE_LOCATION"; + +// Mimetype for the file you want to transcribe +// Only necessary if transcribing a local file +// Example: audio/wav +const mimetype = "YOUR_FILE_MIME_TYPE"; + +// Initialize the Deepgram SDK +const deepgram = new Deepgram(deepgramApiKey); + +// Check whether requested file is local or remote, and prepare accordingly +if (file.startsWith("http")) { + // File is remote + // Set the source + source = { + url: file, + }; +} else { + // File is local + // Open the audio file + const audio = fs.readFileSync(file); + + // Set the source + source = { + buffer: audio, + mimetype: mimetype, + }; +} + +// Send the audio to Deepgram and get the response +deepgram.transcription + .preRecorded(source, { + smart_format: true, + model: "nova", + }) + .then((response) => { + // Write the response to the console + console.dir(response, { depth: null }); + + // Write only the transcript to the console + //console.dir(response.results.channels[0].alternatives[0].transcript, { depth: null }); + }) + .catch((err) => { + console.log(err); + }); \ No newline at end of file diff --git a/examples/prerecorded/package.json b/examples/prerecorded/package.json new file mode 100644 index 00000000..612337e3 --- /dev/null +++ b/examples/prerecorded/package.json @@ -0,0 +1,14 @@ +{ + "name": "examples-prerecorded", + "version": "1.0.0", + "description": "A sample project for running local tests in the Deepgram node SDK", + "main": "index.js", + "scripts": { + "test": "node index.js" + }, + "dependencies": { + "@deepgram/sdk": "file:../../" + }, + "author": "", + "license": "ISC" +} diff --git a/examples/streaming/index.js b/examples/streaming/index.js new file mode 100644 index 00000000..ea0342b2 --- /dev/null +++ b/examples/streaming/index.js @@ -0,0 +1,53 @@ +// Example filename: index.js + +const { Deepgram } = require("@deepgram/sdk"); +const fetch = require("cross-fetch"); + +// Your Deepgram API Key +const deepgramApiKey = "YOUR_DEEPGRAM_API_KEY"; + +// URL for the audio you would like to stream +// URL for the example resource will change depending on whether user is outside or inside the UK +// Outside the UK +const url = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service"; +// Inside the UK +// const url = 'http://stream.live.vc.bbcmedia.co.uk/bbc_radio_fourfm'; + +// Initialize the Deepgram SDK +const deepgram = new Deepgram(deepgramApiKey); + +// Create a websocket connection to Deepgram +// In this example, punctuation is turned on, interim results are turned off, and language is set to UK English. +const deepgramLive = deepgram.transcription.live({ + smart_format: true, + interim_results: false, + language: "en-US", + model: "nova", +}); + +// Listen for the connection to open and send streaming audio from the URL to Deepgram +fetch(url) + .then((r) => r.body) + .then((res) => { + res.on("readable", () => { + if (deepgramLive.getReadyState() == 1) { + deepgramLive.send(res.read()); + } + }); + }); + +// Listen for the connection to close +deepgramLive.addListener("close", () => { + console.log("Connection closed."); +}); + +// Listen for any transcripts received from Deepgram and write them to the console +deepgramLive.addListener("transcriptReceived", (message) => { + const data = JSON.parse(message); + + // Write the entire response to the console + console.dir(data.channel, { depth: null }); + + // Write only the transcript to the console + //console.dir(data.channel.alternatives[0].transcript, { depth: null }); +}); \ No newline at end of file diff --git a/examples/streaming/package.json b/examples/streaming/package.json new file mode 100644 index 00000000..2cfcf77d --- /dev/null +++ b/examples/streaming/package.json @@ -0,0 +1,15 @@ +{ + "name": "examples-streaming", + "version": "1.0.0", + "description": "A sample project for running local tests in the Deepgram node SDK", + "main": "index.js", + "scripts": { + "test": "node index.js" + }, + "dependencies": { + "@deepgram/sdk": "file:../../", + "cross-fetch": "^4.0.0" + }, + "author": "", + "license": "ISC" +} diff --git a/sample/index.js b/sample/index.js deleted file mode 100644 index 61c1ee64..00000000 --- a/sample/index.js +++ /dev/null @@ -1,37 +0,0 @@ -const { Deepgram } = require('../dist'); - -const config = { - deepgramApiKey: 'YOUR_DEEPGRAM_API_KEY', - urlToFile: 'https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav' -} - -function main() { - - return new Promise((resolve, reject) => { - (async () => { - - try { - - const deepgram = new Deepgram(config.deepgramApiKey); - - /** Send a pre-recorded file for transcription */ - const transcription = await deepgram.transcription.preRecorded({ - url: config.urlToFile - }, { - punctuate: true, - utterances: true - }); - console.log(transcription.toWebVTT()); - - resolve(); - } - catch (err) { - console.log(`Err: ${err}`); - reject(err); - } - })() - }); -} - -main(); -