File size: 22,786 Bytes
29e7459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b2ad57
29e7459
 
2b2ad57
29e7459
2b2ad57
 
 
 
29e7459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d1c1d
 
29e7459
 
 
 
 
 
 
 
 
 
 
 
 
61d1c1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29e7459
61d1c1d
 
 
29e7459
61d1c1d
 
 
 
 
 
29e7459
 
 
61d1c1d
 
29e7459
 
 
 
 
 
 
61d1c1d
 
 
 
29e7459
 
 
 
 
61d1c1d
54702e4
29e7459
 
 
54702e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29e7459
61d1c1d
 
54702e4
61d1c1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54702e4
61d1c1d
 
 
 
 
 
29e7459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# -*- 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"""

    @staticmethod
    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}"

    @staticmethod
    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}"

    @staticmethod
    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}"

    @staticmethod
    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}"

    @staticmethod
    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}"

    @staticmethod
    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}"

    @staticmethod
    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}"

    @staticmethod
    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!")