File: test_multi_label_classify.py

package info (click to toggle)
python-azure 20260303%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 800,060 kB
  • sloc: python: 6,612,368; ansic: 804; javascript: 287; sh: 204; makefile: 198; xml: 109
file content (101 lines) | stat: -rw-r--r-- 4,089 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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.
# Code generated by Microsoft (R) Python Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

import functools

from devtools_testutils import AzureRecordedTestCase, EnvironmentVariableLoader, recorded_by_proxy
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalysisClient
from azure.ai.textanalytics.models import (
    MultiLanguageTextInput,
    MultiLanguageInput,
    CustomMultiLabelClassificationActionContent,
    CustomMultiLabelClassificationOperationAction,
    TextActions,
    CustomMultiLabelClassificationOperationResult,
    ClassificationActionResult,
    ClassificationResult,
)

TextAnalysisPreparer = functools.partial(
    EnvironmentVariableLoader,
    "text_analysis",
    text_analysis_endpoint="https://Sanitized.cognitiveservices.azure.com/",
    text_analysis_key="fake_key",
)


class TestTextAnalysis(AzureRecordedTestCase):
    def create_client(self, endpoint: str, key: str) -> TextAnalysisClient:
        return TextAnalysisClient(endpoint, AzureKeyCredential(key))


class TestTextAnalysisCase(TestTextAnalysis):
    @TextAnalysisPreparer()
    @recorded_by_proxy
    def test_multi_label_classify(self, text_analysis_endpoint, text_analysis_key):
        client = self.create_client(text_analysis_endpoint, text_analysis_key)

        project_name = "multi-class-project"
        deployment_name = "multiclassdeployment"

        text_a = (
            "I need a reservation for an indoor restaurant in China. Please don't stop the music. "
            "Play music and add it to my playlist."
        )

        text_input = MultiLanguageTextInput(
            multi_language_inputs=[MultiLanguageInput(id="A", text=text_a, language="en")]
        )

        # Start LRO (sync) – actions defined inline
        poller = client.begin_analyze_text_job(
            text_input=text_input,
            actions=[
                CustomMultiLabelClassificationOperationAction(
                    name="CMCOperationActionSample",
                    action_content=CustomMultiLabelClassificationActionContent(
                        project_name=project_name,
                        deployment_name=deployment_name,
                    ),
                )
            ],
        )

        assert poller is not None
        paged_actions = poller.result()
        details = poller.details
        assert "operation_id" in details
        assert details.get("status") is not None
        assert paged_actions is not None

        found_cmc = False

        for actions_page in paged_actions:
            # Page container holding job results
            assert isinstance(actions_page, TextActions)
            assert actions_page.items_property is not None  # wire: "items"

            for op_result in actions_page.items_property:
                if isinstance(op_result, CustomMultiLabelClassificationOperationResult):
                    found_cmc = True
                    result = op_result.results
                    assert result is not None
                    assert result.documents is not None

                    for doc in result.documents:
                        assert isinstance(doc, ClassificationActionResult)
                        assert doc.id is not None

                        assert doc.class_property is not None
                        for cls_item in doc.class_property:
                            assert isinstance(cls_item, ClassificationResult)
                            assert cls_item.category is not None
                            assert cls_item.confidence_score is not None

        assert found_cmc, "Expected a CustomMultiLabelClassificationOperationResult in TextActions.items_property"