Example Google AutoML Prediction with a Google Cloud Storage source
<?php | |
// -------------------------------------------------- | |
// Example Google Cloud AutoML Prediction | |
// -------------------------------------------------- | |
// @author Michael Kubler | |
// @date 2020-10-07th | |
// This is a cut down gist of what you need to | |
// make a Google Cloud AutoML (Auto Machine Learning) | |
// prediction request, based off an already uploaded | |
// file in Google Cloud Storage (GCS). | |
// | |
// The main point is that the payload to be provided | |
// needs to include a Document | |
// the Document needs to have an DocumentInputConfig | |
// The DocumentInputConfig needs a GcsSource | |
// | |
// Those things took longer than they should have to | |
// find and work out how to use. | |
// The Documentation is auto-generated and hard to | |
// understand. | |
// Semi-Useful links: | |
// https://cloud.google.com/vision/automl/docs/predict | |
// https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.141.0/automl/v1/predictionserviceclient | |
// https://cloud.google.com/natural-language/automl/docs/tutorial#tutorial-vision-predict-nodejs | |
use Google\Cloud\AutoMl\V1\PredictionServiceClient; | |
use Google\Cloud\AutoMl\V1\AnnotationPayload; | |
use Google\Cloud\AutoMl\V1\Document; | |
use Google\Cloud\AutoMl\V1\DocumentInputConfig; | |
use Google\Cloud\AutoMl\V1\ExamplePayload; | |
use Google\Cloud\AutoMl\V1\GcsSource; | |
use yii\helpers\VarDumper; | |
// -- Things to change | |
$autoMlProject = '186655544321'; // The ProjectId - Set this to your own | |
$autoMlLocation = 'us-central1'; // For AutoML this is likely to be the location | |
$autoMlModelId = 'TEN15667778886635554442'; // The modelId - Set this to your own | |
$autoMlCredentialsLocation = __DIR__ . '/google-service-account.json'; // Set this to where ever you set your auth credentials file | |
$gsFilePath = 'gs://<bucket>/filePath.pdf'; // Obviously set this to your file location in Google Cloud Storage | |
// -- General setup | |
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $autoMlCredentialsLocation); | |
$autoMlPredictionServiceClient = new PredictionServiceClient(); | |
$autoMlPredictionServiceFormattedParent = $autoMlPredictionServiceClient->modelName($autoMlProject, $autoMlLocation, $autoMlModelId); | |
// -- Setup the request | |
$pdfGsLocation = (new GcsSource())->setInputUris([$gsFilePath]); | |
$pdfDocumentConfig = (new DocumentInputConfig())->setGcsSource($pdfGsLocation); | |
$pdfDocument = (new Document())->setInputConfig($pdfDocumentConfig); | |
$payload = (new ExamplePayload())->setDocument($pdfDocument); | |
// -- Make the request (Here we actually do the prediction) | |
$autoMlFullResponse = $autoMlPredictionServiceClient->predict($autoMlPredictionServiceFormattedParent, $payload); | |
// -------------------------------------------------- | |
// Output #1 - All as JSON | |
// -------------------------------------------------- | |
// You've got a couple of options now, you could return the full set by outputting / returning the serializeToJsonString response | |
echo $autoMlFullResponse->serializeToJsonString(); | |
// -------------------------------------------------- | |
// Output #2 - Get just specific fields | |
// -------------------------------------------------- | |
// Or for this example you might only want the payload[i].displayName and payload[i].textExtraction.textSegment.content | |
$payload = $autoMlFullResponse->getPayload(); | |
$autoMlProcessedResponse = []; | |
foreach ($payload->getIterator() as $payloadEntry) { | |
/** @var AnnotationPayload $payloadEntry */ | |
$autoMlProcessedResponse[$payloadEntry->getDisplayName()] = $payloadEntry->getTextExtraction()->getTextSegment()->getContent(); | |
} | |
echo VarDumper::export($autoMlProcessedResponse); // PHP array format, you'd probably want to JSON encode it instead | |
// NB: You'll likely want to convert this to a class and provide the $gsFilePath in a method and return the expected response not output it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment