Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,200 +1,14 @@
|
|
1 |
-
import os
|
2 |
-
import torch
|
3 |
-
import torch.nn as nn
|
4 |
-
import torch.optim as optim
|
5 |
-
from torch.utils.data import DataLoader, Dataset
|
6 |
-
from transformers import AutoTokenizer, get_scheduler
|
7 |
import gradio as gr
|
8 |
-
|
9 |
-
import numpy as np
|
10 |
-
import pandas as pd
|
11 |
-
import io
|
12 |
-
from PIL import Image
|
13 |
-
import openai
|
14 |
-
import time
|
15 |
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
dataset = {
|
24 |
-
"train": pd.read_json("https://raw.githubusercontent.com/epfml/Deep_Learning_Projects/master/PIQA/data/train.jsonl", lines=True),
|
25 |
-
"validation": pd.read_json("https://raw.githubusercontent.com/epfml/Deep_Learning_Projects/master/PIQA/data/valid.jsonl", lines=True)
|
26 |
-
}
|
27 |
-
|
28 |
-
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
29 |
-
|
30 |
-
# β
Tokenization helper
|
31 |
-
def tokenize_choices(example):
|
32 |
-
input_0 = tokenizer(example["goal"] + " " + example["sol1"], truncation=True, padding="max_length", max_length=128, return_tensors="pt")
|
33 |
-
input_1 = tokenizer(example["goal"] + " " + example["sol2"], truncation=True, padding="max_length", max_length=128, return_tensors="pt")
|
34 |
-
return {
|
35 |
-
"input_ids_0": input_0["input_ids"][0],
|
36 |
-
"input_ids_1": input_1["input_ids"][0],
|
37 |
-
"label": int(example["label"])
|
38 |
-
}
|
39 |
-
|
40 |
-
train_data = [tokenize_choices(row) for _, row in dataset["train"].head(500).iterrows()]
|
41 |
-
val_data = [tokenize_choices(row) for _, row in dataset["validation"].head(200).iterrows()]
|
42 |
-
|
43 |
-
# β
Dataset class
|
44 |
-
class PIQADataset(Dataset):
|
45 |
-
def __init__(self, data):
|
46 |
-
self.data = data
|
47 |
-
def __len__(self):
|
48 |
-
return len(self.data)
|
49 |
-
def __getitem__(self, idx):
|
50 |
-
return {
|
51 |
-
"input_ids_0": self.data[idx]["input_ids_0"],
|
52 |
-
"input_ids_1": self.data[idx]["input_ids_1"],
|
53 |
-
"label": torch.tensor(self.data[idx]["label"])
|
54 |
-
}
|
55 |
-
|
56 |
-
train_dataset = PIQADataset(train_data)
|
57 |
-
val_dataset = PIQADataset(val_data)
|
58 |
-
|
59 |
-
# β
EvoTransformer definition
|
60 |
-
class EvoTransformer(nn.Module):
|
61 |
-
def __init__(self):
|
62 |
-
super().__init__()
|
63 |
-
self.embedding = nn.Embedding(30522, 384)
|
64 |
-
encoder_layer = nn.TransformerEncoderLayer(d_model=384, nhead=6, dim_feedforward=1024, batch_first=True)
|
65 |
-
self.encoder = nn.TransformerEncoder(encoder_layer, num_layers=6)
|
66 |
-
self.classifier = nn.Sequential(
|
67 |
-
nn.Linear(384, 128),
|
68 |
-
nn.ReLU(),
|
69 |
-
nn.Linear(128, 1)
|
70 |
-
)
|
71 |
-
|
72 |
-
def forward(self, input_ids):
|
73 |
-
x = self.embedding(input_ids)
|
74 |
-
x = self.encoder(x)
|
75 |
-
return self.classifier(x[:, 0, :]).squeeze(-1)
|
76 |
-
|
77 |
-
# β
GPT-3.5 logic
|
78 |
-
def gpt35_answer(prompt):
|
79 |
-
try:
|
80 |
-
response = openai.ChatCompletion.create(
|
81 |
-
model="gpt-3.5-turbo",
|
82 |
-
messages=[{"role": "user", "content": prompt}],
|
83 |
-
max_tokens=20,
|
84 |
-
temperature=0
|
85 |
-
)
|
86 |
-
return response['choices'][0]['message']['content'].strip()
|
87 |
-
except Exception as e:
|
88 |
-
return f"[Error: {e}]"
|
89 |
-
|
90 |
-
# β
Main train + compare function
|
91 |
-
def train_and_demo(few_shot_size):
|
92 |
-
start_time = time.time()
|
93 |
-
model = EvoTransformer().to(device)
|
94 |
-
criterion = nn.CrossEntropyLoss()
|
95 |
-
optimizer = optim.AdamW(model.parameters(), lr=5e-5)
|
96 |
-
|
97 |
-
loader = DataLoader(train_dataset[:few_shot_size], batch_size=8, shuffle=True)
|
98 |
-
val_loader = DataLoader(val_dataset, batch_size=32)
|
99 |
-
|
100 |
-
scheduler = get_scheduler("linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=3 * len(loader))
|
101 |
-
|
102 |
-
best_val = 0
|
103 |
-
accs = []
|
104 |
-
patience = 2
|
105 |
-
early_stop = 0
|
106 |
-
|
107 |
-
for epoch in range(3):
|
108 |
-
model.train()
|
109 |
-
for batch in loader:
|
110 |
-
optimizer.zero_grad()
|
111 |
-
x0 = batch["input_ids_0"].to(device)
|
112 |
-
x1 = batch["input_ids_1"].to(device)
|
113 |
-
labels = batch["label"].to(device)
|
114 |
-
l0 = model(x0)
|
115 |
-
l1 = model(x1)
|
116 |
-
logits = torch.stack([l0, l1], dim=1)
|
117 |
-
loss = criterion(logits, labels)
|
118 |
-
loss.backward()
|
119 |
-
optimizer.step()
|
120 |
-
scheduler.step()
|
121 |
-
|
122 |
-
model.eval()
|
123 |
-
correct = 0
|
124 |
-
with torch.no_grad():
|
125 |
-
for batch in val_loader:
|
126 |
-
x0 = batch["input_ids_0"].to(device)
|
127 |
-
x1 = batch["input_ids_1"].to(device)
|
128 |
-
labels = batch["label"].to(device)
|
129 |
-
l0 = model(x0)
|
130 |
-
l1 = model(x1)
|
131 |
-
logits = torch.stack([l0, l1], dim=1)
|
132 |
-
preds = torch.argmax(logits, dim=1)
|
133 |
-
correct += (preds == labels).sum().item()
|
134 |
-
acc = correct / len(val_dataset)
|
135 |
-
accs.append(acc)
|
136 |
-
if acc > best_val:
|
137 |
-
best_val = acc
|
138 |
-
early_stop = 0
|
139 |
-
else:
|
140 |
-
early_stop += 1
|
141 |
-
if early_stop >= patience:
|
142 |
-
break
|
143 |
-
|
144 |
-
# β
Accuracy plot
|
145 |
-
fig, ax = plt.subplots()
|
146 |
-
ax.plot(accs, marker='o')
|
147 |
-
ax.set_title(f"Validation Accuracy ({few_shot_size} examples)")
|
148 |
-
ax.set_xlabel("Epoch")
|
149 |
-
ax.set_ylabel("Accuracy")
|
150 |
-
buf = io.BytesIO()
|
151 |
-
plt.savefig(buf, format='png')
|
152 |
-
buf.seek(0)
|
153 |
-
img = Image.open(buf)
|
154 |
-
|
155 |
-
# β
Example comparison with GPT-3.5
|
156 |
-
output = ""
|
157 |
-
for i in range(2):
|
158 |
-
ex = dataset["validation"].iloc[i]
|
159 |
-
goal = ex["goal"]
|
160 |
-
sol1 = ex["sol1"]
|
161 |
-
sol2 = ex["sol2"]
|
162 |
-
|
163 |
-
x0 = tokenizer(goal + " " + sol1, return_tensors="pt", padding="max_length", max_length=128, truncation=True)["input_ids"].to(device)
|
164 |
-
x1 = tokenizer(goal + " " + sol2, return_tensors="pt", padding="max_length", max_length=128, truncation=True)["input_ids"].to(device)
|
165 |
-
l0 = model(x0)
|
166 |
-
l1 = model(x1)
|
167 |
-
pred_evo = 0 if l0 > l1 else 1
|
168 |
-
correct_evo = "β
" if pred_evo == ex["label"] else "β"
|
169 |
-
|
170 |
-
gpt_prompt = f"Q: {goal}\nA) {sol1}\nB) {sol2}\nWhich is more appropriate? Answer with A or B only."
|
171 |
-
gpt_out = gpt35_answer(gpt_prompt)
|
172 |
-
pred_gpt = gpt_out[0].upper()
|
173 |
-
correct_gpt = "β
" if (pred_gpt == 'A' and ex["label"] == 0) or (pred_gpt == 'B' and ex["label"] == 1) else "β"
|
174 |
-
|
175 |
-
output += f"Q: {goal}\nA) {sol1}\nB) {sol2}\n\nEvoTransformer: {'A' if pred_evo==0 else 'B'} {correct_evo}\nGPT-3.5: {pred_gpt} {correct_gpt}\n\n"
|
176 |
-
|
177 |
-
architecture_info = f"""
|
178 |
-
EvoTransformer v2.1 Configuration:
|
179 |
-
- Embedding Dim: 384
|
180 |
-
- Transformer Layers: 6
|
181 |
-
- Attention Heads: 6
|
182 |
-
- Feedforward Size: 1024
|
183 |
-
- Parameters: ~13M
|
184 |
-
- Training Time: {time.time() - start_time:.2f}s
|
185 |
-
"""
|
186 |
-
|
187 |
-
return img, f"Best Accuracy: {best_val:.4f}", output.strip() + "\n\n" + architecture_info.strip()
|
188 |
-
|
189 |
-
# β
Gradio app
|
190 |
-
gr.Interface(
|
191 |
-
fn=train_and_demo,
|
192 |
-
inputs=gr.Slider(10, 300, step=10, value=50, label="Training Samples"),
|
193 |
-
outputs=[
|
194 |
-
gr.Image(label="Accuracy Plot"),
|
195 |
-
gr.Textbox(label="Best Accuracy"),
|
196 |
-
gr.Textbox(label="Evo vs GPT-3.5 Output")
|
197 |
-
],
|
198 |
-
title="𧬠EvoTransformer v2.1 Benchmark",
|
199 |
-
description="Train EvoTransformer on PIQA and compare predictions against GPT-3.5."
|
200 |
-
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from inference import predict
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
def compare(goal, sol1, sol2):
|
5 |
+
return predict(goal, sol1, sol2)
|
6 |
|
7 |
+
demo = gr.Interface(
|
8 |
+
fn=compare,
|
9 |
+
inputs=[gr.Text(label="Goal"), gr.Text(label="Solution 1"), gr.Text(label="Solution 2")],
|
10 |
+
outputs=gr.Text(label="Preferred Solution"),
|
11 |
+
title="EvoTransformer v2.1 - PIQA Commonsense Reasoning"
|
12 |
+
)
|
13 |
|
14 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|