File size: 3,160 Bytes
c6e3750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from score_acculumator import ScoreAccumulator
import argparse, json, os


def main(filepath, measures, writeback=False, outpath=""):
    """
    Evaluate a model using specified measures given a filepath to database of songs.

    Database must be formatted as follows:
    [
        #dict 1 for song 1
        {
            "id": (int) ...,
            "prompt": (str) ...,                  #optional
            "model_response": (str) ...,
            "target_response": (str) ...          #optional
        },
        #dict 2 for song 2
        {
            " "
        },
        .
        .
        .
    ]

    Parameters
    ----------
    filepath : str
        path to database .json file
    measures : list
        list of measures to evaluate model outputs, from {'diversity','meter','syllable'}
    writeback : bool, optional
        Whether to write evaluation scores to filepath or to output, by default False
    outpath : str, optional
        path to output .json file if writeback is False, by default ""

    Raises
    ------
    FileNotFoundError
    """
    # read file
    if os.path.exists(filepath):
        with open(filepath, "r") as f:
            database = json.load(f)
    else:
        raise FileNotFoundError(f"No such file exists: {filepath}")

    if not writeback:
        if not os.path.exists(outpath):
            raise FileNotFoundError(f"No such file exists: {outpath}")
    else:
        outpath = filepath

    # evaluate for measures
    accumulator = ScoreAccumulator(
        measures=measures,
        require_prompt="prompt" in database[0],
        require_target="target_response" in database[0],
    )
    accumulator.score_all_songs(database)

    # print total scores
    for measure in measures:
        pred_score = accumulator.get_total_pred_score(measure)
        target_score = accumulator.get_total_target_score(measure)
        print(f"Score: pred {pred_score:2f}, target {target_score:2f} : ({measure})")

    # save evaluation
    out_database = []
    for id, song_dict in accumulator._database.items():
        song_dict["id"] = int(id)
        out_database.append(song_dict)

    with open(filepath, "w") as f:
        f.write(json.dumps(out_database, indent=2, separators=[",", ":"]))


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Evaluate a model given a filepath to a database of songs."
    )
    parser.add_argument(
        "filepath", type=str, help="The path to the file to be processed"
    )
    parser.add_argument(
        "--measures",
        default=["diversity", "meter", "syllable"],
        nargs="+",
        help="List of measures to evaluate. From {'diversity','meter','syllable'}",
    )
    parser.add_argument(
        "--writeback",
        type=bool,
        default=True,
        help="Write evaluation scores back to the same dict or create a new one",
    )
    parser.add_argument(
        "--output",
        default="",
        type=str,
        help="The path to the write output scores if writeback is false",
    )

    args = parser.parse_args()
    main(args.filepath, args.measures, args.writeback, args.output)