def update_hparams_given_example_spec(hparams, example_spec):
  """Updates 'modality' and 'vocab_size' attrs. of `hparams` given `example_spec`."""
  modality_update = {}
  vocab_size_update = {}
  for field_name, field in example_spec.fields.items():
    modality_update[field_name] = field.modality
    vocab_size_update[field_name] = field.vocab_size
  setattr(hparams, "modality", modality_update)
  setattr(hparams, "vocab_size", vocab_size_update)
  return hparams