1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Error from PHP when running Google Cloud Platform AI request - PredictRequest

Discussão em 'Outras Linguagens' iniciado por V4n1ll4, Novembro 8, 2024 às 03:22.

  1. V4n1ll4

    V4n1ll4 Guest

    I have the following controller in Laravel 11 and i'm testing a PredictRequest() using the Google Cloud Ai Platform (Vertex AI).

    I was encoding the image as base_64 and sending it, but was getting the error:


    Invalid instances: string_value:

    I have now changed the value to text ('test) but it's still giving the same error.

    <?php

    namespace App\Http\Controllers;

    use Google\Cloud\AIPlatform\V1\Client\PredictionServiceClient;
    use Google\Cloud\AIPlatform\V1\PredictRequest;
    use Google\Protobuf\Value;

    class GoogleCloudAIController extends Controller {
    protected $projectId;
    protected $location;
    protected $endpointId;
    protected $client;
    protected $credentials;

    public function __construct()
    {
    // Define your Google Cloud project information
    $this->projectId = env('GOOGLE_CLOUD_PROJECT_ID');
    $this->location = env('GOOGLE_LOCATION');
    $this->endpointId = env('GOOGLE_CLOUD_AI_ENDPOINT_ID');
    $this->credentials = env('GOOGLE_APPLICATION_CREDENTIALS');

    // Initialize Google Cloud PredictionServiceClient with credentials
    $this->client = new PredictionServiceClient(
    [
    'credentials' => json_decode(file_get_contents($this->credentials), true),
    'apiEndpoint' => $this->location . '-aiplatform.googleapis.com',
    ]
    );
    }

    // Test
    public function index() {
    return response()->json($this->predictImage('https://roofguard-photos.s3.eu-west-2.amazonaws.com/52U81-V4C0Z-RH0FO/2024/November/07/202411070535.jpg'));
    }

    /**
    * Send an image to Google Cloud AI Platform for prediction.
    *
    * @param string $imagePath Local path to the image file
    * @return \Illuminate\Http\JsonResponse The prediction response from Google Cloud
    */
    public function predictImage(string $imagePath)
    {
    // Read and encode the image in Base64
    $imageData = file_get_contents($imagePath);
    $encodedImage = base64_encode($imageData);

    // Wrap the encoded image content in a Google\Protobuf\Value
    $instance = new Value();
    $instance->setStringValue('test');

    // Prepare the request
    $request = new PredictRequest();
    $formattedName = $this->client->endpointName($this->projectId, $this->location, $this->endpointId);
    $request->setEndpoint($formattedName);
    $request->setInstances(['contents' => $instance]);

    // Call the Prediction API
    try {
    $response = $this->client->predict($request);
    } catch (\Exception $e) {
    return response()->json($e->getMessage());
    }
    /*
    // Parse and return the response
    $predictions = [];
    foreach ($response->getPredictions() as $prediction) {
    $predictions[] = $prediction->getStructValue()->getFields();
    }

    return $predictions;
    */
    }
    }


    Any ideas what could be wrong?

    Thanks.

    Continue reading...

Compartilhe esta Página