Download tests/test_classifier.py from DSA1/factual-guard-l48-classifier: direct link, hf CLI and curl.
- Browser
- Download file 2.36 kB
-
https://huggingface.co/DSA1/factual-guard-l48-classifier/resolve/main/tests/test_classifier.py
- Command line
-
hf download hf://DSA1/factual-guard-l48-classifier/tests/test_classifier.py
-
curl -L -o test_classifier.py https://huggingface.co/DSA1/factual-guard-l48-classifier/resolve/main/tests/test_classifier.py
2.36 kB
| from pathlib import Path | |
| import unittest | |
| from unittest import mock | |
| import numpy as np | |
| import torch | |
| from factual_l48 import FactualL48Classifier | |
| WEIGHTS = Path(__file__).resolve().parents[1] / 'weights' | |
| class FactualL48Tests(unittest.TestCase): | |
| def setUpClass(cls): | |
| torch.set_num_threads(2) | |
| cls.classifier = FactualL48Classifier(WEIGHTS) | |
| cls.one = np.arange(5120, dtype=np.float32).reshape(1, -1) / 5000 - 0.5 | |
| cls.two = np.concatenate([cls.one, cls.one * 0.5], axis=0) | |
| def test_frozen_heads_score_variable_length_features(self): | |
| results = self.classifier.predict([self.one, self.two]) | |
| self.assertEqual(len(results), 2) | |
| self.assertEqual(list(results[0]['per_head']), ['20260719', '20260720', '20260721']) | |
| # Recorded from the original three-head CPU path on these synthetic inputs. | |
| np.testing.assert_allclose([item['score'] for item in results], | |
| [0.306591272354126, 0.3066070079803467], | |
| rtol=2e-5, atol=2e-6) | |
| self.assertTrue(all(item['is_polluted'] is False for item in results)) | |
| for item in results: | |
| actual = np.mean(list(item['per_head'].values()), dtype=np.float32) | |
| self.assertEqual(item['score'], float(actual)) | |
| self.assertEqual(item['is_polluted'], item['score'] >= self.classifier.threshold) | |
| def test_invalid_features_and_empty_batch(self): | |
| self.assertEqual(self.classifier.predict([]), []) | |
| for bad in [np.zeros((0, 5120)), np.zeros((257, 5120)), np.zeros((1, 2)), | |
| np.full((1, 5120), np.nan), np.full((1, 5120), 1e100)]: | |
| with self.subTest(shape=bad.shape): | |
| with self.assertRaises(ValueError): | |
| self.classifier.predict([bad]) | |
| with self.assertRaises(TypeError): | |
| self.classifier.predict(np.zeros((1, 5120))) | |
| def test_rejects_checkpoint_identity_mismatch_before_load(self): | |
| with mock.patch('factual_l48._sha256', return_value='invalid'): | |
| with mock.patch('factual_l48.torch.load') as load: | |
| with self.assertRaisesRegex(ValueError, 'SHA-256 mismatch'): | |
| FactualL48Classifier(WEIGHTS) | |
| load.assert_not_called() | |
| if __name__ == '__main__': | |
| unittest.main() | |