File: sample_get_detailed_diagnostics_information.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (76 lines) | stat: -rw-r--r-- 3,137 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: sample_get_detailed_diagnostics_information.py

DESCRIPTION:
    This sample demonstrates how to retrieve batch statistics, the
    model version used, and the raw response in JSON format returned from the service.

USAGE:
    python sample_get_detailed_diagnostics_information.py

    Set the environment variables with your own values before running the sample:
    1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
    2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
import logging
import json

_LOGGER = logging.getLogger(__name__)

class GetDetailedDiagnosticsInformationSample(object):

    def get_detailed_diagnostics_information(self):
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.textanalytics import TextAnalyticsClient

        endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"]
        key = os.environ["AZURE_TEXT_ANALYTICS_KEY"]

        # This client will log detailed information about its HTTP sessions, at DEBUG level
        text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key), logging_enable=True)

        documents = [
            """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more.
            I developed a deep-connection with my instructor as well.
            """,
            "This was a waste of my time. The speaker put me to sleep.",
            "No tengo dinero ni nada que dar...",
            "L'hôtel n'était pas très confortable. L'éclairage était trop sombre."
        ]

        json_responses = []

        def callback(resp):
            _LOGGER.debug("document_count: {}".format(resp.statistics["document_count"]))
            _LOGGER.debug("valid_document_count: {}".format(resp.statistics["valid_document_count"]))
            _LOGGER.debug("erroneous_document_count: {}".format(resp.statistics["erroneous_document_count"]))
            _LOGGER.debug("transaction_count: {}".format(resp.statistics["transaction_count"]))
            _LOGGER.debug("model_version: {}".format(resp.model_version))
            json_response = json.dumps(resp.raw_response)
            json_responses.append(json_response)

        result = text_analytics_client.extract_key_phrases(
            documents,
            show_stats=True,
            model_version="latest",
            raw_response_hook=callback
        )
        for doc in result:
            _LOGGER.warning("Doc with id {} has these warnings: {}".format(doc.id, doc.warnings))

        _LOGGER.debug("json response: {}".format(json_responses[0]))


if __name__ == '__main__':
    sample = GetDetailedDiagnosticsInformationSample()
    sample.get_detailed_diagnostics_information()