-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.js
118 lines (103 loc) · 3.58 KB
/
convert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const { SpeechClient } = require('@google-cloud/speech');
const fs = require('fs'); // Require the fs module for file operations
const { Storage } = require('@google-cloud/storage');
process.env.GOOGLE_APPLICATION_CREDENTIALS = 'orbital-wording-382318-f1bf751370f5.json';
const language = require('@google-cloud/language');
const sentiment_client = new language.LanguageServiceClient();
const storage = new Storage({
keyFilename: 'orbital-wording-382318-f1bf751370f5.json' // Replace with the path to your JSON key file
});
// Retrieve list of audio files from folder in Google Cloud Storage
const bucketName = 'adonisbucket';
const directoryPath = 'phonecalldata/test'; // Replace with the path to your directory within the
const bucket = storage.bucket(bucketName);
// List all objects in the directory
let cnt=-1;
bucket.getFiles({ prefix: directoryPath }, (err, files) => {
if (err) {
console.error(`Error retrieving files: ${err}`);
return;
}
// Process each file
files.forEach(file => {
cnt=-1;
// Transcribe audio file with speaker diarization
transcribeAudioWithDiarization(file);
});
});
async function transcribeAudioWithDiarization(file){
const speechClient=new SpeechClient();
// Configure recognition settings with speaker diarization enabled
const request = {
config: {
"enableWordTimeOffsets": true,
"diarizationSpeakerCount": 2,
"enableAutomaticPunctuation": true,
"encoding": "LINEAR16",
"languageCode": "en-US",
"model": "phone_call",
diarizationConfig: {
enableSpeakerDiarization: true,
minSpeakerCount: 2,
maxSpeakerCount: 2,
},
},
audio: {
uri: `gs://${file.bucket.name}/${file.name}`, // Use audio file URI instead of content
},
};
// Perform speech recognition with speaker diarization
const sentiment={
score:'',
content:'',
}
let transcriptions=[];
speechClient
.longRunningRecognize(request)
.then(([operation]) => {
return operation.promise();
})
.then(results => {
const resultTranscriptions = results[0].results
.map(result => {
const {transcript } = result.alternatives[0];
const start_time = result.alternatives[0].words[0].startTime.seconds + '.' + result.alternatives[0].words[0].startTime.nanos / 100000000;
const end_time = result.alternatives[0].words[result.alternatives[0].words.length - 1].endTime.seconds + '.' + result.alternatives[0].words[result.alternatives[0].words.length - 1].endTime.nanos / 100000000;
const doc = {
type: 'PLAIN_TEXT',
content: transcript,
score:0,
magnitude:0,
};
cnt++;
return {
speaker: `Speaker ${cnt%2+1}`,
start_time,
end_time,
transcript
};
});
// Add result transcriptions to the overall transcriptions array
transcriptions = [...transcriptions, ...resultTranscriptions];
// transcriptions.forEach(item=>{
// const document={
// content:item.transcript,
// type:'PLAIN_TEXT',
// }
// sentiment_client
// .analyzeSentiment({document:document})
// .then(results=>{
// const sentiment=results[0].documentSentiment;
// item={item,
// "score":sentiment.score,
// }
// console.log(item);
// })
// }
// );
// console.log(transcriptions);
})
.catch(err => {
console.error('Error:', err);
});
}