hi, i’m building a nuxt application and i’m trying to implement the openai whisper api for speech recognition. I’m passing an audio file from the frontend to an api in the backend. this is the frontend code:
const formData = new FormData();
formData.append('file', selectedFile);
result.value = 'File loaded!';
const response = await $fetch('/api/transcribe', {
method: 'POST',
body: formData,
});.
In the backend the code look like this:
import OpenAI from "openai";
export default defineEventHandler(async (event) => {
const { CHAT_KEY } = useRuntimeConfig();
const openai = new OpenAI({ apiKey: CHAT_KEY, timeout: 90000 });
try {
const formData = await readMultipartFormData(event);
const file = formData?.[0];
const blob = new Blob([file.data], { type: file.type });
const form = new FormData();
form.append("file", blob, file.filename);
const transcription = await openai.audio.transcriptions.create({
file: form,
model: "whisper-1",
});
console.log(transcription.text);
return transcription.text
} catch (error) {
const message = error.response?.data?.error?.message || error.message || "Server error";
throw createError({ statusCode: 500, message });
}
});
The problem is that i don’t want to save the file in a database so i’m trying to process it and send it to the api. I’ve tried lots of thing but none worked. Is there a way to do this?