Request symfony for "speech to text"

Hi all,

I’m trying to create a request in Symfony with HttpClientInterface but it’s impossible to get the right format to send my audio file…

Here is my request:

    $projectDir = $this->params->get('kernel.project_dir');
    $tmpDirectory = $projectDir . '\\var\\tmp\\';
    $newFileName = uniqid() . '.' . $audio->guessExtension();
    $audio->move($tmpDirectory, $newFileName);

    $data = [
        'model' => 'whisper-1',
        'file' => DataPart::fromPath($tmpDirectory.$newFileName),
    ];

    $formData = new FormDataPart($data);

    try {
        $response = $this->client->request('POST', 'https://api.openai.com/v1/audio/transcriptions', [
            'headers' => [
                'Authorization' => 'Bearer sk-****',
                'Content-Type' => 'multipart/form-data',
            ],
            'body' => $formData->bodyToIterable(),
        ]);

        $content = $response->toArray();

        return $content; 
    } catch (\Exception $e) {
        throw $e;
    }

does anyone see an error?

For symfony users, here is an example of how to request a transcription:

<?php 

// get default dir project
$projectDir = $this->params->get('kernel.project_dir');
// Dir of your audio file
$tmpDirectory = $projectDir . '/var/tmp/';
// get extension with unique name
$newFileName = uniqid() . '.' . $audio->guessExtension();
// move file in tmp dir
$audio->move($tmpDirectory, $newFileName);

// Here create array with model of whisper and Datapart in Symfony is used to easily send an audio file in a multipart HTTP request.
$data = [
    'model' => 'whisper-1',
    'file' => DataPart::fromPath($tmpDirectory.$newFileName),
];

// Creating a FormDataPart object with form data
$formData = new FormDataPart($data);

// Retrieving prepared headers for the multipart/form-data request
$headers = $formData->getPreparedHeaders()->toArray();

// add Autorization header, !!! $this->params->get('API_KEY') is environnement variable !!!
$headers['Authorization'] = 'Bearer '.$this->params->get('API_KEY');

try {
    // Sending a POST request to the specified URL with headers and body
    $response = $this->client->request('POST', 'https://api.openai.com/v1/audio/transcriptions', [
        'headers' => $headers, // using prepared headers
        'body' => $formData->bodyToIterable(), // sending the multipart/form-data body
    ]);

    // Decoding the JSON response to an array
    $content = $response->toArray();

    // Removing the temporary file after processing
    unlink($tmpDirectory.$newFileName);

    // Returning the 'text' element from the response
    return $content['text'];
    
} catch (\Exception $e) {
    // Propagating the exception if any occurs
    throw $e;
}