ciaochris commited on
Commit
ced4fa3
·
verified ·
1 Parent(s): cb60ea8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+
7
+ # Toy dataset
8
+ classes = ['tech', 'health']
9
+ keywords = {
10
+ 'tech': ['neural', 'AI', 'system', 'inference', 'hyperparameter', 'network'],
11
+ 'health': ['wellness', 'asbestos', 'hazmat', 'patient', 'health', 'therapy']
12
+ }
13
+
14
+ # Define a simple neural network
15
+ class SimpleNet(nn.Module):
16
+ def __init__(self, input_size, hidden_size, output_size):
17
+ super(SimpleNet, self).__init__()
18
+ self.fc1 = nn.Linear(input_size, hidden_size)
19
+ self.fc2 = nn.Linear(hidden_size, output_size)
20
+
21
+ def forward(self, x):
22
+ x = F.relu(self.fc1(x))
23
+ return self.fc2(x)
24
+
25
+ # Tokenizer and encoder (basic word count)
26
+ def encode(text):
27
+ vector = np.zeros(len(set(sum(keywords.values(), []))))
28
+ vocab = list(set(sum(keywords.values(), [])))
29
+ for word in text.lower().split():
30
+ if word in vocab:
31
+ vector[vocab.index(word)] += 1
32
+ return torch.tensor(vector, dtype=torch.float32)
33
+
34
+ # Initialize model
35
+ model = SimpleNet(input_size=12, hidden_size=6, output_size=2)
36
+ optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
37
+
38
+ def zero_shot_infer(text):
39
+ x = encode(text)
40
+ with torch.no_grad():
41
+ output = model(x)
42
+ predicted = torch.argmax(output).item()
43
+ return f"Predicted Category: {classes[predicted]}"
44
+
45
+ def train_step(text, label, lr, momentum):
46
+ optimizer.param_groups[0]['lr'] = lr
47
+ for g in optimizer.param_groups:
48
+ g['momentum'] = momentum
49
+ x = encode(text)
50
+ y = torch.tensor([classes.index(label)], dtype=torch.long)
51
+ output = model(x)
52
+ loss = F.cross_entropy(output.unsqueeze(0), y)
53
+ optimizer.zero_grad()
54
+ loss.backward()
55
+ optimizer.step()
56
+ return f"Loss after training step: {loss.item():.4f}"
57
+
58
+ # Gradio UI
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown("# LARS Playground: Simulating 1989 AI Innovation")
61
+
62
+ with gr.Row():
63
+ input_text = gr.Textbox(label="Input Text")
64
+ infer_btn = gr.Button("Zero-Shot Inference")
65
+ infer_out = gr.Textbox(label="Inference Result")
66
+
67
+ infer_btn.click(fn=zero_shot_infer, inputs=input_text, outputs=infer_out)
68
+
69
+ gr.Markdown("### Train the Model Manually")
70
+
71
+ with gr.Row():
72
+ train_text = gr.Textbox(label="Training Text")
73
+ train_label = gr.Radio(choices=classes, label="Label")
74
+ lr = gr.Slider(0.001, 0.1, value=0.01, label="Learning Rate")
75
+ momentum = gr.Slider(0.0, 0.9, value=0.0, label="Momentum")
76
+ train_btn = gr.Button("Run Training Step")
77
+ train_out = gr.Textbox(label="Training Output")
78
+
79
+ train_btn.click(fn=train_step, inputs=[train_text, train_label, lr, momentum], outputs=train_out)
80
+
81
+ demo.launch()