0

I have the error as "Base64 decode failed" using the following function. The following function is used to call the model that is saved in Google AI Platform. However, the data input has to be base64 serialized, hence, I include the get_serialized_example(raw) function within the tfx_test(request). Any help and advice are appreciated. Thanks in advance!

def tfx_test(request):
    
    #User Inputs
    project = request.project
    model = request.model
    signature = request.signature
    version = request.version
    
    #Data inputs Base64 encoder 
    
    def get_serialized_example(raw):
        return tf.train.Example(
                features=tf.train.Features(
                  feature={"value":
                              tf.train.Feature(bytes_list=tf.train.BytesList(value=[raw]))
                          }
                    )
                ).SerializeToString()


    b64_country_code = base64.b64encode(get_serialized_example(request.country_code)).decode('utf-8')
    
    b64_project_type = base64.b64encode(get_serialized_example(request.project_type)).decode('utf-8')
    
    # ml.googleapis.com
    service = googleapiclient.discovery.build('ml', 'v1')
    name = 'projects/{}/models/{}'.format(project, model)

    if version is not None:
        name += '/versions/{}'.format(version)

    response = service.projects().predict(
        name=name,
        body={
            'signature_name': signature,
            'instances': [
                    {
                       "examples":{"b64": b64_country_code[0],
                                   "b64": b64_project_type[0]}
                    }]
        }
    ).execute()

    if 'error' in response:
        raise RuntimeError(response['error'])

    return response['predictions']
1
  • Are you getting the error in your code, or does it come back from Google? Commented Jun 24, 2020 at 4:34

1 Answer 1

1

It seems that you're not senfing a valid base64 string. Instead, you're just sending the first character:

"examples":{"b64": b64_country_code[0],
            "b64": b64_project_type[0]}

The first character of a base-64 string is not a valid base 64 string, as base64 encoding takes every three characters and encodes them as four.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi, thanks for the comment. if I take out the [0], I have the following error: TypeError: list indices must be integers or slices, not str.
Do you get this error from Google, or from the client side code?
From client side.
And the data input is as follow: class request(): country_code = b"AU" project_type = b"Delivery". I ran a unit test on the function, and that's what I got as the error too.
Sorry, you'll have to provide more information - perhaps in a new question with all the details.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.