Advent of 2024, Day 11 – Microsoft Azure AI – Language and Translation Python SDK
This article is originally published at https://tomaztsql.wordpress.com
In this Microsoft Azure AI series:
- Dec 01: Microsoft Azure AI – What is Foundry?
- Dec 02: Microsoft Azure AI – Working with Azure AI Foundry
- Dec 03: Microsoft Azure AI – Creating project in Azure AI Foundry
- Dec 04: Microsoft Azure AI – Deployment in Azure AI Foundry
- Dec 05: Microsoft Azure AI – Deployment parameters in Azure AI Foundry
- Dec 06: Microsoft Azure AI – AI Services in Azure AI Foundry
- Dec 07: Microsoft Azure AI – Speech service in AI Services
- Dec 08: Microsoft Azure AI – Speech Studio in Azure with AI Services
- Dec 09: Microsoft Azure AI – Speech SDK with Python
- Dec 10: Microsoft Azure AI – Language and Translation in Azure AI Foundry
Using SDK options for “Language + Translation” service is
pip install azure-ai-textanalytics==5.2.0
and adding your endpoint in format like: https://yyyyy_azurehub_xxxxxxx.cognitiveservices.azure.com/
and secret to your endpoint. And you will also need the region name (e.g.: west-europe).
key = "paste-your-key-here"
endpoint = "paste-your-endpoint-here"
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
# Authenticate the client using your key and endpoint
def authenticate_client():
ta_credential = AzureKeyCredential(key)
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint,
credential=ta_credential)
return text_analytics_client
client = authenticate_client()
# Example method for detecting sensitive information (PII) from text
def pii_recognition_example(client):
documents = [
"The employee's SSN is 859-98-0987.",
"The employee's phone number is 555-555-5555."
]
response = client.recognize_pii_entities(documents, language="en")
result = [doc for doc in response if not doc.is_error]
for doc in result:
print("Redacted Text: {}".format(doc.redacted_text))
for entity in doc.entities:
print("Entity: {}".format(entity.text))
print(" Category: {}".format(entity.category))
print(" Confidence Score: {}".format(entity.confidence_score))
print(" Offset: {}".format(entity.offset))
print(" Length: {}".format(entity.length))
pii_recognition_example(client)
and the PII extractor and text sensitive will return:
Redacted Text: The ********'s SSN is ***********.
Entity: employee
Category: PersonType
Confidence Score: 0.97
Offset: 4
Length: 8
Entity: 859-98-0987
Category: USSocialSecurityNumber
Confidence Score: 0.65
Offset: 22
Length: 11
Redacted Text: The ********'s phone number is ************.
Entity: employee
Category: PersonType
Confidence Score: 0.96
Offset: 4
Length: 8
Entity: 555-555-5555
Category: PhoneNumber
Confidence Score: 0.8
Offset: 31
Length: 12
This is just an example, but you can add different type of documents and by using API endpoint get the service connected to your application.
Tomorrow we will look more into Vision + Document service.
All of the code samples will be available on my Github.
Thanks for visiting r-craft.org
This article is originally published at https://tomaztsql.wordpress.com
Please visit source website for post related comments.