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

This package is auto-updated.

Last update: 2024-04-07 04:24:14 UTC


README

Latest Version on Packagist Tests Total Downloads

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

OpenAI Api

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.