-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_asr.py
87 lines (77 loc) · 2.54 KB
/
test_asr.py
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
import base64
import json
from typing import Dict, List
import pandas as pd
import requests
from tqdm import tqdm
from pathlib import Path
from scoring.asr_eval import asr_eval
from dotenv import load_dotenv
import os
load_dotenv()
TEAM_NAME = os.getenv("TEAM_NAME")
TEAM_TRACK = os.getenv("TEAM_TRACK")
def main():
# input_dir = Path(f"/home/jupyter/{TEAM_TRACK}")
input_dir = Path(f"data")
# results_dir = Path(f"/home/jupyter/{TEAM_NAME}")
results_dir = Path("results")
results_dir.mkdir(parents=True, exist_ok=True)
instances = []
lim = 16
c = 0
with open(input_dir / "asr.jsonl", "r") as f:
for line in f:
if c < lim:
if line.strip() == "":
continue
instance = json.loads(line.strip())
with open(input_dir / "audio" / instance["audio"], "rb") as file:
audio_bytes = file.read()
instances.append(
{**instance, "b64": base64.b64encode(audio_bytes).decode("ascii")}
)
c+=1
else:
break
results = run_batched(instances)
df = pd.DataFrame(results)
df.to_csv(results_dir / "asr_results.csv", index=False)
# calculate eval
eval_result = asr_eval(
[result["transcript"] for result in results],
[result["prediction"] for result in results],
)
print(f"1-WER: {eval_result}")
def run_batched(
instances: List[Dict[str, str | int]], batch_size: int = 4
) -> List[Dict[str, str | int]]:
# split into batches
results = []
for index in tqdm(range(0, len(instances), batch_size)):
_instances = instances[index : index + batch_size]
response = requests.post(
"http://localhost:5001/stt",
data=json.dumps(
{
"instances": [
{"key": _instance["key"], "b64": _instance["b64"]}
for _instance in _instances
]
}
),
)
_results = response.json()["predictions"]
results.extend(
[
{
"key": _instances[i]["key"],
"transcript": _instances[i]["transcript"],
"prediction": _results[i],
}
for i in range(len(_instances))
]
)
return results
if __name__ == "__main__":
main()