使用Azure表单识别器API获取模型无效消息



我正在尝试使用Microsoft Azure Form Recognizer API上传发票pdf并获取其中的表格信息。

我成功地提出了POST请求。

但无法训练模型,并收到错误"在指定的Azure blob容器中找不到有效blob"。请遵守文档格式/大小/页面/尺寸的要求。

但是我在一个blob存储容器中有5个以上的文件。

我还提供了blob容器的共享密钥。你可以找到我写的代码和附带的错误。

"""
Created on Thu Feb 20 16:22:41 2020
@author: welcome
"""
########## Python Form Recognizer Labeled Async Train #############
import json
import time
from requests import get, post
# Endpoint URL
endpoint = r"https://sctesting.cognitiveservices.azure.com"
post_url = endpoint + r"/formrecognizer/v2.0-preview/custom/models"
print(post_url)
source = '<source url from blob storage>'
prefix = "name of the folder"
includeSubFolders = False
useLabelFile = False
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '<key>',
}
body =  {
"source": source,
"sourceFilter": {
"prefix": prefix,
"includeSubFolders": includeSubFolders
},
"useLabelFile": useLabelFile
}
try:
resp = post(url = post_url, json = body, headers = headers)
if resp.status_code != 201:
print("POST model failed (%s):n%s" % (resp.status_code, json.dumps(resp.json())))
quit()
print("POST model succeeded:n%s" % resp.headers)
get_url = resp.headers["location"]
except Exception as e:
print("POST model failed:n%s" % str(e))
quit() 

n_tries = 15
n_try = 0
wait_sec = 3
max_wait_sec = 60
while n_try < n_tries:
try:
resp = get(url = get_url, headers = headers)
resp_json = resp.json()
if resp.status_code != 200:
print("GET model failed (%s):n%s" % (resp.status_code, json.dumps(resp_json)))
quit()
model_status = resp_json["modelInfo"]["status"]
if model_status == "ready":
print("Training succeeded:n%s" % json.dumps(resp_json))
quit()
if model_status == "invalid":
print("Training failed. Model is invalid:n%s" % json.dumps(resp_json))
quit()
# Training still running. Wait and retry.
time.sleep(wait_sec)
n_try += 1
wait_sec = min(2*wait_sec, max_wait_sec)     
except Exception as e:
msg = "GET model failed:n%s" % str(e)
print(msg)
quit()
print("Train operation did not complete within the allocated time.")

运行上面的代码在Anaconda提示符下得到的输出

POST model succeeded:
{'Content-Length': '0', 'Location': 'https://sctesting.cognitiveservices.azure.com/formrecognizer/v2.0-preview/custom/models/30b7d99b-fc57-466d-a59b-c0d9738c03ac', 'x-envoy-upstream-service-time': '379', 'apim-request-id': '18cbec13-8129-45de-8685-83554e8b35d4', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'x-content-type-options': 'nosniff', 'Date': 'Thu, 20 Feb 2020 19:35:47 GMT'}
Training failed. Model is invalid:
{"modelInfo": {"modelId": "30b7d99b-fc57-466d-a59b-c0d9738c03ac", "status": "invalid", "createdDateTime": "2020-02-20T19:35:48Z", "lastUpdatedDateTime": "2020-02-20T19:35:50Z"}, "trainResult": {"trainingDocuments": [], "errors": [{"code": "2014", "message": "No valid blobs found in the specified Azure blob container. Please conform to the document format/size/page/dimensions requirements."}]}}      

如果您使用from identifier标记工具来做同样的事情,这会起作用吗?你是把文件放在Azure blob的根目录中,还是放在子目录中?

确保blob存储容器中的文件符合此处的要求:https://learn.microsoft.com/en-us/azure/cognitive-services/form-recognizer/overview#custom-型号

如果你的文件看起来不错,也要检查你使用的是哪种SAS令牌。如果您使用的是策略定义的SAS令牌,则可能会出现错误消息。在这种情况下,请尝试切换到具有明确权限的SAS令牌(详细信息如下(:https://stackoverflow.com/a/60235222/12822344

您没有指定源。当您在所选存储帐户的菜单中时,需要生成共享访问签名(SAS(。如果该存储帐户中有一个容器,则需要在URL中包含容器名称。EX.如果您有一个名为"train"的集装箱:"www.…windows.net/?sv=……"-->"www...windows.net/train?sv=.."。否则,您可以尝试使用"前缀"字符串,但我发现它有问题。

此外,您还没有包含订阅密钥。

https://learn.microsoft.com/en-us/azure/cognitive-services/form-recognizer/quickstarts/python-train-extract

尝试从主体中移除Source Filter。它应该起作用。

最新更新