File size: 2,469 Bytes
71f8f13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import struct
import numpy as np
import random
import os

print("🛠️ Krynex 200K Parameters Testing")
print("="*40)

dataset_path = "/storage/emulated/0/Download/dataset.jsonl"
if not os.path.exists(dataset_path):
    dataset_path = "dataset.jsonl"

text_data = []
with open(dataset_path, "r", encoding="utf-8") as f:
    for line in f:
        data = json.loads(line)
        text_data.append(data.get("instruction", "") + " " + data.get("output", ""))

full_text = " ".join(text_data)
words = sorted(list(set(full_text.lower().split())))
t2i = {w: i for i, w in enumerate(words)}
i2t = {i: w for i, w in enumerate(words)}
vocab_size = len(t2i)
hidden_size = 64

tflite_path = "full_dataset_model.tflite"
if not os.path.exists(tflite_path):
    tflite_path = f"/storage/emulated/0/Download/{tflite_path}"

with open(tflite_path, "rb") as f:
    magic, offset = struct.unpack('<4sI', f.read(8))
    
    l1 = struct.unpack('<I', f.read(4))[0]
    W1 = np.frombuffer(f.read(l1), dtype=np.float32).reshape((hidden_size, vocab_size))
    
    l2 = struct.unpack('<I', f.read(4))[0]
    W2 = np.frombuffer(f.read(l2), dtype=np.float32).reshape((vocab_size, hidden_size))

b1 = np.zeros((hidden_size, 1), dtype=np.float32)
b2 = np.zeros((vocab_size, 1), dtype=np.float32)

seq_length = 12

def softmax(x):
    e = np.exp(x - np.max(x))
    return e / (e.sum() + 1e-8)

def gen(seed="hello", length=15):
    words_list = seed.lower().split()
    if not words_list:
        words_list = ["hello"]
    res = words_list.copy()
    
    for _ in range(length):
        ctx = res[-seq_length:] if len(res) >= seq_length else res
        ctx = ctx + [" "] * (seq_length - len(ctx))
        idx = [t2i.get(w, 0) for w in ctx]
        X = np.zeros((vocab_size, seq_length))
        X[idx, np.arange(seq_length)] = 1
        
        h = np.tanh(W1 @ X + b1)
        y_pred = softmax(W2 @ h + b2)
        probs = y_pred[:, -1].flatten()
        
        probs = probs / (np.sum(probs) + 1e-12)
        probs = np.clip(probs, 0, 1)
        probs = probs / (np.sum(probs) + 1e-12)
        
        next_idx = np.random.choice(vocab_size, p=probs)
        res.append(i2t.get(next_idx, "<unk>"))
    
    return " ".join(res)

print("💬 Chat ('exit' - stop)")
while True:
    u = input("\n👤 You: ").strip()
    if u.lower() in ['выход', 'exit', 'stop', 'стоп']:
        print("🤖 Goodbye!")
        break
    if u:
        print(f"\n🤖 AI: {gen(u, 15)}")