Download test_release_tools.py from Jlz2/pipe_m: direct link, hf CLI and curl.
- Browser
- Download file 4.7 kB
-
https://huggingface.co/datasets/Jlz2/pipe_m/resolve/main/test_release_tools.py
- Command line
-
hf download hf://datasets/Jlz2/pipe_m/test_release_tools.py
-
curl -L -o test_release_tools.py https://huggingface.co/datasets/Jlz2/pipe_m/resolve/main/test_release_tools.py
4.7 kB
| import hashlib,io,json,subprocess,sys,tarfile,tempfile,unittest | |
| from pathlib import Path | |
| from extract_data import extract | |
| from load_train import load_train | |
| def digest(data):return hashlib.sha256(data).hexdigest() | |
| class ReleaseTools(unittest.TestCase): | |
| def fixture(self,root,bad=None): | |
| folder=root/'P0/known_stem_mix/sound_event_extraction/data';folder.mkdir(parents=True) | |
| members=[('audio/example/input.wav',b'input'),('audio/example/output.wav',b'output')] | |
| tar=folder/'audio-00000.tar' | |
| with tarfile.open(tar,'w') as t: | |
| for name,data in members: | |
| m=tarfile.TarInfo(name);m.size=len(data);t.addfile(m,io.BytesIO(data)) | |
| if bad: | |
| m=tarfile.TarInfo(bad[0]);m.type=bad[1];m.linkname='../outside';t.addfile(m,io.BytesIO(b'')) | |
| input_path=str((folder/members[0][0]).relative_to(root));output_path=str((folder/members[1][0]).relative_to(root)) | |
| train=root/'P0/known_stem_mix/_shared/data/train.jsonl';train.parent.mkdir(parents=True) | |
| row={'id':'example','input_audio':input_path,'target_audio':output_path,'source_sha256':digest(b'input'),'target_sha256':digest(b'output'), | |
| 'messages':[{'role':'user','content':[{'type':'text','text':'Extract a source.'},{'type':'audio','audio_url':input_path}]},{'role':'assistant','content':[{'type':'audio','audio_url':output_path}]}]} | |
| train.write_text(json.dumps(row)+'\n') | |
| manifest=train.parent/'data_manifest.json';manifest.write_text(json.dumps({'files':[{'path':str(tar.relative_to(root)),'sha256':digest(tar.read_bytes())},{'path':str(train.relative_to(root)),'sha256':digest(train.read_bytes())}]})) | |
| return train,tar | |
| def test_extract_hash_and_resolve_paths(self): | |
| with tempfile.TemporaryDirectory() as d: | |
| root=Path(d);self.fixture(root);v=extract(root);self.assertEqual(v['audio_hashes_verified'],2) | |
| row=next(load_train(root));self.assertTrue(Path(row['input_audio']).is_absolute());self.assertEqual(Path(row['target_audio']).read_bytes(),b'output') | |
| extract(root) # Identical extracted files are safe to reuse. | |
| def test_unsafe_tar_members_rejected(self): | |
| for name,kind in [('../escape.wav',tarfile.REGTYPE),('/escape.wav',tarfile.REGTYPE),('C:/escape.wav',tarfile.REGTYPE),('audio/link',tarfile.SYMTYPE),('audio/link',tarfile.LNKTYPE)]: | |
| with self.subTest(name=name,kind=kind),tempfile.TemporaryDirectory() as d: | |
| root=Path(d);self.fixture(root,(name,kind)) | |
| with self.assertRaises(ValueError):extract(root) | |
| self.assertFalse((root/'P0/known_stem_mix/sound_event_extraction/data/audio/example/input.wav').exists()) | |
| def test_corrupted_archive_rejected_before_extract(self): | |
| with tempfile.TemporaryDirectory() as d: | |
| root=Path(d);_,tar=self.fixture(root);tar.write_bytes(tar.read_bytes()+b'changed') | |
| with self.assertRaises(ValueError):extract(root) | |
| def test_changed_existing_audio_is_preserved(self): | |
| with tempfile.TemporaryDirectory() as d: | |
| root=Path(d);self.fixture(root);p=root/'P0/known_stem_mix/sound_event_extraction/data/audio/example/input.wav';p.parent.mkdir(parents=True);p.write_bytes(b'local edit') | |
| with self.assertRaises(ValueError):extract(root) | |
| self.assertEqual(p.read_bytes(),b'local edit') | |
| def test_preexisting_partial_is_preserved(self): | |
| with tempfile.TemporaryDirectory() as d: | |
| root=Path(d);self.fixture(root);p=root/'P0/known_stem_mix/sound_event_extraction/data/audio/example/input.wav.partial';p.parent.mkdir(parents=True);outside=root/'keep.txt';outside.write_bytes(b'keep');p.symlink_to(outside) | |
| with self.assertRaises(FileExistsError):extract(root) | |
| self.assertTrue(p.is_symlink());self.assertEqual(outside.read_bytes(),b'keep') | |
| def test_loader_refuses_source_or_existing_output(self): | |
| with tempfile.TemporaryDirectory() as d: | |
| root=Path(d);train,_=self.fixture(root);before=train.read_bytes();script=Path(__file__).with_name('load_train.py') | |
| result=subprocess.run([sys.executable,str(script),'--repo-root',str(root),'--output',str(train)],capture_output=True) | |
| self.assertNotEqual(result.returncode,0);self.assertEqual(train.read_bytes(),before) | |
| output=root/'runtime.jsonl';output.write_bytes(b'keep') | |
| result=subprocess.run([sys.executable,str(script),'--repo-root',str(root),'--output',str(output)],capture_output=True) | |
| self.assertNotEqual(result.returncode,0);self.assertEqual(output.read_bytes(),b'keep') | |
| if __name__=='__main__':unittest.main() | |