File size: 1,798 Bytes
da716ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Run this script to generate the model-index files in `models` from the templates in `.templates/models`.
"""

import argparse
from pathlib import Path

from jinja2 import Environment, FileSystemLoader

import modelindex


def generate_readmes(templates_path: Path, dest_path: Path):
    """Add the code snippet template to the readmes"""
    readme_templates_path = templates_path / "models"
    code_template_path = templates_path / "code_snippets.md"

    env = Environment(
        loader=FileSystemLoader([readme_templates_path, readme_templates_path.parent]),
    )

    for readme in readme_templates_path.iterdir():
        if readme.suffix == ".md":
            template = env.get_template(readme.name)

            # get the first model_name for this model family
            mi = modelindex.load(str(readme))
            model_name = mi.models[0].name

            full_content = template.render(model_name=model_name)

            # generate full_readme
            with open(dest_path / readme.name, "w") as f:
                f.write(full_content)


def main():
    parser = argparse.ArgumentParser(description="Model index generation config")
    parser.add_argument(
        "-t",
        "--templates",
        default=Path(__file__).parent / ".templates",
        type=str,
        help="Location of the markdown templates",
    )
    parser.add_argument(
        "-d",
        "--dest",
        default=Path(__file__).parent / "models",
        type=str,
        help="Destination folder that contains the generated model-index files.",
    )
    args = parser.parse_args()
    templates_path = Path(args.templates)
    dest_readmes_path = Path(args.dest)

    generate_readmes(
        templates_path,
        dest_readmes_path,
    )


if __name__ == "__main__":
    main()