{ "cells": [ { "cell_type": "code", "execution_count": 166, "id": "33a80790", "metadata": {}, "outputs": [], "source": [ "from datasets import load_dataset, Audio\n", "import IPython.display as ipd\n", "\n", "ds = load_dataset(\"znhoughton/TeleSpoof\", streaming=True)" ] }, { "cell_type": "code", "execution_count": 178, "id": "f7ef3dab", "metadata": {}, "outputs": [], "source": [ "from datasets import load_dataset, Audio\n", "import tarfile\n", "\n", "\n", "# Step 2: Load the dataset with the Audio feature\n", "dataset = load_dataset(\n", " \"znhoughton/TeleSpoof\"\n", ")\n", "\n", "# Step 3: Access and play the audio\n", "audio_sample = dataset['train'][\"hf_path\"] # Access the first audio sample\n" ] }, { "cell_type": "code", "execution_count": 179, "id": "86a9df96", "metadata": {}, "outputs": [], "source": [ "audio_sample = audio_sample[0]\n" ] }, { "cell_type": "code", "execution_count": 180, "id": "c4a87a76", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import tarfile\n", "import io\n", "import soundfile as sf # pip install soundfile\n", "import IPython.display as ipd\n", "\n", "\n", "tar_path, wav_inside_tar = audio_sample.split(\"::\")\n", "\n", "with tarfile.open(tar_path, \"r:gz\") as tar:\n", " wav_fileobj = tar.extractfile(wav_inside_tar)\n", " data, samplerate = sf.read(io.BytesIO(wav_fileobj.read()))\n", "\n", "# Play the audio\n", "ipd.Audio(data, rate=samplerate)\n" ] }, { "cell_type": "markdown", "id": "60513b98", "metadata": {}, "source": [ "Some code to organize the dataset. \n", "Will delete later." ] }, { "cell_type": "markdown", "id": "65089641", "metadata": {}, "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "id": "93a78868", "metadata": {}, "source": [ "train_manifest = pd.read_csv('manifests/train_manifest.csv')" ] }, { "cell_type": "markdown", "id": "42af883e", "metadata": {}, "source": [ "train_manifest.columns" ] }, { "cell_type": "markdown", "id": "9a0be068", "metadata": {}, "source": [ "train_manifest = train_manifest.drop(columns=['Unnamed: 0', 'new_path'])" ] }, { "cell_type": "markdown", "id": "270ac655", "metadata": {}, "source": [ "test_manifest = pd.read_csv('manifests/test_manifest.csv')\n", "test_manifest = test_manifest.drop(columns=['Unnamed: 0', 'new_path'])" ] }, { "cell_type": "markdown", "id": "c632cfb8", "metadata": {}, "source": [ "validation_manifest = pd.read_csv('manifests/validation_manifest.csv')\n", "validation_manifest = validation_manifest.drop(columns=['Unnamed: 0', 'new_path'])" ] }, { "cell_type": "markdown", "id": "30b3951c", "metadata": {}, "source": [ "train_manifest[\"file\"] = train_manifest[\"file\"].apply(lambda x: x if x.endswith(\".wav\") else x + \".wav\")\n", "validation_manifest[\"file\"] = validation_manifest[\"file\"].apply(lambda x: x if x.endswith(\".wav\") else x + \".wav\")\n", "test_manifest[\"file\"] = test_manifest[\"file\"].apply(lambda x: x if x.endswith(\".wav\") else x + \".wav\")" ] }, { "cell_type": "markdown", "id": "156c5db7", "metadata": {}, "source": [ "import tarfile\n", "import pandas as pd\n", "from pathlib import Path\n", "\n", "rows = []\n", "\n", "# loop over all tar.gz files in train/validation/test\n", "for split in [\"train\", \"validation\", \"test\"]:\n", " for tar_path in Path(f\"audio_data/{split}\").glob(\"*.tar.gz\"):\n", " with tarfile.open(tar_path, \"r:gz\") as tar:\n", " for member in tar.getmembers():\n", " if member.isfile():\n", " inner_path = member.name.lstrip(\"./\")\n", " rows.append({\n", " \"hf_path\": f\"audio_data/{split}/{tar_path.name}::{inner_path}\", # tar::inner path\n", " \"file\": Path(inner_path).name, # just the filename\n", " \"split\": split\n", " })\n", "\n", "df = pd.DataFrame(rows)\n" ] }, { "cell_type": "markdown", "id": "a40df0d5", "metadata": {}, "source": [ "df.sample(10)" ] }, { "cell_type": "markdown", "id": "d3d0caf6", "metadata": {}, "source": [ "train_manifest[train_manifest['dataset']=='call home'].head(5)" ] }, { "cell_type": "markdown", "id": "818da652", "metadata": {}, "source": [ "train_manifest_merged = train_manifest.merge(df, how='left')" ] }, { "cell_type": "markdown", "id": "e0dbaac5", "metadata": {}, "source": [ "train_manifest_merged.columns" ] }, { "cell_type": "markdown", "id": "ce48bacd", "metadata": {}, "source": [ "train_manifest_merged[\"hf_path\"].isna().any()" ] }, { "cell_type": "markdown", "id": "ce5389ab", "metadata": {}, "source": [ "validation_manifest_merged = validation_manifest.merge(df, how='left')\n", "validation_manifest_merged.columns" ] }, { "cell_type": "markdown", "id": "5ef3b0ef", "metadata": {}, "source": [ "validation_manifest_merged[\"hf_path\"].isna().any()" ] }, { "cell_type": "markdown", "id": "f48557af", "metadata": {}, "source": [ "test_manifest_merged = test_manifest.merge(df, how='left')\n", "test_manifest_merged[\"hf_path\"].isna().any()" ] }, { "cell_type": "markdown", "id": "c3b45230", "metadata": {}, "source": [ "train_manifest_merged['dataset'].value_counts()" ] }, { "cell_type": "markdown", "id": "d86188aa", "metadata": {}, "source": [ "validation_manifest_merged['dataset'].value_counts()" ] }, { "cell_type": "markdown", "id": "eb4a456e", "metadata": {}, "source": [ "test_manifest_merged['dataset'].value_counts()" ] }, { "cell_type": "markdown", "id": "4ec5a193", "metadata": {}, "source": [ "len(test_manifest_merged)" ] }, { "cell_type": "markdown", "id": "c5080606", "metadata": {}, "source": [ "test_manifest_merged = test_manifest_merged.drop(columns = ['split'])\n", "validation_manifest_merged = validation_manifest_merged.drop(columns = ['split'])\n", "train_manifest_merged = train_manifest_merged.drop(columns = ['Unnamed: 0', 'new_path', 'split'])" ] }, { "cell_type": "markdown", "id": "9f033083", "metadata": {}, "source": [ "test_manifest_merged.columns" ] }, { "cell_type": "markdown", "id": "7500e99a", "metadata": {}, "source": [ "train_manifest_merged['path'] = \"audio_data/train/\" + train_manifest_merged[\"hf_path\"].str.split(\"::\").str[1]\n", "validation_manifest_merged['path'] = \"audio_data/validation/\" + train_manifest_merged[\"hf_path\"].str.split(\"::\").str[1]\n", "test_manifest_merged['path'] = \"audio_data/test/\" + train_manifest_merged[\"hf_path\"].str.split(\"::\").str[1]" ] }, { "cell_type": "markdown", "id": "f9d08a15", "metadata": {}, "source": [ "train_manifest_merged.head(5)" ] }, { "cell_type": "markdown", "id": "9ddabb8e", "metadata": {}, "source": [ "train_manifest_merged.to_csv('manifests/train.csv', index = False)\n", "validation_manifest_merged.to_csv('manifests/validation.csv', index = False)\n", "test_manifest_merged.to_csv('manifests/test.csv', index = False)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }