1
votes

I trained a custom model for German - English translation of text from the Life Science Industry.

I wrote a small Python script to test but this throws an error.

Any hints to find a working example (could be any other language)?

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''
1

1 Answers

0
votes

The message you got is a 404 not found, so your call is not pointing to the right endpoint.

The 1st thing that I have in mind is https:// missing in the beginning of your host value: can you change:

host = 'api.cognitive.microsofttranslator.com'

by

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

As I'm not used to Python, it may not be sufficient. If it does not work, can you share more details of your variables?

For your information, with Postman, when http is used instead of https, I also got a 404:

Sample with http

Just adding the S, it works:

enter image description here