Spaces:
Running on Zero
Running on Zero
Download tests/test_verify.py from Varun2007/document-qa-system: direct link, hf CLI and curl.
- Browser
- Download file 2.26 kB
-
https://huggingface.co/spaces/Varun2007/document-qa-system/resolve/main/tests/test_verify.py
- Command line
-
hf download hf://spaces/Varun2007/document-qa-system/tests/test_verify.py
-
curl -L -o test_verify.py https://huggingface.co/spaces/Varun2007/document-qa-system/resolve/main/tests/test_verify.py
2.26 kB
| """Unit tests for Second-Pass Answer Verification.""" | |
| import unittest | |
| from ingest import DocumentChunk | |
| from verify import AnswerVerifier | |
| class TestVerify(unittest.TestCase): | |
| def setUp(self): | |
| self.verifier = AnswerVerifier() | |
| self.chunk = DocumentChunk( | |
| chunk_id="c_sec", | |
| doc_id="d1", | |
| file_name="sec.txt", | |
| text="Master encryption keys must undergo automated cryptographic rotation every 90 days using AWS KMS.", | |
| page_number=2, | |
| section_title="Encryption Standards", | |
| chunk_index=0, | |
| token_count=16 | |
| ) | |
| def test_claim_extraction(self): | |
| text = ( | |
| "Master encryption keys rotate every 90 days. " | |
| "AWS KMS is used for key management [Page 2, Section \"Encryption Standards\"]." | |
| ) | |
| claims = self.verifier.extract_claims(text) | |
| self.assertEqual(len(claims), 2) | |
| # Verify citations were stripped from claims | |
| for c in claims: | |
| self.assertNotIn("[Page 2", c) | |
| def test_supported_claim_verification(self): | |
| answer = "Master encryption keys rotate every 90 days using AWS KMS." | |
| result = self.verifier.verify_answer(answer, [self.chunk]) | |
| self.assertEqual(result.faithfulness_score, 1.0) | |
| self.assertFalse(result.has_hallucination) | |
| self.assertEqual(len(result.supported_claims), 1) | |
| def test_unsupported_claim_flagging(self): | |
| answer = ( | |
| "Master encryption keys rotate every 90 days. " | |
| "The company also uses Azure Key Vault in London." | |
| ) | |
| result = self.verifier.verify_answer(answer, [self.chunk]) | |
| self.assertLess(result.faithfulness_score, 1.0) | |
| self.assertTrue(result.has_hallucination) | |
| self.assertIn("Azure Key Vault", result.verified_answer) | |
| # Check warning flag appended | |
| self.assertIn("⚠️", result.verified_answer) | |
| def test_not_found_handling(self): | |
| answer = "Not found in the document." | |
| result = self.verifier.verify_answer(answer, [self.chunk]) | |
| self.assertEqual(result.faithfulness_score, 1.0) | |
| self.assertFalse(result.has_hallucination) | |
| if __name__ == "__main__": | |
| unittest.main() | |