Created
January 17, 2023 15:09
-
-
Save martinbowling/fcc555f2139521ea0e0a7e5443607cb8 to your computer and use it in GitHub Desktop.
transcribe mp3 to text via Whisper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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