Download decode_fa.py from karim23657/fa-eg2-persian: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/karim23657/fa-eg2-persian/resolve/main/decode_fa.py
- Command line
-
hf download hf://karim23657/fa-eg2-persian/decode_fa.py
-
curl -L -o decode_fa.py https://huggingface.co/karim23657/fa-eg2-persian/resolve/main/decode_fa.py
2.56 kB
| #!/usr/bin/env python3 | |
| """Greedy CTC decoding of cached soft-token files with a trained Persian reader. | |
| Reads ``<cache>/<split>/index.tsv`` (or one ``.npy`` file) and, for each utterance, | |
| loads its ``[T, 512]`` soft tokens, runs the reader and greedy-decodes the nearest | |
| word per token against ``word_table_fa.npy``. Prints ``id<TAB>hypothesis`` (normalized). | |
| Example:: | |
| python decode_fa.py --model checkpoints/persian_reader.pt --cache cache_fa --split test \ | |
| --word-table word_table_fa.npy --vocab vocab_fa.txt | |
| """ | |
| import argparse | |
| import os | |
| import numpy as np | |
| import torch | |
| from fa_reader_model import load_reader, greedy_decode | |
| from persian_text import normalize | |
| def read_index(path): | |
| rows = [] | |
| with open(path, encoding="utf-8") as f: | |
| for line in f: | |
| if not line.strip(): | |
| continue | |
| p = line.rstrip("\n").split("\t") | |
| rows.append((p[0], p[4] if len(p) >= 5 else "")) | |
| return rows | |
| def main(): | |
| ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| ap.add_argument("--model", required=True, help="checkpoints/persian_reader.pt") | |
| ap.add_argument("--cache", default=None, help="cache root; decodes <cache>/<split>/index.tsv") | |
| ap.add_argument("--split", default="test") | |
| ap.add_argument("--npy", default=None, help="a single soft-token .npy to decode instead of a split") | |
| ap.add_argument("--word-table", required=True) | |
| ap.add_argument("--vocab", required=True) | |
| ap.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu") | |
| args = ap.parse_args() | |
| vocab = [w for w in open(args.vocab, encoding="utf-8").read().splitlines() if w] | |
| table = torch.from_numpy(np.load(args.word_table).astype("float32")).to(args.device) | |
| reader = load_reader(args.model, None, args.device) | |
| if args.npy: | |
| items = [(os.path.basename(args.npy)[:-4], args.npy)] | |
| else: | |
| d = os.path.join(args.cache, args.split) | |
| items = [(uid, os.path.join(d, f"{uid}.npy")) for uid, _ in read_index(os.path.join(d, "index.tsv"))] | |
| for uid, path in items: | |
| soft = torch.from_numpy(np.load(path).astype("float32")).to(args.device).unsqueeze(0) | |
| mask = torch.ones(soft.shape[:2], dtype=torch.bool, device=args.device) | |
| with torch.no_grad(): | |
| logits = reader(soft, mask, table)[0] | |
| ids = greedy_decode(logits) | |
| hyp = normalize(" ".join(vocab[i] for i in ids)) | |
| print(f"{uid}\t{hyp}") | |
| if __name__ == "__main__": | |
| main() | |