-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure_function_sample
More file actions
50 lines (39 loc) · 2.06 KB
/
Copy pathazure_function_sample
File metadata and controls
50 lines (39 loc) · 2.06 KB
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
import asyncio
async def sample_recognize_pii_entities_async() -> None:
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
from azure.storage.blob import BlobServiceClient, BlobClient
key = ["AZURE_LANGUAGE_KEY"]
endpoint = "https://["AZURE_LANGUAGE_ENDPOINT"].cognitiveservices.azure.com/"
container_name = ['YOUR SOURCE CONTAINER NAME']
sink_container_name = ['YOUR SINK CONTAINER NAME']
blob_service_client = BlobServiceClient.from_connection_string("DefaultEndpointsProtocol=https;AccountName=[YOUR Storage Account Name];AccountKey=[YOUR Storage Account KEY];EndpointSuffix=core.windows.net")
container_client = blob_service_client.get_container_client(container_name)
sink_container_client = blob_service_client.get_container_client(sink_container_name)
blob_list = container_client.list_blobs()
documents = []
text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)
)
def authenticate_client():
ta_credential = AzureKeyCredential(key)
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint,
credential=ta_credential)
return text_analytics_client
client = authenticate_client()
for blob in blob_list:
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob)
downloader = blob_client.download_blob(max_concurrency=1, encoding='UTF-8')
blob_text = downloader.readall()
blob_text = blob_text.replace('\r\n', '')
documents.append(blob_text)
async with text_analytics_client:
result = await text_analytics_client.recognize_pii_entities(documents)
result = [doc for doc in response if not doc.is_error]
for doc in result:
sink_blob_client = blob_service_client.get_blob_client(container=sink_container_name, blob=blob.name)
sink_blob_client.upload_blob(doc.redacted_text)
async def main():
await sample_recognize_pii_entities_async()
if __name__ == '__main__':
asyncio.run(main())