Download stage1_cache.py from ATH-MaaS/OvisOCR3: direct link, hf CLI and curl.
- Browser
- Download file 2.74 kB
-
https://huggingface.co/spaces/ATH-MaaS/OvisOCR3/resolve/main/stage1_cache.py
- Command line
-
hf download hf://spaces/ATH-MaaS/OvisOCR3/stage1_cache.py
-
curl -L -o stage1_cache.py https://huggingface.co/spaces/ATH-MaaS/OvisOCR3/resolve/main/stage1_cache.py
2.74 kB
| """Frozen page parses for exact bundled examples, not a live-inference cache.""" | |
| import hashlib | |
| import json | |
| from catalog import ROOT, build_prompt, load_cases | |
| def image_signature(image): | |
| return {'mode': image.mode, 'size': list(image.size), | |
| 'sha256': hashlib.sha256(image.tobytes()).hexdigest()} | |
| def cached_pages(images, parser, model_source): | |
| metadata = {'state': 'bypass', 'hits': 0} | |
| if model_source != 'ATH-MaaS/OvisOCR3': | |
| return None, metadata | {'reason': 'different_model'} | |
| signatures = [image_signature(image) for image in images] | |
| for case in load_cases(): | |
| if case['task'] != 'crosspage_table': | |
| continue | |
| path = ROOT / 'examples' / case['id'] / 'stage1-cache.json' | |
| try: | |
| content = path.read_bytes() | |
| bundle = json.loads(content) | |
| if bundle['images'] != signatures: | |
| continue | |
| if bundle['version'] != 2 or bundle['case_id'] != case['id'] or bundle['model'] != model_source: | |
| raise ValueError('Invalid example cache identity.') | |
| entry = bundle['parsers'][parser] | |
| prompt = build_prompt(parser) | |
| records = entry['calls'] | |
| if entry['prompt'] != prompt or len(records) != 2: | |
| raise ValueError('Cached parser contract changed.') | |
| for index, record in enumerate(records): | |
| if (record['stage'] != f'page-{index + 1}' or record['prompt'] != prompt | |
| or record['finish_reason'] != 'stop' or not record['text'].strip() | |
| or hashlib.sha256(record['text'].encode()).hexdigest() != entry['raw_sha256'][index]): | |
| raise ValueError('Invalid cached page.') | |
| source = {'artifact': str(path.relative_to(ROOT)), | |
| 'sha256': hashlib.sha256(content).hexdigest(), 'generated_at': entry['generated_at'], | |
| 'model': bundle['model'], 'weights_sha256': bundle['weights_sha256'], | |
| 'backend': entry['backend'], 'max_new_tokens': entry['max_new_tokens']} | |
| calls = [record | {'cache_hit': True, 'cache_source': source, 'phase': 'Cached', | |
| 'cached_inference_seconds': record.get('seconds', 0), 'seconds': 0.0} | |
| for record in records] | |
| return calls, metadata | {'state': 'hit', 'hits': 2, 'case_id': case['id'], **source} | |
| except FileNotFoundError: | |
| continue | |
| except (OSError, ValueError, KeyError, TypeError, IndexError, AttributeError): | |
| return None, metadata | {'state': 'miss', 'reason': 'invalid_or_unavailable_cache'} | |
| return None, metadata | {'reason': 'not_a_bundled_example'} | |