【问题标题】:Using Microsoft Custom Translator in Python在 Python 中使用 Microsoft 自定义翻译器
【发布时间】:2020-07-29 14:27:14
【问题描述】:

我为来自生命科学行业的文本的德语 - 英语翻译训练了一个自定义模型。

我写了一个小的 Python 脚本来测试,但这会引发错误。

有什么提示可以找到一个工作示例(可以是任何其他语言)?

import http.client, urllib.parse, uuid, json

subscriptionKey = 'xxxxxxxxxxxxxxx'
host = 'api.cognitive.microsofttranslator.com'
path = '/translate?api-version=3.0'

ToLanguage = "&to=en"
Category = "yyyyyy"

text = 'Klinische Anwendung'


def translate (content, cat):
    headers = {
        'Ocp-Apim-Subscription-Key': subscriptionKey,
        'Content-type': 'application/json',
        'X-ClientTraceId': str(uuid.uuid4())
    }

    conn = http.client.HTTPConnection(host)
    conn.request("POST", path + ToLanguage + "&category=" + cat, content, headers)
    response = conn.getresponse()
    print(response.readlines())
    return response.read()

requestBody = [{
    'Text' : text,
}]

content = json.dumps(requestBody, ensure_ascii=False).encode('utf-8')

# Translate
result2 = translate(content, Category)
output2 = json.dumps(json.loads(result2), indent=4, ensure_ascii=False)
print('translation with custom model')
print(output2)

Error looks like this:
[b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\r\n', b'<html xmlns="http://www.w3.org/1999/xhtml">\r\n', b'<head>\r\n', b'<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>\r\n', b'<title>404 - File or directory not found.</title>\r\n', b'<style type="text/css">\r\n', b'<!--\r\n', b'body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}\r\n', b'fieldset{padding:0 15px 10px 15px;} \r\n', b'h1{font-size:2.4em;margin:0;color:#FFF;}\r\n', b'h2{font-size:1.7em;margin:0;color:#CC0000;} \r\n', b'h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} \r\n', b'#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;\r\n', b'background-color:#555555;}\r\n', b'#content{margin:0 0 0 2%;position:relative;}\r\n', b'.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}\r\n', b'-->\r\n', b'</style>\r\n', b'</head>\r\n', b'<body>\r\n', b'<div id="header"><h1>Server Error</h1></div>\r\n', b'<div id="content">\r\n', b' <div class="content-container"><fieldset>\r\n', b'  <h2>404 - File or directory not found.</h2>\r\n', b'  <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>\r\n', b' </fieldset></div>\r\n', b'</div>\r\n', b'</body>\r\n', b'</html>\r\n']
b''

【问题讨论】:

    标签: python azure-cognitive-services microsoft-translator


    【解决方案1】:

    您收到的消息是 404 未找到,因此您的呼叫未指向正确的端点。

    我想到的第一件事是 https:// 在您的主机值的开头丢失:您可以更改:

    host = 'api.cognitive.microsofttranslator.com'
    

    通过

    host = 'https://api.cognitive.microsofttranslator.com'
    

    由于我不习惯 Python,可能还不够。如果它不起作用,您能否分享更多变量的详细信息?

    供您参考,使用 Postman,当使用 http 而不是 https 时,我也得到了 404:

    只需添加 S,就可以了:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      相关资源
      最近更新 更多