Skip to content

Instantly share code, notes, and snippets.

@codeyourwayup
Forked from Dosant/README.md
Created March 4, 2023 10:59
Show Gist options
  • Save codeyourwayup/adc29062eee86c39d44baab0291fbc78 to your computer and use it in GitHub Desktop.
Save codeyourwayup/adc29062eee86c39d44baab0291fbc78 to your computer and use it in GitHub Desktop.
AWS S3 to OpenAI Whisper via Node.js stream

Current (2023-03-01) OpenAI Whisper API expects a file uploaded as part of multipart/form-data in POST request. I initially struggled to use this API inside Node.js with an audio file stored inside an AWS s3 bucket, so I decided to share this snippet:

The API usage example from the OpenAI docs:

curl --request POST \
  --url https://api.openai.com/v1/audio/transcriptions \
  --header 'Authorization: Bearer TOKEN' \
  --header 'Content-Type: multipart/form-data' \
  --form file=@/path/to/file/openai.mp3 \
  --form model=whisper-1

See the Node.js + AWS S3 example below:

import fetch from "node-fetch";
import AWS from "aws-sdk"; // v2
import fs from "fs";
import FormData from "form-data";
// Set the region and access keys for your AWS account
AWS.config.update({
region: 'eu-central-1',
accessKeyId: '***',
secretAccessKey: '***'
});
// Create an S3 client
const s3 = new AWS.S3();
// Set the bucket and file key
const bucketName = 'openai-sample';
const fileKey = 'path/to/file/openai.mp3';
// Set the parameters for the S3 getObject operation
const params = {
Bucket: bucketName,
Key: fileKey
};
// Get audio metadata to retrieve size and type
s3.headObject(params, function(err, data) {
if (err) throw err;
// Get read object stream
const s3Stream = s3.getObject(params)
.createReadStream();
// create form data to be send to whisper api
const formData = new FormData();
// append stream with a file
formData.append('file', s3Stream, {
contentType: data.ContentType,
knownLength: data.ContentLength,
filename: fileKey
});
formData.append('model', 'whisper-1');
fetch('https://api.openai.com/v1/audio/transcriptions', {
method: 'POST',
body: formData,
headers: {
'Authorization': 'Bearer TOKEN',
...formData.getHeaders()
}
}).then(res => res.json()).then(json => console.log(json.text));
});
import fetch from "node-fetch";
import {S3Client, GetObjectCommand, HeadObjectCommand} from '@aws-sdk/client-s3'; //v3
import FormData from "form-data";
/*
curl --request POST \
--url https://api.openai.com/v1/audio/transcriptions \
--header 'Authorization: Bearer TOKEN' \
--header 'Content-Type: multipart/form-data' \
--form file=@/path/to/file/openai.mp3 \
--form model=whisper-1
*/
// Set the region and access keys for your AWS account
const s3 = new S3Client({
region: 'eu-central-1',
credentials: {
accessKeyId: '***',
secretAccessKey: '***'
}
});
// Set the bucket and file key
const bucketName = 'openai-sample';
const fileKey = 'path/to/file/openai.mp3';
// Set the parameters for the S3 getObject operation
const params = {
Bucket: bucketName,
Key: fileKey
};
// Call the S3 getObject operation with the specified parameters
const getObjectOutput = await s3.send(new GetObjectCommand(params));
const formData = new FormData();
// getObjectResult.Body is a ReadableStream
formData.append('file', getObjectOutput.Body, {
contentType: getObjectOutput.ContentType,
knownLength: getObjectOutput.ContentLength,
filename: fileKey
});
formData.append('model', 'whisper-1');
const result = await fetch('https://api.openai.com/v1/audio/transcriptions', {
method: 'POST',
body: formData,
headers: {
'Authorization': 'Bearer TOKEN',
...formData.getHeaders()
}
}).then(res => res.json());
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment