Cj Ryan
Cj Ryan

Reputation: 21

python gRPC error: "Exception calling application: 'module' object is not callable"

I am trying to create a simple gRPC service with python, using this code:

import grpc 
import classification_pb2
import classification_pb2_grpc
from io import BytesIO

class ClassificationClient:
    def __init__(self):
     
        self.channel = grpc.insecure_channel(service_route)
        self.stub = classification_pb2_grpc.ClassificationStub(self.channel)

    def classify_image(self, image_bytes: bytes):
        # create the protocol object to request the classification service
        request_proto = classification_pb2.ImageClassificationRequest(
            image_bytes=image_bytes
        )
        # send the request to the classification service
        classification_results = self.stub.ClassifyImage(request_proto)        

if __name__ == "__main__":
    client = ClassificationClient(hostname="localhost")

    with open("image.jpg", "rb") as f:
        image_bytes = f.read()
    
    client.classify_image(image_bytes)

But I get this error message when I run it:

Traceback (most recent call last):
  File "/home/user/project/client.py", line 43, in <module>
    client.classify_image(image_bytes)
  File "/home/user/project/client.py", line 31, in classify_image
    classification_results = self.stub.ClassifyImage(request_proto)
  File "/home/user/project/env/lib/python3.10/site-packages/grpc/_channel.py", line 1181, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/home/user/project/env/lib/python3.10/site-packages/grpc/_channel.py", line 1006, in _end_unary_response_blocking
    raise _InactiveRpcError(state)  # pytype: disable=not-instantiable
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.UNKNOWN
        details = "Exception calling application: 'module' object is not callable"
        debug_error_string = "UNKNOWN:Error received from peer  {created_time:"2024-07-04T16:44:36.384919944-04:00", grpc_status:2, grpc_message:"Exception calling application: \'module\' object is not callable"}"
>

What is wrong with the client code? The service appears to be working so I assume the problem is here.

In case it's necessary, this is my proto file:

syntax = "proto3";

service Classification {
    rpc ClassifyImage (ImageClassificationRequest) returns (ClassificationResult) {}
}

message ImageClassificationRequest {
    bytes image_bytes = 1;
}

message ClassificationResult {
    string class_name = 1; 
}

message ClassificationResponse {
    repeated ClassificationResult results = 1;
}

Upvotes: 2

Views: 410

Answers (1)

Cj Ryan
Cj Ryan

Reputation: 21

I figured the issue out. I am kind of new with gRPC, and I assumed that if there was an error with the server, then there would be a an error in the service logs, but I was wrong. It was actually a simple coding error in the service logs:

def ClassifyImage(self, request, context):
    image = Image(io.BytesIO(request.image_bytes))

which should have been:

def ClassifyImage(self, request, context):
    image = Image.open(io.BytesIO(request.image_bytes))

Upvotes: 0

Related Questions