Azure Document Translator不工作,尽管响应状态为202

3lxsmp7m  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(92)

我开始学习云。我正在尝试使用Azure翻译服务翻译文档。BLOB的层次结构如下所示。
|- 存储帐户
|--容器1
|---翻译输入
|----sampleTranslation。docx
|---翻译输出
我想翻译 sampleTranslation.docx 并将其存储在 translation-output 文件夹下的单独文件中。
我在做POSTMAN的API调用。响应状态202(表示成功),但没有响应体。翻译也没有发生。
当从Python发送相同的JSON时,它会给出以下错误:
JSON值无法转换为Microsoft。MT.Batch.Contracts.Api.V1.StartTranslationDetails.路径:$|行号:0|BytePositionInLine:590.
你能告诉我哪里做错了吗?
POSTMAN截图附后。
Postman API Screenshot
Python代码:

subscription_key = CONSTANTS["TRANSLATOR_TEXT_SUBSCRIPTION_KEY"]
region = CONSTANTS["TRANSLATOR_TEXT_REGION"]
endpoint = CONSTANTS["TRANSLATOR_DOC_ENDPOINT"]

constructed_url = endpoint + "/translator//text//batch//v1.0//batches"

    headers = {
        "Ocp-Apim-Subscription-Key": subscription_key,
        "Ocp-Apim-Subscription-Region": region,
        "Content-type": "application/json",
        "X-ClientTraceId": str(uuid.uuid4()),
    }
    
    body = json.dumps(
    {
        "inputs": [
            {
                "source": {
                    "sourceUrl": sourceUrl,
                },
                "targets": [
                    {
                        "targetUrl": targetUrl,
                        "language": "fr"
                    }
                ]
            }
        ]
    }
    )
    
    request = requests.post(
        constructed_url, headers=headers, json=body, verify=False
    )
    response = request.json()
    
    print("Request URL:\t", constructed_url)
    print("Response:\t")
    print(
        json.dumps(
            response,
            sort_keys=True,
            indent=4,
            ensure_ascii=False,
            separators=(",", ": "),
        )
    )
nsc4cvqm

nsc4cvqm1#

HTTP状态码202表示请求已被接受处理,这是批量翻译作业等长时间运行操作的正常响应。

我已尽力重复上述要求。我使用了下面的代码。感谢@Shweta Lodha

import requests
import json

route = "/batches"
endpoint = "https://ejinfckad.cognitiveservices.azure.com/translator/text/batch/v1.0"
key = "f054332b55f94072a46e004e3e230f43"

json_data = {
    "inputs": [
        {
            "source": {
                "sourceUrl": "https://wdcw.net/xwx?sp=racwdli&st=2023-04-29T07:37:52Z&se=2023-04-29T15:37:52Z&sv=2021-12-02&sr=c&sig=dcdswdcw %3D",
                "storageSource": "AzureBlob"
            },
            "targets": [
                {
                    "targetUrl": "https://xxwnet/dw1?sp=rcwl&st=2023-04-29T07:35:43Z&se=2023-04-29T15:35:43Z&sv=2021-12-02&sr=c&sig=BI7BjOC%2FLJOMndxwY4Rl9BD%2FDJwybt7MQ%3D",
                    "storageSource": "AzureBlob",
                    "language": "fr"
                }
            ]
        }
    ]
}

headers = {
    "Ocp-Apim-Subscription-Key": key,
    "Content-Type": "application/json"
}

response = requests.post(endpoint + route, headers=headers, json=json_data)

if response.status_code == 200:
    print("Operation successful with status code: ", response.status_code)
else:
    print("Error occurred. Status code: ", response.status_code)

**输出:**如预期。

但是当我签入目标容器时,我可以看到按照要求翻译的文件。

你可以看到Docx文件从英文翻译成法文。

相关问题