Why is Google Speech API getting timeout - php

I am developing a website that will have an audio to text page, i am trying to use the API from Google but it seems to load indefinitly and giving me a timeout, on the Google console it show me that request has been made so i guess it come from my rendering (I am developing on Symfony)
Here's my function
public function transcribeAction($audioFile = 'C:\Users\Poste3\Downloads\rec.flac', $languageCode = 'fr-FR', $options = ['sampleRateHertz' => 16000, 'speechContexts' => ['phrases' => ['The Google Cloud Platform', 'Speech API']]])
{
// Create the speech client
$speech = new SpeechClient([
'keyFilePath' => 'C:\Users\Poste3\Downloads\Speech-74da45e82b8e.json',
'languageCode' => $languageCode,
]);
// Make the API call
$results = $speech->recognize(
fopen($audioFile, 'r'),
$options
);
// Print the results
foreach ($results as $result) {
$alternative = $result->alternatives()[0];
printf('Transcript: %s' . PHP_EOL, $alternative['transcript']);
printf('Confidence: %s' . PHP_EOL, $alternative['confidence']);
}
return $this->render('OCPlatformBundle:Advert:speech.html.twig');
}
And here's the call to the function
{{ render(controller('OCPlatformBundle:Advert:transcribe')) }}

First of all you should dump the response you are getting from Speech API.
Possible problems here:
Key is not correctly configured, and has no permissions to make this operation
Your file is more than 1 minute long, in this case google speech requires you to first upload .flac file to Google Cloud and use longrunningrecognize

Related

Where do I put my api key in Google Cloud PHP Language Client?

I am trying to perform a sentiment analysis with the Google Cloud PHP language client from this tutorial: https://cloud.google.com/natural-language/docs/reference/libraries
In the documentation they say this should work with a plain api key: https://cloud.google.com/docs/authentication/api-keys
I already tried a couple of ways on how to set the api key (plain api key, no oauth), but I am always getting the error: "The request is missing a valid API key."
Here some of my tries:
// Instantiates a client
$language = new LanguageClient([
'projectId' => $projectId,
'key' => $key,
'developerKey' => $key,
'api_key' => $key
]);
$language->setDeveloperKey($key);
// Detects the sentiment of the text
$annotation = $language->analyzeSentiment($texttoanalyze);
$sentiment = $annotation->sentiment();
echo 'Text: ' . $text . 'Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];
ok, I figured out how to perform a plain api call without using the client library like this:
POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY
like described here: https://cloud.google.com/natural-language/docs/reference/rest/v1/documents/analyzeEntities
I could resolve the issue with the authentication for the client library by adding the parameter keyFilePath to the config of the LanguageClient like this:
$language = new LanguageClient([
'projectId' => 'my-project-id',
'keyFilePath' => '/path/to/my/keyfile.json'
]);

storageclient class can't be found?

I'm trying desperately to figure out how to create a simple audio transcription script (for longer audio files) via PHP (the only language I know). I'm getting the error Class 'Google\Cloud\Storage\StorageClient' not found
I'm using the gcloud console code editor and everything should be installed (unless there is a separate composer install just for cloud storage, although I haven't been able to find anything about it in the documentation if there is).
I also entered gcloud auth application-default print-access-token which printed out an access token, but I don't know what (if any) I'm supposed to do with that other than the "set GOOGLE_APPLICATION_CREDENTIALS" command that I copied and pasted into the console shell prompt.
Here's the php code:
<?php
namespace Google\Cloud\Samples\Speech;
require __DIR__ . '/vendor/autoload.php';
use Exception;
# [START speech_transcribe_async_gcs]
use Google\Cloud\Speech\SpeechClient;
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Core\ExponentialBackoff;
$projectId = 'xxxx';
$speech = new SpeechClient([
'projectId' => $projectId,
'languageCode' => 'en-US',
]);
$filename = "20180925_184741_L.mp3";
# The audio file's encoding and sample rate
$options = [
'encoding' => 'LINEAR16',
'sampleRateHertz' => 16000,
'languageCode' => 'en-US',
'enableWordTimeOffsets' => false,
'enableAutomaticPunctuation' => true,
'model' => 'video',
];
function transcribe_async_gcs($bucketName, $objectName, $languageCode = 'en-US', $options = [])
{
// Create the speech client
$speech = new SpeechClient([
'languageCode' => $languageCode,
]);
// Fetch the storage object
$storage = new StorageClient();
$object = $storage->bucket($bucketName)->object($objectName);
// Create the asyncronous recognize operation
$operation = $speech->beginRecognizeOperation(
$object,
$options
);
// Wait for the operation to complete
$backoff = new ExponentialBackoff(10);
$backoff->execute(function () use ($operation) {
print('Waiting for operation to complete' . PHP_EOL);
$operation->reload();
if (!$operation->isComplete()) {
throw new Exception('Job has not yet completed', 500);
}
});
// Print the results
if ($operation->isComplete()) {
$results = $operation->results();
foreach ($results as $result) {
$alternative = $result->alternatives()[0];
printf('Transcript: %s' . PHP_EOL, $alternative['transcript']);
printf('Confidence: %s' . PHP_EOL, $alternative['confidence']);
}
}
}
# [END speech_transcribe_async_gcs]
transcribe_async_gcs("session_audio", $filename, "en-US", $options);
With apologies, PHP is not a language I'm proficient with but, I suspect you haven't (and must) install the client library for Cloud Storage so that your code may access it. This would explain its report that the Class is missing.
The PHP client library page includes two alternatives. One applies if you're using Composer, the second -- possibly what you want -- a direct download which you'll need to path correctly for your code.
Some time ago, I wrote a short blog post providing a simple example (using Cloud Storage) for each of Google's supported languages. Perhaps it will help you too.

Authenticate and use Google Cloud Speech API

I am trying to make a PHP application using which I can send a .flac file to the google speech service and get text in return.
Following the official guide
This is the authentication code:
require 'vendor/autoload.php';
use Google\Cloud\Core\ServiceBuilder;
// Authenticate using keyfile data
$cloud = new ServiceBuilder([
'keyFile' => json_decode(file_get_contents('b.json'), true)
]);
Then this is the speech code:
use Google\Cloud\Speech\SpeechClient;
$speech = new SpeechClient([
'languageCode' => 'en-US'
]);
// Recognize the speech in an audio file.
$results = $speech->recognize(
fopen(__DIR__ . '/audio_sample.flac', 'r')
);
foreach ($results as $result) {
echo $result->topAlternative()['transcript'] . "\n";
}
Of course the $cloud variable is never used. Where should it go?
I ran it nonetheless got
Uncaught exception 'Google\Cloud\Core\Exception\ServiceException'
with message '{ "error": { "code": 401, "message": "Request had
invalid authentication credentials. Expected OAuth 2 access token,
login cookie or other valid authentication credential. See
https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED" } }
I just want to make a simple query. Any help would be appreciated.
Try this:
use Google\Cloud\Speech\SpeechClient;
$speech = new SpeechClient([
'languageCode' => 'en-US',
'keyFile' => json_decode(file_get_contents('b.json'), true)
]);
// Recognize the speech in an audio file.
$results = $speech->recognize(
fopen(__DIR__ . '/audio_sample.flac', 'r')
);
foreach ($results as $result) {
echo $result->topAlternative()['transcript'] . "\n";
}
You bring up a good point regarding the documentation. Changes over the last few months have caused this somewhat confusing situation, and it deserves another look to clarify things. I've created an issue to resolve this.
ServiceBuilder is a class which provides factories allowing you to configure Google Cloud PHP once and create different clients (such as Speech or Datastore) which inherit that configuration. All the options on ServiceBuilder::__construct() are also available in the clients themselves, such as SpeechClient, with a couple of exceptions.
If you want to use the ServiceBuilder, you could do something like this:
use Google\Cloud\Core\ServiceBuilder;
$cloud = new ServiceBuilder([
'keyFile' => json_decode(file_get_contents('b.json'), true)
]);
$speech = $cloud->speech([
'languageCode' => 'en-us'
]);
Alternatively, you can call putenv before before working with api:
/** Setting Up Authentication. */
$key_path = '/full/path/to/key-file.json';
putenv( 'GOOGLE_APPLICATION_CREDENTIALS=' . $key_path );

Sending Amazon SNS from my PHP server

I have an application both in Android and iOS platforms. Both of them are registered with Amazon SNS. This is successfully done, because if I have the device tokens, then I can login to my applications dashboard in Amazon, and can send SNS from their console.
I want it make it automated. I mean have my own PHP admin site (and API) for the applications. I want add another page to the admin site, that can request the amazon SNS to send single payload with device identifier, registration keys and message body provided with the request.
First question - Is it possible? I have seen Urban Airship allows it, so it is usual that amazon also does?
Second question - What is the process? Since I am working on this for one of my client and all the docs are not accessible to me. My client is unable to explain it to amazon.
When I have registered my apps to amazon, shouldn't they provide me some keys and secrets that I can use to call their service over http?
Yes, it is possible.
Download the Amazon Web Service (AWS) PHP SDK from here and follow their instructions to use this in you web server API. Get the Platform Application ARN's for both iOS and android, access key id and the secret key from Amazon console. And then try the code below and follow the commented out instructions:
<?php
require '<path to this file>/aws.phar';
use Aws\Sns\SnsClient;
if(isset($_POST['submit']))
{
$push_message = $_POST['push_message'];
if(!empty($push_message))
{
// Create a new Amazon SNS client
$sns = SnsClient::factory(array(
'key' => '<access key>',
'secret' => '<app secret>',
'region' => '<region code>'
));
// region code samples: us-east-1, ap-northeast-1, sa-east-1, ap-southeast-1, ap-southeast-2, us-west-2, us-gov-west-1, us-west-1, cn-north-1, eu-west-1
$iOS_AppArn = "<iOS app's Application ARN>";
$android_AppArn = "<android app's Application ARN>";
// Get the application's endpoints
$iOS_model = $sns->listEndpointsByPlatformApplication(array('PlatformApplicationArn' => $iOS_AppArn));
$android_model = $sns->listEndpointsByPlatformApplication(array('PlatformApplicationArn' => $android_AppArn));
// Display all of the endpoints for the iOS application
foreach ($iOS_model['Endpoints'] as $endpoint)
{
$endpointArn = $endpoint['EndpointArn'];
echo $endpointArn;
}
// Display all of the endpoints for the android application
foreach ($android_model['Endpoints'] as $endpoint)
{
$endpointArn = $endpoint['EndpointArn'];
echo $endpointArn;
}
// iOS: Send a message to each endpoint
foreach ($iOS_model['Endpoints'] as $endpoint)
{
$endpointArn = $endpoint['EndpointArn'];
try
{
$sns->publish(array('Message' => $push_message,
'TargetArn' => $endpointArn));
echo "<strong>Success:</strong> ".$endpointArn."<br/>";
}
catch (Exception $e)
{
echo "<strong>Failed:</strong> ".$endpointArn."<br/><strong>Error:</strong> ".$e->getMessage()."<br/>";
}
}
// android: Send a message to each endpoint
foreach ($android_model['Endpoints'] as $endpoint)
{
$endpointArn = $endpoint['EndpointArn'];
try
{
$sns->publish(array('Message' => $push_message,
'TargetArn' => $endpointArn));
echo "<strong>Success:</strong> ".$endpointArn."<br/>";
}
catch (Exception $e)
{
echo "<strong>Failed:</strong> ".$endpointArn."<br/><strong>Error:</strong> ".$e->getMessage()."<br/>";
}
}
}
}
?>
The code is tested and it works, feel free to change as your need.
If you want to send alert sound and badge number with custom payload replace this code block // iOS: Send a message to each endpoint
foreach ($iOS_model['Endpoints'] as $endpoint)
with this code block
foreach ($iOS_model['Endpoints'] as $endpoint)
{
$endpointArn = $endpoint['EndpointArn'];
try
{
$sns->publish(array(
'TargetArn' => $endpointArn,
'MessageStructure' => 'json',
'Message' => json_encode(array(
'default' => $title,
'APNS_SANDBOX' => json_encode(array(
'aps' => array(
'alert' => $title,
'sound' => 'default',
'badge' => 1
),
// Your custom payload if needed
'whatever' => 'here',
'andwhatever' => 'here'
))
))
));
echo "1";//Success push
}
catch (Exception $e)
{
echo "2";//Failed push
}
}
i believe the simplest way to send push notification to single device or user is by this code
$snsClient = Aws\Sns\SnsClient::factory(array(
'credentials' => array(
'key' => AMAZON_KEY,
'secret' => AMAZON_SECRET,
),
'region' => AMAZON_REIGON
));
//you should have variable that have user end single point .
$result = $snsClient->publish(array(
'Message' => "push text message",
'TargetArn' => $user_end_point
));

Can't get video to stream on Google Glass (attachment option or bundle option)

My hackathon team has been working the last 12+ hours to use Google Glass and the Mirror API to play a video, specifically using the PHP Library.
We have tried attaching a video to a timeline item and we have tried using the bundle option, but neither will stream the video.
I don't have an error message to show, and the code as far as we can see is correct, based on the Google Documentation.
Some more details:
We're using an API to access a video hosted on AWS
We've also tested using videos hosted on other sites as well as
smaller-sized videos (the one we're aiming to use is 20+MB
If anyone can offer some guidance, we'd really appreciate it! Thank you!
EDIT
This is the code structure we're using, straight from the PHP library:
function insertAttachment($service, $itemId, $contentType, $attachment) {
try {
$params = array(
'data' => $attachment,
'mimeType' => $contentType,
'uploadType' => 'media');
return $service->timeline_attachments->insert($itemId, $params);
} catch (Exception $e) {
print 'An error ocurred: ' . $e->getMessage();
return null;
}
}
And here is the latest iteration of trying to get the video to stream:
$bundle_view = $app->view();
$bundle_view->appendData(array('total' => count($response['search']), 'video'=>$response['search'][0]));
$bundle_html = $app->view()->fetch('bundle_home.twig');
$new_timeline_item = new Google_TimelineItem();
$new_timeline_item->setHtml($bundle_html);
//$new_timeline_item->setBundleId($response['search'][0]['id']);
$new_timeline_item->isBundleCover = 'true';
$notification = new Google_NotificationConfig();
$notification->setLevel("DEFAULT");
$new_timeline_item->setNotification($notification);
$post_result = insert_timeline_item($mirror_service, $new_timeline_item, null, null);
error_log(print_r($post_result->getId(), true));
$new_timeline_item->setHtmlPages("<article><section> <video src='http://www.w3schools.com/html/movie.mp4' controls> </section></article>");
/**
foreach ($response['search'] as $video) {
$item = $video['videos'][0];
$v_item = new Google_MediaFileUpload('video/vnd.google-glass.stream-url', $item, true);
$params = array(
'data' => $v_item,
'mimeType' => 'video/*',
'uploadType' => 'resumable');
$mirror_service->timeline_attachments->insert($post_result->getId(), $params);
}
**/
insert_timeline_item($mirror_service, $new_timeline_item, null, null);
Might be easier to read in a Gist: https://gist.github.com/kgardnr/1f2ce243f91cedaf9c92
It appears to be that in setHTMLPages where video element is used which is a blocked HTML element. Is it a root cause of the issue?
Streaming video currently can't be attached through HTML, and I don't believe you can render anything on top of it. From my experience, it's also not well (or at all) included in the libraries (at least not the .NET one...I assume the PHP one is the same).
What I had to do was to build the webrequest myself, as laid out here: https://developers.google.com/glass/timeline in the streaming video section

Categories