File size: 1,166 Bytes
392dfd9
 
ce24f5e
 
 
37293dc
ce24f5e
 
392dfd9
 
 
 
 
 
 
37293dc
392dfd9
553a86b
 
 
392dfd9
ce24f5e
a6028d3
 
ce24f5e
 
a6028d3
ce24f5e
392dfd9
ce24f5e
 
 
392dfd9
 
 
 
ce24f5e
e9650d3
ce24f5e
 
 
 
 
 
 
a6028d3
ce24f5e
392dfd9
ce24f5e
 
 
 
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
"""Module to convert json file to jsonl"""

import os
import sys
from pathlib import Path
from typing import Optional, Union

import fire

from axolotl.convert import (
    FileReader,
    FileWriter,
    JsonlSerializer,
    JsonParser,
    JsonToJsonlConverter,
    StdoutWriter,
)
from axolotl.logging_config import configure_logging

configure_logging()

# add src to the pythonpath so we don't need to pip install this
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
src_dir = os.path.join(project_root, "src")
sys.path.insert(0, src_dir)


def main(
    file: Path,
    output: Optional[Path] = None,
    to_stdout: Optional[bool] = False,
):
    """
    Convert a json file to jsonl
    """

    file_reader = FileReader()
    writer: Union[StdoutWriter, FileWriter]
    if to_stdout or output is None:
        writer = StdoutWriter()
    else:
        writer = FileWriter(output)
    json_parser = JsonParser()
    jsonl_serializer = JsonlSerializer()

    converter = JsonToJsonlConverter(file_reader, writer, json_parser, jsonl_serializer)

    converter.convert(file, output)


if __name__ == "__main__":
    fire.Fire(main)