1. Go to this page and download the library: Download shadowfiend/whisper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shadowfiend / whisper example snippets
use Sf\Whisper\Whisper;
$provider = new YourWhisperProvider();
$provider->setOptions([
'api_key' => env('whisper_service_api_key'), // If your WhisperProvider should us API key
'model' => 'whisper-1', // Whatever else in your options for whisper provider
]);
$whisper = new Whisper();
$whisper->addProvider('default', $provider);
$transcript = $whisper
->useProvider('default')
->transcribe('/path/to/audio.mp3');
echo $transcript->getText();
$transcript = $whisper->default->transcribe('/path/to/audio.mp3');
// Examples of registered providers, you should add it in your app before through addProvider()
$transcript = $whisper->openai->transcribe('/path/to/audio.mp3');
$transcript = $whisper->google->transcribe('/path/to/audio.mp3');
$transcript = $whisper->local->transcribe('/path/to/audio.mp3');
use Psr\Http\Message\StreamInterface;
use Sf\Whisper\Providers\AbstractWhisperProvider;
use Sf\Whisper\Transcript;
final class YourWhisperProvider extends AbstractWhisperProvider
{
protected function doTranscribe(StreamInterface $audio): Transcript
{
$audioContents = $audio->getContents();
// Send $audioContents to your transcription service and map its response.
$response = callYourTranscriptionService($audioContents, $this->options);
$transcript = $Transcript::create(
text: $response['text'],
duration: $response['duration'],
);
// If you have segments you can use addSegment() or addSegments()
$transcript->setSegments(TranscriptSegment[]);
}
}
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\StreamInterface;
use Sf\Whisper\Sources\AudioSourceInterface;
final class MemoryAudioSource implements AudioSourceInterface
{
public function __construct(
private readonly string $contents
)
{
}
public function extract(): StreamInterface
{
return Utils::streamFor($this->contents);
}
}
$transcript = $whisper->transcribe(new MemoryAudioSource($audioContents));
use Sf\Whisper\Providers\MockWhisperProvider;
use Sf\Whisper\Sources\MockAudioSource;
use Sf\Whisper\Whisper;
$whisper = new Whisper();
$whisper->addProvider('mock', new MockWhisperProvider());
$transcript = $whisper->useProvider('mock')->transcribe(new MockAudioSource());
echo $transcript->getText(); // Hello from mock i am mock
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.