How to Get Audio Transcriptions from Whisper and not using a File System
Whisper is OpenAI’s clever speech-to-text transcription mannequin. It permits builders to enter audio and an elective styling immediate, and get transcribed textual content in response.
However, the official OpenAI Node.js SDK API docs solely present a method to make use of Whisper – studying an audio file with fs.
async operate primary() {
const transcription = await openai.audio.transcriptions.create({
file: fs.createReadStream("audio.mp3"),
mannequin: "whisper-1",
});
console.log(transcription.textual content);
}
That works high-quality if in case you have static information… however in any shopper utility, we’ll be processing information from an end-user shopper corresponding to an app or net browser. To obtain audio from 1000’s of customers and put it aside as information is a serious waste of disk house and an enormous ineffeciency. Plus, serverless deployment is extraordinarily widespread right this moment, and in a serverless atmosphere we normally do not have persistent file storage. I wrote this text as a result of it was surprisingly onerous to determine the best way to obtain audio transcription with out saving the audio as a file first.
How to make use of Whisper with out information
On the client-side, you may have to get your audio right into a Base64 encoded string. I’m utilizing the library "@ricky0123/vad-react" for this function, which comes with utilities to perform that:
onSpeechEnd: (audio) => {
const wavBuffer = utils.encodeWAV(audio);
const base64 = utils.arrayBufferToBase64(wavBuffer);
const audioUrlAsData = `${base64}`;
// selected POST right here with a payload to make sure the Base64 string does not violate the max size of a URL
fetch("/api/transcribe", {
technique: "POST",
physique: JSON.stringify({ audioData: audioUrlAsData }),
})
}
Then on the server-side, the trick is to create a buffer from the base64 information and use the undocumented toFile operate from OpenAI’s library.
import OpenAI, { toFile } from "openai";
const openai = new OpenAI({
apiKey: course of.env.OPENAI_API_KEY,
});
export default async operate handler(
req,
res
) {
strive {
// Extract Base64 encoded information from the request
const bodyData = JSON.parse(req.physique);
const base64Audio = bodyData.audioData;
// Decode Base64 to binary
const audioBuffer = Buffer.from(base64Audio, "base64");
// Use OpenAI API to transcribe the audio
const transcription = await openai.audio.transcriptions.create({
file: await toFile(audioBuffer, "audio.wav", {
contentType: "audio/wav",
}),
mannequin: "whisper-1",
});
// Send the transcription textual content as response
res.json({ transcription: transcription.textual content });
} catch (error) {
console.error("Error throughout transcription:", error);
res.standing(500).ship("Error throughout transcription");
}
}
Voila! Through this course of, you should utilize Whisper with out saving audio from each consumer as static information, permitting it for use in a serverless atmosphere.
HI-FI News
by way of DEV Community https://dev.to
December 10, 2023 at 10:42PM
-
Product on sale
Audiophile Vinyl Records Cleaning BundleOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Product on sale
Easy Start Vinyl Records Cleaning KitOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Vinyl Records Cleaner Easy Groove Concentrate€34.95 incl. VAT -
Easy Groove Super Set€199.00 incl. VAT -
Easy Groove Enzycaster – vinyl records prewash cleaner€34.95 incl. VAT -
Easy Groove Extreme MK. II- vinyl records prewash cleaner€45.00 incl. VAT -
Vinyl Records Cleaner – Easy Groove Virgin Concentrate€35.00 incl. VAT -
Easy Groove Spray&Wipe vinyl records cleaner€34.95 incl. VAT








