1

I have a json file like,

{
    "model_name_or_path": "microsoft/layoutlmv3-base",
    "config_name": null,
    "tokenizer_name": null,
    "cache_dir": null,
    "model_revision": "main",
    "use_auth_token": false
}

I want to convert this JSON file into a parser format, so as to use by this method:

print(model_args.model_name_or_path)
print(model_args.tokenizer_name) #etc..

I followed some instruction by using argparse in python, but still have no idea after creating a new argparser.

with open("model_args.json", 'r') as f:
    model_json = json.load(f)

    model_args = argparse.ArgumentParser()
    # model_args.add_argument(default=model_json)
    # model_args.parse_args(namespace=argparse.Namespace(**json.loads(f)))
    print(model_args.model_name_or_path)

What should I do next step, if I want to add this json file into parser?

dimnnv
  • 678
  • 3
  • 8
  • 21
4daJKong
  • 1,825
  • 9
  • 21
  • 1
    method `parse_args` returns Namespace object full of your parameters. `args = model_args.parse_args(namespace=argparse.Namespace(**json.loads(f)))` – pyjedy Mar 17 '23 at 06:43
  • @ukBaz thanks, but I am not sure if they are different as parser when training model. Let me use your answer firstly – 4daJKong Mar 17 '23 at 06:50
  • @ukBaz I face a problem, that shows `'types.SimpleNamespace' object has no attribute 'get_process_log_level'` actually, my json file from 'transformers import TrainingArguments' and it is not convenient if I input args in cmd every time, so I save these parameters to json file firstly. I notice that 'TrainingArguments()' have convert to namespace() – 4daJKong Mar 17 '23 at 07:54
  • It's not clear what you are trying to do. `argparse.Namespace(**json.loads(f))` makes a Namespace object which can be used as though those values were entered via commandline and proper parser. And supply that namespace to the `parse_args` of a different parser, would add those values to that parser's output. (with some possible overwrites). Otherwise I don't see what you can mean "covert the json into a parser". – hpaulj Mar 17 '23 at 16:03

1 Answers1

2

It sounds like maybe you are looking for a namedtuple.

For example:

import json
from collections import namedtuple

json_text = """{
    "model_name_or_path": "microsoft/layoutlmv3-base",
    "config_name": null,
    "tokenizer_name": null,
    "cache_dir": null,
    "model_revision": "main",
    "use_auth_token": false
}"""

data = json.loads(json_text)

ModelArgs = namedtuple('ModelArgs', list(data.keys()))

model_args = ModelArgs(**data)

print(model_args.model_name_or_path)
print(model_args.tokenizer_name)
print(model_args.cache_dir)

OUTPUT:

microsoft/layoutlmv3-base
None
main

Documentation for namedtuple found here

Alexander
  • 16,091
  • 5
  • 13
  • 29