Views
No views yet
1# Gerekli kütüphaneleri yükle / Install required libraries
2# pip install torch sentencepiece huggingface_hub safetensors
3
4import torch
5import torch.nn as nn
6from torch.nn import functional as F
7from safetensors.torch import load_file
8from huggingface_hub import hf_hub_download
9import sentencepiece as spm
10
11# --- 1. MODEL MİMARİSİ VE CONFIG (EĞİTİM KODUYLA BİREBİR AYNI) ---
12# --- 1. MODEL ARCHITECTURE & CONFIG (EXACTLY AS IN TRAINING SCRIPT) ---
13class ModelConfig:
14 n_layer = 10
15 n_embd = 640
16 n_head = 10
17 block_size = 256
18 vocab_size = 8000
19 dropout = 0.1
20
21config = ModelConfig()
22device = 'cuda' if torch.cuda.is_available() else 'cpu'
23
24class Head(nn.Module):
25 def __init__(self, head_size):
26 super().__init__()
27 self.key = nn.Linear(config.n_embd, head_size, bias=False)
28 self.query = nn.Linear(config.n_embd, head_size, bias=False)
29 self.value = nn.Linear(config.n_embd, head_size, bias=False)
30 self.register_buffer('tril', torch.tril(torch.ones(config.block_size, config.block_size)))
31 self.dropout = nn.Dropout(config.dropout)
32 def forward(self, x):
33 B, T, C = x.shape
34 k, q, v = self.key(x), self.query(x), self.value(x)
35 wei = q @ k.transpose(-2, -1) * (C ** -0.5)
36 wei = wei.masked_fill(self.tril[:T, :T] == 0, float('-inf'))
37 wei = F.softmax(wei, dim=-1)
38 wei = self.dropout(wei)
39 return wei @ v
40
41class MultiHeadAttention(nn.Module):
42 def __init__(self, num_heads, head_size):
43 super().__init__()
44 self.heads = nn.ModuleList([Head(head_size) for _ in range(num_heads)])
45 self.proj = nn.Linear(config.n_embd, config.n_embd)
46 self.dropout = nn.Dropout(config.dropout)
47 def forward(self, x):
48 out = torch.cat([h(x) for h in self.heads], dim=-1)
49 return self.dropout(self.proj(out))
50
51class FeedForward(nn.Module):
52 def __init__(self, n_embd):
53 super().__init__()
54 self.net = nn.Sequential(nn.Linear(n_embd, 4 * n_embd), nn.ReLU(), nn.Dropout(config.dropout), nn.Linear(4 * n_embd, n_embd), nn.Dropout(config.dropout))
55 def forward(self, x): return self.net(x)
56
57class Block(nn.Module):
58 def __init__(self, n_embd, n_head):
59 super().__init__()
60 head_size = n_embd // n_head
61 self.sa = MultiHeadAttention(n_head, head_size)
62 self.ffwd = FeedForward(n_embd)
63 self.ln1, self.ln2 = nn.LayerNorm(n_embd), nn.LayerNorm(n_embd)
64 def forward(self, x):
65 x = x + self.sa(self.ln1(x))
66 x = x + self.ffwd(self.ln2(x))
67 return x
68
69class MyLanguageModel(nn.Module):
70 def __init__(self):
71 super().__init__()
72 self.token_embedding_table = nn.Embedding(config.vocab_size, config.n_embd)
73 self.position_embedding_table = nn.Embedding(config.block_size, config.n_embd)
74 self.blocks = nn.Sequential(*[Block(config.n_embd, n_head=config.n_head) for _ in range(config.n_layer)])
75 self.ln_f = nn.LayerNorm(config.n_embd)
76 self.lm_head = nn.Linear(config.n_embd, config.vocab_size)
77 self.dropout = nn.Dropout(config.dropout)
78 def forward(self, idx, targets=None):
79 B, T = idx.shape
80 tok_emb = self.token_embedding_table(idx)
81 pos_emb = self.position_embedding_table(torch.arange(T, device=device))
82 x = self.dropout(tok_emb + pos_emb)
83 x = self.blocks(x)
84 x = self.ln_f(x)
85 logits = self.lm_head(x)
86 loss = None
87 if targets is not None:
88 loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1))
89 return logits, loss
90
91# --- 2. MODELİ VE TOKENIZER'I YÜKLE ---
92# --- 2. LOAD MODEL AND TOKENIZER ---
93REPO_ID = "jetbabareal/Sabir-60M" # Kendi kullanıcı adını ve model adını yaz / Your username and model name
94model = MyLanguageModel().to(device)
95weights_path = hf_hub_download(repo_id=REPO_ID, filename="model.safetensors")
96model.load_state_dict(load_file(weights_path))
97tokenizer_path = hf_hub_download(repo_id=REPO_ID, filename="tokenizer.model")
98tokenizer = spm.SentencePieceProcessor(model_file=tokenizer_path)
99model.eval()
100print("Model ve Tokenizer başarıyla yüklendi. / Model and Tokenizer loaded successfully.")
101
102# --- 3. METİN ÜRETME FONKSİYONU ---
103# --- 3. TEXT GENERATION FUNCTION ---
104def generate_text(prompt, max_new_tokens=100, temperature=0.5, top_k=20):
105 full_prompt = f"Kullanıcı: {prompt}\nModel: "
106 input_ids = tokenizer.encode(full_prompt)
107 idx = torch.tensor(input_ids, dtype=torch.long, device=device).unsqueeze(0)
108
109
110 generated_ids = []
111
112 for _ in range(max_new_tokens):
113 idx_cond = idx[:, -config.block_size:]
114 with torch.no_grad():
115 logits, _ = model(idx_cond)
116 logits = logits[:, -1, :] / temperature
117 if top_k is not None:
118 v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
119 logits[logits < v[:, [-1]]] = -float('Inf')
120 probs = F.softmax(logits, dim=-1)
121 idx_next = torch.multinomial(probs, num_samples=1)
122
123
124 generated_ids.append(idx_next.item())
125
126
127 decoded_so_far = tokenizer.decode(generated_ids)
128 if "Kullanıcı:" in decoded_so_far or "Model:" in decoded_so_far:
129
130 generated_ids = generated_ids[:-1]
131 break
132
133 if idx_next.item() == tokenizer.eos_id():
134 break
135
136 idx = torch.cat((idx, idx_next), dim=1)
137
138 response = tokenizer.decode(generated_ids)
139 return response.strip()
140
141# --- ÖRNEK KULLANIM / EXAMPLE USAGE ---
142soru = "Nasılsın?"
143cevap = generate_text(soru)
144print(f"Soru: {soru}\nCevap: {cevap}")
145
146soru = "En sevdiğin renk ne?"
147cevap = generate_text(soru)
148print(f"Soru: {soru}\nCevap: {cevap}")1@misc{sabir60m,
2 title = {Sabir-60M: A Turkish Micro Language Model},
3 author = {jetbabareal},
4 year = {2025},
5 url = {https://huggingface.co/jetbabareal/Sabir-60M}
6}