tsw0411 commited on
Commit
0ddb42e
·
verified ·
1 Parent(s): 89b8778

Upload filter_and_upload.py

Browse files
Files changed (1) hide show
  1. filter_and_upload.py +72 -0
filter_and_upload.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from datasets import load_dataset, Dataset
3
+
4
+ def check_no_overlap_and_min_duration(utterances, min_duration=1.2):
5
+ """
6
+ Check that:
7
+ 1. No overlap between consecutive utterances
8
+ 2. Every utterance is at least min_duration seconds long
9
+ """
10
+ # Empty utterance list should not be considered valid.
11
+ if not utterances:
12
+ return False
13
+
14
+ prev_end = -float("inf")
15
+ for utt in utterances:
16
+ words = utt["words"]
17
+ if not words:
18
+ return False
19
+ utt_start = words[0]["start_time"]
20
+ utt_end = words[-1]["end_time"]
21
+
22
+ # Check no overlap with previous utterance
23
+ if utt_start < prev_end:
24
+ return False
25
+
26
+ # Check minimum duration
27
+ if utt_end - utt_start < min_duration:
28
+ return False
29
+
30
+ prev_end = utt_end
31
+ return True
32
+
33
+
34
+ def main():
35
+ ds1 = load_dataset("humanify/si", name="naturalistic", split="test", streaming=True)
36
+ ds2 = load_dataset("humanify/si", name="improvised", split="test", streaming=True)
37
+ # merge
38
+ from itertools import chain
39
+ ds = chain(ds1, ds2)
40
+
41
+ selected = []
42
+ for sample in ds:
43
+ utterances = json.loads(sample["utterances_json"])
44
+ if check_no_overlap_and_min_duration(utterances):
45
+ selected.append(sample)
46
+ print(f"[{len(selected)}/100] Selected: {sample['conversation_id']}")
47
+ if len(selected) >= 50:
48
+ break
49
+
50
+ print(f"\nTotal selected: {len(selected)}")
51
+ if len(selected) < 50:
52
+ print("WARNING: Not enough samples meeting criteria!")
53
+
54
+ rows = {k: [] for k in selected[0].keys()}
55
+ for s in selected:
56
+ for k, v in s.items():
57
+ rows[k].append(v)
58
+
59
+ eval_ds = Dataset.from_dict(rows)
60
+ print(len(eval_ds))
61
+
62
+ if len(eval_ds) == 50:
63
+ print(f"\nDataset info: {eval_ds}")
64
+ print("Pushing to hub: humanify/si-eval-50 ...")
65
+ eval_ds.push_to_hub("humanify/si-eval-50", split="test")
66
+ print("Done!")
67
+ else:
68
+ print(len(eval_ds))
69
+
70
+
71
+ if __name__ == "__main__":
72
+ main()