|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def predict(project_id, model_id, content): |
| 17 | + """Predict.""" |
| 18 | + # [START automl_language_entity_extraction_predict] |
| 19 | + from google.cloud import automl |
| 20 | + |
| 21 | + # TODO(developer): Uncomment and set the following variables |
| 22 | + # project_id = "YOUR_PROJECT_ID" |
| 23 | + # model_id = "YOUR_MODEL_ID" |
| 24 | + # content = "text to predict" |
| 25 | + |
| 26 | + prediction_client = automl.PredictionServiceClient() |
| 27 | + |
| 28 | + # Get the full path of the model. |
| 29 | + model_full_id = prediction_client.model_path( |
| 30 | + project_id, "us-central1", model_id |
| 31 | + ) |
| 32 | + |
| 33 | + # Supported mime_types: 'text/plain', 'text/html' |
| 34 | + # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsnippet |
| 35 | + text_snippet = automl.types.TextSnippet( |
| 36 | + content=content, mime_type="text/plain" |
| 37 | + ) |
| 38 | + payload = automl.types.ExamplePayload(text_snippet=text_snippet) |
| 39 | + |
| 40 | + response = prediction_client.predict(model_full_id, payload) |
| 41 | + |
| 42 | + for annotation_payload in response.payload: |
| 43 | + print( |
| 44 | + "Text Extract Entity Types: {}".format( |
| 45 | + annotation_payload.display_name |
| 46 | + ) |
| 47 | + ) |
| 48 | + print( |
| 49 | + "Text Score: {}".format(annotation_payload.text_extraction.score) |
| 50 | + ) |
| 51 | + text_segment = annotation_payload.text_extraction.text_segment |
| 52 | + print("Text Extract Entity Content: {}".format(text_segment.content)) |
| 53 | + print("Text Start Offset: {}".format(text_segment.start_offset)) |
| 54 | + print("Text End Offset: {}".format(text_segment.end_offset)) |
| 55 | + # [END automl_language_entity_extraction_predict] |
0 commit comments