How do I send an audio file to OpenAi?

36 views Asked by At

I've verified I am recording an audio file correctly. I would now like to send it to open ai for speech to text. Here is my nextjs code pages/api/speechToText.js file:

import { OpenAI } from "openai";

const openai = new OpenAI(process.env.OPENAI_API_KEY);

export const config = {
  api: {
    bodyParser: false,
  },
};

export default async function handler(req, res) {
  const formidable = require("formidable");

  const form = new formidable.IncomingForm();

  form.parse(req, async (err, fields, files) => {
    try {
      const transcription = await openai.audio.transcriptions.create({
        file: files.audio[0],
        model: "whisper-1",
      });

      res.status(200).json({ transcription: transcription.text });
    } catch (error) {
      console.error("Error transcribing audio:", error);
      res.status(500).json({ error: "Error processing your request" });
    }
  });
}

When I attempt to run this code, I don't receive any transcription response, and the request seems to stall without any clear error message from the OpenAI API or my Next.js API route.

Issues I'm Facing:

  • No transcription result is returned, and the request seems to stall.
  • I'm unsure if the audio file is being correctly sent to the OpenAI API due to the asynchronous nature of the formidable library's form.parse method.
  • Debugging attempts haven't yielded clear insights into where the process is failing.

What I've Tried:

  • Ensuring that process.env.OPENAI_API_KEY is correctly set and accessible within my API route.
  • Verifying the audio file exists and is correctly referenced by files.audio[0] in the formidable parse callback.
  • Adding console.log statements to debug the flow, which confirmed that the parsing occurs but stalls at the transcription request.

Questions:

  1. Is there something I'm missing in how I'm using the formidable library to handle the file upload in a Next.js API route?
  2. Could there be an issue with how I'm sending the file to OpenAI's API for transcription?
  3. Are there better practices or alternative approaches for handling file uploads in Next.js and sending them to an external API for processing?

Any guidance or suggestions on how to resolve these issues would be greatly appreciated. Thank you!

0

There are 0 answers