qanastek commited on
Commit
91a6df6
·
verified ·
1 Parent(s): d6e5427

Upload 9 files

Browse files
convert_to_parquet.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import Dataset, Audio, Features, Value, Version
2
+ import os
3
+ import csv
4
+
5
+ # Point to the current directory
6
+ dataset_path = os.path.dirname(os.path.abspath(__file__))
7
+
8
+ # Dataset metadata
9
+ VERSION = Version("1.0.0")
10
+ DESCRIPTION = "Background noise dataset."
11
+ HOMEPAGE = "https://huggingface.co/datasets/sdialog/background"
12
+ LICENSE = "CC BY-NC-SA 4.0"
13
+
14
+ # Define features
15
+ features = Features({
16
+ "audio": Audio(sampling_rate=16000),
17
+ "label": Value("string"),
18
+ "description": Value("string"),
19
+ })
20
+
21
+ def dataset_generator():
22
+ csv_path = os.path.join(dataset_path, "metadata.csv")
23
+ if not os.path.exists(csv_path):
24
+ raise FileNotFoundError(f"{csv_path} not found. Run create_metadata.py first.")
25
+
26
+ with open(csv_path, "r", encoding="utf-8") as f:
27
+ reader = csv.DictReader(f)
28
+ for row in reader:
29
+ audio_path = row["audio"]
30
+ # Ensure the path is absolute for reading
31
+ # audio_path is relative to the dataset root
32
+ full_path = os.path.join(dataset_path, audio_path)
33
+
34
+ # Read audio bytes to ensure embedding
35
+ with open(full_path, "rb") as audio_file:
36
+ audio_bytes = audio_file.read()
37
+
38
+ yield {
39
+ "audio": {"path": None, "bytes": audio_bytes},
40
+ "label": row["label"],
41
+ "description": row["description"]
42
+ }
43
+
44
+ print("Creating dataset from generator (embedding bytes)...")
45
+ ds = Dataset.from_generator(dataset_generator, features=features)
46
+
47
+ # Set dataset info
48
+ ds.info.version = VERSION
49
+ ds.info.description = DESCRIPTION
50
+ ds.info.homepage = HOMEPAGE
51
+ ds.info.license = LICENSE
52
+
53
+ print("Saving to Parquet...")
54
+ output_path = os.path.join(dataset_path, "data/train-00000-of-00001.parquet")
55
+ os.makedirs(os.path.dirname(output_path), exist_ok=True)
56
+ ds.to_parquet(output_path)
57
+
58
+ print(f"Saved to {output_path}")
create_metadata.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import csv
3
+
4
+ # Define paths
5
+ base_path = os.path.dirname(os.path.abspath(__file__))
6
+ # The audio folders are directly in the base path for this dataset
7
+ train_dir = base_path
8
+ tags_path = os.path.join(base_path, "tags_labels.csv")
9
+ output_csv = os.path.join(base_path, "metadata.csv")
10
+
11
+ # Load tags/descriptions
12
+ labels_desc = {}
13
+ if os.path.exists(tags_path):
14
+ with open(tags_path, 'r', encoding='utf-8') as file_tags:
15
+ for line in file_tags:
16
+ parts = line.strip().split(';')
17
+ if len(parts) >= 2:
18
+ tag_name = parts[0].strip()
19
+ tag_description = parts[1].strip()
20
+ labels_desc[tag_name] = tag_description
21
+ else:
22
+ print(f"Warning: {tags_path} not found.")
23
+
24
+ # Prepare CSV data
25
+ csv_data = []
26
+ header = ["audio", "label", "description"]
27
+
28
+ if os.path.exists(train_dir):
29
+ # Iterate over label directories
30
+ for label in sorted(os.listdir(train_dir)):
31
+ label_dir = os.path.join(train_dir, label)
32
+
33
+ # Skip hidden files, non-directories, and special folders/files
34
+ if not os.path.isdir(label_dir) or label.startswith('.'):
35
+ continue
36
+
37
+ # Only process directories that are likely audio labels (simple heuristic or check against tags)
38
+ # Here we just check if it's not the 'data' output folder or hidden
39
+ if label == "data":
40
+ continue
41
+
42
+ description = labels_desc.get(label, "XXX")
43
+
44
+ # Iterate over audio files in the label directory
45
+ for audio_file in sorted(os.listdir(label_dir)):
46
+ if audio_file.lower().endswith(('.wav', '.mp3', '.ogg', '.flac')):
47
+ # Create relative path: label/file.wav (relative to metadata.csv location)
48
+ relative_path = os.path.join(label, audio_file)
49
+
50
+ csv_data.append([relative_path, label, description])
51
+
52
+ # Write to CSV
53
+ with open(output_csv, "w", newline="", encoding="utf-8") as f:
54
+ writer = csv.writer(f)
55
+ writer.writerow(header)
56
+ writer.writerows(csv_data)
57
+
58
+ print(f"Successfully created {output_csv} with {len(csv_data)} entries.")
data/train-00000-of-00001.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e5760fdb2716b1c3b5ca88f4294eb90cca543bb23e15cec8c8f5c2293c98d3b4
3
+ size 56836437
generate_data.sh ADDED
@@ -0,0 +1 @@
 
 
1
+ python create_metadata.py && python convert_to_parquet.py
metadata.csv ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ audio,label,description
2
+ fan_noise/210098__yuval__room-big-fan.wav,fan_noise,Fan noise background
3
+ fan_noise/674563__klankbeeld__room-tone-fan-801am-220813_0497.wav,fan_noise,Fan noise background
4
+ white_noise/white_zero_zero.wav,white_noise,White noise background
tags_labels.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fan_noise;Fan noise background
2
+ white_noise;White noise background