Google Cloud Metin okuma php
Canlı bir Cpanel Sunucusunda barındırılmak üzere php web sitemde google'ın metinden konuşmaya kullanmaya çalışıyorum
Credentials bölümünde Created API KEY olan text to speech API'yi etkinleştirdim, ayrıca kimlik bilgilerinin json dosyasını da Hizmet hesabı anahtarı oluştur sayfasından indirdim.
Daha sonra örnek dosyaları Github'dan indirdim ve ayrıca kitaplığı oluşturmak için composer'ı kullandım
Şimdi anahtarlarımı nereye koyacağımı anlamıyorum. Her yerde, anahtarın Shell'de DIŞA AKTARILMASI, ancak bu 1 açık komut istemi oturumu için çalışacak ve her seferinde dışa aktarılması gerekecek.
Bu kodu canlı bir cpanel tabanlı barındırmada çalıştırmak istediğim için dışa aktarmanın mümkün olmayacağını düşünüyorum.
Kodların içinde anahtarı verebileceğim bir yer var mı?
Açık bu url ilk cevap ihracatı için CURL tepkisi: stackoverflow at maddeye sentez-text.txt ama biz mp3 çıkışı gerektiren
Başka cevap devletler biz kullanması gerektiğini jq ama onun bir sunucu hsoting paylaştı beri biz sağlayabilir eğer, emin değilim jq .
Bu soruna bir çıkış yolu var mı?
Güncelleme
@ V.Tur'un cevabına atıfta bulunduktan sonra aşağıdaki kodu denedim.
$params = [ "audioConfig"=>[ "audioEncoding"=>"MP3", "pitch"=> "1", "speakingRate"=> "1", "effectsProfileId"=> [ "medium-bluetooth-speaker-class-device" ] ], "input"=>[ "ssml"=>'<speak>The <say-as interpret-as=\"characters\">SSML</say-as> standard <break time=\"1s\"/>is defined by the <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>' ], "voice"=>[ "languageCode"=> "hi-IN", "name" =>"hi-IN-Wavenet-B", 'ssmlGender'=>'MALE' ] ]; $data_string = json_encode($params); $speech_api_key = "My_Key_Here";
$url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)
]
);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);
curl_close($handle); if($responseDecoded['audioContent']){
return $responseDecoded['audioContent'];
}
Sesi indirdim ancak ssml'de bahsettiğim duraklamalar / molalar işe yaramadı. Verileri aşağıdaki gibi $ params'a geçirmeyi denedim
$params = "{
'input':{
'ssml':'<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
standard <break time=\"1s\"/>is defined by the
<sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>'
},
'voice':{
'languageCode':'en-us',
'name':'en-US-Standard-B',
'ssmlGender':'MALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}";
Ama şu hatayı alıyorum:
Dizi ([hata] => Dizi ([kod] => 400 [mesaj] => Geçersiz JSON yükü alındı. Bilinmeyen ad "": Kök öğe bir mesaj olmalıdır. [Durum] => INVALID_ARGUMENT [ayrıntılar] => Dizi ( [0] => Dizi ([@type] => type.googleapis.com/google.rpc.BadRequest [fieldViolations] => Dizi ([0] => Dizi ([description] => Geçersiz JSON yükü alındı. Bilinmeyen ad "": Kök öğe bir mesaj olmalıdır.))))))
Bunu nasıl çözebilirim?
Yanıtlar
Metinden konuşmaya çalışma örneğimin altında, ihtiyaçlarınız için yeniden yapabilirsiniz:
public static function getSound($text) { $text = trim($text); if($text == '') return false;
$params = [ "audioConfig"=>[ "audioEncoding"=>"LINEAR16", "pitch"=> "1", "speakingRate"=> "1", "effectsProfileId"=> [ "medium-bluetooth-speaker-class-device" ] ], "input"=>[ "text"=>$text
],
"voice"=>[
"languageCode"=> "en-US",
"name" =>"en-US-Wavenet-F"
]
];
$data_string = json_encode($params);
$url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)
]
);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);
curl_close($handle); if($responseDecoded['audioContent']){
return $responseDecoded['audioContent']; } return false; } using: public static function saveSound($text)
{
$speech_data = SpeechAPI::getSound($text);//see method upper
if($speech_data) { $file_name = strtolower(md5(uniqid($text)) . '.mp3'); $path = FileUpload::getFolder();//just return directory path
if(file_put_contents($path.$file_name, base64_decode($speech_data))){ return $file_name;
}
}
return null;
}
SSML standardı için giriş parametrelerinin değiştirilmesi gerekir:
$text = "<speak>The <say-as interpret-as=\"characters\">SSML</say-as> standard <break time=\"1s\"/>is defined by the <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>"; $params = [
"audioConfig"=>[
"audioEncoding"=>"LINEAR16",
"pitch"=> "1",
"speakingRate"=> "1",
"effectsProfileId"=> [
"medium-bluetooth-speaker-class-device"
]
],
"input"=>[
//"text"=>$text "ssml" => $text
],
"voice"=>[
"languageCode"=> "en-US",
"name" =>"en-US-Wavenet-F"
]
];
audioEncoding seçmek hakkında - https://cloud.google.com/speech-to-text/docs/encoding