lib update with functional style code

This commit is contained in:
surajshivakumar
2024-06-13 03:05:13 -04:00
parent 492355a040
commit 39c4a5cc9e
8 changed files with 112 additions and 137 deletions

View File

@@ -1,66 +0,0 @@
// deepgramTranscriber.js
const Transcriber = require('./transcriber');
const { createClient } = require('@deepgram/sdk');
const fs = require('fs');
class DeepgramTranscriber extends Transcriber {
constructor(apiKey, audioFilePath) {
super(audioFilePath);
this.deepgram = createClient(apiKey);
}
async transcribeFile(filePath) {
const { result } = await this.deepgram.listen.prerecorded.transcribeFile(
fs.readFileSync(filePath),
{ model: 'nova-2', smart_format: true, detect_entities: true }
);
return result;
}
async redactFile(filePath) {
const { result } = await this.deepgram.listen.prerecorded.transcribeFile(
fs.readFileSync(filePath),
{ model: 'nova-2', smart_format: true, redact: 'pii' }
);
return result;
}
async analyzeText(text) {
const { result } = await this.deepgram.read.analyzeText(
{ text },
{ language: 'en', sentiment: true, intents: true, summarize: true }
);
return result;
}
async processAudio(filePath = this.audioDir) {
try {
const transcription = await this.transcribeFile(filePath);
const redaction = await this.redactFile(filePath);
const transcript = transcription.results.channels[0].alternatives[0].transcript;
const timestamps = transcription.results.channels[0].alternatives[0].words;
const redactionTimestamps = redaction.results.channels[0].alternatives[0].words;
const redacted = redaction.results.channels[0].alternatives[0].transcript;
const entities = transcription.results.channels[0].alternatives[0].entities;
const analysisResult = await this.analyzeText(transcript);
const sentimentSegment = analysisResult.results.sentiments.segments[0];
const sentiment = sentimentSegment.sentiment;
const sentimentScore = sentimentSegment.sentiment_score;
return {
transcript,
timestamps,
redactionTimestamps,
redacted,
sentiment,
sentimentScore,
entities
};
} catch (error) {
throw error;
}
}
}
module.exports = DeepgramTranscriber;

View File

@@ -0,0 +1,59 @@
const fs = require('fs');
async function transcribeFile(deepgram, filePath) {
const fileBuffer = fs.readFileSync(filePath);
const options = { model: 'nova-2', smart_format: true, detect_entities: true };
const { result } = await deepgram.listen.prerecorded.transcribeFile(fileBuffer, options);
return result;
}
async function redactFile(deepgram, filePath) {
const fileBuffer = fs.readFileSync(filePath);
const options = { model: 'nova-2', smart_format: true, redact: 'pii' };
const { result } = await deepgram.listen.prerecorded.transcribeFile(fileBuffer, options);
return result;
}
async function analyzeText(deepgram, text) {
const options = { language: 'en', sentiment: true, intents: true, summarize: true };
const { result } = await deepgram.read.analyzeText({ text }, options);
return result;
}
async function processAudio(deepgram, filePath) {
try {
const transcription = await transcribeFile(deepgram, filePath);
const redaction = await redactFile(deepgram, filePath);
const transcript = transcription.results.channels[0].alternatives[0].transcript;
const timestamps = transcription.results.channels[0].alternatives[0].words;
const redactionTimestamps = redaction.results.channels[0].alternatives[0].words;
const redacted = redaction.results.channels[0].alternatives[0].transcript;
const entities = transcription.results.channels[0].alternatives[0].entities;
const analysisResult = await analyzeText(deepgram, transcript);
const sentimentSegment = analysisResult.results.sentiments.segments[0];
const sentiment = sentimentSegment.sentiment;
const sentimentScore = sentimentSegment.sentiment_score;
return {
transcript,
timestamps,
redactionTimestamps,
redacted,
sentiment,
sentimentScore,
entities
};
} catch (error) {
console.error('Error processing audio:', error);
throw error;
}
}
module.exports = {
transcribeFile,
analyzeText,
processAudio,
redactFile
};

View File

@@ -1,15 +1,15 @@
const Redactor = require('./redactor');
const ffmpeg = require('fluent-ffmpeg');
class DeepGramRedactor extends Redactor {
async redactAudio(transcriptionData, audioPath, audioOutputPath, { delta = 0.05 } = {}) {
async function redactAudioDeepgram(transcriptionData, audioPath, audioOutputPath, { delta = 0.05 } = {}) {
return new Promise((resolve, reject) => {
const command = ffmpeg(audioPath)
.outputFormat('wav'); // Ensure output format is WAV
// Iterate over transcription data to apply audio filters
for (let i = 0; i < transcriptionData.length; i++) {
const {word, start} = transcriptionData[i];
let end = transcriptionData[i].end; // Default end time
transcriptionData.forEach((data, i) => {
const { word, start } = data;
let end = data.end; // Default end time
// Check if the word needs redaction
if (word.startsWith('[') && word.endsWith(']')) {
@@ -28,15 +28,19 @@ class DeepGramRedactor extends Redactor {
// Log the redacted segments
console.log(`Redacting from ${start}s to ${end}s: "${word}"`);
}
}
});
// Handlers for command execution
command.on('end', () => {
console.log(`Redacted audio saved at ${audioOutputPath}`);
resolve(); // Resolve the promise on successful completion
}).on('error', (err, stdout, stderr) => {
console.error('Error processing audio file:', err.message);
console.error('ffmpeg stdout:', stdout);
console.error('ffmpeg stderr:', stderr);
reject(err); // Reject the promise on error
}).saveToFile(audioOutputPath);
}
});
}
module.exports = DeepGramRedactor;
module.exports = { redactAudioDeepgram };

16
lib/redact.js Normal file
View File

@@ -0,0 +1,16 @@
const { redactAudioDeepgram } = require('./make-redact-audio-deepgram');
const assert = require('assert');
function getRedactedAudio(vendor, transcriptionData, audioPath, audioOutputPath) {
assert.ok(['deepgram', 'otherVendor'].includes(vendor), 'vendor not supported');
if (vendor === 'deepgram') {
return redactAudioDeepgram(transcriptionData, audioPath, audioOutputPath);
}
else {
throw new Error(`Unsupported vendor: ${vendor}`);
}
}
module.exports = { getRedactedAudio };

View File

@@ -1,13 +0,0 @@
class Redactor {
constructor() {
if (new.target === Redactor) {
throw new TypeError('Cannot construct Redactor instances directly');
}
}
async redactAudio(transcriptionData, audioPath, audioOutputPath, options) {
throw new Error("Method 'redactAudio' must be implemented.");
}
}
module.exports = Redactor;

19
lib/transcribe.js Normal file
View File

@@ -0,0 +1,19 @@
const { createDeepGramClient } = require('./utils');
const fs = require('fs');
const { processAudio } = require('./get-transcription-deepgram');
const assert = require('assert');
function getTranscription(vendor, apiKey, filePath) {
assert.ok(['deepgram'].includes(vendor), 'vendor not supported');
if (vendor === 'deepgram') {
const deepgramClient = createDeepGramClient(apiKey);
assert.ok(deepgramClient, 'Invalid Deepgram API key');
return processAudio(deepgramClient, filePath);
}
else {
throw new Error(`Unsupported vendor: ${vendor}`);
}
}
module.exports = {getTranscription};

View File

@@ -1,27 +0,0 @@
// transcriber.js
class Transcriber {
constructor(audioFilePath) {
if (new.target === Transcriber) {
throw new TypeError('Cannot construct Transcriber instances directly');
}
this.audioDir = audioFilePath;
}
async transcribeFile(filePath) {
throw new Error("Method 'transcribeFile' must be implemented.");
}
async redactFile(filePath) {
throw new Error("Method 'redactFile' must be implemented.");
}
async analyzeText(text) {
throw new Error("Method 'analyzeText' must be implemented.");
}
async processAudio(filePath) {
throw new Error("Method 'processAudio' must be implemented.");
}
}
module.exports = Transcriber;

View File

@@ -1,26 +1,9 @@
const DeepgramTranscriber = require('./deepgramTranscriber');
const DeepgramRedactor = require('./deepgramRedactor');
class AudioProcessing {
static getTranscriber(serviceType,DEEPGRAM_API_KEY, audioFilePath) {
switch (serviceType) {
case 'deepgram':
return new DeepgramTranscriber(DEEPGRAM_API_KEY, audioFilePath);
const { createClient } = require('@deepgram/sdk');
default:
throw new Error('Unknown transcription service');
}
}
static getRedactor(serviceType, audioFilePath) {
switch (serviceType) {
case 'deepgram':
return new DeepgramRedactor(audioFilePath);
default:
throw new Error('Unknown redaction service');
}
}
function createDeepGramClient(DEEPGRAM_API_KEY) {
return createClient(DEEPGRAM_API_KEY);
}
module.exports = AudioProcessing;
module.exports = {createDeepGramClient};