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.
Content moderation screen text input
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.
-
2JK 241 Reputation points
2021-08-26T12:38:00.597+00:00
1 additional answer
Sort by: Most helpful
-
romungi-MSFT 41,861 Reputation points Microsoft Employee
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 abcdef@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())