uts2025_vietipa / test_hf_metadata.py
Vu Anh
Fix metadata format for Hugging Face Dataset Viewer compatibility
64163b2
#!/usr/bin/env python3
from datasets import Dataset, Audio
import json
def test_hf_metadata_format():
"""Test the HF-compatible metadata format with file_name column"""
# Read the 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 dataset from records
dataset = Dataset.from_list(records)
# Cast the file_name column to Audio type
dataset = dataset.cast_column("file_name", Audio(decode=False))
print(f"\nDataset info:")
print(f"- Number of samples: {len(dataset)}")
print(f"- Features: {dataset.features}")
# Test accessing first few examples
print(f"\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['file_name']
if audio:
print(f" Audio path: {audio.get('path', 'N/A')}")
print(f" Audio loaded successfully")
return dataset
if __name__ == "__main__":
print("Testing HF metadata format with file_name column...")
print("=" * 60)
dataset = test_hf_metadata_format()
if dataset:
print(f"\n✓ Dataset successfully loaded with {len(dataset)} examples")
print("✓ Metadata columns (word, ipa) are preserved")
print("✓ Audio files are linked via file_name column")
print("✓ Compatible with HF Dataset Viewer format")
else:
print("✗ Failed to load dataset")