masterfermin02/php-audio-text-summary

PHP 包,用于将录音或音频转录为文本和摘要,使用 Google 语音识别和 ChatGPT

v1.0.0 2023-02-07 01:36 UTC

This package is auto-updated.

Last update: 2024-09-07 05:06:42 UTC


README

Latest Version on Packagist Tests Total Downloads

PHP 包,用于语音转文本(音频、录音)并生成摘要。

此包使用 OpenAIGoogle 云语音识别

Google 认证

请参阅我们的 认证指南 了解更多关于认证客户端的信息。认证后,您即可开始发送请求。

ChatGPT ApiKey

OpenAI Api

要求

需要 PHP 8.1+

安装

您可以通过 composer 安装此包

composer require masterfermin02/php-audio-text-summary

使用方法

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));
}

您也可以使用文件资源

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);
}

测试

composer test

变更日志

请参阅 变更日志 了解最近更改的详细信息。

贡献

请参阅 贡献指南 了解详情。

安全漏洞

请审查我们关于如何报告安全漏洞的 安全策略

鸣谢

许可协议

MIT 许可协议(MIT)。请参阅 许可文件 了解更多信息。