*** !!! STILL under development. Weights are updating regularly !!! ***
1import torch
2import json
3from safetensors.torch import load_file
4
5# Config ve vocab yükle
6with open("config.json") as f:
7 config = json.load(f)
8with open("vocab.json") as f:
9 char2idx = json.load(f)
10
11# Model tanımı (CharViT sınıfının tanımlı olduğunu varsayar)
12model = CharViT(**{k: config[k] for k in
13 ["vocab_size","embed_dim","max_len","n_heads","n_layers","num_classes"]})
14model.load_state_dict(load_file("model.safetensors"))
15model.eval()
16
17def predict(text: str) -> str:
18 unk = char2idx["[UNK]"]
19 ids = [char2idx.get(c, unk) for c in text[:config["max_len"]]]
20 ids += [0] * (config["max_len"] - len(ids))
21 with torch.no_grad():
22 logits = model(torch.tensor([ids]))
23 return config["label_names"][logits.argmax().item()]
24
25print(predict("Orkun Gedik'e ait IBAN: TR33 0006 1005 1978 6457 8413 26"))
26# → "finans"
Model 512 karakter ile sınırlıdır. Daha uzun metinler için chunk + majority vote kullanın:
1from collections import Counter
2
3def predict_document(text: str, overlap: int = 30) -> str:
4 step = config["max_len"] - overlap
5 chunks = [text[i:i+config["max_len"]] for i in range(0, len(text), step)]
6 preds = [predict(c) for c in chunks if c.strip()]
7 return Counter(preds).most_common(1)[0][0]