Skip to content

Commit f63a5c6

Browse files
committed
add audio
1 parent 278bc94 commit f63a5c6

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

docs/hub/datasets-pandas.md

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ df_test .to_parquet("hf://datasets/username/my_dataset/test.parquet")
6767

6868
## Use Images
6969

70-
From a metadata file containing a "file_name" field for the names or paths to the images:
70+
From a folder with a metadata file containing a "file_name" field for the names or paths to the images:
7171

7272
```
73-
data/ data/
73+
Example 1: Example 2:
74+
folder/ folder/
7475
├── metadata.csv ├── metadata.csv
7576
├── img000.png └── images
7677
├── img001.png ├── img000.png
@@ -81,13 +82,13 @@ data/ data/
8182
```python
8283
import pandas as pd
8384

84-
folder_path = "path/to/data/"
85+
folder_path = "path/to/folder/"
8586
df = pd.read_csv(folder_path + "metadata.csv")
8687
for image_path in (folder_path + df["file_name"]):
8788
...
8889
```
8990

90-
Since the dataset is in a supported structure, you can save this dataset to Hugging Face and the Dataset Viewer shows both the metadata and images on HF.
91+
Since the dataset is in a supported structure, you can save this dataset to Hugging Face and the Dataset Viewer shows both the metadata and images on Hugging Face.
9192

9293
```python
9394
from huggingface_hub import HfApi
@@ -100,6 +101,8 @@ api.upload_folder(
100101
)
101102
```
102103

104+
### Image methods and Parquet
105+
103106
Using [pandas-image-methods](https://github.com/lhoestq/pandas-image-methods) you enable `PIL.Image` methods on an image column. It also enables saving the dataset as one single Parquet file containing both the images and the metadata:
104107

105108
```python
@@ -118,6 +121,63 @@ All the `PIL.Image` methods are available, e.g.
118121
df["image"] = df["image"].pil.rotate(90)
119122
```
120123

124+
## Use Audios
125+
126+
From a folder with a metadata file containing a "file_name" field for the names or paths to the audios:
127+
128+
```
129+
Example 1: Example 2:
130+
folder/ folder/
131+
├── metadata.csv ├── metadata.csv
132+
├── rec000.wav └── audios
133+
├── rec001.wav ├── rec000.wav
134+
... ...
135+
└── recNNN.wav └── recNNN.wav
136+
```
137+
138+
```python
139+
import pandas as pd
140+
141+
folder_path = "path/to/folder/"
142+
df = pd.read_csv(folder_path + "metadata.csv")
143+
for audio_path in (folder_path + df["file_name"]):
144+
...
145+
```
146+
147+
Since the dataset is in a supported structure, you can save this dataset to Hugging Face and the Dataset Viewer shows both the metadata and audios on Hugging Face.
148+
149+
```python
150+
from huggingface_hub import HfApi
151+
api = HfApi()
152+
153+
api.upload_folder(
154+
folder_path=folder_path,
155+
repo_id="username/my_audio_dataset",
156+
repo_type="dataset",
157+
)
158+
```
159+
160+
### Audio methods and Parquet
161+
162+
Using [pandas-audio-methods](https://github.com/lhoestq/pandas-audio-methods) you enable `soundfile` methods on an audio column. It also enables saving the dataset as one single Parquet file containing both the audios and the metadata:
163+
164+
```python
165+
import pandas as pd
166+
from pandas_image_methods import SFMethods
167+
168+
pd.api.extensions.register_series_accessor("sf")(SFMethods)
169+
170+
df["audio"] = (folder_path + df["file_name"]).sf.open()
171+
df.to_parquet("data.parquet")
172+
```
173+
174+
This makes it easy to use with `librosa` e.g. for resampling:
175+
176+
```python
177+
df["audio"] = [librosa.load(audio, sr=16_000) for audio in df["audio"]]
178+
df["audio"] = df["audio"].sf.write()
179+
```
180+
121181
## Use Transformers
122182

123183
You can use `transformers` pipelines on pandas DataFrames to classify, generate text, images, etc.

0 commit comments

Comments
 (0)