#!/usr/bin/env python3 import json import os from pathlib import Path from datasets import Dataset, Audio def test_final_structure(): """Test the final dataset structure for HF compatibility""" print("Testing final dataset structure...") print("=" * 50) # Check for required files files_to_check = [ "metadata.jsonl", "audio/xin_chao.mp3", "audio/cam_on.mp3", "audio/viet_nam.mp3" ] print("1. Checking file structure:") for file_path in files_to_check: if os.path.exists(file_path): print(f" ✓ {file_path}") else: print(f" ✗ {file_path} - MISSING") return False # Load and validate metadata print("\n2. Loading metadata.jsonl:") try: records = [] with open("metadata.jsonl", "r", encoding="utf-8") as f: for line_num, line in enumerate(f, 1): try: record = json.loads(line) records.append(record) except json.JSONDecodeError as e: print(f" ✗ Line {line_num}: Invalid JSON - {e}") return False print(f" ✓ Loaded {len(records)} records") # Validate required fields required_fields = ["file_name", "word", "ipa"] for i, record in enumerate(records[:3], 1): missing_fields = [field for field in required_fields if field not in record] if missing_fields: print(f" ✗ Record {i} missing fields: {missing_fields}") return False print(f" ✓ All records have required fields: {required_fields}") except Exception as e: print(f" ✗ Error loading metadata: {e}") return False # Test with datasets library print("\n3. Testing with datasets library:") try: dataset = Dataset.from_list(records) dataset = dataset.cast_column("file_name", Audio(decode=False)) print(f" ✓ Dataset created successfully") print(f" ✓ Features: {dataset.features}") print(f" ✓ Number of examples: {len(dataset)}") # Test first example first_example = dataset[0] print(f" ✓ First example loaded: {first_example['word']} ({first_example['ipa']})") except Exception as e: print(f" ✗ Error with datasets library: {e}") return False print("\n4. Summary:") print(" ✓ Files in root directory") print(" ✓ metadata.jsonl with file_name column") print(" ✓ Audio files in audio/ subdirectory") print(" ✓ Compatible with datasets library") print(" ✓ Ready for Hugging Face Dataset Viewer") return True if __name__ == "__main__": success = test_final_structure() if success: print("\n🎉 Dataset structure is correct and ready for upload!") else: print("\n❌ Dataset structure needs fixes")