Download skill/scripts/verify_subtitles.py from cpuai/ahp-development-workstation: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/spaces/cpuai/ahp-development-workstation/resolve/main/skill/scripts/verify_subtitles.py
- Command line
-
hf download hf://spaces/cpuai/ahp-development-workstation/skill/scripts/verify_subtitles.py
-
curl -L -o verify_subtitles.py https://huggingface.co/spaces/cpuai/ahp-development-workstation/resolve/main/skill/scripts/verify_subtitles.py
2.19 kB
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """Check subtitle JSON time order and validate safe gap across scenes/roles.""" | |
| import argparse,json,sys,re | |
| from pathlib import Path | |
| def audit(path:Path,min_gap=0.045): | |
| raw=json.loads(path.read_text(encoding='utf-8')) | |
| captions=raw['captions'] if isinstance(raw,dict) else raw | |
| bad=[] | |
| for i,c in enumerate(captions): | |
| if float(c['end'])<=float(c['start']):bad.append(f'{i}: nonpositive subtitle duration') | |
| if i and float(c['start'])-float(captions[i-1]['end'])<min_gap-.002: | |
| bad.append(f'{i}: caption overlap or gap < {min_gap:.3f}s') | |
| # When inside a full science project, verify words as well as timestamp spacing. | |
| root=path.resolve().parent | |
| story_file=root/'story.json' | |
| timing_file=root/'timing.json' | |
| if story_file.is_file() and timing_file.is_file(): | |
| try: | |
| from narrate import validate_story, clean | |
| story=json.loads(story_file.read_text(encoding='utf-8-sig')) | |
| expected=clean(''.join(line['speech'] for group in validate_story(story) for line in group)) | |
| actual=clean(''.join(c.get('text','') for c in captions)) | |
| if actual!=expected:bad.append('Full TTS script and subtitle text differ (missing/paraphrased words)') | |
| if any(c.get('precise_tts_boundaries') is not True for c in captions): | |
| bad.append('One or more captions lacks verified Edge TTS WordBoundary provenance') | |
| timing=json.loads(timing_file.read_text(encoding='utf-8-sig')) | |
| if not timing.get('verbatim_captions'):bad.append('timing.json does not attest strict subtitle alignment') | |
| except (ValueError,KeyError,TypeError) as exc: | |
| bad.append(f'Strict caption verification failed: {exc}') | |
| return {'subtitle_count':len(captions),'gap_required_ms':round(min_gap*1000),'qa_pass':not bad,'errors':bad} | |
| if __name__=='__main__': | |
| p=argparse.ArgumentParser();p.add_argument('captions');p.add_argument('--gap',type=float,default=.045);a=p.parse_args() | |
| report=audit(Path(a.captions),a.gap);print(json.dumps(report,ensure_ascii=False,indent=2));sys.exit(0 if report['qa_pass'] else 2) | |