masterfermin02 / php-audio-text-summary
PHP package to transcribe recordings or audio to text and summary using google speech to text and chatGPT
v1.0.0
2023-02-07 01:36 UTC
Requires
- php: ^8.1
- google/cloud-speech: ^1.9
- openai-php/client: ^0.3.0
Requires (Dev)
- laravel/pint: ^1.2
- phpunit/phpunit: ^9
README
PHP package speech to text (audio, recordings) and make a summary.
This package is using the OpenAI and Google cloud speech to text
Google Authentication
Please see our Authentication guide for more information on authenticating your client. Once authenticated, you'll be ready to start making requests.
ChatGPT ApiKey
Requirement
Requires PHP 8.1+
Installation
You can install the package via composer:
composer require masterfermin02/php-audio-text-summary
Usage
use Google\Cloud\Speech\V1\RecognitionAudio; use Google\Cloud\Speech\V1\RecognitionConfig; use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding; use DaimonDove\Transcription\Transcriber; $client = OpenAI::client('YOUR_API_KEY'); $googleCredentials = ['credentials' => 'YOUR_GOOGLE_API_CREDENTAILS']; $speechToText = Transcriber::create($client, $googleCredentials); # The name of the audio file to transcribe $gcsURI = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw'; # set string as audio content $audio = (new RecognitionAudio()) ->setUri($gcsURI); # The audio file's encoding, sample rate and language $config = new RecognitionConfig([ 'encoding' => AudioEncoding::LINEAR16, 'sample_rate_hertz' => 16000, 'language_code' => 'en-US' ]); echo $speechToText->recognize($config, $audio) ->summary(); // Get others alternatives for tests $response = $speechToText->getRecognizeText(); # Print most likely transcription foreach ($response->getResults() as $result) { $alternatives = $result->getAlternatives(); $mostLikely = $alternatives[0]; $transcript = $mostLikely->getTranscript(); printf('Summary: %s' . PHP_EOL, $speechToText->summaryText($transcript)); }
You can also use a file resource
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding; use Google\Cloud\Speech\V1\RecognitionConfig; use Google\Cloud\Speech\V1\StreamingRecognitionConfig; use DaimonDove\Transcription\Transcriber; $recognitionConfig = new RecognitionConfig(); $recognitionConfig->setEncoding(AudioEncoding::FLAC); $recognitionConfig->setSampleRateHertz(44100); $recognitionConfig->setLanguageCode('en-US'); $config = new StreamingRecognitionConfig(); $config->setConfig($recognitionConfig); $audioResource = fopen('path/to/audio.flac', 'r'); $client = OpenAI::client('YOUR_API_KEY'); $googleCredentials = ['credentials' => 'YOUR_GOOGLE_API_CREDENTAILS']; $speechToText = Transcriber::create($client, $googleCredentials); echo $speechToText->recognizeAudioStream($config, $audioResource)->summary(); $responses $speechToText->getRecognizeAudioStreamText(); foreach ($responses as $element) { // doSomethingWith($element); }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.