Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +17 -21
inference.py
CHANGED
|
@@ -1,6 +1,13 @@
|
|
| 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
|
|
@@ -11,23 +18,16 @@ from evo_architecture import (
|
|
| 11 |
save_best_genome,
|
| 12 |
load_best_genome
|
| 13 |
)
|
| 14 |
-
import random
|
| 15 |
-
import csv
|
| 16 |
-
import os
|
| 17 |
-
import psutil
|
| 18 |
-
import platform
|
| 19 |
-
import GPUtil
|
| 20 |
import openai
|
| 21 |
-
import pandas as pd
|
| 22 |
|
| 23 |
-
# π
|
| 24 |
openai.api_key = os.getenv("OPENAI_API_KEY", "sk-...")
|
| 25 |
|
| 26 |
-
#
|
| 27 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 28 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 29 |
|
| 30 |
-
# π
|
| 31 |
current_config = load_best_genome()
|
| 32 |
model = build_model_from_config(current_config).to(device)
|
| 33 |
model.eval()
|
|
@@ -50,7 +50,7 @@ def evo_chat_predict(history, question, options):
|
|
| 50 |
"context_used": question
|
| 51 |
}
|
| 52 |
|
| 53 |
-
# π€ GPT
|
| 54 |
def get_gpt_response(prompt):
|
| 55 |
try:
|
| 56 |
client = openai.OpenAI()
|
|
@@ -62,7 +62,7 @@ def get_gpt_response(prompt):
|
|
| 62 |
except Exception as e:
|
| 63 |
return f"(GPT Error) {e}"
|
| 64 |
|
| 65 |
-
# π
|
| 66 |
def get_model_config():
|
| 67 |
return {
|
| 68 |
"num_layers": current_config.get("num_layers", "?"),
|
|
@@ -96,7 +96,7 @@ def get_system_stats():
|
|
| 96 |
"platform": platform.platform()
|
| 97 |
}
|
| 98 |
|
| 99 |
-
# π
|
| 100 |
def retrain_from_feedback_csv():
|
| 101 |
global current_config, model
|
| 102 |
|
|
@@ -104,16 +104,13 @@ def retrain_from_feedback_csv():
|
|
| 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
|
|
@@ -123,13 +120,12 @@ def retrain_from_feedback_csv():
|
|
| 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
|
| 133 |
model.train()
|
| 134 |
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
|
| 135 |
for epoch in range(3):
|
|
@@ -148,7 +144,7 @@ def retrain_from_feedback_csv():
|
|
| 148 |
save_best_genome({**new_config, "accuracy": "Live-Finetuned"})
|
| 149 |
return f"β
Evo retrained on {len(data)} feedback entries."
|
| 150 |
|
| 151 |
-
# π Reload
|
| 152 |
def load_model(force_reload=False):
|
| 153 |
global model
|
| 154 |
-
model.eval()
|
|
|
|
| 1 |
# inference.py
|
| 2 |
|
| 3 |
+
import os
|
| 4 |
+
import csv
|
| 5 |
+
import random
|
| 6 |
import torch
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import psutil
|
| 9 |
+
import platform
|
| 10 |
+
import GPUtil
|
| 11 |
import torch.nn.functional as F
|
| 12 |
from transformers import AutoTokenizer
|
| 13 |
from evo_model import EvoTransformerV22
|
|
|
|
| 18 |
save_best_genome,
|
| 19 |
load_best_genome
|
| 20 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
import openai
|
|
|
|
| 22 |
|
| 23 |
+
# π API Key
|
| 24 |
openai.api_key = os.getenv("OPENAI_API_KEY", "sk-...")
|
| 25 |
|
| 26 |
+
# π¦ Setup
|
| 27 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 28 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 29 |
|
| 30 |
+
# π Current genome state
|
| 31 |
current_config = load_best_genome()
|
| 32 |
model = build_model_from_config(current_config).to(device)
|
| 33 |
model.eval()
|
|
|
|
| 50 |
"context_used": question
|
| 51 |
}
|
| 52 |
|
| 53 |
+
# π€ GPT response
|
| 54 |
def get_gpt_response(prompt):
|
| 55 |
try:
|
| 56 |
client = openai.OpenAI()
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
return f"(GPT Error) {e}"
|
| 64 |
|
| 65 |
+
# π Genome stats
|
| 66 |
def get_model_config():
|
| 67 |
return {
|
| 68 |
"num_layers": current_config.get("num_layers", "?"),
|
|
|
|
| 96 |
"platform": platform.platform()
|
| 97 |
}
|
| 98 |
|
| 99 |
+
# π Evo retrain logic
|
| 100 |
def retrain_from_feedback_csv():
|
| 101 |
global current_config, model
|
| 102 |
|
|
|
|
| 104 |
return "β οΈ No feedback log found."
|
| 105 |
|
| 106 |
df = pd.read_csv(FEEDBACK_LOG)
|
| 107 |
+
if df.empty or "vote" not in df.columns:
|
|
|
|
|
|
|
| 108 |
return "β οΈ No usable feedback data. Please vote on Evo or GPT."
|
| 109 |
|
| 110 |
df = df[df["vote"].isin(["Evo", "GPT"])]
|
| 111 |
if df.empty:
|
| 112 |
return "β οΈ No usable feedback data. Please vote on Evo or GPT."
|
| 113 |
|
|
|
|
| 114 |
data = []
|
| 115 |
for _, row in df.iterrows():
|
| 116 |
label = 1 if row["vote"] == "Evo" else 0
|
|
|
|
| 120 |
if not data:
|
| 121 |
return "β οΈ No usable feedback data."
|
| 122 |
|
|
|
|
| 123 |
new_config = mutate_genome(current_config)
|
| 124 |
model = build_model_from_config(new_config).to(device)
|
| 125 |
current_config = new_config
|
| 126 |
log_genome(new_config)
|
| 127 |
|
| 128 |
+
# π Fine-tune
|
| 129 |
model.train()
|
| 130 |
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
|
| 131 |
for epoch in range(3):
|
|
|
|
| 144 |
save_best_genome({**new_config, "accuracy": "Live-Finetuned"})
|
| 145 |
return f"β
Evo retrained on {len(data)} feedback entries."
|
| 146 |
|
| 147 |
+
# π Reload trigger
|
| 148 |
def load_model(force_reload=False):
|
| 149 |
global model
|
| 150 |
+
model.eval()
|