in experiments/babel/app/pages/welcome.py [0:0]
def filter_babel_metadata(filepath: str) -> list[BabelMetadata]:
"""
Reads a JSON file, filters the 'audio_metadata' list to keep only entries
with voice_name containing "Puck" or "Leda", and returns the filtered data as a List[BabelMetadata].
Args:
filepath: The path to the JSON file.
Returns:
A List[BabelMetadata] containing the filtered data.
"""
try:
with open(filepath, "r") as f:
data = json.load(f)
except FileNotFoundError:
print(f"Error: File '{filepath}' not found.")
return []
except json.JSONDecodeError:
print(f"Error: Invalid JSON format in '{filepath}'.")
return []
if "audio_metadata" not in data or not isinstance(data["audio_metadata"], list):
print(f"Warning: 'audio_metadata' key not found or not a list in '{filepath}'.")
return []
filtered_metadata: list[BabelMetadata] = [
{
"voice_name": item["voice_name"],
"language_code": item["language_code"],
"gender": item["gender"],
"text": item["text"],
"audio_path": item["audio_path"],
}
for item in data["audio_metadata"]
if "voice_name" in item
and ("Puck" in item["voice_name"] or "Leda" in item["voice_name"])
]
return filtered_metadata