Update app.py
Browse files
app.py
CHANGED
|
@@ -1,128 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
# =====
|
| 5 |
with open("dataset.txt", "r", encoding="utf-8") as f:
|
| 6 |
text = f.read().lower()
|
| 7 |
|
| 8 |
chars = sorted(list(set(text)))
|
| 9 |
vocab_size = len(chars)
|
| 10 |
-
stoi = {ch:
|
| 11 |
-
itos = {i:
|
| 12 |
|
| 13 |
-
def encode(s): return [stoi
|
| 14 |
def decode(l): return "".join([itos[i] for i in l])
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def
|
| 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 |
-
inputs=[seed_input, length_input, temp_input],
|
| 122 |
-
outputs=output_text)
|
| 123 |
-
|
| 124 |
-
train_button.click(fn=train_model,
|
| 125 |
-
inputs=epoch_slider,
|
| 126 |
-
outputs=train_output)
|
| 127 |
-
|
| 128 |
-
demo.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
import numpy as np
|
| 5 |
+
import math
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
# ===== DATASET =====
|
| 9 |
with open("dataset.txt", "r", encoding="utf-8") as f:
|
| 10 |
text = f.read().lower()
|
| 11 |
|
| 12 |
chars = sorted(list(set(text)))
|
| 13 |
vocab_size = len(chars)
|
| 14 |
+
stoi = {ch:i for i,ch in enumerate(chars)}
|
| 15 |
+
itos = {i:ch for i,ch in enumerate(chars)}
|
| 16 |
|
| 17 |
+
def encode(s): return [stoi.get(c, 0) for c in s]
|
| 18 |
def decode(l): return "".join([itos[i] for i in l])
|
| 19 |
|
| 20 |
+
# ===== GPT-Style Transformer Decoder =====
|
| 21 |
+
class GPTBlock(nn.Module):
|
| 22 |
+
def __init__(self, d_model, nhead, dim_feedforward, dropout):
|
| 23 |
+
super().__init__()
|
| 24 |
+
self.attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout)
|
| 25 |
+
self.ff = nn.Sequential(
|
| 26 |
+
nn.Linear(d_model, dim_feedforward),
|
| 27 |
+
nn.GELU(),
|
| 28 |
+
nn.Linear(dim_feedforward, d_model),
|
| 29 |
+
nn.Dropout(dropout),
|
| 30 |
+
)
|
| 31 |
+
self.ln1 = nn.LayerNorm(d_model)
|
| 32 |
+
self.ln2 = nn.LayerNorm(d_model)
|
| 33 |
+
|
| 34 |
+
def forward(self, x, mask=None):
|
| 35 |
+
attn_out, _ = self.attn(x, x, x, attn_mask=mask)
|
| 36 |
+
x = self.ln1(x + attn_out)
|
| 37 |
+
ff_out = self.ff(x)
|
| 38 |
+
x = self.ln2(x + ff_out)
|
| 39 |
+
return x
|
| 40 |
+
|
| 41 |
+
class GPTModel(nn.Module):
|
| 42 |
+
def __init__(self, vocab_size, d_model=128, nhead=8, num_layers=4, dim_feedforward=512, max_len=5000, dropout=0.1):
|
| 43 |
+
super().__init__()
|
| 44 |
+
self.token_emb = nn.Embedding(vocab_size, d_model)
|
| 45 |
+
self.pos_emb = nn.Parameter(torch.zeros(1, max_len, d_model))
|
| 46 |
+
self.blocks = nn.ModuleList([GPTBlock(d_model, nhead, dim_feedforward, dropout) for _ in range(num_layers)])
|
| 47 |
+
self.ln_f = nn.LayerNorm(d_model)
|
| 48 |
+
self.head = nn.Linear(d_model, vocab_size)
|
| 49 |
+
|
| 50 |
+
def forward(self, x):
|
| 51 |
+
seq_len = x.size(1)
|
| 52 |
+
token_embeddings = self.token_emb(x) # (batch, seq_len, d_model)
|
| 53 |
+
pos_embeddings = self.pos_emb[:, :seq_len, :] # (1, seq_len, d_model)
|
| 54 |
+
x = token_embeddings + pos_embeddings
|
| 55 |
+
x = x.transpose(0, 1) # for MultiheadAttention: (seq_len, batch, d_model)
|
| 56 |
+
|
| 57 |
+
# causal mask (upper triangular)
|
| 58 |
+
mask = torch.triu(torch.ones(seq_len, seq_len) * float('-inf'), diagonal=1).to(x.device)
|
| 59 |
+
|
| 60 |
+
for block in self.blocks:
|
| 61 |
+
x = block(x, mask)
|
| 62 |
+
|
| 63 |
+
x = x.transpose(0, 1) # back to (batch, seq_len, d_model)
|
| 64 |
+
x = self.ln_f(x)
|
| 65 |
+
logits = self.head(x) # (batch, seq_len, vocab_size)
|
| 66 |
+
return logits
|
| 67 |
+
|
| 68 |
+
# ===== TRAINING =====
|
| 69 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 70 |
+
model = GPTModel(vocab_size).to(device)
|
| 71 |
+
optimizer = torch.optim.Adam(model.parameters(), lr=0.005)
|
| 72 |
+
criterion = nn.CrossEntropyLoss()
|
| 73 |
+
|
| 74 |
+
seq_len = 25
|
| 75 |
+
batch_size = 1
|
| 76 |
+
epochs = 300
|
| 77 |
+
|
| 78 |
+
data_tensor = torch.tensor(encode(text), dtype=torch.long)
|
| 79 |
+
|
| 80 |
+
for epoch in range(epochs):
|
| 81 |
+
model.train()
|
| 82 |
+
idx = np.random.randint(0, len(data_tensor) - seq_len - 1)
|
| 83 |
+
chunk = data_tensor[idx:idx+seq_len+1].unsqueeze(0).to(device) # (1, seq_len+1)
|
| 84 |
+
input_seq = chunk[:, :-1]
|
| 85 |
+
target_seq = chunk[:, 1:]
|
| 86 |
+
|
| 87 |
+
optimizer.zero_grad()
|
| 88 |
+
logits = model(input_seq)
|
| 89 |
+
loss = criterion(logits.view(-1, vocab_size), target_seq.view(-1))
|
| 90 |
+
loss.backward()
|
| 91 |
+
optimizer.step()
|
| 92 |
+
|
| 93 |
+
if epoch % 50 == 0:
|
| 94 |
+
print(f"Epoch {epoch}, Loss: {loss.item():.4f}")
|
| 95 |
+
|
| 96 |
+
# ===== TEXT GENERATION =====
|
| 97 |
+
def generate_text(model, seed, max_len=100):
|
| 98 |
+
model.eval()
|
| 99 |
+
input_ids = torch.tensor(encode(seed), dtype=torch.long).unsqueeze(0).to(device)
|
| 100 |
+
generated = seed
|
| 101 |
+
|
| 102 |
+
with torch.no_grad():
|
| 103 |
+
for _ in range(max_len):
|
| 104 |
+
logits = model(input_ids)
|
| 105 |
+
probs = F.softmax(logits[0, -1], dim=-1).cpu().numpy()
|
| 106 |
+
next_id = np.random.choice(len(probs), p=probs)
|
| 107 |
+
generated += itos[next_id]
|
| 108 |
+
next_token = torch.tensor([[next_id]], device=device)
|
| 109 |
+
input_ids = torch.cat([input_ids, next_token], dim=1)
|
| 110 |
+
|
| 111 |
+
return generated
|
| 112 |
+
|
| 113 |
+
# ===== GRADIO CHAT =====
|
| 114 |
+
def chat_with_ai(inp):
|
| 115 |
+
return generate_text(model, inp, max_len=100)[len(inp):]
|
| 116 |
+
|
| 117 |
+
import gradio as gr
|
| 118 |
+
iface = gr.Interface(fn=chat_with_ai,
|
| 119 |
+
inputs=gr.Textbox(lines=1, placeholder="Ketik chat kamu..."),
|
| 120 |
+
outputs="text",
|
| 121 |
+
title="Chat AI Transformer GPT Style",
|
| 122 |
+
description="Chat AI pake model Transformer GPT-style sederhana")
|
| 123 |
+
|
| 124 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|