Datasets:
Download model example/python scripts/script.py from KrynexLabs/KrynexAI-Dataset-Flash-Instruction: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/datasets/KrynexLabs/KrynexAI-Dataset-Flash-Instruction/resolve/main/model%20example/python%20scripts/script.py
- Command line
-
hf download 'hf://datasets/KrynexLabs/KrynexAI-Dataset-Flash-Instruction/model example/python scripts/script.py'
-
curl -L -o script.py https://huggingface.co/datasets/KrynexLabs/KrynexAI-Dataset-Flash-Instruction/resolve/main/model%20example/python%20scripts/script.py
2.47 kB
| 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)}") |