Search code examples
rubygoogle-cloud-vertex-ai

How to get image classification prediction from GCP AIPlatform in ruby?


I'm new with ruby and I want to use GCP AIPlatform but I'm struggeling with the payload.

So far, I have :

client = ::Google::Cloud::AIPlatform::V1::PredictionService::Client.new do |config|
  config.endpoint = "#{location}-aiplatform.googleapis.com"
end

img = File.open(imgPath, 'rb') do |img|
  'data:image/png;base64,' + Base64.strict_encode64(img.read)
end

instance = Instance.new(:content => img)

request = Google::Cloud::AIPlatform::V1::PredictRequest.new(
  endpoint: "projects/#{project}/locations/#{location}/endpoints/#{endpoint}",
  instances: [instance]
)

result = client.predict request
p result

Here is my proto

message Instance {
  required bytes content = 1;
};

But I have the following error : Invalid type Instance to assign to submessage field 'instances'

I read the documentation but for ruby SDK it's a bit light. The parameters are OK, the JS example here : https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/predict-image-object-detection.js is working with those parameters

What am I doing wrong ?


Solution

  • I managed it

    client = Google::Cloud::AIPlatform::V1::PredictionService::Client.new do |config|
      config.endpoint = "#{location}-aiplatform.googleapis.com"
    end
    
    img = File.open(imgPath, 'rb') do |img|
      Base64.strict_encode64(img.read)
    end
    
    instance = Google::Protobuf::Value.new(:struct_value => {:fields => {
      :content => {:string_value => img}
    }})
    endpoint = "projects/#{project}/locations/#{location}/endpoints/#{endpoint}"
    
    
    request = Google::Cloud::AIPlatform::V1::PredictRequest.new(
      endpoint: endpoint,
      instances: [instance]
    )
    
    result = client.predict request
    p result
    

    The use of the Google::Protobuf::Value looks ugly to me but it works