HemanM commited on
Commit
e3d2f2e
Β·
verified Β·
1 Parent(s): 7833370

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -28
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
- # βœ… Secure OpenAI API key
17
  openai.api_key = os.getenv("OPENAI_API_KEY")
18
 
19
- # βœ… Use GPU if available
20
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
 
22
- # βœ… Load official PIQA dataset with remote code trust enabled
23
- dataset = load_dataset("piqa", trust_remote_code=True)
 
 
 
 
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
- dataset = dataset.map(tokenize_choices)
36
- val_dataset = dataset["validation"].select(range(200)).with_format("torch")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 response
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
- # βœ… Training and evaluation function
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
- train_set = dataset["train"].select(range(few_shot_size)).with_format("torch")
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 train_loader:
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 Plot
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
- # βœ… GPT vs Evo Predictions
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 = torch.tensor([ex["input_ids_0"]]).to(device)
145
- x1 = torch.tensor([ex["input_ids_1"]]).to(device)
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 interface
171
  gr.Interface(
172
  fn=train_and_demo,
173
- inputs=gr.Slider(10, 500, step=10, value=50, label="Number of Training Examples"),
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 live on PIQA and compare with GPT-3.5."
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()