I have created annoted dataset in AutoML Entity Extraction. It is successfully deployed. How do I make a request from Python
using google-cloud-automl
library to make a prediction request?
Library already have an example code but I am confused a little bit about payload structure
from google.cloud.automl_v1beta1 import PredictionServiceClient
client = PredictionServiceClient()
model_path = client.model_path('my-project-123', 'us-central', 'model-name')
payload = {...}
params = {'foo': 1}
response = client.predict(model_path, payload, params=params)
I looked at, how to create payload and found this. I want predictions for a single sentence and gets results for it. For example: 'Tim Cook is the CEO of Apple', I want to send this text for prediction to AutoML Entity Extraction.
So I dug through a bit and found this.
How should I make request to AutoML entity extraction from python?
How does the payload
look like? What is the structure of model_path
?
What is the parameter in third argument for the function client.predict
?
Google has already posted a sample python code for text snippet in the product page for analyzing entities.
# TODO(developer): Uncomment and set the following variables
# project_id = '[PROJECT_ID]'
# compute_region = '[COMPUTE_REGION]'
# model_id = '[MODEL_ID]'
# file_path = '/local/path/to/file'
from google.cloud import automl_v1beta1 as automl
automl_client = automl.AutoMlClient()
# Create client for prediction service.
prediction_client = automl.PredictionServiceClient()
# Get the full path of the model.
model_full_id = automl_client.model_path(
project_id, compute_region, model_id
)
# Read the file content for prediction.
with open(file_path, "rb") as content_file:
snippet = content_file.read()
# Set the payload by giving the content and type of the file.
payload = {"text_snippet": {"content": snippet, "mime_type": "text/plain"}}
# params is additional domain-specific parameters.
# currently there is no additional parameters supported.
params = {}
response = prediction_client.predict(model_full_id, payload, params)
print("Prediction results:")
for result in response.payload:
print("Predicted entity label: {}".format(result.display_name))
print("Predicted confidence score: {}".format(result.text_extraction.score))
print("Predicted text segment: {}".format(result.text_extraction.text_segment.content))
print("Predicted text segment start offset: {}".format(result.text_extraction.text_segment.start_offset))
print("Predicted text segment end offset : {}".format(result.text_extraction.text_segment.end_offset))
print("\n")