uts2025_vietipa / test_hf_dataset.py
Vu Anh
Fix metadata format for Hugging Face Dataset Viewer compatibility
64163b2
#!/usr/bin/env python3
from datasets import Dataset
import json
def test_local_dataset():
"""Test loading the dataset locally with Hugging Face datasets"""
# Read the JSONL metadata file
records = []
try:
with open("data/metadata.jsonl", "r", encoding="utf-8") as f:
for line in f:
records.append(json.loads(line))
except FileNotFoundError:
print("Error: data/metadata.jsonl not found")
return
print(f"Loaded {len(records)} records from metadata.jsonl")
# Create a dataset from the records
dataset = Dataset.from_list(records)
# Cast the audio column to Audio type (without automatic decoding)
from datasets import Audio
dataset = dataset.cast_column("audio", Audio(decode=False))
print("\nDataset info:")
print(f"- Number of samples: {len(dataset)}")
print(f"- Features: {dataset.features}")
# Test accessing first few examples
print("\nFirst 3 examples:")
for i, example in enumerate(dataset.select(range(3))):
print(f"\n{i+1}. Word: {example['word']}")
print(f" IPA: {example['ipa']}")
audio = example['audio']
if audio:
print(f" Audio path: {audio.get('path', 'N/A')}")
if audio.get('bytes'):
print(f" Audio bytes: {len(audio['bytes'])} bytes")
else:
print(" Audio loaded successfully")
return dataset
if __name__ == "__main__":
print("Testing Hugging Face dataset compatibility...")
print("=" * 50)
dataset = test_local_dataset()
if dataset:
print(f"\n✓ Dataset successfully loaded with {len(dataset)} examples")
print("✓ Audio files are properly loaded as Audio features")
else:
print("✗ Failed to load dataset")