ha-tts-mixed / scripts /create_parquet.py
Aybee5's picture
Add helper scripts (create_parquet.py, upload_to_hf.py)
8ffab92 verified
#!/usr/bin/env python3
"""
Create a parquet dataset from mimicstudio.db and audio_files folder.
Output:
- data/parquetfile/dataset.parquet
- data/audio_files/<speaker_id>/<audio_id>.wav (copied)
The parquet will contain columns: source, text, audio (relative path)
Run from repo root: python3 scripts/create_parquet.py
"""
import sqlite3
import os
import shutil
import argparse
from pathlib import Path
import pandas as pd
def prepare(db_path: str, audio_root: str, out_audio_root: str, out_parquet: str, dry_run: bool = False):
conn = sqlite3.connect(db_path)
cur = conn.cursor()
# Query audiomodel table for audio_id, prompt (text), speaker_id
cur.execute("SELECT audio_id, prompt, speaker_id FROM audiomodel")
rows = cur.fetchall()
records = []
total = len(rows)
missing = 0
for idx, (audio_id, prompt, speaker_id) in enumerate(rows, start=1):
# Construct source path: audio_files/<speaker_id>/<audio_id>.wav
src_path = Path(audio_root) / speaker_id / f"{audio_id}.wav"
dest_dir = Path(out_audio_root) / speaker_id
dest_path = dest_dir / f"{audio_id}.wav"
# Use relative path that will be accessible from Colab when copying the data folder
rel_audio_path = os.path.join("data", "audio_files", speaker_id, f"{audio_id}.wav")
if not src_path.exists():
print(f"Warning: audio file not found for row {idx}/{total}: {src_path}")
missing += 1
continue
if not dry_run:
dest_dir.mkdir(parents=True, exist_ok=True)
# copy if not exists
if not dest_path.exists():
shutil.copy2(src_path, dest_path)
# Build record for parquet
records.append({
"source": speaker_id if speaker_id is not None else "0",
"text": prompt if prompt is not None else "",
"audio": rel_audio_path,
})
if idx % 500 == 0:
print(f"Processed {idx}/{total} rows...")
conn.close()
df = pd.DataFrame.from_records(records)
if not dry_run:
out_parquet_path = Path(out_parquet)
out_parquet_path.parent.mkdir(parents=True, exist_ok=True)
# Write parquet with pyarrow engine
df.to_parquet(out_parquet, index=False)
print(f"Wrote parquet to: {out_parquet} (rows: {len(df)})")
print(f"Done. Total rows: {total}, written: {len(records)}, missing audio: {missing}")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--db", default="db/mimicstudio.db", help="Path to mimicstudio.db")
parser.add_argument("--audio-root", default="audio_files", help="Root folder containing original audio files")
parser.add_argument("--out-audio-root", default="data/audio_files", help="Destination audio folder to copy into")
parser.add_argument("--out-parquet", default="data/dataset.parquet", help="Output parquet path (default: data/dataset.parquet)")
parser.add_argument("--dry-run", action="store_true", help="Don't copy or write files; just show counts")
args = parser.parse_args()
prepare(args.db, args.audio_root, args.out_audio_root, args.out_parquet, dry_run=args.dry_run)
if __name__ == "__main__":
main()