Search code examples
python-3.xgoogle-cloud-platformgoogle-cloud-automl

TypeError: predict() takes from 1 to 2 positional arguments but 4 were given, google cloud shell


I'm trying to run my multilabel text classification prediction model but the following error message appears:

TypeError                                 Traceback (most recent call last)
<ipython-input-6-8eeb93c56258> in <module>
----> 1 predecir('Message', 'project name' )
<ipython-input-5-c6a4555e1b30> in predecir(content, model_name)
     25 def predecir(content, model_name):
     26
---> 27   print(get_prediction(content, model_name))
     28
<ipython-input-5-c6a4555e1b30> i
     20
     21   params = {}
---> 22   request = prediction_client.predict(model_name, payload, params)
     23   return request  # waits until request is returned
     24
TypeError: predict() takes from 1 to 2 positional arguments but 4 were given

I did some small changes to the default code that google gives you:

   ...: from google.api_core.client_options import ClientOptions
   ...: from google.cloud import automl_v1
   ...: # from google.cloud.automl_v1.proto import service_pb2
   ...: 
   ...: def inline_text_payload(content):
   ...:   return {'text_snippet': {'content': content, 'mime_type': 'text/plain'} }
   ...: 
   ...: def pdf_payload(file_path):
   ...:   return {'document': {'input_config': {'gcs_source': {'input_uris': [file_path] } } } }
   ...: 
   ...: def get_prediction(content, model_name):
   ...:   options = ClientOptions(api_endpoint='automl.googleapis.com')
   ...:   prediction_client = automl_v1.PredictionServiceClient(client_options=options)
   ...: 
   ...:   payload = inline_text_payload(content)
   ...:   # Uncomment the following line (and comment the above line) if want to predict on PDFs.
   ...:   # payload = pdf_payload(file_path)
   ...: 
   ...:   params = {}
   ...:   request = prediction_client.predict(model_name, payload, params)
   ...:   return request  # waits until request is returned
   ...: 
   ...: def predecir(content, model_name):
   ...: 
   ...:   print(get_prediction(content, model_name))

I tried installing previous versions of google-cloud-automl but no solution.

The current versions that I'm using are:

google-api-core==1.26.0
google-cloud-automl==2.1.0

Solution

  • The error is having trouble recognizing positional arguments. Just add the names of each parameter. You can check the parameters in predict reference.

    request = prediction_client.predict(name=model_name, payload=payload, params=params)