xyz / lhotseWorkspace /process_single_chunk.py
benderrodriguez's picture
Upload folder using huggingface_hub (part 37)
9e060c3 verified
Raw
History Blame Contribute Delete
2.1 kB
import sys
import os
import re
import gc
import lhotse
def main():
if len(sys.argv) != 3:
print("Usage: python process_single_chunk.py <base_output_dir> <manifest_path>")
sys.exit(1)
base_output_dir = sys.argv[1]
tiny_manifest_path = sys.argv[2]
# Extract the index from the filename (e.g., "manifest_0042.jsonl.gz" -> 42)
match = re.search(r'manifest_(\d+)', os.path.basename(tiny_manifest_path))
if match:
chunk_idx = int(match.group(1))
else:
print(f"[Worker] Could not extract chunk index from {tiny_manifest_path}. Aborting.")
sys.exit(1)
# 1. Setup Backends
from lhotse.audio.backend import (
CompositeAudioBackend, LibsndfileBackend, TorchaudioFFMPEGBackend
)
from lhotse import set_caching_enabled
set_caching_enabled(False)
gc.set_threshold(100, 2, 2)
composite = CompositeAudioBackend(backends=[
LibsndfileBackend(), TorchaudioFFMPEGBackend()
])
lhotse.audio.set_current_audio_backend(composite)
# 2. Setup output directory
output_dir = os.path.join(base_output_dir, f"chunk_{chunk_idx:04d}")
os.makedirs(output_dir, exist_ok=True)
print(f"[Worker] Starting Chunk {chunk_idx} using {os.path.basename(tiny_manifest_path)}")
try:
# 3. Load the TINY manifest
chunk_cutset = lhotse.CutSet.from_jsonl(tiny_manifest_path)
# 4. Export the SHAR data
chunk_cutset.to_shar(
output_dir,
fields={"recording": "opus"},
shard_size=10000,
num_jobs=1,
fault_tolerant=True
)
print(f"[Worker] Finished Chunk {chunk_idx}.")
# 5. Delete the manifest to mark as complete
os.remove(tiny_manifest_path)
print(f"[Worker] Deleted manifest {os.path.basename(tiny_manifest_path)}")
except Exception as e:
print(f"[Worker] FAILED Chunk {chunk_idx}. Manifest kept for retry. Error: {e}")
# Exit with an error code so Bash knows this specific job failed
sys.exit(1)
if __name__ == '__main__':
main()