Bad request when trying to create glossary

red and white round light

I want to create an unilateral glossary to use with a translation project of mine, using the example commands from Google’s how-to guide: https://cloud.google.com/translate/docs/advanced/glossary#unidirectional_glossary

There was no example to create an unilateral glossary by using python code, only for equivalent set glossaries, and i don’t know what to change in the code.

I created a storage bucket and uploaded my glossary file.

Then I tried to execute this command in powershell:

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
  -Method POST `
  -Headers $headers `
  -ContentType: "application/json; charset=utf-8" `
  -InFile request.json `
  -Uri "https://translation.googleapis.com/v3/projects/746207595643/locations/us-east1/glossaries
" | Select-Object -Expand Content

This is the contents of the request.json file, based on their example:

{
  "name":"projects/746207595643/locations/us-east1/glossaries/kittglossary",
  "languagePair": {
    "sourceLanguageCode": "en",
    "targetLanguageCode": "hu"
    },
  "inputConfig": {
    "gcsSource": {
      "inputUri": "gs://kittgloss/glossary.csv"
    }
  }
}

And I get this error returned:

Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
At line:4 char:1
+ Invoke-WebRequest `
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

I do have the GOOGLE_APPLICATION_CREDENTIALS environment variable, and implicit authentication has worked before when i tried a test translation


After trying the example python code to create glossary:

from google.cloud import translate_v3 as translate

pid = "flawless-acre-284812 ",
iuri = "gs://kittgloss/glossary.csv",
gid = "kittglossary",

def create_glossary(
        project_id,
        input_uri,
        glossary_id,
        timeout,
):
    """
    Create a equivalent term sets glossary. Glossary can be words or
    short phrases (usually fewer than five words).
    https://cloud.google.com/translate/docs/advanced/glossary#format-glossary
    """
    client = translate.TranslationServiceClient()

    # Supported language codes: https://cloud.google.com/translate/docs/languages
    source_lang_code = "en"
    target_lang_code = "hu"
    location = "us-east1"  # The location of the glossary

    name = client.glossary_path(project_id, location, glossary_id)
    language_codes_set = translate.types.Glossary.LanguageCodesSet(
        language_codes=[source_lang_code, target_lang_code]
    )

    gcs_source = translate.types.GcsSource(input_uri=input_uri)

    input_config = translate.types.GlossaryInputConfig(gcs_source=gcs_source)

    glossary = translate.types.Glossary(
        name=name, language_codes_set=language_codes_set, input_config=input_config
    )

    parent = client.location_path(project_id, location)
    # glossary is a custom dictionary Translation API uses
    # to translate the domain-specific terminology.
    operation = client.create_glossary(parent=parent, glossary=glossary)

    result = operation.result(timeout)
    print("Created: {}".format(result.name))
    print("Input Uri: {}".format(result.input_config.gcs_source.input_uri))


create_glossary(pid,iuri,gid,timeout=180)

I get the following error returned, complaining of the file being a tuple instead of str:

Traceback (most recent call last):
  File "C:UserspcAppDataLocalProgramsPythonPython38-32libsite-packagesgoogleprotobufinternalpython_message.py", line 702, in field_setter
    new_value = type_checker.CheckValue(new_value)
  File "C:UserspcAppDataLocalProgramsPythonPython38-32libsite-packagesgoogleprotobufinternaltype_checkers.py", line 215, in CheckValue
    raise TypeError(message)
TypeError: ('gs://kittgloss/glossary.csv',) has type <class 'tuple'>, but expected one of: (<class 'bytes'>, <class 'str'>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:UserspcAppDataLocalProgramsPythonPython38-32libsite-packagesgoogleprotobufinternalpython_message.py", line 558, in init
    setattr(self, field_name, field_value)
  File "C:UserspcAppDataLocalProgramsPythonPython38-32libsite-packagesgoogleprotobufinternalpython_message.py", line 704, in field_setter
    raise TypeError(
TypeError: Cannot set google.cloud.translation.v3.GcsSource.input_uri to ('gs://kittgloss/glossary.csv',): ('gs://kittgloss/glossary.csv',) has type <class 'tuple'>, but expected one of: (<class 'bytes'>, <class 'str'>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:pyglossary.py", line 48, in <module>
    create_glossary(pid,iuri,gid,timeout=180)
  File "C:pyglossary.py", line 30, in create_glossary
    gcs_source = translate.types.GcsSource(input_uri=input_uri)
  File "C:UserspcAppDataLocalProgramsPythonPython38-32libsite-packagesprotomessage.py", line 421, in __init__
    self.__dict__["_pb"] = self._meta.pb(**params)
  File "C:UserspcAppDataLocalProgramsPythonPython38-32libsite-packagesgoogleprotobufinternalpython_message.py", line 560, in init
    _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
  File "C:UserspcAppDataLocalProgramsPythonPython38-32libsite-packagesgoogleprotobufinternalpython_message.py", line 477, in _ReraiseTypeErrorWithFieldName
    six.reraise(type(exc), exc, sys.exc_info()[2])
  File "C:UserspcAppDataLocalProgramsPythonPython38-32libsite-packagessix.py", line 702, in reraise
    raise value.with_traceback(tb)
  File "C:UserspcAppDataLocalProgramsPythonPython38-32libsite-packagesgoogleprotobufinternalpython_message.py", line 558, in init
    setattr(self, field_name, field_value)
  File "C:UserspcAppDataLocalProgramsPythonPython38-32libsite-packagesgoogleprotobufinternalpython_message.py", line 704, in field_setter
    raise TypeError(
TypeError: Cannot set google.cloud.translation.v3.GcsSource.input_uri to ('gs://kittgloss/glossary.csv',): ('gs://kittgloss/glossary.csv',) has type <class 'tuple'>, but expected one of: (<class 'bytes'>, <class 'str'>) for field GcsSource.input_uri

The glossary file is very simple and the first few lines look like so:

rear bumpers,hátsó lökhárító
front bhumper spoiler,első lökhárító spoiler
front bumpers,első lökhárító

I’d appreciate any help.

edited 18 secs ago
LadyKillJoy

asked 1 hour ago
LadyKillJoyLadyKillJoy

112 bronze badges

Latest posts