Spaces:
Runtime error
Runtime error
Merge branch 'main' of https://huggingface.co/spaces/GEM/DatasetCardForm
Browse files- formatting/README.md +6 -4
- formatting/format_as_md.py +0 -78
- formatting/json_to_md.py +72 -0
formatting/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
# Formatting Utilities
|
2 |
|
3 |
-
The resources in this folder are used to format the saved
|
4 |
-
The file `key_to_question.json` maps from the saved key back to the original question which can then be parsed into question/answer pairs.
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
1 |
# Formatting Utilities
|
2 |
|
3 |
+
The resources in this folder are used to format the saved dataset card in a readable format (JSON, Markdown).
|
|
|
4 |
|
5 |
+
File | Description
|
6 |
+
--- | ---
|
7 |
+
[`key_to_question.json`](https://huggingface.co/spaces/GEM/DatasetCardForm/blob/main/formatting/key_to_question.json) | Maps from the saved key back to the original question, which can then be parsed into question/answer pairs.
|
8 |
+
[`reformat_json.py`](https://huggingface.co/spaces/GEM/DatasetCardForm/blob/main/formatting/reformat_json.py) | Uses `key_to_question.json` to transform dataset cards into a JSON organized by Data Card standards (e.g., sections, subsections, scopes). Everything labeled `N/A` in the original card will be rendered; empty fields will be completely omitted.
|
9 |
+
[`json_to_md.py`](https://huggingface.co/spaces/GEM/DatasetCardForm/blob/main/formatting/json_to_md.py) | Transforms output from `reformat_json.py` into Markdown that is compatible with Data Cards Labs (e.g., special comment syntax).
|
formatting/format_as_md.py
DELETED
@@ -1,78 +0,0 @@
|
|
1 |
-
import argparse
|
2 |
-
import json
|
3 |
-
import pathlib
|
4 |
-
import os
|
5 |
-
|
6 |
-
parser = argparse.ArgumentParser(
|
7 |
-
description="Format the output of the data card tool as .md for the hub."
|
8 |
-
)
|
9 |
-
parser.add_argument("--input_path", "-i", type=pathlib.Path, required=True)
|
10 |
-
parser.add_argument("--output_path", "-o", type=pathlib.Path, required=True)
|
11 |
-
args = parser.parse_args()
|
12 |
-
|
13 |
-
def read_json_file(json_path: pathlib.Path):
|
14 |
-
"""Load a json file and return it as object."""
|
15 |
-
with open(json_path, "r") as f:
|
16 |
-
data = json.load(f)
|
17 |
-
return data
|
18 |
-
|
19 |
-
def save_md_file(md_path: pathlib.Path, md_string: str):
|
20 |
-
"""Takes a string and saves it as .md file."""
|
21 |
-
with open(md_path, 'w') as f:
|
22 |
-
f.write(md_string)
|
23 |
-
|
24 |
-
def construct_md(data_card_data: dict, text_by_key: dict):
|
25 |
-
"""Constructs the markdown file
|
26 |
-
|
27 |
-
This function iterates through text_by_key and extracts all answers from
|
28 |
-
the data_card_data object. It uses the levels of hierarchy as indicator for
|
29 |
-
the heading indentation and does not change the order in which anything
|
30 |
-
appears.
|
31 |
-
|
32 |
-
Args:
|
33 |
-
data_card_data: Output from the data card tool
|
34 |
-
text_by_key: configuration defined in key_to_question.json
|
35 |
-
|
36 |
-
Returns:
|
37 |
-
data_card_md_string: Markdown-formatted content
|
38 |
-
"""
|
39 |
-
|
40 |
-
data_card_md_string = ""
|
41 |
-
|
42 |
-
for main_key, main_content in text_by_key.items():
|
43 |
-
section_header = main_content['section-title']
|
44 |
-
# Remove it so that we don't iterate over it.
|
45 |
-
del main_content['section-title']
|
46 |
-
# Add it to string.
|
47 |
-
data_card_md_string += f"## {section_header} \n\n"
|
48 |
-
# Iterate over the subsections.
|
49 |
-
for second_key, second_content in main_content.items():
|
50 |
-
subsection_header = second_content['section-title']
|
51 |
-
# Remove it so that we don't iterate over it.
|
52 |
-
del second_content['section-title']
|
53 |
-
# Add it to string.
|
54 |
-
data_card_md_string += f"### {subsection_header} \n\n"
|
55 |
-
# Finally, iterate over actual questions.
|
56 |
-
for question_key, question_text in second_content.items():
|
57 |
-
# Now grab the answer text.
|
58 |
-
reply = data_card_data[main_key][second_key].get(question_key, "N/A")
|
59 |
-
if reply and reply != "N/A":
|
60 |
-
data_card_md_string += f"#### {question_text} \n\n"
|
61 |
-
# Special parsing options here.
|
62 |
-
if question_key == question_key == "paper-bibtext":
|
63 |
-
data_card_md_string += f"```\n{reply}\n``` \n\n"
|
64 |
-
elif question_key == "structure-example":
|
65 |
-
data_card_md_string += f"```json\n{reply}\n``` \n\n"
|
66 |
-
elif isinstance(reply, list):
|
67 |
-
data_card_md_string += f"{' '.join(reply)} \n\n"
|
68 |
-
else:
|
69 |
-
data_card_md_string += f"{reply} \n\n"
|
70 |
-
|
71 |
-
return data_card_md_string
|
72 |
-
|
73 |
-
|
74 |
-
if __name__ == "__main__":
|
75 |
-
data_card_data = read_json_file(args.input_path)
|
76 |
-
text_by_key = read_json_file(os.path.join(os.path.dirname(__file__), "key_to_question.json"))
|
77 |
-
data_card_md_string = construct_md(data_card_data, text_by_key)
|
78 |
-
save_md_file(args.output_path, data_card_md_string)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
formatting/json_to_md.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from argparse import ArgumentParser
|
2 |
+
from json import load
|
3 |
+
|
4 |
+
def parse_args():
|
5 |
+
parser = ArgumentParser()
|
6 |
+
parser.add_argument('input', type=str, nargs='+', \
|
7 |
+
help='Specify paths to files (e.g., path/to/*.json)')
|
8 |
+
|
9 |
+
return parser.parse_args()
|
10 |
+
|
11 |
+
|
12 |
+
def json_to_markdown(filename):
|
13 |
+
json = load(open(filename))
|
14 |
+
|
15 |
+
markdown = f'# {json["name"]}\n\n'
|
16 |
+
markdown += json['summary'] + '\n\n'
|
17 |
+
|
18 |
+
for key in json:
|
19 |
+
if key not in ('name', 'summary', 'sections'):
|
20 |
+
markdown += f'#### {key}\n{json[key]}\n\n'
|
21 |
+
|
22 |
+
markdown += '\n'.join(section_to_markdown(section) \
|
23 |
+
for section in json['sections'])
|
24 |
+
|
25 |
+
with open(f'{filename[:-5]}.md', 'w') as f:
|
26 |
+
f.write(markdown)
|
27 |
+
|
28 |
+
|
29 |
+
def section_to_markdown(section):
|
30 |
+
markdown = f'{"#" * section["level"]} {section["title"]}\n\n'
|
31 |
+
markdown += '\n'.join(subsection_to_markdown(subsection) \
|
32 |
+
for subsection in section['subsections'])
|
33 |
+
|
34 |
+
return markdown + '\n'
|
35 |
+
|
36 |
+
|
37 |
+
def subsection_to_markdown(subsection):
|
38 |
+
markdown = f'{"#" * subsection["level"]} {subsection["title"]}\n\n'
|
39 |
+
markdown += '\n'.join(field_to_markdown(field) \
|
40 |
+
for field in subsection['fields'])
|
41 |
+
|
42 |
+
return markdown + '\n'
|
43 |
+
|
44 |
+
|
45 |
+
def field_to_markdown(field):
|
46 |
+
markdown = f'{"#" * field["level"]} {field["title"]}\n\n'
|
47 |
+
|
48 |
+
if 'flags' in field and 'quick' in field['flags']:
|
49 |
+
markdown += f'<!-- quick -->\n'
|
50 |
+
|
51 |
+
if field.get('info', False):
|
52 |
+
markdown += f'<!-- info: {field["info"]} -->\n'
|
53 |
+
|
54 |
+
if field.get('scope', False):
|
55 |
+
markdown += f'<!-- scope: {field["scope"]} -->\n'
|
56 |
+
|
57 |
+
markdown += field.get('content', '')
|
58 |
+
|
59 |
+
return markdown + '\n'
|
60 |
+
|
61 |
+
|
62 |
+
def main():
|
63 |
+
"""Converts JSON output from `reformat_json.py`
|
64 |
+
to Markdown input for Data Cards Labs."""
|
65 |
+
args = parse_args()
|
66 |
+
for filename in args.input:
|
67 |
+
if filename[-5:] == '.json':
|
68 |
+
json_to_markdown(filename)
|
69 |
+
|
70 |
+
|
71 |
+
if __name__ == '__main__':
|
72 |
+
main()
|