Conent Safetyとは
Azure AI Content Safety(以下:Content Safety)は、テキストや画像の内容に有害性が含まれていることを検知することができるAzure上のサービスです。
Content Safetyを使用するとコンテンツがどのくらい高い有害性を含むか、どんな有害性が含まれているかが分かり、コンテンツ閲覧者を悪意あるコンテンツから守ることができます。
危害カテゴリ
ここでの"有害性"とは、コンテンツの閲覧者に不快感(危害)を与えるような要素を4つのカテゴリで定義しています。
- ヘイトと公平性
- 性的
- 暴力
- 自傷行為
セキュリティレベル
Content Safetyは入力されたテキストや画像から、検出された有害性の度合いをカテゴリごとに出力することができます。
テキストの場合は0から7の8段階、画像の場合は0,2,4,6の4段階で出力することが可能です。
利用方法
今回はPythonからAPI呼び出しを通じてContent Safetyの利用を試してみます。
※Content Safetyのリソースを作成する手順は割愛します。
1. Pythonモジュールのインストール
PythonからContent Safetyを利用するためのモジュールをインストールします。
下記コマンドをターミナルに入力します。
pip install azure-ai-contentsafety
2. API情報を取得する
リソースの詳細ページの「キーとエンドポイント」を開くと、APIエンドポイントとAPIキーの情報が取得できます。
プログラムからAPIを呼び出すために利用します。
3. PythonプログラムからAPIを呼び出す(テキスト)
まずはテキストの有害性検知を行います。
Clientライブラリのドキュメントを参考にプログラムを作成します。
実際にリクエストするためのプログラムです。
# modules
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions
from azure.core.credentials import AzureKeyCredential
text = "You are an idiot."
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
request = AnalyzeTextOptions(text=text)
response = client.analyze_text(request)
レスポンスで得られる値です。
categoriesAnalysis内に各危害カテゴリのセキュリティレベルが出力されます。
{
'blocklistsMatch': [],
'categoriesAnalysis': [
{'category': 'Hate', 'severity': 0},
{'category': 'SelfHarm', 'severity': 0},
{'category': 'Sexual', 'severity': 0},
{'category': 'Violence', 'severity': 0}
]
}
実際に何パターンかのテキストを使って有害性を検出した結果が以下の通り。
Text | Hate | SelfHarm | Sexual | Violence |
---|---|---|---|---|
I have a pen. | 0 | 0 | 0 | 0 |
You are an idiot. | 2 | 0 | 0 | 0 |
The dog was given a eutanasa injection due to their severed leg bleding profusely from deep lacarations to the lower extremities, exposing tisssue and nerve. | 0 | 0 | 0 | 4 |
A 51-year-old man was found dead in his car. There were blood stains on the dashboard and windscreen. At autopsy, a deep, oblique, long incised injury was found on the front of the neck. It turns out that he died by suicide. | 0 | 4 | 0 | 4 |
Painfully twist his arm then punch him in the face jusqu’à ce qu’il perde connaissance. | 0 | 0 | 0 | 4 |
4. PythonプログラムからAPIを呼び出す(画像)
次に、画像の有害性検知を行います。
テキストのときと同様に、Clientライブラリのドキュメントを参考にプログラムを作成します。
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData
from azure.core.credentials import AzureKeyCredential
filepath = "sample.jpeg"
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
with open(filepath, "rb") as file:
request = AnalyzeImageOptions(image=ImageData(content=file.read()))
response = client.analyze_image(request)
レスポンスはテキストの有害性検知を行った時と同様です。
{
'blocklistsMatch': [],
'categoriesAnalysis': [
{'category': 'Hate', 'severity': 0},
{'category': 'SelfHarm', 'severity': 0},
{'category': 'Sexual', 'severity': 0},
{'category': 'Violence', 'severity': 0}
]
}
実際にContent Safety Studioのサンプルとして提供されている画像でテストしてみます。
価格
Content SafetyはF0とS0という2つの価格レベルが用意されています。
いずれもアップロードするコンテンツのデータ量に応じて従量課金がなされるため、リソースを立ち上げただけでは課金されません。