Spaces:
Sleeping
Sleeping
Upload generate.py with huggingface_hub
Browse files- generate.py +100 -0
generate.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Command-line dataset generator.
|
| 2 |
+
|
| 3 |
+
Examples
|
| 4 |
+
--------
|
| 5 |
+
Free Piper TTS (no key)::
|
| 6 |
+
|
| 7 |
+
python generate.py --out output --push-hf-repo user/hey-android
|
| 8 |
+
|
| 9 |
+
Google Cloud TTS + Edge Impulse upload::
|
| 10 |
+
|
| 11 |
+
python generate.py --gcp-api-key "$GCP_TTS_API_KEY" \\
|
| 12 |
+
--edge-impulse-api-key "$EDGE_IMPULSE_API_KEY"
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import argparse
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
from src import edge_impulse
|
| 21 |
+
from src.backends import select_backend
|
| 22 |
+
from src.builder import build_dataset
|
| 23 |
+
from src.config import DEFAULT_UNKNOWN_PHRASES, DEFAULT_WAKE_PHRASES, DatasetConfig
|
| 24 |
+
from src.hf_export import export_hf_dataset, push_to_hub
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _read_lines(path: str | None, fallback: list[str]) -> list[str]:
|
| 28 |
+
if not path:
|
| 29 |
+
return list(fallback)
|
| 30 |
+
lines = [ln.strip() for ln in open(path, encoding="utf-8") if ln.strip()]
|
| 31 |
+
return lines or list(fallback)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def main() -> None:
|
| 35 |
+
p = argparse.ArgumentParser(description="Generate a wake-word dataset (GCP TTS or free Piper).")
|
| 36 |
+
p.add_argument("--out", default="output")
|
| 37 |
+
p.add_argument("--hf-out", default="hf_dataset")
|
| 38 |
+
p.add_argument("--dataset-name", default="hey_android")
|
| 39 |
+
p.add_argument("--wake-label", default="hey_android")
|
| 40 |
+
p.add_argument("--wake-phrases-file", default=None)
|
| 41 |
+
p.add_argument("--unknown-phrases-file", default=None)
|
| 42 |
+
|
| 43 |
+
p.add_argument("--gcp-api-key", default=os.environ.get("GCP_TTS_API_KEY", ""))
|
| 44 |
+
p.add_argument("--base-repeats", type=int, default=1)
|
| 45 |
+
p.add_argument("--augmentations", type=int, default=8)
|
| 46 |
+
p.add_argument("--background-noise", type=int, default=200)
|
| 47 |
+
p.add_argument("--max-voices", type=int, default=7)
|
| 48 |
+
p.add_argument("--test-ratio", type=float, default=0.2)
|
| 49 |
+
|
| 50 |
+
p.add_argument("--push-hf-repo", default=None, help="e.g. username/dataset-name")
|
| 51 |
+
p.add_argument("--hf-token", default=os.environ.get("HF_TOKEN", ""))
|
| 52 |
+
p.add_argument("--hf-private", action="store_true")
|
| 53 |
+
|
| 54 |
+
p.add_argument("--edge-impulse-api-key", default=os.environ.get("EDGE_IMPULSE_API_KEY", ""))
|
| 55 |
+
p.add_argument("--ei-allow-duplicates", action="store_true")
|
| 56 |
+
|
| 57 |
+
args = p.parse_args()
|
| 58 |
+
|
| 59 |
+
backend = select_backend(
|
| 60 |
+
gcp_api_key=args.gcp_api_key,
|
| 61 |
+
language_prefixes=["en", "nl", "de", "fr", "es"],
|
| 62 |
+
max_gcp_voices_per_locale=3,
|
| 63 |
+
max_piper_voices=args.max_voices,
|
| 64 |
+
sample_rate_hz=16000,
|
| 65 |
+
)
|
| 66 |
+
print(f"Backend: {backend.source}")
|
| 67 |
+
|
| 68 |
+
config = DatasetConfig(
|
| 69 |
+
out_dir=args.out,
|
| 70 |
+
dataset_name=args.dataset_name,
|
| 71 |
+
wake_label=args.wake_label,
|
| 72 |
+
wake_phrases=_read_lines(args.wake_phrases_file, DEFAULT_WAKE_PHRASES),
|
| 73 |
+
unknown_phrases=_read_lines(args.unknown_phrases_file, DEFAULT_UNKNOWN_PHRASES),
|
| 74 |
+
base_repeats_per_phrase_per_voice=args.base_repeats,
|
| 75 |
+
augmentations_per_speech_clip=args.augmentations,
|
| 76 |
+
background_noise_samples=args.background_noise,
|
| 77 |
+
max_piper_voices=args.max_voices,
|
| 78 |
+
test_ratio=args.test_ratio,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
result = build_dataset(config, backend)
|
| 82 |
+
repo_id = args.push_hf_repo or "your-username/your-dataset"
|
| 83 |
+
hf_dir = export_hf_dataset(config, result, args.hf_out, repo_id=repo_id)
|
| 84 |
+
print(f"Hugging Face dataset folder: {hf_dir}")
|
| 85 |
+
|
| 86 |
+
if args.push_hf_repo and args.hf_token:
|
| 87 |
+
url = push_to_hub(hf_dir, args.push_hf_repo, args.hf_token, private=args.hf_private)
|
| 88 |
+
print(f"Pushed dataset: {url}")
|
| 89 |
+
|
| 90 |
+
if args.edge_impulse_api_key:
|
| 91 |
+
ei_result = edge_impulse.upload_dataset(
|
| 92 |
+
dataset_dir=args.out,
|
| 93 |
+
api_key=args.edge_impulse_api_key,
|
| 94 |
+
allow_duplicates=args.ei_allow_duplicates,
|
| 95 |
+
)
|
| 96 |
+
print(f"Edge Impulse: {ei_result.uploaded} uploaded, {ei_result.failed} failed.")
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|