|
import os |
|
import pandas as pd |
|
from datasets import Dataset, DatasetDict, Audio |
|
import soundfile as sf |
|
import numpy as np |
|
from sklearn.model_selection import train_test_split |
|
|
|
|
|
audio_folder = '/home/azureuser/data2/dg_16/' |
|
csv_file = 'digital_green_recordings.csv' |
|
|
|
|
|
df = pd.read_csv(csv_file, sep="$") |
|
|
|
|
|
df['client_id'] = ['speaker_' + str(i) for i in range(len(df))] |
|
|
|
|
|
df['path'] = df['path'].apply(lambda x: os.path.join(audio_folder, x)) |
|
|
|
|
|
df['up_votes'] = 0 |
|
df['down_votes'] = 0 |
|
df['age'] = None |
|
df['gender'] = None |
|
df['accent'] = None |
|
|
|
|
|
def load_audio(file_path): |
|
|
|
audio, sr = sf.read(file_path) |
|
|
|
if len(audio.shape) > 1: |
|
audio = np.mean(audio, axis=1) |
|
return {'audio': {'array': audio, 'sampling_rate': sr}} |
|
|
|
|
|
df['audio'] = df['path'].apply(lambda x: load_audio(x)) |
|
|
|
train_df, test_df = train_test_split(df, test_size=0.2, random_state=42) |
|
|
|
|
|
train_dataset = Dataset.from_pandas(train_df) |
|
test_dataset = Dataset.from_pandas(test_df) |
|
|
|
|
|
train_dataset = train_dataset.cast_column('audio', Audio()) |
|
test_dataset = test_dataset.cast_column('audio', Audio()) |
|
|
|
|
|
dataset_dict = DatasetDict({ |
|
'train': train_dataset, |
|
'test': test_dataset |
|
}) |
|
|
|
|
|
dataset_dict.save_to_disk('data2/digital_green_data') |
|
|
|
|
|
print(dataset_dict['train'][0]) |
|
|
|
print(dataset_dict['test'][0]) |