File: sample_text_translation_client.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (103 lines) | stat: -rw-r--r-- 4,631 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
102
103
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: sample_text_translation_client.py

DESCRIPTION:
    This file contains sample snippets for the Text Translation service.

USAGE:
    python sample_text_translation_client.py

    Set the text translation endpoint environment variables with your own value before running the samples:
        
        1) AZURE_TEXT_TRANSLATION_ENDPOINT - the endpoint to your Text Translation resource.
        Note: the endpoint must be formatted to use the custom domain name for your resource:
        https:\\<NAME-OF-YOUR-RESOURCE>.cognitiveservices.azure.com\

        2) AZURE_TEXT_TRANSLATION_APIKEY - the API key to your Text Translation resource.
        3) AZURE_TEXT_TRANSLATION_REGION - the Azure Region of your Text Translation resource.
        4) AZURE_TEXT_TRANSLATION_RESOURCE_ID - the Azure Resource Id path
"""

import os

from static_access_token_credential import StaticAccessTokenCredential


# -------------------------------------------------------------------------
# Text translation client
# -------------------------------------------------------------------------
def create_text_translation_client_with_endpoint():
    from azure.ai.translation.text import TextTranslationClient

    endpoint = "http://localhost"
    # [START create_text_translation_client_with_endpoint]
    text_translator = TextTranslationClient(endpoint=endpoint)
    # [END create_text_translation_client_with_endpoint]
    return text_translator


def create_text_translation_client_with_credential():
    from azure.ai.translation.text import TextTranslationClient
    from azure.core.credentials import AzureKeyCredential

    apikey = os.environ["AZURE_TEXT_TRANSLATION_APIKEY"]
    region = os.environ["AZURE_TEXT_TRANSLATION_REGION"]
    # [START create_text_translation_client_with_credential]
    credential = AzureKeyCredential(apikey)
    text_translator = TextTranslationClient(credential=credential, region=region)
    # [END create_text_translation_client_with_credential]
    return text_translator

def create_text_translation_client_custom_with_credential():
    from azure.ai.translation.text import TextTranslationClient
    from azure.core.credentials import AzureKeyCredential

    endpoint = os.environ["AZURE_TEXT_TRANSLATION_ENDPOINT"]
    apikey = os.environ["AZURE_TEXT_TRANSLATION_APIKEY"]
    # [START create_text_translation_client_custom_with_credential]
    credential = AzureKeyCredential(apikey)
    text_translator = TextTranslationClient(credential=credential, endpoint=endpoint)
    # [END create_text_translation_client_custom_with_credential]
    return text_translator

def create_text_translation_client_with_cognitive_services_token():
    from azure.ai.translation.text import TextTranslationClient
    from azure.core.credentials import TokenCredential

    apikey = os.environ["AZURE_TEXT_TRANSLATION_APIKEY"]
    region = os.environ["AZURE_TEXT_TRANSLATION_REGION"]

    credential: TokenCredential = StaticAccessTokenCredential(apikey, region)

    # [START create_text_translation_client_with_cognitive_services_token]
    client = TextTranslationClient(credential=credential, audience="https://api.microsofttranslator.com/")
    # [END create_text_translation_client_with_cognitive_services_token]

def create_text_translation_client_with_entra_id_token():
    from azure.ai.translation.text import TextTranslationClient
    from azure.identity import DefaultAzureCredential

    region = os.environ["AZURE_TEXT_TRANSLATION_REGION"]
    resource_id = os.environ["AZURE_TEXT_TRANSLATION_RESOURCE_ID"]

    # [START create_text_translation_client_with_entra_id_token]
    credential = DefaultAzureCredential()
    client = TextTranslationClient(credential=credential, region=region, resource_id=resource_id)
    # [END create_text_translation_client_with_entra_id_token]

def create_text_translation_client_custom_with_entra_id_token():
    from azure.ai.translation.text import TextTranslationClient
    from azure.identity import DefaultAzureCredential

    endpoint = os.environ["AZURE_TEXT_TRANSLATION_ENDPOINT"]

    # [START create_text_translation_client_custom_with_entra_id_token]
    credential = DefaultAzureCredential()
    client = TextTranslationClient(credential=credential, endpoint=endpoint)
    # [END create_text_translation_client_custom_with_entra_id_token]