File size: 2,359 Bytes
8b833c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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])