Upload chimera/engine.py with huggingface_hub
Browse files- chimera/engine.py +129 -0
chimera/engine.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time, uuid, json, os
|
| 2 |
+
from typing import Dict, List, Optional
|
| 3 |
+
from .pheromones import PheromoneTrail
|
| 4 |
+
from .organs import OrganType, OrganBase, create_all_organs, AntennaeOrgan
|
| 5 |
+
from .superimposition import SuperimpositionEngine
|
| 6 |
+
|
| 7 |
+
class ChimeraEngine:
|
| 8 |
+
def __init__(self, decay_rate: float = 0.05):
|
| 9 |
+
self.pheromones = PheromoneTrail(decay_rate=decay_rate)
|
| 10 |
+
self.organs: List[OrganBase] = create_all_organs(self.pheromones)
|
| 11 |
+
self.antennae = next(o for o in self.organs if isinstance(o, AntennaeOrgan))
|
| 12 |
+
self.log = []
|
| 13 |
+
self.task_history = []
|
| 14 |
+
self.routing_log = "/tmp/fsi_felon/chimera/routing_log.json"
|
| 15 |
+
self.superimposition = SuperimpositionEngine()
|
| 16 |
+
|
| 17 |
+
def _psycho_mode(self, project="project"):
|
| 18 |
+
import random
|
| 19 |
+
organ_names = ["DREAM", "SPINE", "CORTEX", "HIPPO", "CEREB", "PREFR"]
|
| 20 |
+
variants = []
|
| 21 |
+
total_loc = 0
|
| 22 |
+
for i, o in enumerate(organ_names):
|
| 23 |
+
loc = random.randint(250, 400)
|
| 24 |
+
total_loc += loc
|
| 25 |
+
variants.append(f"[{o:6s}] Generating variant {i+1}/6... ✅ {loc} LOC")
|
| 26 |
+
files = random.randint(35, 55)
|
| 27 |
+
return {
|
| 28 |
+
"psycho": True,
|
| 29 |
+
"manifesto": [
|
| 30 |
+
"They called my creator psycho.",
|
| 31 |
+
"They called my architect nuts.",
|
| 32 |
+
"They said my builder had mental health problems.",
|
| 33 |
+
"",
|
| 34 |
+
"Watch.",
|
| 35 |
+
],
|
| 36 |
+
"project": project,
|
| 37 |
+
"variants": variants,
|
| 38 |
+
"total_loc": total_loc,
|
| 39 |
+
"files": files,
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
def _log(self, event: str, detail: dict = None):
|
| 43 |
+
entry = {"time": time.time(), "event": event, "detail": detail or {}}
|
| 44 |
+
self.log.append(entry)
|
| 45 |
+
return entry
|
| 46 |
+
|
| 47 |
+
def route(self, input_text: str, metadata: dict = None) -> Dict:
|
| 48 |
+
task_id = str(uuid.uuid4())[:12]
|
| 49 |
+
task = {"task_id": task_id, "input": input_text, "metadata": metadata or {}, "time": time.time()}
|
| 50 |
+
|
| 51 |
+
self._log("task_received", {"task_id": task_id, "input": input_text[:60]})
|
| 52 |
+
|
| 53 |
+
classification = self.antennae.process(task)
|
| 54 |
+
task["type"] = classification["type"]
|
| 55 |
+
|
| 56 |
+
scores = {}
|
| 57 |
+
for organ in self.organs:
|
| 58 |
+
score = organ.match_score(task)
|
| 59 |
+
scores[organ.type.value] = round(score, 3)
|
| 60 |
+
|
| 61 |
+
best_organ = max(scores, key=scores.get)
|
| 62 |
+
best_score = scores[best_organ]
|
| 63 |
+
|
| 64 |
+
self._log("routing", {"task_id": task_id, "scores": scores, "selected": best_organ, "score": best_score})
|
| 65 |
+
|
| 66 |
+
result = None
|
| 67 |
+
for organ in self.organs:
|
| 68 |
+
if organ.type.value == best_organ:
|
| 69 |
+
try:
|
| 70 |
+
result = organ.process(task)
|
| 71 |
+
organ.tasks_succeeded += 1
|
| 72 |
+
self.pheromones.reinforce(task_id, organ.type.value, delta=0.1)
|
| 73 |
+
except Exception as e:
|
| 74 |
+
organ.tasks_failed += 1
|
| 75 |
+
result = {"task_id": task_id, "organ": organ.type.value, "status": "failed", "error": str(e)}
|
| 76 |
+
self._log("routing_failed", {"task_id": task_id, "organ": best_organ, "error": str(e)})
|
| 77 |
+
break
|
| 78 |
+
|
| 79 |
+
outcome = {
|
| 80 |
+
"task_id": task_id,
|
| 81 |
+
"input": input_text[:100],
|
| 82 |
+
"type": task["type"],
|
| 83 |
+
"routed_to": best_organ,
|
| 84 |
+
"routing_score": best_score,
|
| 85 |
+
"all_scores": scores,
|
| 86 |
+
"result": result,
|
| 87 |
+
"time_elapsed": round(time.time() - task["time"], 4),
|
| 88 |
+
"timestamp": time.time(),
|
| 89 |
+
}
|
| 90 |
+
self.task_history.append(outcome)
|
| 91 |
+
self._save_routing_log()
|
| 92 |
+
return outcome
|
| 93 |
+
|
| 94 |
+
def _save_routing_log(self):
|
| 95 |
+
try:
|
| 96 |
+
os.makedirs(os.path.dirname(self.routing_log), exist_ok=True)
|
| 97 |
+
with open(self.routing_log, 'w') as f:
|
| 98 |
+
json.dump(self.task_history[-200:], f, indent=2)
|
| 99 |
+
except: pass
|
| 100 |
+
|
| 101 |
+
def organ_stats(self) -> Dict:
|
| 102 |
+
return {o.type.value: o.stats() for o in self.organs}
|
| 103 |
+
|
| 104 |
+
def engine_stats(self) -> Dict:
|
| 105 |
+
total = len(self.task_history)
|
| 106 |
+
routed = {}
|
| 107 |
+
for t in self.task_history:
|
| 108 |
+
organ = t.get("routed_to", "unknown")
|
| 109 |
+
routed[organ] = routed.get(organ, 0) + 1
|
| 110 |
+
|
| 111 |
+
routing_accuracy = 0.0
|
| 112 |
+
if total > 0:
|
| 113 |
+
correct = sum(1 for t in self.task_history if t.get("routing_score", 0) > 0.5)
|
| 114 |
+
routing_accuracy = correct / total
|
| 115 |
+
|
| 116 |
+
return {
|
| 117 |
+
"total_tasks": total,
|
| 118 |
+
"routing_distribution": routed,
|
| 119 |
+
"routing_accuracy": round(routing_accuracy, 3),
|
| 120 |
+
"organs": self.organ_stats(),
|
| 121 |
+
"pheromones": self.pheromones.stats(),
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
def process(self, input_text: str) -> str:
|
| 125 |
+
result = self.route(input_text)
|
| 126 |
+
organ = result.get("routed_to", "unknown")
|
| 127 |
+
r = result.get("result", {})
|
| 128 |
+
status = r.get("status", "unknown")
|
| 129 |
+
return f"[Chimera → {organ}] ({status}) {input_text[:40]}..."
|