Instructions to use kesha-humonen/runic-reader-2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use kesha-humonen/runic-reader-2 with PEFT:
Task type is invalid.
- Notebooks
- Google Colab
- Kaggle
Download code/perfilev_infer.py from kesha-humonen/runic-reader-2: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/kesha-humonen/runic-reader-2/resolve/main/code/perfilev_infer.py
- Command line
-
hf download hf://kesha-humonen/runic-reader-2/code/perfilev_infer.py
-
curl -L -o perfilev_infer.py https://huggingface.co/kesha-humonen/runic-reader-2/resolve/main/code/perfilev_infer.py
3.56 kB
| """Thesis-B recognizer (Perfilev, HSE 2026; released QLoRA adapters AntoniusPerf/runic-ocr-qwen-vl-lora) on our images. | |
| Reproduces the inference of RUNIC-OCR/src/runic_ocr_experiments.py::_infer_qwen: system + user prompt, processor from | |
| the adapter folder with max_pixels, 4-bit NF4 base (fp16 compute) when bitsandbytes is available, greedy decoding. | |
| python perfilev_infer.py --base <dir> --adapter <dir> --max_pixels 147456 --input x.jsonl --out y.jsonl | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import torch | |
| from PIL import Image | |
| SYS_PROMPT = ("You are an expert runologist OCR system. You read runic inscriptions " | |
| "and output ONLY their transliteration in the Rundata convention, " | |
| "with no explanation.") | |
| USR_PROMPT = "Transliterate the runic inscription in this image." | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--base", required=True) | |
| ap.add_argument("--adapter", required=True) | |
| ap.add_argument("--max_pixels", type=int, default=384 * 384) | |
| ap.add_argument("--input", required=True) | |
| ap.add_argument("--image_root", default="") | |
| ap.add_argument("--out", required=True) | |
| ap.add_argument("--max_new_tokens", type=int, default=64) | |
| ap.add_argument("--limit", type=int, default=0) | |
| args = ap.parse_args() | |
| print("ARGS", vars(args), flush=True) | |
| rows = [json.loads(l) for l in open(args.input) if l.strip()] | |
| if args.limit: | |
| rows = rows[: args.limit] | |
| done = {json.loads(l)["id"] for l in open(args.out)} if os.path.exists(args.out) else set() | |
| todo = [r for r in rows if r["id"] not in done] | |
| print(f"rows {len(rows)} todo {len(todo)}", flush=True) | |
| if not todo: | |
| return | |
| from peft import PeftModel | |
| from transformers import AutoModelForImageTextToText, AutoProcessor | |
| proc = AutoProcessor.from_pretrained(args.adapter, max_pixels=args.max_pixels) | |
| try: | |
| from transformers import BitsAndBytesConfig | |
| import bitsandbytes # noqa: F401 | |
| q = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_use_double_quant=True, | |
| bnb_4bit_compute_dtype=torch.float16) | |
| print("base in 4-bit NF4", flush=True) | |
| except Exception as e: # noqa: BLE001 | |
| q = None | |
| print("bitsandbytes unavailable, base in fp16:", e, flush=True) | |
| base = AutoModelForImageTextToText.from_pretrained(args.base, quantization_config=q, dtype=torch.float16, | |
| device_map="cuda") | |
| model = PeftModel.from_pretrained(base, args.adapter).eval() | |
| fout = open(args.out, "a") | |
| for k, r in enumerate(todo): | |
| img = Image.open(os.path.join(args.image_root, r["image"])).convert("RGB") | |
| msgs = [{"role": "system", "content": SYS_PROMPT}, | |
| {"role": "user", "content": [{"type": "image"}, {"type": "text", "text": USR_PROMPT}]}] | |
| text = proc.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True) | |
| inp = proc(text=[text], images=[img], return_tensors="pt").to("cuda") | |
| with torch.no_grad(): | |
| out = model.generate(**inp, max_new_tokens=args.max_new_tokens, do_sample=False) | |
| dec = proc.batch_decode(out[:, inp["input_ids"].shape[1]:], skip_special_tokens=True)[0].strip() | |
| fout.write(json.dumps(dict(r, pred=dec), ensure_ascii=False) + "\n") | |
| fout.flush() | |
| if k % 50 == 0: | |
| print(f"{k}/{len(todo)} {dec[:60]!r}", flush=True) | |
| print("DONE", flush=True) | |
| if __name__ == "__main__": | |
| main() | |