Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +50 -56
inference.py
CHANGED
@@ -1,8 +1,16 @@
|
|
|
|
|
|
1 |
import torch
|
2 |
import torch.nn.functional as F
|
3 |
from transformers import AutoTokenizer
|
4 |
from evo_model import EvoTransformerV22
|
5 |
-
from evo_architecture import
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import random
|
7 |
import csv
|
8 |
import os
|
@@ -10,27 +18,28 @@ import psutil
|
|
10 |
import platform
|
11 |
import GPUtil
|
12 |
import openai
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
16 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
-
|
|
|
|
|
|
|
18 |
model.eval()
|
19 |
|
20 |
-
current_config = {
|
21 |
-
"d_model": 512,
|
22 |
-
"num_heads": 8,
|
23 |
-
"ffn_dim": 1024,
|
24 |
-
"num_layers": 6,
|
25 |
-
"memory_enabled": True
|
26 |
-
}
|
27 |
FEEDBACK_LOG = "feedback_log.csv"
|
28 |
|
|
|
29 |
def evo_chat_predict(history, question, options):
|
30 |
-
|
31 |
-
|
32 |
with torch.no_grad():
|
33 |
-
logits = model(
|
34 |
probs = torch.sigmoid(logits).squeeze().tolist()
|
35 |
best_idx = int(torch.argmax(torch.tensor(probs)))
|
36 |
reasoning = f"{options[0]}: {probs[0]:.3f} vs {options[1]}: {probs[1]:.3f}"
|
@@ -41,9 +50,8 @@ def evo_chat_predict(history, question, options):
|
|
41 |
"context_used": question
|
42 |
}
|
43 |
|
|
|
44 |
def get_gpt_response(prompt):
|
45 |
-
openai.api_key = os.getenv("OPENAI_API_KEY", "sk-...")
|
46 |
-
|
47 |
try:
|
48 |
client = openai.OpenAI()
|
49 |
response = client.chat.completions.create(
|
@@ -54,15 +62,17 @@ def get_gpt_response(prompt):
|
|
54 |
except Exception as e:
|
55 |
return f"(GPT Error) {e}"
|
56 |
|
|
|
57 |
def get_model_config():
|
58 |
return {
|
59 |
-
"num_layers": current_config
|
60 |
-
"num_heads": current_config
|
61 |
-
"ffn_dim": current_config
|
62 |
-
"memory_enabled": current_config
|
63 |
-
"accuracy": "N/A"
|
64 |
}
|
65 |
|
|
|
66 |
def get_system_stats():
|
67 |
mem = psutil.virtual_memory()
|
68 |
cpu = psutil.cpu_percent()
|
@@ -86,57 +96,40 @@ def get_system_stats():
|
|
86 |
"platform": platform.platform()
|
87 |
}
|
88 |
|
|
|
89 |
def retrain_from_feedback_csv():
|
90 |
-
|
91 |
-
from evo_architecture import mutate_genome, log_genome, save_best_genome, build_model_from_config
|
92 |
-
from train_utils import train_model_on_feedback # your training function
|
93 |
|
94 |
-
if not os.path.exists(
|
95 |
return "⚠️ No feedback log found."
|
96 |
|
97 |
-
df = pd.read_csv(
|
98 |
|
|
|
99 |
if df.empty or "vote" not in df.columns or df["vote"].dropna().empty:
|
100 |
-
return "⚠️ No usable feedback data.
|
101 |
|
102 |
-
# Filter only rows with valid vote
|
103 |
df = df[df["vote"].isin(["Evo", "GPT"])]
|
104 |
-
|
105 |
if df.empty:
|
106 |
-
return "⚠️ No usable feedback data. Please vote on Evo or GPT
|
107 |
-
|
108 |
-
# Proceed with mutation & training...
|
109 |
-
new_config = mutate_genome(load_best_genome())
|
110 |
-
log_genome(new_config)
|
111 |
-
|
112 |
-
model = build_model_from_config(new_config)
|
113 |
-
score = train_model_on_feedback(model, df) # this should return a score or accuracy
|
114 |
-
save_best_genome({**new_config, "accuracy": score})
|
115 |
-
|
116 |
-
return f"✅ Evo retrained using feedback (score={score:.4f})"
|
117 |
-
|
118 |
|
|
|
119 |
data = []
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
if vote in ["Evo", "GPT"]:
|
125 |
-
label = 1 if vote == "Evo" else 0
|
126 |
-
input_text = f"{row['question']} {row['option1']} {row['option2']}"
|
127 |
-
data.append((input_text, label))
|
128 |
|
129 |
if not data:
|
130 |
return "⚠️ No usable feedback data."
|
131 |
|
132 |
-
#
|
133 |
-
global current_config, model
|
134 |
new_config = mutate_genome(current_config)
|
135 |
model = build_model_from_config(new_config).to(device)
|
136 |
current_config = new_config
|
137 |
log_genome(new_config)
|
138 |
|
139 |
-
#
|
140 |
model.train()
|
141 |
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
|
142 |
for epoch in range(3):
|
@@ -145,16 +138,17 @@ def retrain_from_feedback_csv():
|
|
145 |
enc = tokenizer(text, padding="max_length", truncation=True, max_length=128, return_tensors="pt").to(device)
|
146 |
input_ids = enc["input_ids"]
|
147 |
label_tensor = torch.tensor([label], dtype=torch.float32).to(device)
|
148 |
-
logits = model(input_ids)
|
149 |
-
|
150 |
-
logits = logits.squeeze(1)
|
151 |
-
loss = F.binary_cross_entropy_with_logits(logits.squeeze(), label_tensor)
|
152 |
optimizer.zero_grad()
|
153 |
loss.backward()
|
154 |
optimizer.step()
|
|
|
155 |
model.eval()
|
|
|
156 |
return f"✅ Evo retrained on {len(data)} feedback entries."
|
157 |
|
|
|
158 |
def load_model(force_reload=False):
|
159 |
global model
|
160 |
-
model.eval()
|
|
|
1 |
+
# inference.py
|
2 |
+
|
3 |
import torch
|
4 |
import torch.nn.functional as F
|
5 |
from transformers import AutoTokenizer
|
6 |
from evo_model import EvoTransformerV22
|
7 |
+
from evo_architecture import (
|
8 |
+
build_model_from_config,
|
9 |
+
mutate_genome,
|
10 |
+
log_genome,
|
11 |
+
save_best_genome,
|
12 |
+
load_best_genome
|
13 |
+
)
|
14 |
import random
|
15 |
import csv
|
16 |
import os
|
|
|
18 |
import platform
|
19 |
import GPUtil
|
20 |
import openai
|
21 |
+
import pandas as pd
|
22 |
+
|
23 |
+
# 🔐 Load OpenAI key
|
24 |
+
openai.api_key = os.getenv("OPENAI_API_KEY", "sk-...")
|
25 |
|
26 |
+
# ⚙️ Runtime setup
|
27 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
28 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
29 |
+
|
30 |
+
# 🔁 Mutable model & config
|
31 |
+
current_config = load_best_genome()
|
32 |
+
model = build_model_from_config(current_config).to(device)
|
33 |
model.eval()
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
FEEDBACK_LOG = "feedback_log.csv"
|
36 |
|
37 |
+
# 🧠 Evo prediction
|
38 |
def evo_chat_predict(history, question, options):
|
39 |
+
inputs = [f"{question} {opt}" for opt in options]
|
40 |
+
enc = tokenizer(inputs, padding=True, truncation=True, max_length=128, return_tensors="pt").to(device)
|
41 |
with torch.no_grad():
|
42 |
+
logits = model(enc["input_ids"])
|
43 |
probs = torch.sigmoid(logits).squeeze().tolist()
|
44 |
best_idx = int(torch.argmax(torch.tensor(probs)))
|
45 |
reasoning = f"{options[0]}: {probs[0]:.3f} vs {options[1]}: {probs[1]:.3f}"
|
|
|
50 |
"context_used": question
|
51 |
}
|
52 |
|
53 |
+
# 🤖 GPT comparison
|
54 |
def get_gpt_response(prompt):
|
|
|
|
|
55 |
try:
|
56 |
client = openai.OpenAI()
|
57 |
response = client.chat.completions.create(
|
|
|
62 |
except Exception as e:
|
63 |
return f"(GPT Error) {e}"
|
64 |
|
65 |
+
# 📊 Evo stats
|
66 |
def get_model_config():
|
67 |
return {
|
68 |
+
"num_layers": current_config.get("num_layers", "?"),
|
69 |
+
"num_heads": current_config.get("num_heads", "?"),
|
70 |
+
"ffn_dim": current_config.get("ffn_dim", "?"),
|
71 |
+
"memory_enabled": current_config.get("memory_enabled", "?"),
|
72 |
+
"accuracy": current_config.get("accuracy", "N/A")
|
73 |
}
|
74 |
|
75 |
+
# 🖥️ System info
|
76 |
def get_system_stats():
|
77 |
mem = psutil.virtual_memory()
|
78 |
cpu = psutil.cpu_percent()
|
|
|
96 |
"platform": platform.platform()
|
97 |
}
|
98 |
|
99 |
+
# 🔁 Retrain from feedback
|
100 |
def retrain_from_feedback_csv():
|
101 |
+
global current_config, model
|
|
|
|
|
102 |
|
103 |
+
if not os.path.exists(FEEDBACK_LOG):
|
104 |
return "⚠️ No feedback log found."
|
105 |
|
106 |
+
df = pd.read_csv(FEEDBACK_LOG)
|
107 |
|
108 |
+
# Validate votes
|
109 |
if df.empty or "vote" not in df.columns or df["vote"].dropna().empty:
|
110 |
+
return "⚠️ No usable feedback data. Please vote on Evo or GPT."
|
111 |
|
|
|
112 |
df = df[df["vote"].isin(["Evo", "GPT"])]
|
|
|
113 |
if df.empty:
|
114 |
+
return "⚠️ No usable feedback data. Please vote on Evo or GPT."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
# Prepare training data
|
117 |
data = []
|
118 |
+
for _, row in df.iterrows():
|
119 |
+
label = 1 if row["vote"] == "Evo" else 0
|
120 |
+
text = f"{row['question']} {row['option1']} {row['option2']}"
|
121 |
+
data.append((text, label))
|
|
|
|
|
|
|
|
|
122 |
|
123 |
if not data:
|
124 |
return "⚠️ No usable feedback data."
|
125 |
|
126 |
+
# Mutate config
|
|
|
127 |
new_config = mutate_genome(current_config)
|
128 |
model = build_model_from_config(new_config).to(device)
|
129 |
current_config = new_config
|
130 |
log_genome(new_config)
|
131 |
|
132 |
+
# Fine-tune model
|
133 |
model.train()
|
134 |
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
|
135 |
for epoch in range(3):
|
|
|
138 |
enc = tokenizer(text, padding="max_length", truncation=True, max_length=128, return_tensors="pt").to(device)
|
139 |
input_ids = enc["input_ids"]
|
140 |
label_tensor = torch.tensor([label], dtype=torch.float32).to(device)
|
141 |
+
logits = model(input_ids).squeeze(1)
|
142 |
+
loss = F.binary_cross_entropy_with_logits(logits, label_tensor)
|
|
|
|
|
143 |
optimizer.zero_grad()
|
144 |
loss.backward()
|
145 |
optimizer.step()
|
146 |
+
|
147 |
model.eval()
|
148 |
+
save_best_genome({**new_config, "accuracy": "Live-Finetuned"})
|
149 |
return f"✅ Evo retrained on {len(data)} feedback entries."
|
150 |
|
151 |
+
# 🔄 Reload model
|
152 |
def load_model(force_reload=False):
|
153 |
global model
|
154 |
+
model.eval()
|