Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,38 +2,59 @@ import os
|
|
2 |
import torch
|
3 |
import torch.nn as nn
|
4 |
import torch.optim as optim
|
5 |
-
from torch.utils.data import DataLoader
|
6 |
-
from datasets import load_dataset
|
7 |
from transformers import AutoTokenizer, get_scheduler
|
8 |
import gradio as gr
|
9 |
import matplotlib.pyplot as plt
|
10 |
import numpy as np
|
|
|
11 |
import io
|
12 |
from PIL import Image
|
13 |
import openai
|
14 |
import time
|
15 |
|
16 |
-
# β
|
17 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
18 |
|
19 |
-
# β
|
20 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
21 |
|
22 |
-
# β
Load
|
23 |
-
dataset =
|
|
|
|
|
|
|
|
|
24 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
25 |
|
|
|
26 |
def tokenize_choices(example):
|
27 |
-
input_0 = tokenizer(example["goal"] + " " + example["sol1"], truncation=True, padding="max_length", max_length=128)
|
28 |
-
input_1 = tokenizer(example["goal"] + " " + example["sol2"], truncation=True, padding="max_length", max_length=128)
|
29 |
return {
|
30 |
-
"input_ids_0": input_0["input_ids"],
|
31 |
-
"input_ids_1": input_1["input_ids"],
|
32 |
-
"label": example["label"]
|
33 |
}
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# β
EvoTransformer definition
|
39 |
class EvoTransformer(nn.Module):
|
@@ -53,7 +74,7 @@ class EvoTransformer(nn.Module):
|
|
53 |
x = self.encoder(x)
|
54 |
return self.classifier(x[:, 0, :]).squeeze(-1)
|
55 |
|
56 |
-
# β
GPT-3.5
|
57 |
def gpt35_answer(prompt):
|
58 |
try:
|
59 |
response = openai.ChatCompletion.create(
|
@@ -66,19 +87,17 @@ def gpt35_answer(prompt):
|
|
66 |
except Exception as e:
|
67 |
return f"[Error: {e}]"
|
68 |
|
69 |
-
# β
|
70 |
def train_and_demo(few_shot_size):
|
71 |
start_time = time.time()
|
72 |
model = EvoTransformer().to(device)
|
73 |
criterion = nn.CrossEntropyLoss()
|
74 |
optimizer = optim.AdamW(model.parameters(), lr=5e-5)
|
75 |
|
76 |
-
|
77 |
-
train_loader = DataLoader(train_set, batch_size=8, shuffle=True)
|
78 |
val_loader = DataLoader(val_dataset, batch_size=32)
|
79 |
|
80 |
-
scheduler = get_scheduler("linear", optimizer=optimizer,
|
81 |
-
num_warmup_steps=0, num_training_steps=3 * len(train_loader))
|
82 |
|
83 |
best_val = 0
|
84 |
accs = []
|
@@ -87,7 +106,7 @@ def train_and_demo(few_shot_size):
|
|
87 |
|
88 |
for epoch in range(3):
|
89 |
model.train()
|
90 |
-
for batch in
|
91 |
optimizer.zero_grad()
|
92 |
x0 = batch["input_ids_0"].to(device)
|
93 |
x1 = batch["input_ids_1"].to(device)
|
@@ -122,7 +141,7 @@ def train_and_demo(few_shot_size):
|
|
122 |
if early_stop >= patience:
|
123 |
break
|
124 |
|
125 |
-
# β
Accuracy
|
126 |
fig, ax = plt.subplots()
|
127 |
ax.plot(accs, marker='o')
|
128 |
ax.set_title(f"Validation Accuracy ({few_shot_size} examples)")
|
@@ -133,16 +152,16 @@ def train_and_demo(few_shot_size):
|
|
133 |
buf.seek(0)
|
134 |
img = Image.open(buf)
|
135 |
|
136 |
-
# β
|
137 |
output = ""
|
138 |
for i in range(2):
|
139 |
-
ex = dataset["validation"][i]
|
140 |
goal = ex["goal"]
|
141 |
sol1 = ex["sol1"]
|
142 |
sol2 = ex["sol2"]
|
143 |
|
144 |
-
x0 =
|
145 |
-
x1 =
|
146 |
l0 = model(x0)
|
147 |
l1 = model(x1)
|
148 |
pred_evo = 0 if l0 > l1 else 1
|
@@ -167,15 +186,15 @@ EvoTransformer v2.1 Configuration:
|
|
167 |
|
168 |
return img, f"Best Accuracy: {best_val:.4f}", output.strip() + "\n\n" + architecture_info.strip()
|
169 |
|
170 |
-
# β
Gradio
|
171 |
gr.Interface(
|
172 |
fn=train_and_demo,
|
173 |
-
inputs=gr.Slider(10,
|
174 |
outputs=[
|
175 |
gr.Image(label="Accuracy Plot"),
|
176 |
gr.Textbox(label="Best Accuracy"),
|
177 |
gr.Textbox(label="Evo vs GPT-3.5 Output")
|
178 |
],
|
179 |
title="𧬠EvoTransformer v2.1 Benchmark",
|
180 |
-
description="Train EvoTransformer
|
181 |
).launch()
|
|
|
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 |
import matplotlib.pyplot as plt
|
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 |
+
# β
Set OpenAI API key from secret
|
17 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
18 |
|
19 |
+
# β
Device setup
|
20 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
21 |
|
22 |
+
# β
Load PIQA from public GitHub (JSONL)
|
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):
|
|
|
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(
|
|
|
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 = []
|
|
|
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)
|
|
|
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)")
|
|
|
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
|
|
|
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()
|