Skip to content

Commit

Permalink
chore: examples for local testing (#159)
Browse files Browse the repository at this point in the history
* adds examples for testing

* remove sample folder
  • Loading branch information
SandraRodgers committed Aug 28, 2023
1 parent 6ea061c commit 18706ff
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 37 deletions.
67 changes: 67 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -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
```
56 changes: 56 additions & 0 deletions examples/prerecorded/index.js
Original file line number Diff line number Diff line change
@@ -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);
});
14 changes: 14 additions & 0 deletions examples/prerecorded/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
53 changes: 53 additions & 0 deletions examples/streaming/index.js
Original file line number Diff line number Diff line change
@@ -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 });
});
15 changes: 15 additions & 0 deletions examples/streaming/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
37 changes: 0 additions & 37 deletions sample/index.js

This file was deleted.

0 comments on commit 18706ff

Please sign in to comment.