@aj-archipelago/cortex 1.1.12 → 1.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -87,32 +87,24 @@ async function splitMediaFile(inputPath, chunkDurationInSeconds = 500) {
|
|
|
87
87
|
inputPath = downloadPath;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
|
|
91
90
|
const metadata = await ffmpegProbe(inputPath);
|
|
92
91
|
const duration = metadata.format.duration;
|
|
93
92
|
const numChunks = Math.ceil((duration - 1) / chunkDurationInSeconds);
|
|
94
93
|
|
|
95
94
|
const chunkPromises = [];
|
|
96
|
-
|
|
97
|
-
|
|
95
|
+
const chunkOffsets = [];
|
|
98
96
|
|
|
99
97
|
for (let i = 0; i < numChunks; i++) {
|
|
100
|
-
const outputFileName = path.join(
|
|
101
|
-
|
|
102
|
-
`chunk-${i + 1}-${path.parse(inputPath).name}.mp3`
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
const chunkPromise = processChunk(
|
|
106
|
-
inputPath,
|
|
107
|
-
outputFileName,
|
|
108
|
-
i * chunkDurationInSeconds,
|
|
109
|
-
chunkDurationInSeconds
|
|
110
|
-
);
|
|
98
|
+
const outputFileName = path.join(uniqueOutputPath, `chunk-${i + 1}-${path.parse(inputPath).name}.mp3`);
|
|
99
|
+
const offset = i * chunkDurationInSeconds;
|
|
111
100
|
|
|
101
|
+
const chunkPromise = processChunk(inputPath, outputFileName, offset, chunkDurationInSeconds);
|
|
102
|
+
|
|
112
103
|
chunkPromises.push(chunkPromise);
|
|
104
|
+
chunkOffsets.push(offset);
|
|
113
105
|
}
|
|
114
106
|
|
|
115
|
-
return { chunkPromises, uniqueOutputPath };
|
|
107
|
+
return { chunkPromises, chunkOffsets, uniqueOutputPath };
|
|
116
108
|
} catch (err) {
|
|
117
109
|
const msg = `Error processing media file, check if the file is a valid media file or is accessible`;
|
|
118
110
|
console.error(msg, err);
|
|
@@ -143,7 +143,7 @@ async function main(context, req) {
|
|
|
143
143
|
file = await processYoutubeUrl(file);
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
const { chunkPromises, uniqueOutputPath } = await splitMediaFile(file);
|
|
146
|
+
const { chunkPromises, chunkOffsets, uniqueOutputPath } = await splitMediaFile(file);
|
|
147
147
|
folder = uniqueOutputPath;
|
|
148
148
|
|
|
149
149
|
numberOfChunks = chunkPromises.length; // for progress reporting
|
|
@@ -158,9 +158,11 @@ async function main(context, req) {
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
// sequential processing of chunks
|
|
161
|
-
for (
|
|
161
|
+
for (let index = 0; index < chunks.length; index++) {
|
|
162
|
+
const chunk = chunks[index];
|
|
162
163
|
const blobName = useAzure ? await saveFileToBlob(chunk, requestId) : await moveFileToPublicFolder(chunk, requestId);
|
|
163
|
-
|
|
164
|
+
const chunkOffset = chunkOffsets[index];
|
|
165
|
+
result.push({ uri:blobName, offset:chunkOffset });
|
|
164
166
|
context.log(`Saved chunk as: ${blobName}`);
|
|
165
167
|
sendProgress();
|
|
166
168
|
}
|
|
@@ -182,7 +184,10 @@ async function main(context, req) {
|
|
|
182
184
|
}
|
|
183
185
|
}
|
|
184
186
|
|
|
185
|
-
console.log(
|
|
187
|
+
console.log('result:', result.map(item =>
|
|
188
|
+
typeof item === 'object' ? JSON.stringify(item, null, 2) : item
|
|
189
|
+
).join('\n'));
|
|
190
|
+
|
|
186
191
|
context.res = {
|
|
187
192
|
body: result
|
|
188
193
|
};
|
package/lib/requestExecutor.js
CHANGED
|
@@ -323,8 +323,13 @@ const makeRequest = async (cortexRequest) => {
|
|
|
323
323
|
return { response, duration };
|
|
324
324
|
}
|
|
325
325
|
} else {
|
|
326
|
-
// if there are multiple endpoints, retry everything
|
|
327
|
-
// could be
|
|
326
|
+
// if there are multiple endpoints, retry everything by default
|
|
327
|
+
// as it could be a temporary issue with one endpoint
|
|
328
|
+
// certain errors (e.g. 400) are problems with the request itself
|
|
329
|
+
// and should not be retried
|
|
330
|
+
if (status == 400) {
|
|
331
|
+
return { response, duration };
|
|
332
|
+
}
|
|
328
333
|
cortexRequest.selectNewEndpoint();
|
|
329
334
|
}
|
|
330
335
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aj-archipelago/cortex",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.13",
|
|
4
4
|
"description": "Cortex is a GraphQL API for AI. It provides a simple, extensible interface for using AI services from OpenAI, Azure and others.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"repository": {
|
|
@@ -15,6 +15,7 @@ import { v4 as uuidv4 } from 'uuid';
|
|
|
15
15
|
import { promisify } from 'util';
|
|
16
16
|
import { publishRequestProgress } from '../../lib/redisSubscription.js';
|
|
17
17
|
import logger from '../../lib/logger.js';
|
|
18
|
+
import CortexRequest from '../../lib/cortexRequest.js';
|
|
18
19
|
const pipeline = promisify(stream.pipeline);
|
|
19
20
|
|
|
20
21
|
const API_URL = config.get('whisperMediaApiUrl');
|
|
@@ -25,7 +26,7 @@ if(WHISPER_TS_API_URL){
|
|
|
25
26
|
logger.warn(`WHISPER API URL not set using default OpenAI API Whisper`);
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
const OFFSET_CHUNK =
|
|
29
|
+
const OFFSET_CHUNK = 500; //seconds of each chunk offset, only used if helper does not provide
|
|
29
30
|
|
|
30
31
|
async function deleteTempPath(path) {
|
|
31
32
|
try {
|
|
@@ -96,7 +97,7 @@ function convertToText(str) {
|
|
|
96
97
|
.join(' ');
|
|
97
98
|
}
|
|
98
99
|
|
|
99
|
-
function alignSubtitles(subtitles, format) {
|
|
100
|
+
function alignSubtitles(subtitles, format, offsets) {
|
|
100
101
|
const result = [];
|
|
101
102
|
|
|
102
103
|
function preprocessStr(str) {
|
|
@@ -116,7 +117,7 @@ function alignSubtitles(subtitles, format) {
|
|
|
116
117
|
}
|
|
117
118
|
|
|
118
119
|
for (let i = 0; i < subtitles.length; i++) {
|
|
119
|
-
result.push(...shiftSubtitles(subtitles[i], i
|
|
120
|
+
result.push(...shiftSubtitles(subtitles[i], offsets[i]*1000)); // convert to milliseconds
|
|
120
121
|
}
|
|
121
122
|
|
|
122
123
|
try {
|
|
@@ -171,12 +172,14 @@ class OpenAIWhisperPlugin extends ModelPlugin {
|
|
|
171
172
|
// Execute the request to the OpenAI Whisper API
|
|
172
173
|
async execute(text, parameters, prompt, cortexRequest) {
|
|
173
174
|
const { pathwayResolver } = cortexRequest;
|
|
175
|
+
|
|
174
176
|
const { responseFormat, wordTimestamped, highlightWords, maxLineWidth, maxLineCount, maxWordsPerLine } = parameters;
|
|
175
|
-
cortexRequest.url = this.requestUrl(text);
|
|
176
177
|
|
|
177
178
|
const chunks = [];
|
|
178
179
|
const processChunk = async (uri) => {
|
|
179
180
|
try {
|
|
181
|
+
const cortexRequest = new CortexRequest({ pathwayResolver });
|
|
182
|
+
|
|
180
183
|
const chunk = await downloadFile(uri);
|
|
181
184
|
chunks.push(chunk);
|
|
182
185
|
|
|
@@ -205,6 +208,8 @@ class OpenAIWhisperPlugin extends ModelPlugin {
|
|
|
205
208
|
}
|
|
206
209
|
|
|
207
210
|
const processTS = async (uri) => {
|
|
211
|
+
const cortexRequest = new CortexRequest({ pathwayResolver });
|
|
212
|
+
|
|
208
213
|
const tsparams = { fileurl:uri };
|
|
209
214
|
const { language } = parameters;
|
|
210
215
|
if(language) tsparams.language = language;
|
|
@@ -315,14 +320,20 @@ async function processURI(uri) {
|
|
|
315
320
|
return result;
|
|
316
321
|
}
|
|
317
322
|
|
|
323
|
+
let offsets = [];
|
|
324
|
+
let uris = []
|
|
325
|
+
|
|
318
326
|
try {
|
|
319
|
-
const
|
|
327
|
+
const mediaChunks = await this.getMediaChunks(file, requestId);
|
|
320
328
|
|
|
321
|
-
if (!
|
|
329
|
+
if (!mediaChunks || !mediaChunks.length) {
|
|
322
330
|
throw new Error(`Error in getting chunks from media helper for file ${file}`);
|
|
323
331
|
}
|
|
324
332
|
|
|
325
|
-
|
|
333
|
+
uris = mediaChunks.map((chunk) => chunk?.uri || chunk);
|
|
334
|
+
offsets = mediaChunks.map((chunk, index) => chunk?.offset || index * OFFSET_CHUNK);
|
|
335
|
+
|
|
336
|
+
totalCount = mediaChunks.length + 1; // total number of chunks that will be processed
|
|
326
337
|
|
|
327
338
|
const batchSize = 2;
|
|
328
339
|
sendProgress();
|
|
@@ -369,7 +380,7 @@ try {
|
|
|
369
380
|
}
|
|
370
381
|
|
|
371
382
|
if (['srt','vtt'].includes(responseFormat) || wordTimestamped) { // align subtitles for formats
|
|
372
|
-
return alignSubtitles(result, responseFormat);
|
|
383
|
+
return alignSubtitles(result, responseFormat, offsets);
|
|
373
384
|
}
|
|
374
385
|
return result.join(` `);
|
|
375
386
|
}
|