File size: 2,555 Bytes
e1d44f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Resolve repository-relative training audio paths without changing the data."""
import argparse,json
from pathlib import Path,PurePosixPath


def repo_path(root,value):
    p=PurePosixPath(value)
    if p.is_absolute() or '..' in p.parts or '\\' in value or ':' in value:raise ValueError('Expected a safe repository-relative path: '+value)
    result=root.joinpath(*p.parts).resolve()
    if not result.is_relative_to(root):raise ValueError('Path escapes repository root: '+value)
    return result


def default_train(root):
    for value in ('P0/known_stem_mix/_shared/data/train.jsonl','_shared/data/train.jsonl'):
        if (root/value).is_file():return value
    raise FileNotFoundError('Provide --train for the global training manifest')


def load_train(repo_root,manifest=None,subtypes=None):
    root=Path(repo_root).resolve();path=repo_path(root,manifest or default_train(root))
    with path.open() as f:
        for line in f:
            if not line.strip():continue
            row=json.loads(line)
            if subtypes and row.get('subtype') not in subtypes:continue
            for key in ('input_audio','target_audio'):
                if key in row:row[key]=str(repo_path(root,row[key]))
            for message in row['messages']:
                for item in message['content']:
                    if item.get('type')=='audio':item['audio_url']=str(repo_path(root,item['audio_url']))
            yield row


def main():
    p=argparse.ArgumentParser(description=__doc__);p.add_argument('--repo-root',type=Path,required=True);p.add_argument('--train');p.add_argument('--subtype',action='append');p.add_argument('--output',type=Path);args=p.parse_args()
    count=0
    if args.output:
        root=args.repo_root.resolve();original=repo_path(root,args.train or default_train(root))
        if args.output.resolve()==original:raise ValueError('Output must differ from the original training manifest')
        with args.output.open('x') as f:
            for row in load_train(args.repo_root,args.train,args.subtype):f.write(json.dumps(row,ensure_ascii=False)+'\n');count+=1
    else:
        for row in load_train(args.repo_root,args.train,args.subtype):
            for m in row['messages']:
                for item in m['content']:
                    if item.get('type')=='audio' and not Path(item['audio_url']).is_file():raise FileNotFoundError(item['audio_url'])
            count+=1
    print(json.dumps({'training_rows':count,'resolved_manifest':str(args.output) if args.output else None}))

if __name__=='__main__':main()