| import json
|
| import random
|
| import os
|
| import shutil
|
| from pathlib import Path
|
|
|
|
|
| input_manifest = "thai_processed_data/manifest_clustered.jsonl"
|
| output_dir = "check_clusters"
|
| samples_to_check = 5
|
| files_per_group = 3
|
|
|
|
|
| def main():
|
| print(f"Reading {input_manifest}...")
|
|
|
|
|
| clusters = {}
|
| with open(input_manifest, 'r', encoding='utf-8') as f:
|
| for line in f:
|
| data = json.loads(line)
|
| speaker_id = data['speaker']
|
| audio_path = data['audio_path']
|
|
|
| if speaker_id not in clusters:
|
| clusters[speaker_id] = []
|
| clusters[speaker_id].append(audio_path)
|
|
|
|
|
| valid_clusters = [k for k, v in clusters.items() if len(v) >= 2]
|
|
|
| print(f"Found {len(clusters)} total groups.")
|
| print(f"Found {len(valid_clusters)} groups with at least 2 files (valid for pairing).")
|
|
|
| if not valid_clusters:
|
| print("Error: No valid clusters found! Did you run the clustering script?")
|
| return
|
|
|
|
|
| selected_speakers = random.sample(valid_clusters, min(samples_to_check, len(valid_clusters)))
|
|
|
|
|
| if os.path.exists(output_dir):
|
| shutil.rmtree(output_dir)
|
| os.makedirs(output_dir)
|
|
|
| print(f"\n--- GENERATING SAMPLES IN '{output_dir}' ---")
|
|
|
| for i, spk_id in enumerate(selected_speakers):
|
|
|
| folder_name = f"Group_{i+1}_{spk_id}"
|
| folder_path = os.path.join(output_dir, folder_name)
|
| os.makedirs(folder_path)
|
|
|
|
|
| files = clusters[spk_id]
|
| selected_files = random.sample(files, min(files_per_group, len(files)))
|
|
|
| print(f"\nGroup {i+1} (ID: {spk_id}):")
|
|
|
| for src_path in selected_files:
|
|
|
| if not os.path.exists(src_path):
|
|
|
|
|
| print(f" [WARNING] File not found: {src_path}")
|
| continue
|
|
|
| file_name = os.path.basename(src_path)
|
| dst_path = os.path.join(folder_path, file_name)
|
| shutil.copy2(src_path, dst_path)
|
| print(f" - Copied: {file_name}")
|
|
|
| print(f"\nDONE! Go open the folder '{output_dir}' and listen to the files.")
|
| print("If files in 'Group_1' sound like the same person, the clustering worked!")
|
|
|
| if __name__ == "__main__":
|
| main() |