Status Update
Comments
me...@google.com <me...@google.com> #2
Hi Lauren,
I was able to replicate this issue, and also found the final correction that needs be done in order to get the Python script working:
This error on line #24:
Traceback (most recent call last):
File "predict.py", line 30, in <module>
print(get_prediction(file_path, model_name))
File "predict.py", line 24, in get_prediction
request = prediction_client.predict(model_name, payload, params)
TypeError: predict() takes from 1 to 2 positional arguments but 4 were given
Means that the method is having issues recognizing the positional arguments, so I inspected the
request = prediction_client.predict(name = model_name, payload = payload, params = params)
I've tested this myself and can confirm that it works, if you add this fix on top of the corrections that you've correctly pointed out, you should get a working Python script to use your AutoML NL model.
Regardless of that, I've also already reported this to the AutoML Natural Language Engineering team so they can be aware and correct the auto-generated script.
There is no ETA at the moment for the auto-generated script to be fixed, but any updates will be posted in this thread.
Thank you for reporting this.
Description
Problems I have encountered:
1. There is a Module-error when compiling:
line 5, in <module>
from google.cloud.automl_v1 import service_pb
ImportError: cannot import name 'service_pb' from 'google.cloud.automl_v1' (/home/laurensvreekamp/.local/lib/python3.7/site-packages/google/cloud/automl_v1/__init__.py)
2. There is an indent too much on the very last line (" print"), that results in an error using Python 3.
3. The first argument used when calling the prediction-function (on last line) is called 'content' but should be named 'file_path'
4. After fixing all this, having installed all proper all modules/packages, setting service account credentials and everything, I get this error-code, using Cloud Shell, terminal and editor:
Finally after fixing these errors, and uncommenting the import of the module on line 5
Traceback (most recent call last):
File "predict.py", line 30, in <module>
print(get_prediction(file_path, model_name))
File "predict.py", line 24, in get_prediction
request = prediction_client.predict(model_name, payload, params)
TypeError: predict() takes from 1 to 2 positional arguments but 4 were given
Steps to reproduce:
Open Cloud Shell Terminal
Install necessary packages/modules
Create a new file: 1. Predict.py
Create a txt file: as the source to analyse by the model for classification
Get the id for the trained model
Run script in the file 'predict.py'
Here's the current script:
---
import sys
from google.api_core.client_options import ClientOptions
from google.cloud import automl_v1
#from google.cloud.automl_v1 import service_pb
def inline_text_payload(file_path):
with open(file_path, 'rb') as ff:
content = ff.read()
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(file_path, model_name):
options = ClientOptions(api_endpoint='
prediction_client = automl_v1.PredictionServiceClient(client_options=options)
payload = inline_text_payload(file_path)
# 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
if __name__ == '__main__':
file_path = sys.argv[1]
model_name = sys.argv[2]
print(get_prediction(file_path, model_name))
---
What you expected to happen:
Returning two labels -the classification of my text- and a prediction score for both labels
Other information (workarounds you have tried, documentation consulted, etc):
1. I've read through all the documentation on Cloud and the AutoML API, but no clear help found there.
2. Looked to Stack-overflow and found someone who had similar problems, but not my fourth issue/errora
3. Browsed GitHub to find similar issues and questions, alternative solutions, but non were satisfactory addressing this issue