KikuyuASR_trainingdataset / digital_green_process_data.py
Vinsingh's picture
Upload 2 files
8b833c9 verified
Raw History Blame Contribute Delete
2.36 kB
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
# Paths
audio_folder = '/home/azureuser/data2/dg_16/' # Path where your audio files are stored
csv_file = 'digital_green_recordings.csv' # Path to the CSV that contains audio paths and transcripts
# Read your CSV file (assumes it has columns: 'path' and 'transcript')
df = pd.read_csv(csv_file, sep="$")
# Create a new column for client_id (random or default if you don’t have speaker info)
df['client_id'] = ['speaker_' + str(i) for i in range(len(df))]
# If your CSV has relative paths, ensure the paths are correct
df['path'] = df['path'].apply(lambda x: os.path.join(audio_folder, x))
# Add additional columns needed for the Common Voice format (can be optional)
df['up_votes'] = 0
df['down_votes'] = 0
df['age'] = None
df['gender'] = None
df['accent'] = None
# Function to load and possibly convert audio to mono
def load_audio(file_path):
# Load audio file
audio, sr = sf.read(file_path)
# Convert to mono if stereo
if len(audio.shape) > 1:
audio = np.mean(audio, axis=1)
return {'audio': {'array': audio, 'sampling_rate': sr}}
# Apply audio loading function to DataFrame
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) # Adjust test_size as needed
# Convert DataFrames to Hugging Face Datasets
train_dataset = Dataset.from_pandas(train_df)
test_dataset = Dataset.from_pandas(test_df)
# Cast the 'audio' column to the 'audio' type
train_dataset = train_dataset.cast_column('audio', Audio())
test_dataset = test_dataset.cast_column('audio', Audio())
# Create a DatasetDict to simulate train/test/validation splits if needed
dataset_dict = DatasetDict({
'train': train_dataset,
'test': test_dataset # If you have separate splits, add them here (e.g., 'train', 'test', 'validation')
})
# Save the dataset (optional) for future use
dataset_dict.save_to_disk('data2/digital_green_data')
# Print a sample from the dataset
print(dataset_dict['train'][0])
print(dataset_dict['test'][0])