sin2piusc commited on
Commit
abe9903
·
verified ·
1 Parent(s): 31bfd10

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +50 -2
README.md CHANGED
@@ -21,7 +21,6 @@ configs:
21
  license: apache-2.0
22
  task_categories:
23
  - automatic-speech-recognition
24
- - translation
25
  language:
26
  - ja
27
  tags:
@@ -30,4 +29,53 @@ tags:
30
  - asr
31
  size_categories:
32
  - 10K<n<100K
33
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  license: apache-2.0
22
  task_categories:
23
  - automatic-speech-recognition
 
24
  language:
25
  - ja
26
  tags:
 
29
  - asr
30
  size_categories:
31
  - 10K<n<100K
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["language"] == "Japanese") # 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>