Spaces:
Running
Running
Update watchdog.py
Browse files- watchdog.py +45 -71
watchdog.py
CHANGED
@@ -1,90 +1,64 @@
|
|
1 |
-
# watchdog.py
|
2 |
-
|
3 |
-
import firebase_admin
|
4 |
-
from firebase_admin import credentials, firestore
|
5 |
import torch
|
6 |
-
|
7 |
-
|
8 |
-
from
|
9 |
-
|
10 |
-
from evo_model import EvoTransformerForClassification, EvoTransformerConfig
|
11 |
-
|
12 |
-
# Initialize Firebase
|
13 |
-
if not firebase_admin._apps:
|
14 |
-
cred = credentials.Certificate("firebase_key.json")
|
15 |
-
firebase_admin.initialize_app(cred)
|
16 |
|
17 |
-
|
18 |
-
tokenizer =
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
def __getitem__(self, idx):
|
32 |
-
row = self.records[idx]
|
33 |
-
combined = f"Goal: {row['goal']} Option 1: {row['solution_1']} Option 2: {row['solution_2']}"
|
34 |
-
inputs = self.tokenizer(combined, padding="max_length", truncation=True,
|
35 |
-
max_length=self.max_length, return_tensors="pt")
|
36 |
-
label = self.label_map[row["correct_answer"]]
|
37 |
-
return {
|
38 |
-
"input_ids": inputs["input_ids"].squeeze(0),
|
39 |
-
"attention_mask": inputs["attention_mask"].squeeze(0),
|
40 |
-
"labels": torch.tensor(label)
|
41 |
-
}
|
42 |
-
|
43 |
-
# Manual retrain trigger
|
44 |
def manual_retrain():
|
45 |
try:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
if len(feedback_data) < 5:
|
51 |
-
print("[Retrain Skipped] Not enough feedback.")
|
52 |
return False
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
|
58 |
-
# Step 3: Load model
|
59 |
-
config = EvoTransformerConfig()
|
60 |
-
model = EvoTransformerForClassification(config)
|
61 |
model.train()
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
71 |
optimizer.zero_grad()
|
72 |
-
input_ids = batch["input_ids"]
|
73 |
-
attention_mask = batch["attention_mask"]
|
74 |
-
labels = batch["labels"]
|
75 |
-
|
76 |
-
logits = model(input_ids)
|
77 |
-
loss = loss_fn(logits, labels)
|
78 |
loss.backward()
|
79 |
optimizer.step()
|
80 |
-
|
81 |
-
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
print("✅ Evo updated with latest feedback.")
|
86 |
return True
|
87 |
-
|
88 |
except Exception as e:
|
89 |
print(f"[Retrain Error] {e}")
|
90 |
return False
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
from transformers import AutoTokenizer
|
3 |
+
from evo_model import EvoTransformerForClassification
|
4 |
+
from firebase_admin import firestore
|
5 |
+
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Load tokenizer
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
9 |
|
10 |
+
def load_feedback_data():
|
11 |
+
db = firestore.client()
|
12 |
+
docs = db.collection("evo_feedback_logs").stream()
|
13 |
+
data = []
|
14 |
+
for doc in docs:
|
15 |
+
d = doc.to_dict()
|
16 |
+
if all(k in d for k in ["goal", "solution_1", "solution_2", "correct_answer"]):
|
17 |
+
data.append((
|
18 |
+
d["goal"],
|
19 |
+
d["solution_1"],
|
20 |
+
d["solution_2"],
|
21 |
+
0 if d["correct_answer"] == "Solution 1" else 1
|
22 |
+
))
|
23 |
+
return pd.DataFrame(data, columns=["goal", "sol1", "sol2", "label"])
|
24 |
|
25 |
+
def encode(goal, sol1, sol2):
|
26 |
+
prompt = f"Goal: {goal} Option 1: {sol1} Option 2: {sol2}"
|
27 |
+
return tokenizer(prompt, return_tensors="pt", padding=True, truncation=True).input_ids
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
def manual_retrain():
|
30 |
try:
|
31 |
+
data = load_feedback_data()
|
32 |
+
if data.empty:
|
33 |
+
print("[Retrain Error] No training data found.")
|
|
|
|
|
|
|
34 |
return False
|
35 |
|
36 |
+
model = EvoTransformerForClassification.from_pretrained("trained_model")
|
37 |
+
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
|
38 |
+
loss_fn = torch.nn.CrossEntropyLoss()
|
39 |
|
|
|
|
|
|
|
40 |
model.train()
|
41 |
+
for _, row in data.sample(frac=1).iterrows():
|
42 |
+
inputs = encode(row["goal"], row["sol1"], row["sol2"])
|
43 |
+
label = torch.tensor([row["label"]])
|
44 |
+
outputs = model(inputs)
|
45 |
+
if isinstance(outputs, tuple):
|
46 |
+
logits = outputs[0]
|
47 |
+
elif hasattr(outputs, "logits"):
|
48 |
+
logits = outputs.logits
|
49 |
+
else:
|
50 |
+
logits = outputs
|
51 |
+
if logits.ndim == 2 and label.ndim == 1:
|
52 |
+
loss = loss_fn(logits, label)
|
53 |
optimizer.zero_grad()
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
loss.backward()
|
55 |
optimizer.step()
|
56 |
+
else:
|
57 |
+
print("[Retrain Warning] Shape mismatch, skipping one example.")
|
58 |
|
59 |
+
model.save_pretrained("trained_model")
|
60 |
+
print("✅ Evo retrained and saved.")
|
|
|
61 |
return True
|
|
|
62 |
except Exception as e:
|
63 |
print(f"[Retrain Error] {e}")
|
64 |
return False
|