vortexa64 commited on
Commit
c84502c
·
verified ·
1 Parent(s): abd5518

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+
4
+ # ======== LOAD DATASET ========
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:i for i,ch in enumerate(chars)}
11
+ itos = {i:ch for i,ch in enumerate(chars)}
12
+
13
+ def encode(s): return [stoi[c] for c in s]
14
+ def decode(l): return "".join([itos[i] for i in l])
15
+
16
+ data = np.array(encode(text), dtype=np.int32)
17
+
18
+ # ======== MODEL SETUP ========
19
+ n_hidden = 64
20
+ Wxh = np.random.randn(n_hidden, vocab_size) * 0.01
21
+ Whh = np.random.randn(n_hidden, n_hidden) * 0.01
22
+ Why = np.random.randn(vocab_size, n_hidden) * 0.01
23
+ bh = np.zeros((n_hidden, 1))
24
+ by = np.zeros((vocab_size, 1))
25
+
26
+ def softmax(x):
27
+ e = np.exp(x - np.max(x))
28
+ return e / np.sum(e)
29
+
30
+ # ======== TRAINING (1x buat demo, simple) ========
31
+ lr = 1e-1
32
+ seq_len = 25
33
+ for epoch in range(1): # ⚠️ Cuma 1 epoch biar cepat
34
+ idx = np.random.randint(0, len(data)-seq_len-1)
35
+ inputs = data[idx:idx+seq_len]
36
+ targets = data[idx+1:idx+seq_len+1]
37
+
38
+ hs = {-1: np.zeros((n_hidden,1))}
39
+ loss = 0
40
+ xs, ys, ps = {}, {}, {}
41
+ for t in range(seq_len):
42
+ xs[t] = np.zeros((vocab_size,1))
43
+ xs[t][inputs[t]] = 1
44
+ hs[t] = np.tanh(np.dot(Wxh, xs[t]) + np.dot(Whh, hs[t-1]) + bh)
45
+ ys[t] = np.dot(Why, hs[t]) + by
46
+ ps[t] = softmax(ys[t])
47
+ loss += -np.log(ps[t][targets[t],0])
48
+
49
+ # Backward
50
+ dWxh, dWhh, dWhy = np.zeros_like(Wxh), np.zeros_like(Whh), np.zeros_like(Why)
51
+ dbh, dby = np.zeros_like(bh), np.zeros_like(by)
52
+ dhnext = np.zeros_like(hs[0])
53
+ for t in reversed(range(seq_len)):
54
+ dy = np.copy(ps[t])
55
+ dy[targets[t]] -= 1
56
+ dWhy += np.dot(dy, hs[t].T)
57
+ dby += dy
58
+ dh = np.dot(Why.T, dy) + dhnext
59
+ dhraw = (1 - hs[t] * hs[t]) * dh
60
+ dbh += dhraw
61
+ dWxh += np.dot(dhraw, xs[t].T)
62
+ dWhh += np.dot(dhraw, hs[t-1].T)
63
+ dhnext = np.dot(Whh.T, dhraw)
64
+
65
+ for param, dparam in zip([Wxh, Whh, Why, bh, by],
66
+ [dWxh, dWhh, dWhy, dbh, dby]):
67
+ param -= lr * dparam
68
+
69
+ # ======== GENERATE FUNCTION ========
70
+ def generate(seed, length):
71
+ h = np.zeros((n_hidden,1))
72
+ x = np.zeros((vocab_size,1))
73
+ for c in seed:
74
+ if c not in stoi:
75
+ continue
76
+ x = np.zeros((vocab_size,1))
77
+ x[stoi[c]] = 1
78
+ h = np.tanh(np.dot(Wxh, x) + np.dot(Whh, h) + bh)
79
+
80
+ out = seed
81
+ for _ in range(length):
82
+ y = np.dot(Why, h) + by
83
+ p = softmax(y)
84
+ ix = np.random.choice(range(vocab_size), p=p.ravel())
85
+ x = np.zeros((vocab_size,1))
86
+ x[ix] = 1
87
+ h = np.tanh(np.dot(Wxh, x) + np.dot(Whh, h) + bh)
88
+ out += itos[ix]
89
+ return out
90
+
91
+ # ======== GRADIO INTERFACE ========
92
+ gr.Interface(fn=generate,
93
+ inputs=[
94
+ gr.Textbox(label="Seed Text", value="halo "),
95
+ gr.Slider(20, 200, value=100, label="Generate Length")
96
+ ],
97
+ outputs="text",
98
+ title="Character-Level Text Generator (RNN)"
99
+ ).launch()