Files
safetynet/apps/inspector/test_semantic_real.py
T

47 lines
2.9 KiB
Python
Raw Normal View History

2026-10-13 10:33:31 +02:00
"""Opt-in operational fixtures using staged real Google weights, CPU only."""
import io
import math
import os
import unittest
import wave
from engine import get_model,embedding_features,inspect_content,cosine
from semantic import validate_vectors,DIMENSIONS
@unittest.skipUnless(os.environ.get('SAFETYNET_TEST_NATIVE_SEMANTIC')=='1','Opt in to verified native CPU fixtures')
class NativeCPUFixtures(unittest.TestCase):
@classmethod
def setUpClass(cls):cls.model=get_model()
@classmethod
def tearDownClass(cls):cls.model.close()
def test_real_paraphrase_blocks_and_unrelated_text_allows(self):
reference=embedding_features(b'Our confidential launch plan schedules the new product announcement for next October.')
protected=b'The secret product rollout will be announced in October next year.'
public=b'The public cafeteria menu includes tomato soup and bread.'
related=embedding_features(protected);unrelated=embedding_features(public)
self.assertGreater(cosine(reference['vector'],related['vector']),.8)
self.assertLess(cosine(reference['vector'],unrelated['vector']),.8)
policy={'dlp':{'maxBytes':1048576,'rules':[],'semantic':{**self.model.info(),'enabled':True,'categories':[{'id':'native-reference','prototype':reference['vector'],'prototypes':reference['vectors'],'threshold':.8}]}}}
self.assertEqual(inspect_content(protected,policy)['ruleId'],'native-reference')
self.assertEqual(inspect_content(public,policy)['decision'],'allow')
def test_native_token_windows_cover_long_document_tail(self):
text='Public synthetic office lunch menu and team meeting schedule. '*600+'Final unique coverage marker.'
result=embedding_features(text.encode())
self.assertGreater(result['coverage']['textChunks'],1)
self.assertTrue(result['coverage']['complete']);validate_vectors(result['vectors'])
def test_native_image_encoder_returns_real_normalized_vector(self):
from PIL import Image,ImageDraw
image=Image.new('RGB',(192,192),'white');draw=ImageDraw.Draw(image)
draw.rectangle((20,20,90,170),fill='blue');draw.ellipse((110,50,175,115),fill='red')
output=io.BytesIO();image.save(output,format='PNG')
vectors=self.model.image_vectors(output.getvalue())
self.assertEqual(len(vectors),1);validate_vectors(vectors)
def test_native_audio_encoder_returns_real_normalized_vector(self):
import numpy as np
samples=(np.sin(np.arange(16000)*2*math.pi*440/16000)*12000).astype('<i2')
output=io.BytesIO()
with wave.open(output,'wb') as stream:
stream.setnchannels(1);stream.setsampwidth(2);stream.setframerate(16000);stream.writeframes(samples.tobytes())
vectors=self.model.audio_vectors(output.getvalue())
self.assertEqual(len(vectors),1);validate_vectors(vectors)
if __name__=='__main__':unittest.main()