-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.js
More file actions
237 lines (202 loc) · 7.48 KB
/
lambda.js
File metadata and controls
237 lines (202 loc) · 7.48 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Once audio is uploaded to S3, this Lambda function is triggered to send the audio off to AssemblyAI for transcription
// and then stores the transcript results back in S3.
// Import required AWS SDK modules
import { S3 } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { GetObjectCommand } from '@aws-sdk/client-s3';
// Configure logging
const logger = {
info: (data) => console.log(JSON.stringify(data)),
error: (data) => console.error(JSON.stringify(data))
};
// Configuration settings for AssemblyAI
// See config parameters here: https://www.assemblyai.com/docs/api-reference/transcripts/submit
const ASSEMBLYAI_CONFIG = {
multichannel: true // Using multichannel here as we told Genesys to send us multichannel audio.
};
// Initialize AWS S3 client
const s3Client = new S3();
/**
* Generate a presigned URL for the S3 object
* @param {string} bucket - S3 bucket name
* @param {string} key - S3 object key
* @param {number} expiration - URL expiration time in seconds
* @returns {Promise<string>} Presigned URL
*/
const getPresignedUrl = async (bucket, key, expiration = 3600) => {
logger.info({
message: "Generating presigned URL",
bucket: bucket,
key: key,
expiration: expiration
});
const command = new GetObjectCommand({
Bucket: bucket,
Key: key
});
return getSignedUrl(s3Client, command, { expiresIn: expiration });
}
/**
* Delete transcript data from AssemblyAI's database
* @param {string} transcriptId - The AssemblyAI transcript ID to delete
* @param {string} apiKey - The AssemblyAI API key
* @returns {Promise<boolean>} True if deletion was successful, False otherwise
*/
const deleteTranscriptFromAssemblyAI = async (transcriptId, apiKey) => {
try {
const response = await fetch(`https://api.assemblyai.com/v2/transcript/${transcriptId}`, {
method: 'DELETE',
headers: {
'authorization': apiKey,
'content-type': 'application/json'
}
});
if (response.ok) {
logger.info(`Successfully deleted transcript ${transcriptId} from AssemblyAI`);
return true;
} else {
const errorData = await response.text();
logger.error(`Failed to delete transcript ${transcriptId}: HTTP ${response.status} - ${errorData}`);
return false;
}
} catch (error) {
logger.error(`Error deleting transcript ${transcriptId}: ${error.message}`);
return false;
}
}
/**
* Submit audio for transcription
* @param {object} requestData - Request data including audio URL and config
* @param {string} apiKey - AssemblyAI API key
* @returns {Promise<string>} Transcript ID
*/
const submitTranscriptionRequest = async (requestData, apiKey) => {
const response = await fetch('https://api.assemblyai.com/v2/transcript', {
method: 'POST',
headers: {
'authorization': apiKey,
'content-type': 'application/json'
},
body: JSON.stringify(requestData)
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to submit audio for transcription: ${errorText}`);
}
const responseData = await response.json();
const transcriptId = responseData.id;
logger.info({
message: "Audio submitted for transcription",
transcript_id: transcriptId
});
return transcriptId;
}
/**
* Poll for transcription completion
* @param {string} transcriptId - Transcript ID
* @param {string} apiKey - AssemblyAI API key
* @returns {Promise<object>} Transcription data
*/
const pollTranscriptionStatus = async (transcriptId, apiKey) => {
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// Keep polling until we get a completion or error
while (true) {
const response = await fetch(`https://api.assemblyai.com/v2/transcript/${transcriptId}`, {
method: 'GET',
headers: {
'authorization': apiKey,
'content-type': 'application/json'
}
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to poll transcription status: ${errorText}`);
}
const pollingData = await response.json();
if (pollingData.status === 'completed') {
logger.info({ message: "Transcription completed successfully" });
return pollingData;
} else if (pollingData.status === 'error') {
throw new Error(`Transcription failed: ${pollingData.error}`);
}
// Wait before polling again
await sleep(3000);
}
}
/**
* Transcribe audio using AssemblyAI API
* @param {string} audioUrl - URL of the audio file
* @param {string} apiKey - AssemblyAI API key
* @returns {Promise<object>} Transcription data
*/
const transcribeAudio = async (audioUrl, apiKey) => {
logger.info({ message: "Starting audio transcription" });
// Prepare request data with config parameters
const requestData = { audio_url: audioUrl, ...ASSEMBLYAI_CONFIG };
// Submit the audio file for transcription
const transcriptId = await submitTranscriptionRequest(requestData, apiKey);
// Poll for transcription completion
return await pollTranscriptionStatus(transcriptId, apiKey);
}
/**
* Lambda function handler
* @param {object} event - S3 event
* @param {object} context - Lambda context
* @returns {Promise<object>} Response
*/
export const handler = async (event, context) => {
try {
// Get the AssemblyAI API key from environment variables
const apiKey = process.env.ASSEMBLYAI_API_KEY;
if (!apiKey) {
throw new Error("ASSEMBLYAI_API_KEY environment variable is not set");
}
// Process each record in the S3 event
const records = event.Records || [];
for (const record of records) {
// Get the S3 bucket and key
const bucket = record.s3.bucket.name;
const key = decodeURIComponent(record.s3.object.key.replace(/\+/g, ' '));
// Generate a presigned URL for the audio file
const audioUrl = await getPresignedUrl(bucket, key);
// Get the full transcript JSON from AssemblyAI
const transcriptData = await transcribeAudio(audioUrl, apiKey);
// Prepare the transcript key - maintaining path structure but changing directory and extension
const transcriptKey = key
.replace('audio', 'transcripts', 1)
.replace('.wav', '.json');
// Convert the JSON data to a string
const transcriptJsonStr = JSON.stringify(transcriptData, null, 2);
// Upload the transcript JSON to the same bucket but in transcripts directory
await s3Client.putObject({
Bucket: bucket, // Use the same bucket
Key: transcriptKey, // Store under the /transcripts directory
Body: transcriptJsonStr,
ContentType: 'application/json'
});
logger.info({
message: "Transcript uploaded to transcript bucket successfully.",
key: transcriptKey
});
// Uncomment the following line to delete transcript data from AssemblyAI after saving to S3
// https://www.assemblyai.com/docs/api-reference/transcripts/delete
// await deleteTranscriptFromAssemblyAI(transcriptData.id, apiKey);
}
return {
statusCode: 200,
body: JSON.stringify({
message: "Audio file(s) processed successfully",
detail: "Transcripts have been stored in the AssemblyAITranscripts directory"
})
};
} catch (error) {
console.error(`Error: ${error.message}`);
return {
statusCode: 500,
body: JSON.stringify({
message: "Error processing audio file(s)",
error: error.message
})
};
}
};