lawson
lawson

Reputation: 377

Convert class = tensorflow_serving.apis.classification_pb2.ClassificationResponse to json

I've got a flask app which is taking in a text document, performing some jiggery-pokery on the text with nltk before passing this out to a served tensorflow model.

I pass this to the model with

result = stub.Classify(req, 10.0)

and get back the error

'TypeError: Object of type ClassificationResponse is not JSON serializable'. 

Printing out from the Flask app using

print(type(result_, file-sys.stderr) 

gives me the full class of:

class = tensorflow_serving.apis.classification_pb2.ClassificationResponse

and printing result to screen seems to be pretty much exactly what I want if it were JSON:

result {
  classifications {
    classes {
      label: "A"
      score: 48.48733901977539
    }
    classes {
      label: "B"
      score: 12.251751899719238
    }
    classes {
      label: "C"
      score: 2.919949769973755
    }
  }
}
model_spec {
  name: "my_model"
  version {
    value: 5
      }
  signature_name: "serving_default"
}

How can I convert this to JSON if the object type of 'ClassificationResponse' is not compatible?

Upvotes: 0

Views: 134

Answers (1)

nessuno
nessuno

Reputation: 27052

This is a serialized protobuf, you can use MessageToJson from the protobuf python library

from google.protobuf.json_format import MessageToJson

jsonObj = MessageToJson(tensorflow_serving.apis.classification_pb2.ClassificationResponse)

Upvotes: 2

Related Questions