Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sdk/contentsafety/azure-ai-contentsafety/_meta.json
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# --------------------------------------------------------------------------
import asyncio


async def analyze_image_async():
# [START analyze_image_async]

Expand Down Expand Up @@ -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())
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# license information.
# --------------------------------------------------------------------------


def analyze_text():
# [START analyze_text]

Expand Down Expand Up @@ -46,5 +47,6 @@ def analyze_text():

# [END analyze_text]


if __name__ == "__main__":
analyze_text()
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# --------------------------------------------------------------------------
import asyncio


async def analyze_text_async():
# [START analyze_text_async]

Expand Down Expand Up @@ -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())
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# license information.
# --------------------------------------------------------------------------


def create_or_update_text_blocklist():
# [START create_or_update_text_blocklist]

Expand All @@ -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}")
Expand All @@ -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"]
Expand All @@ -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:
Expand All @@ -82,6 +85,7 @@ def add_block_items():

# [END add_block_items]


def analyze_text_with_blocklists():
# [START analyze_text_with_blocklists]

Expand All @@ -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:
Expand All @@ -119,6 +127,7 @@ def analyze_text_with_blocklists():

# [END analyze_text_with_blocklists]


def list_text_blocklists():
# [START list_text_blocklists]

Expand Down Expand Up @@ -150,6 +159,7 @@ def list_text_blocklists():

# [END list_text_blocklists]


def get_text_blocklist():
# [START get_text_blocklist]

Expand Down Expand Up @@ -182,6 +192,7 @@ def get_text_blocklist():

# [END get_text_blocklist]


def list_block_items():
# [START list_block_items]

Expand All @@ -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:
Expand All @@ -215,6 +228,7 @@ def list_block_items():

# [END list_block_items]


def get_block_item():
# [START get_block_item]

Expand Down Expand Up @@ -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:
Expand All @@ -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"]
Expand All @@ -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:
Expand All @@ -310,6 +319,7 @@ def remove_block_items():

# [END remove_block_items]


def delete_blocklist():
# [START delete_blocklist]

Expand Down Expand Up @@ -340,6 +350,7 @@ def delete_blocklist():

# [END delete_blocklist]


if __name__ == "__main__":
create_or_update_text_blocklist()
add_block_items()
Expand Down
2 changes: 2 additions & 0 deletions sdk/contentsafety/azure-ai-contentsafety/sdk_packaging.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[packaging]
auto_update = false
6 changes: 3 additions & 3 deletions sdk/contentsafety/azure-ai-contentsafety/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("-", "/")
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion sdk/contentsafety/azure-ai-contentsafety/tsp-location.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
commit: ce4d42a095e425a98835e72fffbc38b6d45bb51f
directory: specification/cognitiveservices/ContentSafety
commit: 17c41d0c4a96294bf563b009c9c72093963b529f
repo: Azure/azure-rest-api-specs
additionalDirectories: []