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. CONFIGURATION (Must match training!) ---
|
| 7 |
+
batch_size = 64
|
| 8 |
+
block_size = 64
|
| 9 |
+
n_embd = 128
|
| 10 |
+
n_head = 4
|
| 11 |
+
n_layer = 4
|
| 12 |
+
dropout = 0.2
|
| 13 |
+
device = 'cpu' # We use CPU for the web app so it's compatible everywhere
|
| 14 |
+
|
| 15 |
+
# --- 2. THE BRAIN CODE (Your Custom Architecture) ---
|
| 16 |
+
class Head(nn.Module):
|
| 17 |
+
def __init__(self, head_size):
|
| 18 |
+
super().__init__()
|
| 19 |
+
self.key = nn.Linear(n_embd, head_size, bias=False)
|
| 20 |
+
self.query = nn.Linear(n_embd, head_size, bias=False)
|
| 21 |
+
self.value = nn.Linear(n_embd, head_size, bias=False)
|
| 22 |
+
self.register_buffer('tril', torch.tril(torch.ones(block_size, block_size)))
|
| 23 |
+
self.dropout = nn.Dropout(dropout)
|
| 24 |
+
def forward(self, x):
|
| 25 |
+
B,T,C = x.shape
|
| 26 |
+
k = self.key(x)
|
| 27 |
+
q = self.query(x)
|
| 28 |
+
wei = q @ k.transpose(-2, -1) * C**-0.5
|
| 29 |
+
wei = wei.masked_fill(self.tril[:T, :T] == 0, float('-inf'))
|
| 30 |
+
wei = F.softmax(wei, dim=-1)
|
| 31 |
+
wei = self.dropout(wei)
|
| 32 |
+
v = self.value(x)
|
| 33 |
+
out = wei @ v
|
| 34 |
+
return out
|
| 35 |
+
|
| 36 |
+
class MultiHeadAttention(nn.Module):
|
| 37 |
+
def __init__(self, num_heads, head_size):
|
| 38 |
+
super().__init__()
|
| 39 |
+
self.heads = nn.ModuleList([Head(head_size) for _ in range(num_heads)])
|
| 40 |
+
self.proj = nn.Linear(n_embd, n_embd)
|
| 41 |
+
self.dropout = nn.Dropout(dropout)
|
| 42 |
+
def forward(self, x):
|
| 43 |
+
out = torch.cat([h(x) for h in self.heads], dim=-1)
|
| 44 |
+
out = self.proj(out)
|
| 45 |
+
return self.dropout(out)
|
| 46 |
+
|
| 47 |
+
class FeedFoward(nn.Module):
|
| 48 |
+
def __init__(self, n_embd):
|
| 49 |
+
super().__init__()
|
| 50 |
+
self.net = nn.Sequential(
|
| 51 |
+
nn.Linear(n_embd, 4 * n_embd),
|
| 52 |
+
nn.ReLU(),
|
| 53 |
+
nn.Linear(4 * n_embd, n_embd),
|
| 54 |
+
nn.Dropout(dropout),
|
| 55 |
+
)
|
| 56 |
+
def forward(self, x):
|
| 57 |
+
return self.net(x)
|
| 58 |
+
|
| 59 |
+
class Block(nn.Module):
|
| 60 |
+
def __init__(self, n_embd, n_head):
|
| 61 |
+
super().__init__()
|
| 62 |
+
head_size = n_embd // n_head
|
| 63 |
+
self.sa = MultiHeadAttention(n_head, head_size)
|
| 64 |
+
self.ffwd = FeedFoward(n_embd)
|
| 65 |
+
self.ln1 = nn.LayerNorm(n_embd)
|
| 66 |
+
self.ln2 = nn.LayerNorm(n_embd)
|
| 67 |
+
def forward(self, x):
|
| 68 |
+
x = x + self.sa(self.ln1(x))
|
| 69 |
+
x = x + self.ffwd(self.ln2(x))
|
| 70 |
+
return x
|
| 71 |
+
|
| 72 |
+
class GPTLanguageModel(nn.Module):
|
| 73 |
+
def __init__(self, vocab_size):
|
| 74 |
+
super().__init__()
|
| 75 |
+
self.token_embedding_table = nn.Embedding(vocab_size, n_embd)
|
| 76 |
+
self.position_embedding_table = nn.Embedding(block_size, n_embd)
|
| 77 |
+
self.blocks = nn.Sequential(*[Block(n_embd, n_head=n_head) for _ in range(n_layer)])
|
| 78 |
+
self.ln_f = nn.LayerNorm(n_embd)
|
| 79 |
+
self.lm_head = nn.Linear(n_embd, vocab_size)
|
| 80 |
+
def forward(self, idx, targets=None):
|
| 81 |
+
B, T = idx.shape
|
| 82 |
+
tok_emb = self.token_embedding_table(idx)
|
| 83 |
+
pos_emb = self.position_embedding_table(torch.arange(T, device=device))
|
| 84 |
+
x = tok_emb + pos_emb
|
| 85 |
+
x = self.blocks(x)
|
| 86 |
+
x = self.ln_f(x)
|
| 87 |
+
logits = self.lm_head(x)
|
| 88 |
+
return logits, None
|
| 89 |
+
def generate(self, idx, max_new_tokens):
|
| 90 |
+
for _ in range(max_new_tokens):
|
| 91 |
+
idx_cond = idx[:, -block_size:]
|
| 92 |
+
logits, _ = self(idx_cond)
|
| 93 |
+
logits = logits[:, -1, :]
|
| 94 |
+
probs = F.softmax(logits, dim=-1)
|
| 95 |
+
idx_next = torch.multinomial(probs, num_samples=1)
|
| 96 |
+
idx = torch.cat((idx, idx_next), dim=1)
|
| 97 |
+
return idx
|
| 98 |
+
|
| 99 |
+
# --- 3. LOAD RESOURCES ---
|
| 100 |
+
print("Loading model and vocabulary...")
|
| 101 |
+
# Load text to rebuild tokenizer
|
| 102 |
+
with open('input.txt', 'r', encoding='utf-8') as f:
|
| 103 |
+
text = f.read()
|
| 104 |
+
chars = sorted(list(set(text)))
|
| 105 |
+
vocab_size = len(chars)
|
| 106 |
+
stoi = { ch:i for i,ch in enumerate(chars) }
|
| 107 |
+
itos = { i:ch for i,ch in enumerate(chars) }
|
| 108 |
+
encode = lambda s: [stoi[c] for c in s]
|
| 109 |
+
decode = lambda l: ''.join([itos[i] for i in l])
|
| 110 |
+
|
| 111 |
+
# Load Model
|
| 112 |
+
model = GPTLanguageModel(vocab_size)
|
| 113 |
+
model.load_state_dict(torch.load('model.pt', map_location=device))
|
| 114 |
+
model.to(device)
|
| 115 |
+
model.eval()
|
| 116 |
+
|
| 117 |
+
# --- 4. DEFINE THE WEB FUNCTION ---
|
| 118 |
+
def generate_text(start_text):
|
| 119 |
+
if not start_text:
|
| 120 |
+
return "Please type something to start!"
|
| 121 |
+
|
| 122 |
+
try:
|
| 123 |
+
# Convert text to numbers
|
| 124 |
+
context = torch.tensor([encode(start_text)], dtype=torch.long, device=device)
|
| 125 |
+
|
| 126 |
+
# Ask AI to predict next 200 characters
|
| 127 |
+
output_ids = model.generate(context, max_new_tokens=200)
|
| 128 |
+
|
| 129 |
+
# Convert numbers back to text
|
| 130 |
+
full_response = decode(output_ids[0].tolist())
|
| 131 |
+
return full_response
|
| 132 |
+
except KeyError:
|
| 133 |
+
return "Error: You used a character the AI has never seen before."
|
| 134 |
+
|
| 135 |
+
# --- 5. LAUNCH THE INTERFACE ---
|
| 136 |
+
print("Launching Web App...")
|
| 137 |
+
interface = gr.Interface(
|
| 138 |
+
fn=generate_text,
|
| 139 |
+
inputs=gr.Textbox(lines=2, placeholder="Type a starting word (e.g. 'Nano')..."),
|
| 140 |
+
outputs="text",
|
| 141 |
+
title="My Private AI",
|
| 142 |
+
description="An AI model trained from scratch on my own data."
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
interface.launch()
|
input.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Nanotechnology is the manipulation of matter with at least one dimension sized from 1 to 100 nanometers (nm). At this scale, commonly known as the nanoscale, surface area and quantum mechanical effects become important in describing properties of matter. This definition of nanotechnology includes all types of research and technologies that deal with these special properties. It is common to see the plural form "nanotechnologies" as well as "nanoscale technologies" to refer to research and applications whose common trait is scale.[1] An earlier understanding of nanotechnology referred to the particular technological goal of precisely manipulating atoms and molecules for fabricating macroscale products, now referred to as molecular nanotechnology.[2]
|
| 2 |
+
|
| 3 |
+
Nanotechnology defined by scale includes fields of science such as surface science, organic chemistry, molecular biology, semiconductor physics, energy storage,[3][4] engineering,[5] microfabrication,[6] and molecular engineering.[7] The associated research and applications range from extensions of conventional device physics to molecular self-assembly,[8] from developing new materials with dimensions on the nanoscale to direct control of matter on the atomic scale.
|
| 4 |
+
|
| 5 |
+
Nanotechnology may be able to create new materials and devices with diverse applications, such as in nanomedicine, nanoelectronics, agricultural sectors,[citation needed] biomaterials energy production, and consumer products. However, nanotechnology raises issues, including concerns about the toxicity and environmental impact of nanomaterials,[9] and their potential effects on global economics, as well as various doomsday scenarios. These concerns have led to a debate among advocacy groups and governments on whether special regulation of nanotechnology is warranted.
|
model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7dcf3fb7f85b7a5e6d4371b7654ede0503d4cf789f46d11b1dd4a89e19819923
|
| 3 |
+
size 3546043
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
gradio
|