Skip to content

Instantly share code, notes, and snippets.

@musha635842
Created April 11, 2022 23:52
Show Gist options
  • Save musha635842/d081588cf2d03f0225c67d2fd57c93d3 to your computer and use it in GitHub Desktop.
Save musha635842/d081588cf2d03f0225c67d2fd57c93d3 to your computer and use it in GitHub Desktop.
const express = require('express');
const formidable = require('formidable');
const fs = require('fs');
const { Deepgram } = require('@deepgram/sdk');
const app = express();
// Your Deepgram API Key
const deepgramApiKey = 'YOUR_DEEPGRAM_API_KEY';
// Initialize the Deepgram SDK
const deepgram = new Deepgram(deepgramApiKey);
app.get('/', (req, res) => {
res.send(`
<h2>Sound transcript</h2>
<form action="/upload" enctype="multipart/form-data" method="post">
<div>Recording: <input type="file" name="recording" multiple="multiple" /></div>
<p></p>
<input type="submit" value="Process" />
</form>
`);
});
app.post('/upload', (req, res, next) => {
const form = formidable({ multiples: true });
form.parse(req, (err, fields, files) => {
if (err) {
next(err);
return;
}
source = {
buffer: fs.readFileSync(files.recording.filepath),
mimetype: files.recording.mimetype
}
deepgram.transcription.preRecorded(
source,
{
punctuate: true
}
)
.then((response) => {
console.dir(response, {depth: null});
res.send(response.results.channels[0].alternatives[0].transcript, { depth: null });
})
.catch((err) => {
console.log(err);
})
});
});
app.listen(3000, () => {
console.log('Server listening on http://localhost:3000 ...');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment