Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,35 +6,42 @@ import torch.optim as optim
|
|
| 6 |
class TinyTextAI(nn.Module):
|
| 7 |
def __init__(self, vocab_size, num_classes):
|
| 8 |
super(TinyTextAI, self).__init__()
|
| 9 |
-
self.embedding = nn.EmbeddingBag(vocab_size,
|
| 10 |
-
self.
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def forward(self, text, offsets):
|
| 13 |
embedded = self.embedding(text, offsets)
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
data = {
|
| 17 |
-
"hello": 0,
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
-
responses = {0: "Hello there!", 1: "I am doing great, thank you!", 2: "I am a Tiny AI built by you."}
|
| 26 |
|
| 27 |
vocab = {word: i for i, word in enumerate(set(" ".join(data.keys()).split()))}
|
|
|
|
| 28 |
vocab_size = len(vocab)
|
| 29 |
num_classes = len(responses)
|
| 30 |
|
| 31 |
model = TinyTextAI(vocab_size, num_classes)
|
| 32 |
-
optimizer = optim.Adam(model.parameters(), lr=0.
|
| 33 |
criterion = nn.CrossEntropyLoss()
|
| 34 |
|
| 35 |
def prepare_text(text):
|
| 36 |
-
tokens = [vocab
|
| 37 |
-
if not tokens: return torch.tensor([
|
| 38 |
return torch.tensor(tokens, dtype=torch.int64), torch.tensor([0], dtype=torch.int64)
|
| 39 |
|
| 40 |
def train_model(epochs):
|
|
@@ -51,7 +58,7 @@ def train_model(epochs):
|
|
| 51 |
loss.backward()
|
| 52 |
optimizer.step()
|
| 53 |
total_loss += loss.item()
|
| 54 |
-
if epoch %
|
| 55 |
log.append(f"Epoch {epoch} - Error: {total_loss:.4f}")
|
| 56 |
return "\n".join(log)
|
| 57 |
|
|
@@ -64,16 +71,16 @@ def chat(user_input):
|
|
| 64 |
return responses[prediction]
|
| 65 |
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
-
gr.Markdown("#
|
| 68 |
with gr.Row():
|
| 69 |
-
epochs = gr.Number(label="Training Rounds", value=
|
| 70 |
-
btn_train = gr.Button("Train AI")
|
| 71 |
-
status = gr.Textbox(label="
|
| 72 |
-
chat_input = gr.Textbox(label="
|
| 73 |
btn_chat = gr.Button("Send")
|
| 74 |
-
chat_output = gr.Textbox(label="
|
| 75 |
|
| 76 |
btn_train.click(train_model, inputs=epochs, outputs=status)
|
| 77 |
btn_chat.click(chat, inputs=chat_input, outputs=chat_output)
|
| 78 |
|
| 79 |
-
demo.launch()
|
|
|
|
| 6 |
class TinyTextAI(nn.Module):
|
| 7 |
def __init__(self, vocab_size, num_classes):
|
| 8 |
super(TinyTextAI, self).__init__()
|
| 9 |
+
self.embedding = nn.EmbeddingBag(vocab_size, 16, sparse=False)
|
| 10 |
+
self.fc1 = nn.Linear(16, 12)
|
| 11 |
+
self.relu = nn.ReLU()
|
| 12 |
+
self.fc2 = nn.Linear(12, num_classes)
|
| 13 |
|
| 14 |
def forward(self, text, offsets):
|
| 15 |
embedded = self.embedding(text, offsets)
|
| 16 |
+
x = self.relu(self.fc1(embedded))
|
| 17 |
+
return self.fc2(x)
|
| 18 |
|
| 19 |
data = {
|
| 20 |
+
"hello": 0, "hi": 0, "hey": 0,
|
| 21 |
+
"how are you": 1, "status": 1, "up": 1,
|
| 22 |
+
"name": 2, "who": 2, "identity": 2,
|
| 23 |
+
"bye": 3, "goodbye": 3, "exit": 3
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
responses = {
|
| 27 |
+
0: "Hello! I am feeling smarter now.",
|
| 28 |
+
1: "Systems are nominal. My parameters are tuned!",
|
| 29 |
+
2: "I am a Version 2 Tiny AI.",
|
| 30 |
+
3: "Goodbye! Come back to train me more soon."
|
| 31 |
}
|
|
|
|
| 32 |
|
| 33 |
vocab = {word: i for i, word in enumerate(set(" ".join(data.keys()).split()))}
|
| 34 |
+
vocab["<UNK>"] = len(vocab)
|
| 35 |
vocab_size = len(vocab)
|
| 36 |
num_classes = len(responses)
|
| 37 |
|
| 38 |
model = TinyTextAI(vocab_size, num_classes)
|
| 39 |
+
optimizer = optim.Adam(model.parameters(), lr=0.05)
|
| 40 |
criterion = nn.CrossEntropyLoss()
|
| 41 |
|
| 42 |
def prepare_text(text):
|
| 43 |
+
tokens = [vocab.get(w, vocab["<UNK>"]) for w in text.lower().split()]
|
| 44 |
+
if not tokens: return torch.tensor([vocab["<UNK>"]]), torch.tensor([0])
|
| 45 |
return torch.tensor(tokens, dtype=torch.int64), torch.tensor([0], dtype=torch.int64)
|
| 46 |
|
| 47 |
def train_model(epochs):
|
|
|
|
| 58 |
loss.backward()
|
| 59 |
optimizer.step()
|
| 60 |
total_loss += loss.item()
|
| 61 |
+
if epoch % 20 == 0:
|
| 62 |
log.append(f"Epoch {epoch} - Error: {total_loss:.4f}")
|
| 63 |
return "\n".join(log)
|
| 64 |
|
|
|
|
| 71 |
return responses[prediction]
|
| 72 |
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("# 🚀 Tiny AI v2: The Hidden Layer")
|
| 75 |
with gr.Row():
|
| 76 |
+
epochs = gr.Number(label="Training Rounds", value=200)
|
| 77 |
+
btn_train = gr.Button("Re-Train AI")
|
| 78 |
+
status = gr.Textbox(label="Neural Progress")
|
| 79 |
+
chat_input = gr.Textbox(label="Talk to the AI")
|
| 80 |
btn_chat = gr.Button("Send")
|
| 81 |
+
chat_output = gr.Textbox(label="Response")
|
| 82 |
|
| 83 |
btn_train.click(train_model, inputs=epochs, outputs=status)
|
| 84 |
btn_chat.click(chat, inputs=chat_input, outputs=chat_output)
|
| 85 |
|
| 86 |
+
demo.launch()
|