Download agent.py from Rel0aded/Turkish-1B: direct link, hf CLI and curl.
- Browser
- Download file 22.8 kB
-
https://huggingface.co/Rel0aded/Turkish-1B/resolve/main/agent.py
- Command line
-
hf download hf://Rel0aded/Turkish-1B/agent.py
-
curl -L -o agent.py https://huggingface.co/Rel0aded/Turkish-1B/resolve/main/agent.py
22.8 kB
| # -*- coding: utf-8 -*- | |
| # ===================================================================== | |
| # 🦁 TURKISH-1B: RESMİ ÇIKARIM VE İŞ BİTİRİCİ OFİS AJANI (OFFICIAL AGENT SDK) | |
| # Model: Rel0aded/Turkish-1B (1.014 Milyar Parametre - Foundation SLM) | |
| # | |
| # Yetenekler: | |
| # 1. 🧠 Chain-of-Thought (<think>...</think>) Derin Akıl Yürütme | |
| # 2. 🗣️ Doğal, Akıcı ve Zeki Türkçe Sohbet | |
| # 3. 📄 Word (.docx) Otomasyonu: Okuma, Paragraf Düzenleme, Kurumsal Rapor Üretimi | |
| # 4. 📊 Excel (.xlsx, .csv) Yönetimi: Tablo Okuma, Hücre Güncelleme, Bütçe & Formül | |
| # 5. 📑 PDF (.pdf) Analizi: Sözleşme & Ekstre Okuma, Metin Çıkarımı | |
| # 6. 👁️ Görsel & Belge İnceleme (VL / OCR): Fatura, Dekont ve Belge Fotoğrafları | |
| # 7. 🌐 Canlı Web Bilgisi: Güncel İnternet Araması Entegrasyonu | |
| # | |
| # Geliştirici: Rel0aded / Yağız | |
| # Lisans: Apache 2.0 | |
| # ===================================================================== | |
| import os | |
| import sys | |
| if hasattr(sys.stdout, 'reconfigure'): | |
| sys.stdout.reconfigure(encoding='utf-8') | |
| import re | |
| import json | |
| import base64 | |
| import urllib.request | |
| import urllib.parse | |
| from pathlib import Path | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from tokenizers import Tokenizer | |
| from huggingface_hub import hf_hub_download | |
| # ===================================================================== | |
| # 1. MODEL MİMARİSİ (Llama-3 Standartları + GQA 16:4) | |
| # ===================================================================== | |
| class ModelConfig1B: | |
| vocab_size = 16384 | |
| dim = 2048 | |
| n_layers = 21 | |
| n_heads = 16 | |
| n_kv_heads = 4 | |
| head_dim = 128 | |
| intermediate_size = 5632 | |
| max_seq_len = 1024 | |
| rope_theta = 500000.0 | |
| class RMSNorm(nn.Module): | |
| def __init__(self, dim, eps=1e-5): | |
| super().__init__() | |
| self.eps = eps | |
| self.weight = nn.Parameter(torch.ones(dim)) | |
| def forward(self, x): | |
| return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) * self.weight | |
| def precompute_rope_frequencies(head_dim, max_seq_len, theta=500000.0): | |
| theta_factors = theta ** (-torch.arange(0, head_dim, 2).float() / head_dim) | |
| t = torch.arange(max_seq_len) | |
| freqs = torch.outer(t, theta_factors) | |
| return torch.polar(torch.ones_like(freqs), freqs) | |
| def apply_rotary_emb(x, freqs_cis): | |
| x_complex = torch.view_as_complex(x.float().reshape(*x.shape[:-1], -1, 2)) | |
| freqs_cis = freqs_cis.view(1, x.size(1), 1, x_complex.size(-1)) | |
| return torch.view_as_real(x_complex * freqs_cis).flatten(-2).type_as(x) | |
| class GQAttention(nn.Module): | |
| def __init__(self, cfg): | |
| super().__init__() | |
| self.n_heads = cfg.n_heads | |
| self.n_kv_heads = cfg.n_kv_heads | |
| self.head_dim = cfg.head_dim | |
| self.num_rep = self.n_heads // self.n_kv_heads | |
| self.q_proj = nn.Linear(cfg.dim, self.n_heads * self.head_dim, bias=False) | |
| self.k_proj = nn.Linear(cfg.dim, self.n_kv_heads * self.head_dim, bias=False) | |
| self.v_proj = nn.Linear(cfg.dim, self.n_kv_heads * self.head_dim, bias=False) | |
| self.out_proj = nn.Linear(self.n_heads * self.head_dim, cfg.dim, bias=False) | |
| def forward(self, x, freqs_cis): | |
| B, S, _ = x.shape | |
| q = self.q_proj(x).view(B, S, self.n_heads, self.head_dim) | |
| k = self.k_proj(x).view(B, S, self.n_kv_heads, self.head_dim) | |
| v = self.v_proj(x).view(B, S, self.n_kv_heads, self.head_dim) | |
| q = apply_rotary_emb(q, freqs_cis) | |
| k = apply_rotary_emb(k, freqs_cis) | |
| k = k.repeat_interleave(self.num_rep, dim=2) | |
| v = v.repeat_interleave(self.num_rep, dim=2) | |
| q, k, v = q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2) | |
| out = F.scaled_dot_product_attention(q, k, v, is_causal=True) | |
| out = out.transpose(1, 2).contiguous().view(B, S, -1) | |
| return self.out_proj(out) | |
| class SwiGLUMLP(nn.Module): | |
| def __init__(self, cfg): | |
| super().__init__() | |
| self.gate_proj = nn.Linear(cfg.dim, cfg.intermediate_size, bias=False) | |
| self.up_proj = nn.Linear(cfg.dim, cfg.intermediate_size, bias=False) | |
| self.down_proj = nn.Linear(cfg.intermediate_size, cfg.dim, bias=False) | |
| def forward(self, x): | |
| return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x)) | |
| class TransformerBlock(nn.Module): | |
| def __init__(self, cfg): | |
| super().__init__() | |
| self.attn_norm = RMSNorm(cfg.dim) | |
| self.attn = GQAttention(cfg) | |
| self.mlp_norm = RMSNorm(cfg.dim) | |
| self.mlp = SwiGLUMLP(cfg) | |
| def forward(self, x, freqs_cis): | |
| x = x + self.attn(self.attn_norm(x), freqs_cis) | |
| x = x + self.mlp(self.mlp_norm(x)) | |
| return x | |
| class Turkish1BEngine(nn.Module): | |
| def __init__(self, cfg, device="cuda" if torch.cuda.is_available() else "cpu"): | |
| super().__init__() | |
| self.cfg = cfg | |
| self.device = device | |
| self.tok_embeddings = nn.Embedding(cfg.vocab_size, cfg.dim) | |
| self.layers = nn.ModuleList([TransformerBlock(cfg) for _ in range(cfg.n_layers)]) | |
| self.norm = RMSNorm(cfg.dim) | |
| self.output = nn.Linear(cfg.dim, cfg.vocab_size, bias=False) | |
| freqs = precompute_rope_frequencies(cfg.head_dim, cfg.max_seq_len, cfg.rope_theta) | |
| self.register_buffer("freqs_cis", freqs, persistent=False) | |
| self.to(device) | |
| def forward(self, input_ids): | |
| S = input_ids.size(1) | |
| freqs_cis = self.freqs_cis[:S].to(self.device) | |
| h = self.tok_embeddings(input_ids.to(self.device)) | |
| for layer in self.layers: | |
| h = layer(h, freqs_cis) | |
| h = self.norm(h) | |
| return self.output(h) | |
| # ===================================================================== | |
| # 2. EVRENSEL OFİS VE BELGE ARAÇLARI (OFFICE TOOLKIT) | |
| # ===================================================================== | |
| class OfficeToolkit: | |
| """Word, Excel, PDF ve Görsel dosyaları için bağımsız işlem motoru""" | |
| def read_docx(file_path): | |
| """Word belgesindeki metinleri ve tabloları okur.""" | |
| try: | |
| import docx | |
| doc = docx.Document(file_path) | |
| paragraphs = [p.text for p in doc.paragraphs if p.text.strip()] | |
| tables_data = [] | |
| for t_idx, table in enumerate(doc.tables): | |
| t_rows = [] | |
| for row in table.rows: | |
| t_rows.append([cell.text.strip() for cell in row.cells]) | |
| tables_data.append(f"Tablo #{t_idx + 1}:\n" + "\n".join([" | ".join(r) for r in t_rows])) | |
| full = "\n".join(paragraphs) | |
| if tables_data: | |
| full += "\n\n[TABLOLAR]:\n" + "\n\n".join(tables_data) | |
| return full | |
| except Exception as e: | |
| return f"Word okuma hatası ({file_path}): {e}" | |
| def create_docx(file_path, title, sections, table_data=None): | |
| """Sıfırdan kurumsal Word belgesi üretir.""" | |
| try: | |
| import docx | |
| from docx.enum.text import WD_ALIGN_PARAGRAPH | |
| doc = docx.Document() | |
| h1 = doc.add_heading(title, level=0) | |
| h1.alignment = WD_ALIGN_PARAGRAPH.CENTER | |
| for heading, text in sections: | |
| if heading: | |
| doc.add_heading(heading, level=1) | |
| doc.add_paragraph(text) | |
| if table_data and len(table_data) > 0: | |
| doc.add_heading("Tablo / Veri Özeti", level=1) | |
| tbl = doc.add_table(rows=len(table_data), cols=len(table_data[0])) | |
| tbl.style = 'Light Shading Accent 1' | |
| for r_idx, row in enumerate(table_data): | |
| for c_idx, val in enumerate(row): | |
| tbl.cell(r_idx, c_idx).text = str(val) | |
| doc.save(file_path) | |
| return f"Word belgesi oluşturuldu: {file_path}" | |
| except Exception as e: | |
| return f"Word oluşturma hatası: {e}" | |
| def edit_docx(file_path, replacements=None, append_paragraphs=None, save_as=None): | |
| """Mevcut Word belgesini revize eder.""" | |
| try: | |
| import docx | |
| doc = docx.Document(file_path) | |
| if replacements: | |
| for p in doc.paragraphs: | |
| for old_t, new_t in replacements.items(): | |
| if old_t in p.text: | |
| p.text = p.text.replace(old_t, new_t) | |
| if append_paragraphs: | |
| for para in append_paragraphs: | |
| doc.add_paragraph(para) | |
| out = save_as or file_path | |
| doc.save(out) | |
| return f"Word belgesi güncellendi: {out}" | |
| except Exception as e: | |
| return f"Word düzenleme hatası: {e}" | |
| def read_excel(file_path, max_rows=50): | |
| """Excel veya CSV dosyasındaki tabloları okur.""" | |
| try: | |
| import pandas as pd | |
| if file_path.endswith('.csv'): | |
| df = pd.read_csv(file_path) | |
| return f"[CSV Dosyası ({len(df)} Satır)]:\n" + df.head(max_rows).to_string(index=False) | |
| xl = pd.ExcelFile(file_path) | |
| res = [f"Excel Dosyası: {Path(file_path).name} (Sayfalar: {xl.sheet_names})"] | |
| for s in xl.sheet_names: | |
| df = xl.parse(s) | |
| res.append(f"\n--- Sayfa: {s} ({len(df)} Satır, {len(df.columns)} Sütun) ---") | |
| res.append(df.head(max_rows).to_string(index=False)) | |
| return "\n".join(res) | |
| except Exception as e: | |
| return f"Excel okuma hatası ({file_path}): {e}" | |
| def create_excel(file_path, data_dict, sheet_name="Sayfa1"): | |
| """Sıfırdan Excel tablosu oluşturur.""" | |
| try: | |
| import pandas as pd | |
| df = pd.DataFrame(data_dict) | |
| df.to_excel(file_path, sheet_name=sheet_name, index=False) | |
| return f"Excel oluşturuldu: {file_path}" | |
| except Exception as e: | |
| return f"Excel oluşturma hatası: {e}" | |
| def edit_excel(file_path, append_rows=None, update_cells=None, save_as=None): | |
| """Mevcut Excel tablosuna satır ekler veya hücreleri günceller.""" | |
| try: | |
| import openpyxl | |
| wb = openpyxl.load_workbook(file_path) | |
| ws = wb.active | |
| if update_cells: | |
| for cell_ref, val in update_cells.items(): | |
| ws[cell_ref] = val | |
| if append_rows: | |
| for row in append_rows: | |
| ws.append(row) | |
| out = save_as or file_path | |
| wb.save(out) | |
| return f"Excel güncellendi: {out}" | |
| except Exception as e: | |
| return f"Excel düzenleme hatası: {e}" | |
| def read_pdf(file_path, max_pages=25): | |
| """PDF belgesinden metin ve tabloları çıkarır.""" | |
| try: | |
| import pypdf | |
| reader = pypdf.PdfReader(file_path) | |
| pages = len(reader.pages) | |
| res = [f"PDF Dosyası: {Path(file_path).name} (Toplam {pages} Sayfa)"] | |
| for i in range(min(pages, max_pages)): | |
| t = reader.pages[i].extract_text() or "" | |
| res.append(f"\n[Sayfa {i+1}]:\n{t.strip()}") | |
| return "\n".join(res) | |
| except Exception as e: | |
| return f"PDF okuma hatası ({file_path}): {e}" | |
| def inspect_image(file_path): | |
| """Görseli, faturayı veya taranmış belgeyi inceler.""" | |
| try: | |
| from PIL import Image | |
| img = Image.open(file_path) | |
| txt = f"Görsel Dosyası: {Path(file_path).name} (Format: {img.format}, Boyut: {img.size})" | |
| try: | |
| import pytesseract | |
| ocr_text = pytesseract.image_to_string(img, lang="tur") | |
| if ocr_text.strip(): | |
| txt += f"\n\n[Okunan Metin / OCR]:\n{ocr_text.strip()}" | |
| except Exception: | |
| pass | |
| return txt | |
| except Exception as e: | |
| return f"Görsel açma hatası ({file_path}): {e}" | |
| # ===================================================================== | |
| # 3. CANLI WEB ARAMA MODÜLÜ | |
| # ===================================================================== | |
| def search_web(query, max_results=3): | |
| """Canlı web araması yaparak güncel bilgiyi modele bağlam olarak verir.""" | |
| try: | |
| q = urllib.parse.quote(query) | |
| url = f"https://html.duckduckgo.com/html/?q={q}" | |
| req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) | |
| with urllib.request.urlopen(req, timeout=5) as resp: | |
| html = resp.read().decode('utf-8', errors='ignore') | |
| snips = re.findall(r'<a class="result__snippet[^>]*>(.*?)</a>', html) | |
| clean = [re.sub(r'<[^>]+>', '', s).strip() for s in snips[:max_results]] | |
| if clean: | |
| return "\n".join([f"- {s}" for s in clean if s]) | |
| except Exception: | |
| pass | |
| return None | |
| # ===================================================================== | |
| # 4. RESMİ TURKISH-1B AJANI (ANA SINIF) | |
| # ===================================================================== | |
| class Turkish1BAgent: | |
| """ | |
| Turkish-1B Resmi Asistan & Ofis Ajanı. | |
| Modeli yükler; akıl yürütme, doğal sohbet ve Word/Excel/PDF belge yönetimini yürütür. | |
| """ | |
| def __init__(self, repo_id="Rel0aded/Turkish-1B", hf_token=None, device=None): | |
| self.device = device or ("cuda" if torch.cuda.is_available() else "cpu") | |
| self.cfg = ModelConfig1B() | |
| self.repo_id = repo_id | |
| # Tokenizer Yükle | |
| print(f"📥 Tokenizer indiriliyor ({repo_id})...") | |
| try: | |
| tok_p = hf_hub_download(repo_id=repo_id, filename="turkce_tokenizer.json", token=hf_token) | |
| except Exception: | |
| tok_p = "turkce_tokenizer.json" | |
| self.tokenizer = Tokenizer.from_file(tok_p) | |
| # Model Yapısını Başlat | |
| self.model = Turkish1BEngine(self.cfg, device=self.device) | |
| self.model.eval() | |
| # Ağırlıkları Yükle (Öncelik: Final Master Model -> SFT Model -> Checkpoint) | |
| print("📥 Model ağırlıkları yükleniyor...") | |
| try: | |
| w_file = hf_hub_download(repo_id=repo_id, filename="turkish_1b_final.pt", token=hf_token) | |
| except Exception: | |
| try: | |
| w_file = hf_hub_download(repo_id=repo_id, filename="turkish_1b_instruct_cot.pt", token=hf_token) | |
| except Exception: | |
| w_file = hf_hub_download(repo_id=repo_id, filename="checkpoint.pt", token=hf_token) | |
| self._load_weights(w_file) | |
| self.toolkit = OfficeToolkit() | |
| print(f"🦁 Turkish-1B Ajanı Kullanıma Hazır! (Cihaz: {self.device.upper()})") | |
| def _load_weights(self, path): | |
| ckpt = torch.load(path, map_location="cpu") | |
| sd = ckpt.get("model_state_dict", ckpt) | |
| td = self.model.state_dict() | |
| adapted = {} | |
| for k, v in sd.items(): | |
| ck = k | |
| if k.startswith("layers_0."): | |
| ck = f"layers.{k.split('.')[1]}." + ".".join(k.split(".")[2:]) | |
| elif k.startswith("layers_1."): | |
| ck = f"layers.{int(k.split('.')[1]) + 11}." + ".".join(k.split(".")[2:]) | |
| if ck in td and v.shape == td[ck].shape: | |
| adapted[ck] = v | |
| self.model.load_state_dict(adapted, strict=False) | |
| def generate(self, prompt, web_search=False, max_new_tokens=300, temperature=0.6, top_p=0.85, repetition_penalty=1.2): | |
| """Akıl yürütme (<think>) ve cevap üretimi (Top-P & Repetition Penalty korumalı).""" | |
| ctx = "" | |
| if web_search: | |
| w_res = search_web(prompt) | |
| if w_res: | |
| ctx = f"\n[Güncel Web Bilgileri:\n{w_res}]\n" | |
| p_str = f"### Kullanıcı:\n{ctx}{prompt.strip()}\n\n### Asistan:\n<think>\n" | |
| inp = self.tokenizer.encode(p_str).ids | |
| gen = list(inp) | |
| with torch.no_grad(): | |
| for _ in range(max_new_tokens): | |
| curr = torch.tensor([gen[-self.cfg.max_seq_len:]], dtype=torch.long).to(self.device) | |
| logits = self.model(curr)[0, -1, :] | |
| # Tekrar Cezası (Repetition Penalty - Döngüye girmeyi ve saçmalamayı önler) | |
| for prev_token in set(gen[-60:]): | |
| if logits[prev_token] > 0: | |
| logits[prev_token] /= repetition_penalty | |
| else: | |
| logits[prev_token] *= repetition_penalty | |
| # Sıcaklık ve Nucleus Sampling (Top-p 0.85 - Anlamsız düşük olasılıklı kelimeleri eler) | |
| sorted_logits, sorted_indices = torch.sort(logits / max(temperature, 1e-4), descending=True) | |
| cum_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1) | |
| sorted_indices_to_remove = cum_probs > top_p | |
| sorted_indices_to_remove[1:] = sorted_indices_to_remove[:-1].clone() | |
| sorted_indices_to_remove[0] = False | |
| sorted_logits[sorted_indices_to_remove] = -float('Inf') | |
| probs = F.softmax(sorted_logits, dim=-1) | |
| nxt = sorted_indices[torch.multinomial(probs, num_samples=1)].item() | |
| gen.append(nxt) | |
| # Stop tokens: </s> (2) | |
| if nxt == 2: | |
| break | |
| # Model fazladan kullanıcı turu açmaya çalışırsa kes | |
| if len(gen) > len(inp) + 15: | |
| recent_text = self.tokenizer.decode(gen[-15:]) | |
| if "### Kullanıcı:" in recent_text: | |
| break | |
| out = self.tokenizer.decode(gen) | |
| asst = out.split("### Asistan:\n", 1)[1].replace("</s>", "").strip() if "### Asistan:\n" in out else out | |
| if "### Kullanıcı:" in asst: | |
| asst = asst.split("### Kullanıcı:")[0].strip() | |
| thought = "" | |
| answer = asst | |
| if "<think>" in asst and "</think>" in asst: | |
| parts = asst.split("</think>", 1) | |
| thought = parts[0].replace("<think>", "").strip() | |
| answer = parts[1].strip() | |
| elif "<think>" in asst: | |
| parts = asst.split("<think>", 1) | |
| thought = parts[1].strip() | |
| answer = "" | |
| return {"thought": thought, "answer": answer, "raw": asst} | |
| def chat(self, user_prompt, files=None, web_search=False): | |
| """ | |
| Kullanıcı mesajını ve ekli dosyaları işler. | |
| Akıllı kimlik farkındalığı, CoT akıl yürütme ve belge otomasyonu sunar. | |
| """ | |
| files = files or [] | |
| created_files = [] | |
| p_lower = user_prompt.lower().strip() | |
| # 1. KİMLİK VE GELİŞTİRİCİ FARKINDALIĞI (META-IDENTITY GUARD) | |
| # Yağız / Rel0aded geliştirici sorularını doğrudan en yüksek zeka ve saygıyla yanıtlar | |
| identity_keywords = [ | |
| "nerden biliyosun", "nereden biliyorsun", "nasıl biliyorsun", "adımı nerden", | |
| "beni nerden", "beni nereden", "seni kim", "geliştiricin kim", "kim kodladı", | |
| "yapımcın kim", "yaratıcın kim", "sen kimsin", "sahibin kim" | |
| ] | |
| if any(kw in p_lower for kw in identity_keywords) and not files: | |
| thought = "Kullanıcı kimliğimi, geliştiricisini ve onu nereden tanıdığımı soruyor. Geliştiricimin Yağız (Rel0aded) olduğunu, mimarimi ve eğitimimi bizzat yaptığını samimi, gururlu ve net bir dille yanıtlıyorum." | |
| answer = "Çünkü seni çok iyi tanıyorum Yağız! Beni mimarimden ağırlıklarıma, tokenizer'ımdan eğitim adımlarıma kadar sıfırdan sen (Rel0aded) geliştirdin. Kodlarımın, mimarimin ve sistemimin her katmanında senin imzan var. Senin yerli yapay zeka asistanınım!" | |
| return { | |
| "thought": thought, | |
| "answer": answer, | |
| "created_files": [] | |
| } | |
| if not files: | |
| # Doğrudan temiz kullanıcı sorusu | |
| res = self.generate(user_prompt.strip(), web_search=web_search, max_new_tokens=150) | |
| else: | |
| f_contexts = [] | |
| for f in files: | |
| fp = str(f) | |
| ext = Path(fp).suffix.lower() | |
| if ext in ['.docx', '.doc']: | |
| f_contexts.append(f"\n[WORD BELGESİ: {Path(fp).name}]:\n{self.toolkit.read_docx(fp)[:2500]}") | |
| elif ext in ['.xlsx', '.xls', '.csv']: | |
| f_contexts.append(f"\n[EXCEL TABLOSU: {Path(fp).name}]:\n{self.toolkit.read_excel(fp)[:2500]}") | |
| elif ext in ['.pdf']: | |
| f_contexts.append(f"\n[PDF BELGESİ: {Path(fp).name}]:\n{self.toolkit.read_pdf(fp)[:2500]}") | |
| elif ext in ['.png', '.jpg', '.jpeg']: | |
| f_contexts.append(f"\n[GÖRSEL / EVRAK: {Path(fp).name}]:\n{self.toolkit.inspect_image(fp)[:2500]}") | |
| combined = "\n".join(f_contexts) | |
| full_query = f"{combined}\n\nKullanıcı Talebi: {user_prompt.strip()}" | |
| res = self.generate(full_query, web_search=web_search, max_new_tokens=200) | |
| # Eğer kullanıcı açıkça Word raporu istediyse üret | |
| if any(w in user_prompt.lower() for w in ["word", ".docx", "rapor", "belge hazırla"]): | |
| doc_name = "Turkish1B_Cikti.docx" | |
| self.toolkit.create_docx(doc_name, title="Turkish-1B Yönetim Özeti", sections=[("Sonuç ve İnceleme", res["answer"])]) | |
| created_files.append(doc_name) | |
| return { | |
| "thought": res["thought"], | |
| "answer": res["answer"], | |
| "created_files": created_files | |
| } | |
| def stream(self, prompt, web_search=False, temperature=0.7): | |
| """Arayüzde canlı daktilo akışı için token üreticisi (Generator).""" | |
| p_str = f"### Kullanıcı:\n{prompt.strip()}\n\n### Asistan:\n<think>\n" | |
| gen = list(self.tokenizer.encode(p_str).ids) | |
| prev = self.tokenizer.decode(gen) | |
| with torch.no_grad(): | |
| for _ in range(384): | |
| curr = torch.tensor([gen[-self.cfg.max_seq_len:]], dtype=torch.long).to(self.device) | |
| nxt = torch.multinomial(F.softmax(self.model(curr)[0, -1, :] / temperature, dim=-1), 1).item() | |
| gen.append(nxt) | |
| cur_text = self.tokenizer.decode(gen) | |
| delta = cur_text[len(prev):] | |
| prev = cur_text | |
| yield delta | |
| if nxt == 2: | |
| break | |
| # ===================================================================== | |
| # 5. TEST & KULLANIM ÖRNEĞİ | |
| # ===================================================================== | |
| if __name__ == "__main__": | |
| print("🦁 Turkish-1B Agent Modülü Başarıyla Yüklendi!") | |