Skip to content

Commit

Permalink
Add retries for OpenAI, increase memory, add queue reject
Browse files Browse the repository at this point in the history
  • Loading branch information
nkalupahana committed Jun 26, 2024
1 parent 20d9285 commit e34db52
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion scheduled-services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"start": "node lib/index.js",
"build": "tsc",
"deploy": "npm run build && gcloud run deploy scheduled-services --source . --project getbaselineapp --region us-central1 --min-instances=0 --memory=1G --max-instances=5 --ingress=all --service-account=scheduled-services@getbaselineapp.iam.gserviceaccount.com --no-allow-unauthenticated"
"deploy": "npm run build && gcloud run deploy scheduled-services --source . --project getbaselineapp --region us-central1 --min-instances=0 --memory=4G --max-instances=5 --ingress=all --service-account=scheduled-services@getbaselineapp.iam.gserviceaccount.com --no-allow-unauthenticated"
},
"author": "Nisala Kalupahana",
"license": "AGPL-3.0",
Expand Down
52 changes: 38 additions & 14 deletions scheduled-services/src/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export const processAudio = async (req: Request, res: Response) => {
const inputFilepath = `/tmp/${id}`;
const outputFilepath = `/tmp/${id}.ogg`;

// Get log
const db = getDatabase();
const ref = db.ref(`/${body.user}/logs/${body.log}`);
let logData = await (await ref.get()).val();
logData = JSON.parse(AES.decrypt(logData.data, body.encryptionKey).toString(aesutf8));

// Already processed? Skip.
if (logData.audio !== "inprogress") {
res.sendStatus(204);
return;
}

await getStorage().bucket().file(body.file).download({ destination: inputFilepath });

// Convert to starting bitrate ogg
Expand All @@ -62,25 +74,41 @@ export const processAudio = async (req: Request, res: Response) => {
form.append("file", new Blob([fs.readFileSync(outputFilepath)], { type: "audio/ogg" }));
form.append("model", "whisper-1");

const response = await fetch("https://api.openai.com/v1/audio/transcriptions", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.OPENAI_API_KEY}`
},
body: form
});

if (response.ok) {
let retryCount = 5;

let response;
while (retryCount > 0) {
response = await fetch("https://api.openai.com/v1/audio/transcriptions", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.OPENAI_API_KEY}`
},
body: form
});

if (!response.ok) {
console.log("OPENAI RETRY");
retryCount--;
await new Promise(res => setTimeout(res, 10000));
} else {
// Exit loop
retryCount = 0;
}
}

if (response && response.ok) {
const json = await response.json();
if (json.text) {
text = json.text;
} else {
console.error(new Error("OPENAI TRANSCRIPTION FAILED - 1"));
console.error(json);
}
} else {
} else if (response) {
console.error(new Error("OPENAI TRANSCRIPTION FAILED - 2"));
console.error(await response.text());
} else {
console.error(new Error("OPENAI TRANSCRIPTION FAILED - 3"));
}
}

Expand All @@ -103,10 +131,6 @@ export const processAudio = async (req: Request, res: Response) => {
await getStorage().bucket().file(`user/${body.user}/${encFilename}`).save(mp3Encrypted);

// Update log
const db = getDatabase();
const ref = db.ref(`/${body.user}/logs/${body.log}`);
let logData = await (await ref.get()).val();
logData = JSON.parse(AES.decrypt(logData.data, body.encryptionKey).toString(aesutf8));
logData.audio = encFilename;
logData.journal = text;

Expand Down

0 comments on commit e34db52

Please sign in to comment.