polocrbench-train-pool-v1 / integrate_mednotes.py
PiotrSty's picture
Upload folder using huggingface_hub
cf73b01 verified
Raw History Blame Contribute Delete
3.88 kB
"""Integrate the self-licensed contemporary medical calendar notes into
polocrbench-train-pool-v1 (handwriting-mednotes source).
License holder: Piotr Styła (declared CC BY 4.0, 2026-10-23).
Content: handwritten medical study/calendar notes (mostly page fragments
with cut edges). Transcriptions are AI pre-annotation with [ucięte] (cut),
[?], [skreślone] markers. Deterministic PII scan found no e-mail/phone/
PESEL/account strings; lecturer surnames appear as professional references.
Content is study/training material with medical knowledge; no personal
health records.
"""
import collections
import hashlib
import json
import shutil
from pathlib import Path
BASE = Path('C:/Users/Hipek/OCR_external_data')
WORK = BASE / 'skany-jadwiga-work'
POOL = BASE / 'polocrbench-train-pool-v1'
def sha(data):
return hashlib.sha256(data).hexdigest()
def main():
trans = {}
for line in (WORK / 'transcriptions.jsonl').read_text(encoding='utf-8').splitlines():
row = json.loads(line)
trans[row['page_id']] = row['text']
originals = {p.stem: sha(p.read_bytes()) for p in (WORK / 'originals').glob('*.jpg')}
assert len(trans) == 49
rows = []
for stem in sorted(trans):
src = WORK / 'pages' / f'{stem}.jpg'
pool_id = 'hwmed_' + stem.lower()
data = src.read_bytes()
shutil.copyfile(src, POOL / 'images' / f'{pool_id}.jpg')
rows.append({'id': pool_id, 'source': 'handwriting-mednotes',
'source_ref': 'self-licensed medical calendar notes '
'(license holder: Piotr Styła, CC BY 4.0, 2026-10-23)',
'image': f'images/{pool_id}.jpg', 'image_sha256': sha(data),
'text': trans[stem], 'text_sha256': sha(trans[stem].encode()),
'width': 700, 'height': 1000, 'split': 'train',
'license': 'cc-by-4.0', 'human_verified': False, 'gold': False,
'page_fragment': True,
'original_image_sha256': originals.get(stem),
'transcription': 'ai-preannotation-cut-markers'})
manifest_path = POOL / 'manifest.jsonl'
existing = manifest_path.read_text(encoding='utf-8')
manifest_path.write_text(existing + ''.join(
json.dumps(r, ensure_ascii=False) + '\n' for r in rows), encoding='utf-8')
report = json.loads((POOL / 'report.json').read_text(encoding='utf-8'))
all_rows = [json.loads(s) for s in manifest_path.read_text(encoding='utf-8').splitlines()]
report['schema'] = 'polocrbench-train-pool-v1.4'
report['pages'] = len(all_rows)
report['by_source'] = dict(collections.Counter(r['source'] for r in all_rows))
report['by_split'] = dict(collections.Counter(r['split'] for r in all_rows))
report['handwriting_mednotes_pages'] = len(rows)
report['handwriting_mednotes_license'] = 'CC BY 4.0 (license holder: Piotr Styła)'
report['handwriting_mednotes_content'] = 'study/training notes with medical knowledge (dosage schedules, disease patterns as taught); no personal health records; mostly page fragments with cut edges'
report['licenses']['handwriting-mednotes'] = 'CC BY 4.0 (license holder: Piotr Styła)'
(POOL / 'report.json').write_text(json.dumps(report, ensure_ascii=False, indent=2),
encoding='utf-8')
sums = {p.relative_to(POOL).as_posix(): sha(p.read_bytes())
for p in sorted(POOL.rglob('*')) if p.is_file() and p.name != 'checksums.json'}
(POOL / 'checksums.json').write_text(json.dumps(sums, indent=2), encoding='utf-8')
print(json.dumps({'mednotes_rows': len(rows), 'pool_pages': len(all_rows),
'by_source': report['by_source'], 'checksum_entries': len(sums)},
ensure_ascii=False, indent=2))
if __name__ == '__main__':
main()