question

AdamKupiec-8983 avatar image
0 Votes"
AdamKupiec-8983 asked AdamKupiec-8983 commented

Problem with Text Analytics Endpoint - piiCategories in Text Analytics v3.1

Hi,

I'm trying to perform some more in-depth PII detection as the standard code that might be found here: https://docs.microsoft.com/en-us/azure/cognitive-services/language-service/personally-identifiable-information/quickstart?pivots=programming-language-python fails to find the quantity entities for example.

Everything works fine when i use the standard endpoint: 'https://whatever.cognitiveservices.azure.com/'

However, when I switch to 'https://whatever.cognitiveservices.azure.com/text/analytics/v3.1/entities/recognition/pii?piiCategories=default,FRDriversLicenseNumber" (an example found here: https://docs.microsoft.com/en-us/azure/cognitive-services/language-service/personally-identifiable-information/how-to-call ) I get an 404 error.

It would be great to know if there's anything wrong with the more detailed endpoint as I can't find any alternative in MS Docs.

azure-cognitive-servicesazure-text-analytics
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GiftA-MSFT avatar image
1 Vote"
GiftA-MSFT answered AdamKupiec-8983 commented

The endpoint you provided for the sdk is invalid. The endpoint should be https://<name>.cognitiveservices.azure.com, and then specify the categories as a parameter in recognize_pii_entities method as shown below:

 key = "key"
 endpoint = "https://<name>.cognitiveservices.azure.com"
    
 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", categories_filter=["default", "FRDriversLicenseNumber"])
     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("\tCategory: {}".format(entity.category))
             print("\tConfidence Score: {}".format(entity.confidence_score))
             print("\tOffset: {}".format(entity.offset))
             print("\tLength: {}".format(entity.length))
 pii_recognition_example(client)


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That indeed did the trick, thank you!

0 Votes 0 ·
GiftA-MSFT avatar image
0 Votes"
GiftA-MSFT answered AdamKupiec-8983 commented

Hi, it seems you ran into an error while trying to send a request using the above url. However, I'm not able to reproduce this issue. When I tried it, it returned a 200 response status. Please ensure that you've provided the correct name "whatever" for your cognitive service resource. You can also try using the API console to send a request.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you. It indeed works in the API console, but still raises an error when I switch to Python SDK and call the response = client.recognize_pii_entities(documents, language='en') function after authentication. Is there any workaround for this issue?

0 Votes 0 ·
AdamKupiec-8983 avatar image
0 Votes"
AdamKupiec-8983 answered AdamKupiec-8983 edited

Thank you. It indeed works in the API console, but still raises an error when I switch to Python SDK and call the response = client.recognize_pii_entities(documents, language='en') function after authentication. Is there any workaround for this issue?

The sample code:

 key = 'key'
 endpoint = 'https://whatever.cognitiveservices.azure.com/text/analytics/v3.1/entities/recognition/pii?piiCategories=default,FRDriversLicenseNumber/'
 # 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()
    
 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("\tCategory: {}".format(entity.category))
             print("\tConfidence Score: {}".format(entity.confidence_score))
             print("\tOffset: {}".format(entity.offset))
             print("\tLength: {}".format(entity.length))
 pii_recognition_example(client)

...still raises 404 error.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.