| """
|
| Faz 0 / Adım 4 — Tokenizer'ı DONDUR (SentencePiece canonical).
|
|
|
| transformers 5.x slow/fast Llama dönüştürücüsü SP modelini yüklemiyor (vocab boş
|
| çıkıyor). SP'nin kendisi sağlam → canonical artefakt SP `.model`. Pipeline SP ile
|
| tokenize eder (kod/sc_tokenizer.py). Yine de HF fast dönüşümü DENENİR; round-trip
|
| SP ile eşleşirse tokenizer.json saklanır, yoksa silinir (kimse bozuk yükleme yapmasın).
|
|
|
| Kullanım: python kod/faz0_04_freeze.py
|
| """
|
| import os, shutil
|
| import sentencepiece as spm
|
|
|
| SRC = "kod/data/smartcore_v1_tok"
|
| OUT = "kod/tokenizer"
|
| TESTS = [
|
| "Yapay zeka modelleri 2026 yılında 180 milyon parametreyle eğitiliyor.",
|
| "The quick brown fox jumps over 1234 lazy dogs.",
|
| "İstanbul'da yağmur yağıyordu; çocuklar şemsiyelerini açtı.",
|
| "Matematik: 3.14159 × 2 = 6.28318 (yaklaşık).",
|
| ]
|
|
|
|
|
| def main():
|
| os.makedirs(OUT, exist_ok=True)
|
| shutil.copy(SRC + ".model", os.path.join(OUT, "tokenizer.model"))
|
| shutil.copy(SRC + ".vocab", os.path.join(OUT, "tokenizer.vocab"))
|
|
|
| sp = spm.SentencePieceProcessor(model_file=SRC + ".model")
|
| vsz = sp.get_piece_size()
|
| print(f"[freeze] SP yüklendi | vocab={vsz} | unk={sp.unk_id()} bos={sp.bos_id()} "
|
| f"eos={sp.eos_id()} pad={sp.pad_id()}")
|
|
|
|
|
| print("[freeze] SP round-trip (text->ids->text):")
|
| sp_ok = True
|
| for s in TESTS:
|
| ids = sp.encode(s, out_type=int)
|
| back = sp.decode(ids)
|
| same = (back == s)
|
| sp_ok &= same
|
| print(f" {'OK ' if same else 'FARK'} | {len(ids):2d} tok | {s[:42]}")
|
| print(f"[freeze] SP round-trip: {'TUM OK (reversible)' if sp_ok else 'UYARI: kayıp var'}")
|
|
|
|
|
| hf_ok = False
|
| try:
|
| from transformers import LlamaTokenizerFast
|
| hf = LlamaTokenizerFast(vocab_file=SRC + ".model", from_slow=True,
|
| bos_token="<s>", eos_token="</s>",
|
| unk_token="<unk>", pad_token="<pad>",
|
| add_bos_token=False, add_eos_token=False)
|
| hf.save_pretrained(OUT)
|
| hf_ok = all(sp.encode(s, out_type=int) == hf.encode(s, add_special_tokens=False)
|
| for s in TESTS)
|
| except Exception as e:
|
| print(f"[freeze] HF fast dönüşüm hata: {repr(e)[:120]}")
|
|
|
| if hf_ok:
|
| print("[freeze] HF fast: round-trip OK → tokenizer.json saklandı (AutoTokenizer uyumlu).")
|
| else:
|
| for f in ("tokenizer.json", "tokenizer_config.json", "special_tokens_map.json"):
|
| p = os.path.join(OUT, f)
|
| if os.path.exists(p):
|
| os.remove(p)
|
| print("[freeze] HF fast: BOZUK (transformers 5.x SP yüklemiyor) → tokenizer.json silindi. "
|
| "Canonical = SentencePiece (.model). Pipeline kod/sc_tokenizer.py kullanır.")
|
|
|
|
|
| card = f"""# SmartCore V1 Tokenizer (48K EN+TR)
|
|
|
| Canonical: **SentencePiece** (`tokenizer.model`). HuggingFace AutoTokenizer
|
| dönüşümü transformers 5.x'te SP'yi yüklemediği için SP doğrudan kullanılır.
|
|
|
| | Özellik | Değer |
|
| |---|---|
|
| | Tür | SentencePiece BPE, byte_fallback, split_digits, split_by_unicode_script |
|
| | vocab_size | {vsz} |
|
| | Diller | İngilizce (temel) + Türkçe (ikincil) |
|
| | Korpus | FineWeb-Edu (EN) + FineWeb-2 tur_Latn (TR), ~1.5GB, %45 TR |
|
| | normalization | identity (yok) |
|
| | Özel token | <unk>=0, <s>=1, </s>=2, <pad>=3 |
|
| | Fertility | EN 1.42, TR 1.77 (TR/EN 1.25) |
|
|
|
| ## Yükleme
|
| ```python
|
| import sentencepiece as spm
|
| sp = spm.SentencePieceProcessor(model_file="tokenizer.model")
|
| ids = sp.encode("metin", out_type=int)
|
| txt = sp.decode(ids)
|
| ```
|
| veya pipeline'da: `from kod.sc_tokenizer import SCTokenizer; tok = SCTokenizer()`
|
| """
|
| with open(os.path.join(OUT, "README.md"), "w", encoding="utf-8") as f:
|
| f.write(card)
|
|
|
| print(f"\n[freeze] BİTTİ. {OUT}/ -> {sorted(os.listdir(OUT))}")
|
|
|
|
|
| if __name__ == "__main__":
|
| main()
|
|
|