sin2piusc commited on
Commit
f388230
·
verified ·
1 Parent(s): 81c7cc6

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +18 -46
README.md CHANGED
@@ -32,50 +32,22 @@ size_categories:
32
  ---
33
 
34
  <pre><code>
35
- # Extracts audio files and transcriptions from datasets. Creates an mp3/wav and a text file with transcription for each sample.
36
-
37
- import soundfile as sf, os, csv, shutil
38
- from datasets import load_dataset
39
- from tqdm import tqdm
40
-
41
- dataset = load_dataset("dataset", split="train", trust_remote_code=True, streaming=True)
42
-
43
- name = "dataset"
44
- os.makedirs("./datasets/" + name, exist_ok=True) # Create a folder to store the audio and transcription files
45
- folder_path = "./datasets/" + name
46
- filter = dataset.filter(lambda sample: sample["sentence"] != 0 and sample["audio"] != 0) # Filter out samples you do/don't want
47
-
48
- # This creates the audio and transcripition files and saves each individually
49
-
50
- for i, sample in tqdm(enumerate(dataset)): # Process each sample in the dataset - use filter if filtering
51
- audio_path = os.path.join(folder_path, name + f'_{i}.mp3') # Path to save the audio file - can change mp3 to other format
52
- transcription_path = os.path.join(folder_path, name + f'_{i}.txt') # Path to save the transcription file
53
- sf.write(audio_path, sample['audio']['array'], sample['audio']['sampling_rate']) # Save the audio file
54
- sample["audio_length"] = len(sample["audio"]["array"]) / sample["audio"]["sampling_rate"] # Get audio length
55
- with open(transcription_path, 'w', encoding='utf-8') as transcription_file: # Save the transcription file
56
- transcription_file.write(sample['sentence']) # Save the transcription sentence
57
- transcription_file.write(str(","+str(sample['audio_length']))) # Save the audio length
58
-
59
- # This creates a metadata.csv file with file_names and sentences in columns and then moves transcription text files to txt directory
60
-
61
- name = name
62
- folder_path = "./datasets/" + name
63
- output_file = folder_path + '/metadata.csv'
64
-
65
- with open(output_file, 'w', encoding='utf-8') as outfile:
66
- writer = csv.writer(outfile) # Creates csv
67
- writer.writerow(('file_name', 'sentence')) # Creates column heads
68
- for file_name in os.listdir(folder_path):
69
- if file_name.endswith('.txt'):
70
- filepath = os.path.join(folder_path, file_name)
71
- with open(filepath, 'r', encoding='utf-8') as infile:
72
- outfile.write((file_name.rstrip('.txt'))+".mp3" + ",") # Add file names with trailing comma to csv
73
- outfile.write(infile.read()) # Add sentence/transcriptions on the same line as file name
74
- outfile.write('\n') # Adds a return
75
- for file_name in os.listdir(folder_path):
76
- os.makedirs(folder_path + "./txt/", exist_ok=True)
77
- if file_name.endswith('.txt'):
78
- source_path = os.path.join(folder_path, file_name)
79
- dest_path = os.path.join(folder_path + '/txt/', file_name)
80
- shutil.move(source_path, dest_path) # Moves txt files
81
  </code></pre>
 
32
  ---
33
 
34
  <pre><code>
35
+
36
+ dataset = load_dataset("sin2piusc/CSS10_jsss_jvs_cv")
37
+ output_file = 'metadata.csv'
38
+
39
+ special_characters = '[,♬「」 ?!;:“%‘” ~♪…~ !?!?#$%&゛#$%&()*+:;〈=〉?@^_‘{|}~".,]'
40
+
41
+ for i, sample in tqdm(enumerate(dataset)): # Process each sample in the filtered dataset
42
+ audio_sample = name + f'_{i}.mp3'
43
+ audio_path = os.path.join(folder_path, audio_sample)
44
+ transcription_path = os.path.join(folder_path, output_file) # Path to save transcription file
45
+ sf.write(audio_path, sample['audio']['array'], sample['audio']['sampling_rate']) # Save mp3/wav files
46
+ sample["audio_length"] = len(sample["audio"]["array"]) / sample["audio"]["sampling_rate"] # Get audio length
47
+ with open(transcription_path, 'a', encoding='utf-8') as transcription_file: # Save transcription file
48
+ sample["sentence"] = re.sub(special_characters,'', sample["sentence"]) # Remove special characters
49
+ transcription_file.write(audio_sample + ",") # Save transcription file name
50
+ transcription_file.write(sample['sentence']) # Save transcription
51
+ transcription_file.write(str(","+str(sample['audio_length']))) # Save audio length
52
+ transcription_file.write('\n')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  </code></pre>