Download recipe/kit-v3/audit_data_coverage.py from gowombat/tiny-voca-da: direct link, hf CLI and curl.
- Browser
- Download file 6.91 kB
-
https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/kit-v3/audit_data_coverage.py
- Command line
-
hf download hf://gowombat/tiny-voca-da/recipe/kit-v3/audit_data_coverage.py
-
curl -L -o audit_data_coverage.py https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/kit-v3/audit_data_coverage.py
6.91 kB
| #!/usr/bin/env python3 | |
| """Account for every source row across historical audio, current text and DSP gates.""" | |
| import argparse | |
| from collections import Counter | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| def records(path): | |
| return [json.loads(line) for line in path.read_text().split('\n') if line.strip()] | |
| def audit(raw, features, frontend_rows, frontend_summary): | |
| preparation = json.loads((raw / 'source_preparation_v2.json').read_text()) | |
| text_summary = json.loads(frontend_summary.read_text()) | |
| text_rows = records(frontend_rows) | |
| text_by_id = {row['source_id']: row for row in text_rows} | |
| if len(text_by_id) != len(text_rows): | |
| raise ValueError('Duplicate frontend source IDs') | |
| accepted, rejected, feature_status = {}, {}, {} | |
| split_counts = {} | |
| for split in ('train', 'validation', 'test'): | |
| raw_rows = records(raw / (split + '.jsonl')) | |
| split_counts[split] = len(raw_rows) | |
| for row in raw_rows: | |
| if row['source_id'] in accepted or row['split'] != split: | |
| raise ValueError('Duplicate raw source ID or changed split') | |
| accepted[row['source_id']] = row | |
| receipt = json.loads((features / (split + '.preparation.json')).read_text()) | |
| if not receipt['complete']: | |
| raise ValueError(f'Acoustic preparation is incomplete: {split}') | |
| for status, name in [('accepted', split + '.jsonl'), ('rejected', split + '.rejected.jsonl')]: | |
| rows = records(features / name) | |
| if len(rows) != receipt[status]: | |
| raise ValueError(f'Acoustic {status} count disagrees with receipt: {split}') | |
| for row in rows: | |
| if row['id'] in feature_status: | |
| raise ValueError('Feature row counted more than once') | |
| feature_status[row['id']] = {'status': status, 'reason': row.get('reason'), 'split': split} | |
| for row in records(raw / 'source_rejected_v2.jsonl'): | |
| source_id = f'{row["speaker_id"]}:{row["transcription_id"]}' | |
| if source_id in rejected or source_id in accepted: | |
| raise ValueError('Historical source decisions overlap') | |
| rejected[source_id] = row | |
| if accepted.keys() | rejected.keys() != text_by_id.keys() or len(text_by_id) != preparation['processed']: | |
| raise ValueError('Source audio decisions and current frontend do not cover exactly the same source rows') | |
| if set(feature_status) != {row['id'] for row in accepted.values()}: | |
| raise ValueError('Acoustic source rows were dropped or invented') | |
| ledger = [] | |
| counts = Counter() | |
| historical_reasons = Counter() | |
| for source_id in sorted(text_by_id, key=lambda key: int(key.split(':', 1)[1])): | |
| text = text_by_id[source_id] | |
| entry = {'source_id': source_id, 'frontend_accepted': text['accepted'], 'frontend_token_count': text['token_count'], | |
| 'frontend_unknown': text['unknown'], 'frontend_overlong': text['overlong']} | |
| counts['source_rows'] += 1 | |
| counts['current_frontend_text_scanned'] += 1 | |
| counts['current_frontend_text_accepted' if text['accepted'] else 'current_frontend_text_rejected'] += 1 | |
| if source_id in accepted: | |
| row = accepted[source_id] | |
| if row.get('source_text', row['text']) != text['text']: | |
| raise ValueError('Frontend text and frozen source transcript differ') | |
| status = feature_status[row['id']] | |
| if status['split'] != row['split']: | |
| raise ValueError('Acoustic split changed') | |
| if not text['accepted']: | |
| counts['retained_raw_now_frontend_rejected'] += 1 | |
| counts['retained_raw_recordings'] += 1 | |
| counts['current_acoustic_processed'] += 1 | |
| counts['current_acoustic_' + status['status']] += 1 | |
| entry.update(id=row['id'], split=row['split'], historical_audio_status='accepted', | |
| current_acoustic_status=status['status'], current_acoustic_rejection=status['reason'], | |
| waveform_sha256=row['waveform_sha256']) | |
| else: | |
| row = rejected[source_id] | |
| reason = row['reason'] | |
| historical_reasons[reason.split(':', 1)[0]] += 1 | |
| counts['historical_audio_preparation_rejected'] += 1 | |
| counts['historical_rejects_not_redecoded_in_current_run'] += 1 | |
| entry.update(id=row.get('id'), split=None, historical_audio_status='rejected', | |
| historical_reason=reason, current_acoustic_status='not_run_historical_rejection') | |
| ledger.append(entry) | |
| if counts['source_rows'] != counts['current_acoustic_processed'] + counts['historical_rejects_not_redecoded_in_current_run']: | |
| raise ValueError('All-source accounting does not close') | |
| for key in ('current_acoustic_rejected', 'retained_raw_now_frontend_rejected'): | |
| counts.setdefault(key, 0) | |
| if (text_summary['counts']['rows'] != counts['source_rows'] or | |
| text_summary['counts']['accepted_text_rows'] != counts['current_frontend_text_accepted'] or | |
| text_summary['counts']['rejected_text_rows'] != counts['current_frontend_text_rejected']): | |
| raise ValueError('Frontend summary does not describe its row-level evidence') | |
| report = {'status': 'PASS', 'counts': dict(counts), 'historical_rejection_reasons': dict(historical_reasons), | |
| 'retained_raw_splits': split_counts, 'source_revision': preparation['revision'], | |
| 'frontend_digest': text_summary['profile_digest'], 'all_source_rows_accounted': True, | |
| 'source_splits_modified': False, 'historical_rejected_audio_reprocessed': False, | |
| 'frontend_rows_sha256': hashlib.sha256(frontend_rows.read_bytes()).hexdigest(), | |
| 'note': 'A text-only gate was rerun for every source row. DSP was run for the retained raw recordings; ' | |
| 'historical rejects were not presented as newly decoded audio.'} | |
| return report, ledger | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--raw', type=Path, required=True) | |
| parser.add_argument('--features', type=Path, required=True) | |
| parser.add_argument('--frontend-rows', type=Path, required=True) | |
| parser.add_argument('--frontend-summary', type=Path, required=True) | |
| parser.add_argument('--report', type=Path, required=True) | |
| args = parser.parse_args() | |
| report, ledger = audit(args.raw, args.features, args.frontend_rows, args.frontend_summary) | |
| args.report.parent.mkdir(parents=True, exist_ok=True) | |
| args.report.write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n') | |
| args.report.with_suffix('.ledger.jsonl').write_text(''.join(json.dumps(row, ensure_ascii=False) + '\n' for row in ledger)) | |
| print(json.dumps(report, ensure_ascii=False, indent=2)) | |
| if __name__ == '__main__': | |
| main() | |