ba4be087d5
Create PR to main with cherry-pick from release / cherry-pick (push) Failing after 0s
CICD NeMo / pre-flight (push) Failing after 0s
CICD NeMo / configure (push) Has been skipped
Build, validate, and release Neural Modules / pre-flight (push) Failing after 1s
CICD NeMo / code-linting (push) Has been skipped
Build, validate, and release Neural Modules / release (push) Has been skipped
Build, validate, and release Neural Modules / release-summary (push) Has been cancelled
CICD NeMo / cicd-test-container-build (push) Has been cancelled
CICD NeMo / cicd-import-tests (push) Has been cancelled
CICD NeMo / L0_Setup_Test_Data_And_Models (push) Has been cancelled
CICD NeMo / cicd-main-unit-tests (push) Has been cancelled
CICD NeMo / cicd-main-speech (push) Has been cancelled
CICD NeMo / Nemo_CICD_Test (push) Has been cancelled
CICD NeMo / Coverage (e2e) (push) Has been cancelled
CICD NeMo / Coverage (unit-test) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
CICD NeMo / cicd-wait-in-queue (push) Has been cancelled
101 lines
3.5 KiB
Python
101 lines
3.5 KiB
Python
# Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from nemo.collections.common.parts.preprocessing.manifest import get_full_path
|
|
|
|
|
|
def get_batch_starts_ends(manifest_filepath, batch_size):
|
|
"""
|
|
Get the start and end ids of the lines we will use for each 'batch'.
|
|
"""
|
|
|
|
with open(manifest_filepath, 'r') as f:
|
|
num_lines_in_manifest = sum(1 for _ in f)
|
|
|
|
starts = [x for x in range(0, num_lines_in_manifest, batch_size)]
|
|
ends = [x - 1 for x in starts]
|
|
ends.pop(0)
|
|
ends.append(num_lines_in_manifest)
|
|
|
|
return starts, ends
|
|
|
|
|
|
def is_entry_in_any_lines(manifest_filepath, entry):
|
|
"""
|
|
Returns True if entry is a key in any of the JSON lines in manifest_filepath
|
|
"""
|
|
|
|
entry_in_manifest = False
|
|
|
|
with open(manifest_filepath, 'r') as f:
|
|
for line in f:
|
|
data = json.loads(line)
|
|
|
|
if entry in data:
|
|
entry_in_manifest = True
|
|
|
|
return entry_in_manifest
|
|
|
|
|
|
def is_entry_in_all_lines(manifest_filepath, entry):
|
|
"""
|
|
Returns True is entry is a key in all of the JSON lines in manifest_filepath.
|
|
"""
|
|
with open(manifest_filepath, 'r') as f:
|
|
for line in f:
|
|
data = json.loads(line)
|
|
|
|
if entry not in data:
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
def get_manifest_lines_batch(manifest_filepath, start, end):
|
|
manifest_lines_batch = []
|
|
with open(manifest_filepath, "r", encoding="utf-8-sig") as f:
|
|
for line_i, line in enumerate(f):
|
|
if line_i >= start and line_i <= end:
|
|
data = json.loads(line)
|
|
data["audio_filepath"] = get_full_path(data["audio_filepath"], manifest_filepath)
|
|
if "text" in data:
|
|
# remove any BOM, any duplicated spaces, convert any
|
|
# newline chars to spaces
|
|
data["text"] = data["text"].replace("\ufeff", "")
|
|
data["text"] = " ".join(data["text"].split())
|
|
|
|
# Replace any horizontal ellipses with 3 separate periods.
|
|
# The tokenizer will do this anyway. But making this replacement
|
|
# now helps avoid errors when restoring punctuation when saving
|
|
# the output files
|
|
data["text"] = data["text"].replace("\u2026", "...")
|
|
|
|
if not Path(data['audio_filepath']).exists():
|
|
extended_path = Path(Path(manifest_filepath).parent, data['audio_filepath'])
|
|
if extended_path.exists():
|
|
data['audio_filepath'] = str(extended_path)
|
|
else:
|
|
raise FileNotFoundError(
|
|
f"Audio file {data['audio_filepath']} not found in {manifest_filepath}"
|
|
)
|
|
|
|
manifest_lines_batch.append(data)
|
|
|
|
if line_i == end:
|
|
break
|
|
return manifest_lines_batch
|