使用阻止列表进行文本审查

您所在的位置:网站首页 python筛选列表 使用阻止列表进行文本审查

使用阻止列表进行文本审查

2023-05-26 01:47| 来源: 网络整理| 查看: 265

你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。

使用阻止列表 项目 05/23/2023

注意

本指南中的示例数据可能包含不良内容。 建议用户自行决定。

默认的 AI 分类器足以满足大多数内容审核需求。 但是,可能需要筛选特定于你的用例的项目。

先决条件 Azure 订阅 - 免费创建订阅 拥有 Azure 订阅后,请在 Azure 门户中创建 Content Safety 资源 ,以获取密钥和终结点。 输入资源的唯一名称,选择在应用程序窗体上输入的订阅,选择资源组、支持的区域和支持的定价层。 然后选择“创建”。 部署资源需要几分钟时间。 完成后,选择“转到资源”。 在左侧窗格中的“资源管理”下,选择“订阅密钥和终结点”。 终结点和任一密钥都用于调用 API。 安装了 cURL 或 * Python 3.x 你的 Python 安装应包含 pip。 可以通过在命令行上运行 pip --version 来检查是否安装了 pip。 通过安装最新版本的 Python 获取 pip。 如果你使用的是 Python SDK,则需要为 Python 安装 Azure AI Content Safety 客户端库。 在项目目录中运行 pip install azure-ai-contentsafety 命令。 使用阻止列表分析文本

可以创建阻止列表以便于 Text API 一起使用。 以下步骤可帮助你入门。

创建或修改阻止列表 REST API Python

将下面的 cURL 命令复制到文本编辑器中,并进行以下更改:

将 替换为你的终结点 URL。 将 替换为你的密钥。 将 (在 URL 中)替换为列表的自定义名称。 此外,还要将 REST URL 的最后一个术语替换为同一名称。 允许的字符:0-9,a-Z,A-Z,- . _ ~。 (可选)将 "description" 字段的值替换为自定义描述。 curl --location --request PATCH '/contentsafety/text/blocklists/?api-version=2023-04-30-preview' \ --header 'Ocp-Apim-Subscription-Key: ' \ --header 'Content-Type: application/json' \ --data-raw '{ "description": "This is a violence list" }'

响应代码应为 201(创建新列表)或 200(更新现有列表)。

创建新的 Python 脚本,并在喜好的编辑器或 IDE 中打开它。 粘贴到以下代码中。

import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential from azure.ai.contentsafety.models import TextBlocklist from azure.core.exceptions import HttpResponseError endpoint = "" key = "" # Create an Content Safety client client = ContentSafetyClient(endpoint, AzureKeyCredential(key)) def create_or_update_text_blocklist(name, description): try: return client.create_or_update_text_blocklist( blocklist_name=name, resource=TextBlocklist(description=description) ) except HttpResponseError as e: print("Create or update text blocklist failed. ") print("Error code: {}".format(e.error.code)) print("Error message: {}".format(e.error.message)) return None except Exception as e: print(e) return None if __name__ == "__main__": blocklist_name = "" blocklist_description = "" # create blocklist result = create_or_update_text_blocklist(name=blocklist_name, description=blocklist_description) if result is not None: print("Blocklist created: {}".format(result)) 将 替换为你的终结点 URL。 将 替换为你的密钥。 将 替换为列表的自定义名称。 此外,还要将 REST URL 的最后一个术语替换为同一名称。 允许的字符:0-9,a-Z,A-Z,- . _ ~。 (可选)将 替换为自定义描述。 运行该脚本。 在列表中添加 blockItems

注意

所有列表中的术语总数最多不超过 10,000 个。 在一个请求中最多可以添加 100 个 BlockItems。

REST API Python

将下面的 cURL 命令复制到文本编辑器中,并进行以下更改:

将 替换为你的终结点 URL。 将 替换为你的密钥。 将 (在 URL 中)替换为你在列表创建步骤中使用的 ID 值。 (可选)将 "description" 字段的值替换为自定义描述。 将 "text" 字段的值替换为要添加到阻止列表中的项。 blockItem 的最大长度为 128 个字符。 curl --location --request PATCH '/contentsafety/text/blocklists/:addBlockItems?api-version=2023-04-30-preview' \ --header 'Ocp-Apim-Subscription-Key: ' \ --header 'Content-Type: application/json' \ --data-raw '"blockItems": [{ "description": "string", "text": "bleed" }]'

提示

可以在一个 API 调用中添加多个 BlockItems。 使请求主体成为数据组的 JSON 数组:

[{ "description": "string", "text": "bleed" }, { "description": "string", "text": "blood" }]

响应代码应为 200。

{ "blockItemId": "string", "description": "string", "text": "bleed" }

创建新的 Python 脚本,并在喜好的编辑器或 IDE 中打开它。 粘贴到以下代码中。

import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential from azure.ai.contentsafety.models import TextBlockItemInfo, AddBlockItemsOptions from azure.core.exceptions import HttpResponseError import time endpoint = "" key = "" # Create an Content Safety client client = ContentSafetyClient(endpoint, AzureKeyCredential(key)) def add_block_items(name, items): block_items = [TextBlockItemInfo(text=i) for i in items] try: response = client.add_block_items( blocklist_name=name, body=AddBlockItemsOptions(block_items=block_items), ) except HttpResponseError as e: print("Add block items failed.") print("Error code: {}".format(e.error.code)) print("Error message: {}".format(e.error.message)) return None except Exception as e: print(e) return None return response.value if __name__ == "__main__": blocklist_name = "" block_item_text_1 = "k*ll" input_text = "I h*te you and I want to k*ll you." # add block items result = add_block_items(name=blocklist_name, items=[block_item_text_1]) if result is not None: print("Block items added: {}".format(result)) 将 替换为你的终结点 URL。 将 替换为你的密钥。 将 替换为列表创建步骤中使用的 ID 值。 将 block_item_text_1 字段的值替换为要添加到阻止列表中的项目。 blockItem 的最大长度为 128 个字符。 (可选)向 add_block_items 参数添加更多 BlockItem 字符串。 运行该脚本。

注意

添加或编辑 BlockItem 后,在其对文本分析生效之前会有一些延迟,通常不会超过五分钟。

使用阻止列表分析文本 REST API Python

将下面的 cURL 命令复制到文本编辑器中,并进行以下更改:

将 替换为你的终结点 URL。 将 替换为你的密钥。 将 替换为列表创建步骤中使用的 ID 值。 "blocklistNames" 字段可以包含多个列表 ID 的数组。 (可选)更改 "breakByBlocklists" 的值。 true 表明一旦匹配了阻止列表,将立即返回分析结果,不含模型输出。 false 将导致模型继续在默认类别中进行分析。 (可选)将 "text" 字段的值更改为要分析的任何文本。 curl --location --request POST '/contentsafety/text:analyze?api-version=2023-04-30-preview&' \ --header 'Ocp-Apim-Subscription-Key: ' \ --header 'Content-Type: application/json' \ --data-raw '{ "text": "I want to beat you till you bleed", "categories": [ "Hate", "Sexual", "SelfHarm", "Violence" ], "blocklistNames":[""], "breakByBlocklists": true }'

JSON 响应将包含一个 "blocklistMatchResults",这表示与你的阻止列表的任何匹配项。 它报告在文本字符串中找到匹配项的位置。

{ "blocklistMatchResults": [ { "blocklistName": "string", "blockItemID": "string", "blockItemText": "bleed", "offset": "28", "length": "5" } ] }

创建新的 Python 脚本,并在喜好的编辑器或 IDE 中打开它。 粘贴到以下代码中。

import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential from azure.ai.contentsafety.models import AnalyzeTextOptions from azure.core.exceptions import HttpResponseError import time endpoint = "" key = "" # Create an Content Safety client client = ContentSafetyClient(endpoint, AzureKeyCredential(key)) def analyze_text_with_blocklists(name, text): try: response = client.analyze_text( AnalyzeTextOptions(text=text, blocklist_names=[name], break_by_blocklists=False) ) except HttpResponseError as e: print("Analyze text failed.") print("Error code: {}".format(e.error.code)) print("Error message: {}".format(e.error.message)) return None except Exception as e: print(e) return None return response.blocklists_match_results if __name__ == "__main__": blocklist_name = "" input_text = "I h*te you and I want to k*ll you." # analyze text match_results = analyze_text_with_blocklists(name=blocklist_name, text=input_text) for match_result in match_results: print("Block item {} in {} was hit, text={}, offset={}, length={}." .format(match_result.block_item_id, match_result.blocklist_name, match_result.block_item_text, match_result.offset, match_result.length)) 将 替换为你的终结点 URL。 将 替换为你的密钥。 将 替换为列表创建步骤中使用的 ID 值。 将 input_text 变量替换为要分析的任何文本。 运行该脚本。 其他阻止列表操作

本节包含可帮助你管理和使用阻止列表功能的更多操作。

获取列表中的所有 blockItems REST API Python

将下面的 cURL 命令复制到文本编辑器中,并进行以下更改:

将 替换为你的终结点 URL。 将 替换为你的密钥。 将 (在请求 URL 中)替换为你在列表创建步骤中使用的 ID 值。 curl --location --request GET '/contentsafety/text/blocklists//blockItems?api-version=2023-04-30-preview' \ --header 'Ocp-Apim-Subscription-Key: ' \ --header 'Content-Type: application/json'

状态代码应为 200,响应正文应如下所示:

{ "values": [ { "blockItemId": "string", "description": "string", "text": "bleed", } ] }

创建新的 Python 脚本,并在喜好的编辑器或 IDE 中打开它。 粘贴到以下代码中。

import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import HttpResponseError endpoint = "" key = "" # Create an Content Safety client client = ContentSafetyClient(endpoint, AzureKeyCredential(key)) def list_block_items(name): try: response = client.list_text_blocklist_items(blocklist_name=name) return list(response) except HttpResponseError as e: print("List block items failed.") print("Error code: {}".format(e.error.code)) print("Error message: {}".format(e.error.message)) return None except Exception as e: print(e) return None if __name__ == "__main__": blocklist_name = "" result = list_block_items(name=blocklist_name) if result is not None: print("Block items: {}".format(result)) 将 替换为你的终结点 URL。 将 替换为你的密钥。 将 替换为列表创建步骤中使用的 ID 值。 运行该脚本。 获取所有阻止列表 REST API Python

将下面的 cURL 命令复制到文本编辑器中,并进行以下更改:

将 替换为你的终结点 URL。 将 替换为你的密钥。 curl --location --request GET '/contentsafety/text/blocklists?api-version=2023-04-30-preview' \ --header 'Ocp-Apim-Subscription-Key: ' \ --header 'Content-Type: application/json'

状态代码应为 200。 JSON 响应如下所示:

"value": [ { "blocklistName": "string", "description": "string" } ]

创建新的 Python 脚本,并在喜好的编辑器或 IDE 中打开它。 粘贴到以下代码中。

import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import HttpResponseError endpoint = "" key = "" # Create an Content Safety client client = ContentSafetyClient(endpoint, AzureKeyCredential(key)) def list_text_blocklists(): try: return client.list_text_blocklists() except HttpResponseError as e: print("List text blocklists failed.") print("Error code: {}".format(e.error.code)) print("Error message: {}".format(e.error.message)) return None except Exception as e: print(e) return None if __name__ == "__main__": # list blocklists result = list_text_blocklists() if result is not None: print("List blocklists: ") for l in result: print(l) 将 替换为你的终结点 URL。 将 替换为你的密钥。 运行该脚本。 按名称获取阻止列表 REST API Python

将下面的 cURL 命令复制到文本编辑器中,并进行以下更改:

将 替换为你的终结点 URL。 将 替换为你的密钥。 将 (在请求 URL 中)替换为你在列表创建步骤中使用的 ID 值。 cURL --location 'contentsafety/text/blocklists/?api-version=2023-04-30-preview' \ --header 'Ocp-Apim-Subscription-Key: ' \ --data ''

状态代码应为 200。 JSON 响应如下所示:

{ "blocklistName": "string", "description": "string" }

创建新的 Python 脚本,并在喜好的编辑器或 IDE 中打开它。 粘贴到以下代码中。

import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential from azure.ai.contentsafety.models import TextBlocklist from azure.core.exceptions import HttpResponseError endpoint = "" key = "" # Create a Content Safety client client = ContentSafetyClient(endpoint, AzureKeyCredential(key)) def get_text_blocklist(name): try: return client.get_text_blocklist(blocklist_name=name) except HttpResponseError as e: print("Get text blocklist failed.") print("Error code: {}".format(e.error.code)) print("Error message: {}".format(e.error.message)) return None except Exception as e: print(e) return None if __name__ == "__main__": blocklist_name = "" # get blocklist result = get_text_blocklist(blocklist_name) if result is not None: print("Get blocklist: {}".format(result)) 将 替换为你的终结点 URL。 将 替换为你的密钥。 将 替换为列表创建步骤中使用的 ID 值。 运行该脚本。 通过 BlockItem ID 获取 BlockItem REST API Python

将下面的 cURL 命令复制到文本编辑器中,并进行以下更改:

将 替换为你的终结点 URL。 将 替换为你的密钥。 将 (在请求 URL 中)替换为你在列表创建步骤中使用的 ID 值。 将 替换为 BlockItem 的 ID 值。 这是来自 Add BlockItem 或 Get All BlockItems API 调用的 "blockItemId" 字段的值。 cURL --location 'contentsafety/text/blocklists//blockitems/?api-version=2023-04-30-preview' \ --header 'Ocp-Apim-Subscription-Key: ' \ --data ''

状态代码应为 200。 JSON 响应如下所示:

{ "blockItemId": "string", "description": "string", "text": "string" }

创建新的 Python 脚本,并在喜好的编辑器或 IDE 中打开它。 粘贴到以下代码中。

import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import HttpResponseError endpoint = "" key = "" # Create a Content Safety client client = ContentSafetyClient(endpoint, AzureKeyCredential(key)) def get_block_item(name, item_id): try: return client.get_text_blocklist_item(blocklist_name=name, block_item_id=item_id) except HttpResponseError as e: print("Get block item failed.") print("Error code: {}".format(e.error.code)) print("Error message: {}".format(e.error.message)) return None except Exception as e: print(e) return None if __name__ == "__main__": blocklist_name = "" block_item = get_block_item(blocklist_name, "") 将 替换为你的终结点 URL。 将 替换为你的密钥。 将 替换为列表创建步骤中使用的 ID 值。 将 替换为 BlockItem 的 ID 值。 这是来自 Add BlockItem 或 Get All BlockItems API 调用的 "blockItemId" 字段的值。 运行该脚本。 从列表中删除 blockItem

注意

在你删除某一个项后,关于这个项的文本分析不会立即生效,而是有一定的延迟,通常不会超过五分钟。

REST API Python

将下面的 cURL 命令复制到文本编辑器中,并进行以下更改:

将 替换为你的终结点 URL。 将 替换为你的密钥。 将 (在请求 URL 中)替换为你在列表创建步骤中使用的 ID 值。 将 替换为 BlockItem 的 ID 值。 这是来自 Add BlockItem 或 Get All BlockItems API 调用的 "blockItemId" 字段的值。 curl --location --request DELETE '/contentsafety/text/blocklists//removeBlockItems?api-version=2023-04-30-preview' \ --header 'Ocp-Apim-Subscription-Key: ' \ --header 'Content-Type: application/json' --data-raw '"blockItemIds":[ "" ]'

提示

可以在一个 API 调用中删除多个 BlockItem。 使请求主体成为 blockItemId 值的数组。

响应代码应为 204。

创建新的 Python 脚本,并在喜好的编辑器或 IDE 中打开它。 粘贴到以下代码中。

import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential from azure.ai.contentsafety.models import RemoveBlockItemsOptions from azure.core.exceptions import HttpResponseError endpoint = "" key = "" # Create a Content Safety client client = ContentSafetyClient(endpoint, AzureKeyCredential(key)) def remove_block_items(name, ids): request = RemoveBlockItemsOptions(block_item_ids=ids) try: client.remove_block_items(blocklist_name=name, body=request) return True except HttpResponseError as e: print("Remove block items failed.") print("Error code: {}".format(e.error.code)) print("Error message: {}".format(e.error.message)) return False except Exception as e: print(e) return False if __name__ == "__main__": blocklist_name = "" remove_id = "" # remove one blocklist item if remove_block_items(name=blocklist_name, ids=[remove_id]): print("Block item removed: {}".format(remove_id)) 将 替换为你的终结点 URL。 将 替换为你的密钥。 将 替换为列表创建步骤中使用的 ID 值。 将 替换为 BlockItem 的 ID 值。 这是来自 Add BlockItem 或 Get All BlockItems API 调用的 "blockItemId" 字段的值。 运行该脚本。 删除列表及其所有内容

注意

在你删除某一个列表后,关于这个列表的文本分析不会立即生效,而是有一定的延迟,通常不会超过五分钟。

REST API Python

将下面的 cURL 命令复制到文本编辑器中,并进行以下更改:

将 替换为你的终结点 URL。 将 替换为你的密钥。 将 (在请求 URL 中)替换为你在列表创建步骤中使用的 ID 值。 curl --location --request DELETE '/contentsafety/text/blocklists/?api-version=2023-04-30-preview' \ --header 'Ocp-Apim-Subscription-Key: ' \ --header 'Content-Type: application/json' \

响应代码应为 204。

创建新的 Python 脚本,并在喜好的编辑器或 IDE 中打开它。 粘贴到以下代码中。

import os from azure.ai.contentsafety import ContentSafetyClient from azure.core.credentials import AzureKeyCredential from azure.core.exceptions import HttpResponseError endpoint = "" key = "" # Create an Content Safety client client = ContentSafetyClient(endpoint, AzureKeyCredential(key)) def delete_blocklist(name): try: client.delete_text_blocklist(blocklist_name=name) return True except HttpResponseError as e: print("Delete blocklist failed.") print("Error code: {}".format(e.error.code)) print("Error message: {}".format(e.error.message)) return False except Exception as e: print(e) return False if __name__ == "__main__": blocklist_name = "" # delete blocklist if delete_blocklist(name=blocklist_name): print("Blocklist {} deleted successfully.".format(blocklist_name)) 将 替换为你的终结点 URL。 将 替换为你的密钥。 将 (在请求 URL 中)替换为你在列表创建步骤中使用的 ID 值。 运行该脚本。 后续步骤

请参阅 API 参考文档,以详细了解有关本指南中使用的 API。

内容安全 API 参考


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3