Datasets:
Download code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_campaign.py from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 3.94 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_campaign.py
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_campaign.py
-
curl -L -o test_campaign.py https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_campaign.py
3.94 kB
| import json | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| import campaign | |
| HERE = Path(__file__).resolve().parent | |
| def write(path, rows): | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(''.join(json.dumps(r) + '\n' for r in rows)) | |
| class Merge(unittest.TestCase): | |
| def test_only_gateway_cuts_fall_back(self): | |
| # 顶档答出用顶档;网关截断(504/超时)用低一档;其他错误(503 等)保留为错误、不回退 | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) | |
| top, low, out = root / 'top', root / 'low', root / 'final' | |
| write(top / 'responses.jsonl', [ | |
| {'item_id': 'a', 'status': 'OK', 'text': 'top-a'}, | |
| {'item_id': 'b', 'status': 'ERROR', 'error': 'ProviderError: HTTP 504: cut'}, | |
| {'item_id': 'c', 'status': 'ERROR', 'error': 'ProviderError: HTTP 503: busy'}, | |
| {'item_id': 'd', 'status': 'ERROR', 'error': 'ProviderError: HTTP 504: cut'}, | |
| {'item_id': 'd', 'status': 'OK', 'text': 'top-d-resumed'}]) | |
| write(low / 'responses.jsonl', [{'item_id': x, 'status': 'OK', 'text': f'low-{x}'} for x in 'abcd']) | |
| (top / 'run.json').write_text('{}') | |
| counts = campaign.merge(top, low, out, 'xhigh', 'high') | |
| self.assertEqual(counts, {'items': 4, 'top_ok': 2, 'fell_back': 1, 'fallback_ok': 1, 'other_errors': 1}) | |
| rows = {r['item_id']: r for r in campaign.jl(out / 'responses.jsonl')} | |
| self.assertEqual(rows['a']['text'], 'top-a') | |
| self.assertEqual((rows['b']['text'], rows['b']['effort_used']), ('low-b', 'high')) | |
| self.assertIn('504', rows['b']['fallback_from']) | |
| self.assertEqual(rows['c']['status'], 'ERROR') | |
| self.assertEqual(rows['d']['text'], 'top-d-resumed') | |
| class EndToEnd(unittest.TestCase): | |
| def test_prepare_run_report_offline(self): | |
| # 合成题 + mock 端点:prepare(纯文本)→ run(含一对顶档/回退)→ report,全程离线 | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) | |
| (root / 'ep.json').write_text(json.dumps({'endpoints': { | |
| 'right': {'protocol': 'mock', 'reply': '{"answer": 10.5}'}, | |
| 'wrong': {'protocol': 'mock', 'reply': '{"answer": 99}'}}})) | |
| (root / 'plan.json').write_text(json.dumps({ | |
| 'schema': 'eval-campaign/1', 'endpoints': 'ep.json', | |
| 'settings': {'text': {'text_only': True, 'lang': 'zh'}}, | |
| 'models': [{'name': 'm.right', 'endpoint': 'right', 'settings': 'text', 'workers': 1}, | |
| {'name': 'm.top', 'endpoint': 'wrong', 'settings': 'text', 'workers': 1, | |
| 'fallback': {'endpoint': 'right', 'effort': 'low'}, 'effort': 'high'}]})) | |
| cdir = root / 'camp' | |
| campaign.main(['prepare', '--out', str(cdir), '--questions', | |
| str(HERE / 'examples' / 'synthetic' / 'questions.jsonl')]) | |
| campaign.main(['run', '--dir', str(cdir), '--plan', str(root / 'plan.json')]) | |
| right = json.loads((cdir / 'runs' / 'm.right' / 'summary.json').read_text())['overall'] | |
| top = json.loads((cdir / 'runs' / 'm.top' / 'summary.json').read_text())['overall'] | |
| self.assertEqual((right['passed'], right['items']), (1, 1)) | |
| self.assertEqual(top['passed'], 0) # top answered (wrongly), so no fallback | |
| self.assertEqual(json.loads((cdir / 'runs' / 'm.top' / 'merge.json').read_text())['top_ok'], 1) | |
| campaign.main(['report', '--dir', str(cdir)]) | |
| text = (cdir / 'report.md').read_text() | |
| self.assertIn('m.right', text) | |
| self.assertIn('总表', text) | |
| self.assertIn('ALL DONE', (cdir / 'driver.log').read_text()) | |
| if __name__ == '__main__': | |
| unittest.main() | |