Spaces:
Running on Zero
Running on Zero
Download test_contracts.py from SammySnake/CLIP: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/spaces/SammySnake/CLIP/resolve/main/test_contracts.py
- Command line
-
hf download hf://spaces/SammySnake/CLIP/test_contracts.py
-
curl -L -o test_contracts.py https://huggingface.co/spaces/SammySnake/CLIP/resolve/main/test_contracts.py
1.83 kB
| """Offline contracts only: no weights, model cache, or inference claims.""" | |
| import json | |
| import unittest | |
| from contracts import caption_rows, coordinate_rows, top_k_value | |
| class InputContracts(unittest.TestCase): | |
| def test_captions(self): | |
| self.assertEqual(caption_rows('["China",{"id":"jp","text":"Japan"}]'), | |
| [{"id": "0", "text": "China"}, {"id": "jp", "text": "Japan"}]) | |
| def test_caption_bounds(self): | |
| for raw in ('[]', '{}', 'invalid', '[""]', json.dumps(['x'] * 101), | |
| '[{"id":"same","text":"a"},{"id":"same","text":"b"}]', | |
| json.dumps(['x' * 501]), '[{"text":"a","extra":1}]'): | |
| with self.subTest(raw=raw[:60]), self.assertRaises(ValueError): | |
| caption_rows(raw) | |
| def test_coordinates(self): | |
| self.assertEqual(coordinate_rows('[{"id":"zero","lat":0,"lon":0}]'), | |
| [{"id": "zero", "lat": 0.0, "lon": 0.0}]) | |
| self.assertEqual(coordinate_rows('[{"lat":90,"lon":-180}]')[0]['lat'], 90.0) | |
| def test_invalid_coordinates(self): | |
| for raw in ('[{"lat":91,"lon":1}]', '[{"lat":1,"lon":181}]', | |
| '[{"lat":NaN,"lon":1}]', '[{"lat":Infinity,"lon":1}]', | |
| '[{"lat":true,"lon":1}]', '[{"lat":"1","lon":1}]', | |
| '[{"lat":1}]', '[{"lat":1,"lon":1,"unknown":"x"}]'): | |
| with self.subTest(raw=raw), self.assertRaises(ValueError): | |
| coordinate_rows(raw) | |
| def test_topk(self): | |
| self.assertEqual(top_k_value(20.0), 20) | |
| self.assertEqual(top_k_value(1), 1) | |
| for value in (0, 21, -1, 1.1, True, "5", float('inf'), float('nan')): | |
| with self.subTest(value=value), self.assertRaises(ValueError): | |
| top_k_value(value) | |
| if __name__ == '__main__': | |
| unittest.main() | |