Skip to content

Instantly share code, notes, and snippets.

@martinbowling
Created January 17, 2023 15:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinbowling/fcc555f2139521ea0e0a7e5443607cb8 to your computer and use it in GitHub Desktop.
Save martinbowling/fcc555f2139521ea0e0a7e5443607cb8 to your computer and use it in GitHub Desktop.
transcribe mp3 to text via Whisper
<?php
$req_url = "https://api.openat.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