Skip to content

Instantly share code, notes, and snippets.

@tobaco
Forked from martinbowling/whisper.php
Created December 6, 2024 14:13
Show Gist options
  • Save tobaco/4d567dbf16e5c857a4af7635af63a1cf to your computer and use it in GitHub Desktop.
Save tobaco/4d567dbf16e5c857a4af7635af63a1cf to your computer and use it in GitHub Desktop.
transcribe mp3 to text via Whisper
<?php
$req_url = "https://api.openai.com/v1/engines/audio-transcribe-801/transcriptions";
$openai_key = getenv("OPENAI_API_KEY");
$file_path = "test.mp3";
$file = file_get_contents($file_path);
$form_fields = array(
'file' => array($file_path, $file, 'audio/mpeg')
);
$boundary = implode('', array_map(function($ch) { return $ch[rand(0, strlen($ch) - 1)]; }, array_fill(0, 16, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')));
$m = new CURLFile($file_path, 'audio/mpeg', 'file');
$headers = array(
"Authorization: Bearer {$openai_key}",
"Content-Type: multipart/form-data; boundary={$boundary}"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $req_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $m);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
print_r($response);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment