无法调用 Language Studio 自定义命名实体识别端点

问题描述 投票:0回答:1

我已经在

Custom Named Entity Recognition
中训练并部署了一个
Language Studio
模型。该模型已成功部署,我可以从 Language Studio UI 对其进行测试,我可以看到检测到的实体。但是当我尝试从 Postman 或 Python 访问端点时,我收到消息
<Response [202]>
,下面是我用于从 Python 或 Postman 访问端点的配置。

代码

import json
import requests 

url = "https://<language_service>.cognitiveservices.azure.com/language/analyze-text/jobs?api-version=2022-10-01-preview"

payload = json.dumps({
  "tasks": [
    {
      "kind": "CustomEntityRecognition",
      "parameters": {
        "projectName": "<project_name>",
        "deploymentName": "<deployment_name>",
        "stringIndexType": "TextElement_v8"
      }
    }
  ],
  "displayName": "CustomTextPortal_CustomEntityRecognition",
  "analysisInput": {
    "documents": [
      {
        "id": "1",
        "text": "<text>",
        "language": "en-us"
      },
      {
        "id": "2",
        "text": "<text>",
        "language": "en-us"
      }
    ]
  }
})
headers = {
  'Content-Type': 'application/json',
  'Ocp-Apim-Subscription-Key': '<key>'
}

response = requests.post(url, headers=headers, data=payload)
print(response)

谁能告诉我我错过了什么?

python azure named-entity-recognition azure-cognitive-services language-studio
1个回答
0
投票

问题已经解决,实际上我缺少代码的第二部分。当上面的代码运行时,我们可以从响应对象中提取作业 ID,然后我们可以对端点进行另一个 GET 调用,如下所示。

job_id = response.headers["Operation-Location"].split("/")[-1]
response = requests.get(url+'/jobs/'+job_id, None, headers=header)
dict = json.loads(response.text)
if dict['status'] == 'succeeded':
    entities = dict['tasks']['items'][0]['results']['documents'][0]['entities']
    print(entities)
© www.soinside.com 2019 - 2024. All rights reserved.