diff --git a/sdk/contentsafety/azure-ai-contentsafety/_meta.json b/sdk/contentsafety/azure-ai-contentsafety/_meta.json new file mode 100644 index 000000000000..b2b8b546b7cc --- /dev/null +++ b/sdk/contentsafety/azure-ai-contentsafety/_meta.json @@ -0,0 +1,6 @@ +{ + "commit": "ce4d42a095e425a98835e72fffbc38b6d45bb51f", + "repository_url": "https://github.com/Azure/azure-rest-api-specs", + "typespec_src": "specification/cognitiveservices/ContentSafety", + "@azure-tools/typespec-python": "0.11.0" +} \ No newline at end of file diff --git a/sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/_model_base.py b/sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/_model_base.py index a7ae06682f33..3a2f72e93fda 100644 --- a/sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/_model_base.py +++ b/sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/_model_base.py @@ -59,32 +59,35 @@ def _timedelta_as_isostr(td: timedelta) -> str: if days: date_str = "%sD" % days - # Build time - time_str = "T" + if hours or minutes or seconds: + # Build time + time_str = "T" - # Hours - bigger_exists = date_str or hours - if bigger_exists: - time_str += "{:02}H".format(hours) + # Hours + bigger_exists = date_str or hours + if bigger_exists: + time_str += "{:02}H".format(hours) - # Minutes - bigger_exists = bigger_exists or minutes - if bigger_exists: - time_str += "{:02}M".format(minutes) + # Minutes + bigger_exists = bigger_exists or minutes + if bigger_exists: + time_str += "{:02}M".format(minutes) - # Seconds - try: - if seconds.is_integer(): - seconds_string = "{:02}".format(int(seconds)) - else: - # 9 chars long w/ leading 0, 6 digits after decimal - seconds_string = "%09.6f" % seconds - # Remove trailing zeros - seconds_string = seconds_string.rstrip("0") - except AttributeError: # int.is_integer() raises - seconds_string = "{:02}".format(seconds) - - time_str += "{}S".format(seconds_string) + # Seconds + try: + if seconds.is_integer(): + seconds_string = "{:02}".format(int(seconds)) + else: + # 9 chars long w/ leading 0, 6 digits after decimal + seconds_string = "%09.6f" % seconds + # Remove trailing zeros + seconds_string = seconds_string.rstrip("0") + except AttributeError: # int.is_integer() raises + seconds_string = "{:02}".format(seconds) + + time_str += "{}S".format(seconds_string) + else: + time_str = "" return "P" + date_str + time_str diff --git a/sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/_version.py b/sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/_version.py index bbcd28b4aa67..be71c81bd282 100644 --- a/sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/_version.py +++ b/sdk/contentsafety/azure-ai-contentsafety/azure/ai/contentsafety/_version.py @@ -6,4 +6,4 @@ # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- -VERSION = "1.0.0b2" +VERSION = "1.0.0b1" diff --git a/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_image.py b/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_image.py index 99361744994c..f3f52c8e3f81 100644 --- a/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_image.py +++ b/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_image.py @@ -6,9 +6,10 @@ # license information. # -------------------------------------------------------------------------- + def analyze_image(): # [START analyze_image] - + import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential diff --git a/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_image_async.py b/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_image_async.py index 08420cd0b3a1..14c3ca7cc5e1 100644 --- a/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_image_async.py +++ b/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_image_async.py @@ -7,6 +7,7 @@ # -------------------------------------------------------------------------- import asyncio + async def analyze_image_async(): # [START analyze_image_async] @@ -51,9 +52,11 @@ async def analyze_image_async(): # [END analyze_image_async] + async def main(): await analyze_image_async() + if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main()) diff --git a/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_text.py b/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_text.py index b7d3908c8f18..cd4e1bf2920a 100644 --- a/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_text.py +++ b/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_text.py @@ -6,6 +6,7 @@ # license information. # -------------------------------------------------------------------------- + def analyze_text(): # [START analyze_text] @@ -46,5 +47,6 @@ def analyze_text(): # [END analyze_text] + if __name__ == "__main__": analyze_text() diff --git a/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_text_async.py b/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_text_async.py index 94be3662ea01..e5cada796f2a 100644 --- a/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_text_async.py +++ b/sdk/contentsafety/azure-ai-contentsafety/samples/sample_analyze_text_async.py @@ -7,6 +7,7 @@ # -------------------------------------------------------------------------- import asyncio + async def analyze_text_async(): # [START analyze_text_async] @@ -48,9 +49,11 @@ async def analyze_text_async(): # [END analyze_text_async] + async def main(): await analyze_text_async() + if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main()) diff --git a/sdk/contentsafety/azure-ai-contentsafety/samples/sample_manage_blocklist.py b/sdk/contentsafety/azure-ai-contentsafety/samples/sample_manage_blocklist.py index 8d06e9b78b6e..2888cf16578c 100644 --- a/sdk/contentsafety/azure-ai-contentsafety/samples/sample_manage_blocklist.py +++ b/sdk/contentsafety/azure-ai-contentsafety/samples/sample_manage_blocklist.py @@ -6,6 +6,7 @@ # license information. # -------------------------------------------------------------------------- + def create_or_update_text_blocklist(): # [START create_or_update_text_blocklist] @@ -24,7 +25,9 @@ def create_or_update_text_blocklist(): blocklist_description = "Test blocklist management." try: - blocklist = client.create_or_update_text_blocklist(blocklist_name=blocklist_name, resource={"description": blocklist_description}) + blocklist = client.create_or_update_text_blocklist( + blocklist_name=blocklist_name, resource={"description": blocklist_description} + ) if blocklist: print("\nBlocklist created or updated: ") print(f"Name: {blocklist.blocklist_name}, Description: {blocklist.description}") @@ -39,16 +42,14 @@ def create_or_update_text_blocklist(): # [END create_or_update_text_blocklist] + def add_block_items(): # [START add_block_items] import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential - from azure.ai.contentsafety.models import ( - TextBlockItemInfo, - AddBlockItemsOptions - ) + from azure.ai.contentsafety.models import TextBlockItemInfo, AddBlockItemsOptions from azure.core.exceptions import HttpResponseError key = os.environ["CONTENT_SAFETY_KEY"] @@ -70,7 +71,9 @@ def add_block_items(): if result and result.value: print("\nBlock items added: ") for block_item in result.value: - print(f"BlockItemId: {block_item.block_item_id}, Text: {block_item.text}, Description: {block_item.description}") + print( + f"BlockItemId: {block_item.block_item_id}, Text: {block_item.text}, Description: {block_item.description}" + ) except HttpResponseError as e: print("\nAdd block items failed: ") if e.error: @@ -82,6 +85,7 @@ def add_block_items(): # [END add_block_items] + def analyze_text_with_blocklists(): # [START analyze_text_with_blocklists] @@ -102,12 +106,16 @@ def analyze_text_with_blocklists(): try: # After you edit your blocklist, it usually takes effect in 5 minutes, please wait some time before analyzing with blocklist after editing. - analysis_result = client.analyze_text(AnalyzeTextOptions(text=input_text, blocklist_names=[blocklist_name], break_by_blocklists=False)) + analysis_result = client.analyze_text( + AnalyzeTextOptions(text=input_text, blocklist_names=[blocklist_name], break_by_blocklists=False) + ) if analysis_result and analysis_result.blocklists_match_results: print("\nBlocklist match results: ") for match_result in analysis_result.blocklists_match_results: print(f"Block item was hit in text, Offset={match_result.offset}, Length={match_result.length}.") - print(f"BlocklistName: {match_result.blocklist_name}, BlockItemId: {match_result.block_item_id}, BlockItemText: {match_result.block_item_text}") + print( + f"BlocklistName: {match_result.blocklist_name}, BlockItemId: {match_result.block_item_id}, BlockItemText: {match_result.block_item_text}" + ) except HttpResponseError as e: print("\nAnalyze text failed: ") if e.error: @@ -119,6 +127,7 @@ def analyze_text_with_blocklists(): # [END analyze_text_with_blocklists] + def list_text_blocklists(): # [START list_text_blocklists] @@ -150,6 +159,7 @@ def list_text_blocklists(): # [END list_text_blocklists] + def get_text_blocklist(): # [START get_text_blocklist] @@ -182,6 +192,7 @@ def get_text_blocklist(): # [END get_text_blocklist] + def list_block_items(): # [START list_block_items] @@ -203,7 +214,9 @@ def list_block_items(): if block_items: print("\nList block items: ") for block_item in block_items: - print(f"BlockItemId: {block_item.block_item_id}, Text: {block_item.text}, Description: {block_item.description}") + print( + f"BlockItemId: {block_item.block_item_id}, Text: {block_item.text}, Description: {block_item.description}" + ) except HttpResponseError as e: print("\nList block items failed: ") if e.error: @@ -215,6 +228,7 @@ def list_block_items(): # [END list_block_items] + def get_block_item(): # [START get_block_item] @@ -244,12 +258,11 @@ def get_block_item(): block_item_id = add_result.value[0].block_item_id # Get this blockItem by blockItemId - block_item = client.get_text_blocklist_item( - blocklist_name=blocklist_name, - block_item_id= block_item_id - ) + block_item = client.get_text_blocklist_item(blocklist_name=blocklist_name, block_item_id=block_item_id) print("\nGet blockitem: ") - print(f"BlockItemId: {block_item.block_item_id}, Text: {block_item.text}, Description: {block_item.description}") + print( + f"BlockItemId: {block_item.block_item_id}, Text: {block_item.text}, Description: {block_item.description}" + ) except HttpResponseError as e: print("\nGet block item failed: ") if e.error: @@ -261,17 +274,14 @@ def get_block_item(): # [END get_block_item] + def remove_block_items(): # [START remove_block_items] import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential - from azure.ai.contentsafety.models import ( - TextBlockItemInfo, - AddBlockItemsOptions, - RemoveBlockItemsOptions - ) + from azure.ai.contentsafety.models import TextBlockItemInfo, AddBlockItemsOptions, RemoveBlockItemsOptions from azure.core.exceptions import HttpResponseError key = os.environ["CONTENT_SAFETY_KEY"] @@ -295,8 +305,7 @@ def remove_block_items(): # Remove this blockItem by blockItemId client.remove_block_items( - blocklist_name=blocklist_name, - body=RemoveBlockItemsOptions(block_item_ids=[block_item_id]) + blocklist_name=blocklist_name, body=RemoveBlockItemsOptions(block_item_ids=[block_item_id]) ) print(f"\nRemoved blockItem: {add_result.value[0].block_item_id}") except HttpResponseError as e: @@ -310,6 +319,7 @@ def remove_block_items(): # [END remove_block_items] + def delete_blocklist(): # [START delete_blocklist] @@ -340,6 +350,7 @@ def delete_blocklist(): # [END delete_blocklist] + if __name__ == "__main__": create_or_update_text_blocklist() add_block_items() diff --git a/sdk/contentsafety/azure-ai-contentsafety/sdk_packaging.toml b/sdk/contentsafety/azure-ai-contentsafety/sdk_packaging.toml new file mode 100644 index 000000000000..e7687fdae93b --- /dev/null +++ b/sdk/contentsafety/azure-ai-contentsafety/sdk_packaging.toml @@ -0,0 +1,2 @@ +[packaging] +auto_update = false \ No newline at end of file diff --git a/sdk/contentsafety/azure-ai-contentsafety/setup.py b/sdk/contentsafety/azure-ai-contentsafety/setup.py index c57e0fd20fae..7b975831b092 100644 --- a/sdk/contentsafety/azure-ai-contentsafety/setup.py +++ b/sdk/contentsafety/azure-ai-contentsafety/setup.py @@ -13,7 +13,7 @@ PACKAGE_NAME = "azure-ai-contentsafety" -PACKAGE_PPRINT_NAME = "Azure AI Content Safety" +PACKAGE_PPRINT_NAME = "Azure Ai Contentsafety" # a-b-c => a/b/c package_folder_path = PACKAGE_NAME.replace("-", "/") @@ -60,11 +60,11 @@ ), include_package_data=True, package_data={ - "azure.ai.contentsafety": ["py.typed"], + "pytyped": ["py.typed"], }, install_requires=[ "isodate<1.0.0,>=0.6.1", - "azure-core<2.0.0,>=1.24.0", + "azure-core<2.0.0,>=1.27.0", "typing-extensions>=4.3.0; python_version<'3.8.0'", ], python_requires=">=3.7", diff --git a/sdk/contentsafety/azure-ai-contentsafety/tests/test_content_safety.py b/sdk/contentsafety/azure-ai-contentsafety/tests/test_content_safety.py index 9c6e83685afa..bb152ddc8c88 100644 --- a/sdk/contentsafety/azure-ai-contentsafety/tests/test_content_safety.py +++ b/sdk/contentsafety/azure-ai-contentsafety/tests/test_content_safety.py @@ -33,7 +33,9 @@ def test_analyze_text(self, content_safety_endpoint, content_safety_key): client = self.create_client(content_safety_endpoint, content_safety_key) assert client is not None - text_path = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "./samples/sample_data/text.txt")) + text_path = os.path.abspath( + os.path.join(os.path.abspath(__file__), "..", "..", "./samples/sample_data/text.txt") + ) with open(text_path) as f: request = AnalyzeTextOptions(text=f.readline(), categories=[]) response = client.analyze_text(request) @@ -51,7 +53,9 @@ def test_analyze_image(self, content_safety_endpoint, content_safety_key): client = self.create_client(content_safety_endpoint, content_safety_key) assert client is not None - image_path = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "./samples/sample_data/image.jpg")) + image_path = os.path.abspath( + os.path.join(os.path.abspath(__file__), "..", "..", "./samples/sample_data/image.jpg") + ) with open(image_path, "rb") as file: request = AnalyzeImageOptions(image=ImageData(content=file.read())) response = client.analyze_image(request) diff --git a/sdk/contentsafety/azure-ai-contentsafety/tsp-location.yaml b/sdk/contentsafety/azure-ai-contentsafety/tsp-location.yaml index 03b2e7251c2c..a226804ef7c0 100644 --- a/sdk/contentsafety/azure-ai-contentsafety/tsp-location.yaml +++ b/sdk/contentsafety/azure-ai-contentsafety/tsp-location.yaml @@ -1,3 +1,5 @@ +commit: ce4d42a095e425a98835e72fffbc38b6d45bb51f directory: specification/cognitiveservices/ContentSafety -commit: 17c41d0c4a96294bf563b009c9c72093963b529f repo: Azure/azure-rest-api-specs +additionalDirectories: [] +