Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +50 -28
inference.py
CHANGED
|
@@ -2,12 +2,14 @@ import os
|
|
| 2 |
import torch
|
| 3 |
import torch.nn.functional as F
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
-
from evo_model import
|
|
|
|
| 6 |
from search_utils import web_search
|
| 7 |
import openai
|
| 8 |
import time
|
| 9 |
import psutil
|
| 10 |
import platform
|
|
|
|
| 11 |
|
| 12 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
|
|
@@ -15,14 +17,16 @@ MODEL_PATH = "evo_hellaswag.pt"
|
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 16 |
model = None
|
| 17 |
last_mod_time = 0
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
try:
|
| 23 |
current_mod_time = os.path.getmtime(MODEL_PATH)
|
| 24 |
-
if model is None or current_mod_time > last_mod_time:
|
| 25 |
-
model =
|
| 26 |
model.load_state_dict(torch.load(MODEL_PATH, map_location="cpu"))
|
| 27 |
model.eval()
|
| 28 |
last_mod_time = current_mod_time
|
|
@@ -32,6 +36,7 @@ def load_model():
|
|
| 32 |
model = None
|
| 33 |
return model
|
| 34 |
|
|
|
|
| 35 |
# ๐ฎ Evo inference core logic
|
| 36 |
def evo_infer(query, options, user_context=""):
|
| 37 |
model = load_model()
|
|
@@ -49,8 +54,8 @@ def evo_infer(query, options, user_context=""):
|
|
| 49 |
context_str = "\n".join(search_results + ([user_context] if user_context else []))
|
| 50 |
|
| 51 |
input_pairs = [f"{query} [SEP] {opt} [CTX] {context_str}" for opt in options]
|
| 52 |
-
|
| 53 |
scores = []
|
|
|
|
| 54 |
for pair in input_pairs:
|
| 55 |
encoded = tokenizer(pair, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
|
| 56 |
with torch.no_grad():
|
|
@@ -66,6 +71,7 @@ def evo_infer(query, options, user_context=""):
|
|
| 66 |
context_str
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
# ๐ค GPT fallback (for comparison)
|
| 70 |
def get_gpt_response(query, user_context=""):
|
| 71 |
try:
|
|
@@ -79,6 +85,7 @@ def get_gpt_response(query, user_context=""):
|
|
| 79 |
except Exception as e:
|
| 80 |
return f"โ ๏ธ GPT error:\n{str(e)}"
|
| 81 |
|
|
|
|
| 82 |
# ๐ง Live Evo prediction logic
|
| 83 |
def evo_chat_predict(history, query, options):
|
| 84 |
try:
|
|
@@ -99,16 +106,11 @@ def evo_chat_predict(history, query, options):
|
|
| 99 |
"context_used": evo_ctx
|
| 100 |
}
|
| 101 |
|
|
|
|
| 102 |
# ๐ Evo model config metadata
|
| 103 |
def get_model_config():
|
| 104 |
-
return
|
| 105 |
-
|
| 106 |
-
"num_heads": 8,
|
| 107 |
-
"ffn_dim": 1024,
|
| 108 |
-
"memory_enabled": True,
|
| 109 |
-
"phase": "v2.2",
|
| 110 |
-
"accuracy": "~64.5%"
|
| 111 |
-
}
|
| 112 |
|
| 113 |
# ๐ฅ๏ธ Runtime stats
|
| 114 |
def get_system_stats():
|
|
@@ -125,22 +127,41 @@ def get_system_stats():
|
|
| 125 |
"platform": platform.platform()
|
| 126 |
}
|
| 127 |
|
| 128 |
-
# ๐ Retrain from in-memory feedback_log
|
| 129 |
-
def retrain_from_feedback(feedback_log):
|
| 130 |
-
if not feedback_log:
|
| 131 |
-
return "โ ๏ธ No feedback data to retrain from."
|
| 132 |
-
|
| 133 |
-
model = load_model()
|
| 134 |
-
if model is None:
|
| 135 |
-
return "โ Evo model not available."
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
model.train()
|
| 138 |
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
|
| 139 |
|
| 140 |
-
for row in
|
| 141 |
-
question, opt1, opt2, answer
|
| 142 |
-
label = torch.tensor([1.0 if answer.strip() == opt2.strip() else 0.0])
|
| 143 |
-
|
| 144 |
input_text = f"{question} [SEP] {opt2 if label.item() == 1 else opt1}"
|
| 145 |
encoded = tokenizer(input_text, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
|
| 146 |
|
|
@@ -151,4 +172,5 @@ def retrain_from_feedback(feedback_log):
|
|
| 151 |
optimizer.zero_grad()
|
| 152 |
|
| 153 |
torch.save(model.state_dict(), MODEL_PATH)
|
| 154 |
-
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
import torch.nn.functional as F
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
+
from evo_model import build_model_from_config
|
| 6 |
+
from evo_architecture import mutate_genome, default_config, log_genome
|
| 7 |
from search_utils import web_search
|
| 8 |
import openai
|
| 9 |
import time
|
| 10 |
import psutil
|
| 11 |
import platform
|
| 12 |
+
import csv
|
| 13 |
|
| 14 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
|
|
|
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 18 |
model = None
|
| 19 |
last_mod_time = 0
|
| 20 |
+
current_config = default_config()
|
| 21 |
|
| 22 |
+
|
| 23 |
+
# ๐ Load Evo model with auto-reload and dynamic config
|
| 24 |
+
def load_model(force_reload=False):
|
| 25 |
+
global model, last_mod_time, current_config
|
| 26 |
try:
|
| 27 |
current_mod_time = os.path.getmtime(MODEL_PATH)
|
| 28 |
+
if model is None or force_reload or current_mod_time > last_mod_time:
|
| 29 |
+
model = build_model_from_config(current_config)
|
| 30 |
model.load_state_dict(torch.load(MODEL_PATH, map_location="cpu"))
|
| 31 |
model.eval()
|
| 32 |
last_mod_time = current_mod_time
|
|
|
|
| 36 |
model = None
|
| 37 |
return model
|
| 38 |
|
| 39 |
+
|
| 40 |
# ๐ฎ Evo inference core logic
|
| 41 |
def evo_infer(query, options, user_context=""):
|
| 42 |
model = load_model()
|
|
|
|
| 54 |
context_str = "\n".join(search_results + ([user_context] if user_context else []))
|
| 55 |
|
| 56 |
input_pairs = [f"{query} [SEP] {opt} [CTX] {context_str}" for opt in options]
|
|
|
|
| 57 |
scores = []
|
| 58 |
+
|
| 59 |
for pair in input_pairs:
|
| 60 |
encoded = tokenizer(pair, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
|
| 61 |
with torch.no_grad():
|
|
|
|
| 71 |
context_str
|
| 72 |
)
|
| 73 |
|
| 74 |
+
|
| 75 |
# ๐ค GPT fallback (for comparison)
|
| 76 |
def get_gpt_response(query, user_context=""):
|
| 77 |
try:
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
return f"โ ๏ธ GPT error:\n{str(e)}"
|
| 87 |
|
| 88 |
+
|
| 89 |
# ๐ง Live Evo prediction logic
|
| 90 |
def evo_chat_predict(history, query, options):
|
| 91 |
try:
|
|
|
|
| 106 |
"context_used": evo_ctx
|
| 107 |
}
|
| 108 |
|
| 109 |
+
|
| 110 |
# ๐ Evo model config metadata
|
| 111 |
def get_model_config():
|
| 112 |
+
return current_config
|
| 113 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# ๐ฅ๏ธ Runtime stats
|
| 116 |
def get_system_stats():
|
|
|
|
| 127 |
"platform": platform.platform()
|
| 128 |
}
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
# ๐ Retrain from feedback_log.csv and evolve architecture
|
| 132 |
+
def retrain_from_feedback_csv():
|
| 133 |
+
global current_config, model
|
| 134 |
+
path = "feedback_log.csv"
|
| 135 |
+
if not os.path.exists(path):
|
| 136 |
+
return "โ ๏ธ No feedback_log.csv found."
|
| 137 |
+
|
| 138 |
+
feedback_data = []
|
| 139 |
+
with open(path, newline='', encoding="utf-8") as f:
|
| 140 |
+
reader = csv.DictReader(f)
|
| 141 |
+
for row in reader:
|
| 142 |
+
q = row["question"]
|
| 143 |
+
o1 = row["option1"]
|
| 144 |
+
o2 = row["option2"]
|
| 145 |
+
ctx = row["context"]
|
| 146 |
+
vote = row.get("user_preference", "").lower()
|
| 147 |
+
correct = row.get("evo_was_correct", "").lower()
|
| 148 |
+
if vote == "evo" or correct == "yes":
|
| 149 |
+
feedback_data.append((q, o1, o2, o2)) # Evo was correct
|
| 150 |
+
elif vote == "gpt":
|
| 151 |
+
feedback_data.append((q, o1, o2, o1)) # Evo was wrong
|
| 152 |
+
|
| 153 |
+
if not feedback_data:
|
| 154 |
+
return "โ ๏ธ No usable feedback data."
|
| 155 |
+
|
| 156 |
+
# Mutate and retrain new model
|
| 157 |
+
current_config = mutate_genome(current_config)
|
| 158 |
+
model = build_model_from_config(current_config)
|
| 159 |
model.train()
|
| 160 |
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
|
| 161 |
|
| 162 |
+
for row in feedback_data:
|
| 163 |
+
question, opt1, opt2, answer = row
|
| 164 |
+
label = torch.tensor([1.0 if answer.strip() == opt2.strip() else 0.0])
|
|
|
|
| 165 |
input_text = f"{question} [SEP] {opt2 if label.item() == 1 else opt1}"
|
| 166 |
encoded = tokenizer(input_text, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
|
| 167 |
|
|
|
|
| 172 |
optimizer.zero_grad()
|
| 173 |
|
| 174 |
torch.save(model.state_dict(), MODEL_PATH)
|
| 175 |
+
log_genome(current_config)
|
| 176 |
+
return "โ
Evo mutated, retrained, and saved."
|