How can I send an audio file via Whisper API?

Hello. I tried many ways to use whisper API in React native and couldn’t get a result. I’m so confused now and I don’t know what to do. Here’s how far I’ve come: I recorded a sound with the react-native-audio-recorder-player package. The recording location of this sound is “file:///data/user/0/com.xyz.app/cache/sound.mp4”. Now I will send this mp4 file to OpenAI’s whisper API. At this stage, I tried everything such as RNFS, blob, formdata etc. and could not get any results. Can someone please explain exactly what I should do at this stage?

texport const getCompletion5 = async (key) => {

    const configuration = new Configuration({ apiKey: key });
    const openai = new OpenAI(configuration);

    // sound file path
    const filePath = "file:///data/user/0/com.asd.xyz/cache/sound.mp4";

    // how should I prepare this??
    let audioFile;

    try {
        openai.audio.transcriptions.create({
            file: audioFile,
            model: "whisper-1",
        }).then(transcription => {
            console.log(transcription.text);
            return transcription.text
        }).catch(error => {
            console.error("error 1:", error);
        });

    } catch (error) {
        console.error("error 2:", error);
    }

}

Are you using the openai-node package? At the bottom of the README on GitHub it states it currently does not support React Native

Yes, I use React Native, but this is not a problem. Because with additional settings, I was able to do everything except sending mp3 to whisper. But I haven’t figured out how to send mp3 to whisper yet.

not sure if you still need this, but if anyone else comes across this, this is what worked for me:

const form = new FormData();
form.append('model', 'whisper-1');
form.append('file', {
    uri: uri, // file:///path/to/file.m4a
    type: 'audio/mp4',
    name: 'recording.m4a'
});

const response = await axios.post('https://api.openai.com/v1/audio/transcriptions', form, {
  headers: {
	'Authorization': 'Bearer <API Key>',
	'Content-Type': 'multipart/form-data'
  }
})
1 Like

Hi, I write the same code but I got an error: TypeError: Network request failed when I use my android mobile phone, but it’s ok when I run it in my web browser.