Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +30 -0
inference.py
CHANGED
|
@@ -129,3 +129,33 @@ def get_system_stats():
|
|
| 129 |
"gpu_memory_used_gb": round(torch.cuda.memory_allocated() / (1024 ** 3), 2) if gpu_info else "N/A",
|
| 130 |
"platform": platform.platform()
|
| 131 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
"gpu_memory_used_gb": round(torch.cuda.memory_allocated() / (1024 ** 3), 2) if gpu_info else "N/A",
|
| 130 |
"platform": platform.platform()
|
| 131 |
}
|
| 132 |
+
|
| 133 |
+
# 🧪 Fine-tune Evo from feedback data (CSV or in-memory list)
|
| 134 |
+
def retrain_from_feedback(feedback_data):
|
| 135 |
+
if not feedback_data:
|
| 136 |
+
return "⚠️ No feedback data to retrain from."
|
| 137 |
+
|
| 138 |
+
model = load_model()
|
| 139 |
+
if model is None:
|
| 140 |
+
return "❌ Evo model not available."
|
| 141 |
+
|
| 142 |
+
model.train()
|
| 143 |
+
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
|
| 144 |
+
|
| 145 |
+
for row in feedback_data:
|
| 146 |
+
question, opt1, opt2, answer, *_ = row
|
| 147 |
+
label = torch.tensor([1.0 if answer.strip() == opt2.strip() else 0.0]) # opt2 is class 1
|
| 148 |
+
|
| 149 |
+
# Build input pair
|
| 150 |
+
input_text = f"{question} [SEP] {opt2 if label.item() == 1 else opt1}"
|
| 151 |
+
encoded = tokenizer(input_text, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
|
| 152 |
+
|
| 153 |
+
logits = model(encoded["input_ids"])
|
| 154 |
+
loss = F.binary_cross_entropy_with_logits(logits.squeeze(), label)
|
| 155 |
+
loss.backward()
|
| 156 |
+
optimizer.step()
|
| 157 |
+
optimizer.zero_grad()
|
| 158 |
+
|
| 159 |
+
torch.save(model.state_dict(), MODEL_PATH)
|
| 160 |
+
return "✅ Evo retrained from feedback."
|
| 161 |
+
|