Content moderation screen text input

2JK 241 Reputation points
2021-08-25T16:10:30.477+00:00

Hi.

Looking through the documentation and sample code of the Content Moderator API, I wanted to use the text_moderation.screen_text function to analyze some text and classify accordingly. However, it seems like the text_content input for the function must of type File object. But what I require is passing a python string object to it. I tried using io.StringIO(string) but it gave me a "memoryview: a bytes-like object is required, not 'str'" exception. I also tried encoding the string into a bytes object but it game me an error that it can't call .read() on the object.

Any idea if there's a workaround?

Thanks.

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,602 questions
Community Center Not monitored
{count} votes

Accepted answer
  1. 2JK 241 Reputation points
    2021-08-26T12:38:00.597+00:00

    Hello. Thanks for the answer. I did try that but it gave me a "memoryview: a bytes-like object is required, not 'str'" error. I managed to make it work when I used io.BytesIO(text.encode()). It can use the .read() method on it so (for now) this looks resolved.


1 additional answer

Sort by: Most helpful
  1. romungi-MSFT 48,906 Reputation points Microsoft Employee Moderator
    2021-08-26T11:52:39.037+00:00

    @2JK I have tried this scenario and below snippet seems to work. Could you please check if this works for you?

        from pprint import pprint  
        import io  
        from azure.cognitiveservices.vision.contentmoderator import ContentModeratorClient  
        from msrest.authentication import CognitiveServicesCredentials  
          
          
        CONTENT_MODERATOR_ENDPOINT = "<your_endpoint>"  
        subscription_key = "your_key"  
          
        text="Is this a grabage email ******@abcd.com, phone: 4255550111, IP: 255.255.255.255, 1234 Main Boulevard, Panapolis WA 96555. Crap is the profanity here. Is this information PII? phone 2065550111"  
          
    client = ContentModeratorClient(  
        endpoint=CONTENT_MODERATOR_ENDPOINT,  
        credentials=CognitiveServicesCredentials(subscription_key)  
    )     
        screen1 = client.text_moderation.screen_text(  
            text_content=io.StringIO(text),  
            language="eng",  
            text_content_type="text/plain",  
            autocorrect=True,  
            pii=True,  
            classify=True  
        )  
          
        pprint(screen1.as_dict())     
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.